There’s a cost-benefit component missing from the analysis.
“Bad” code is probably “good enough for now” code that was written some time ago on a bet that doing it better would never be needed as it wouldn’t need to change.
Also, “good” code is costly especially if taking longer to build the thing causes the company to miss its market.
Bad code is not written on a bet. It's written because either competent engineers crunch, or because they are incompetent.
Good code could be costly, yes, but often is not. In fact it's very often economically sound to periodically address tech debt i.e. not allow it to accumulate because it seems to increase exponentially until one day a critical customer-acquisition-stakes feature cannot be shipped on time due to it. Only then do the executives wake up and even then 80% of the time they just blame the engineers and move on. They are never at fault, the angels.
Finally, many companies are already on the market and are relatively OK economically. Let's be honest: most deadlines are entirely artificial and are just power moves by management; they are not mandated by critical business needs.
In my experience a lot of bad was bad even when it was written. And that writing good code is often cheaper (within reason, perfection is bad) over any project bigger than a couple of months. The payoff from having good code shows up very quickly.
“Bad” code is probably “good enough for now” code that was written some time ago on a bet that doing it better would never be needed as it wouldn’t need to change.
Also, “good” code is costly especially if taking longer to build the thing causes the company to miss its market.