Optimizing compilers don't intentionally break programs with UB, they just ignore the possibility... with very unpredictable results.
Optimizing compilers don't intentionally break programs with UB, they just ignore the possibility... with very unpredictable results.
And neither is "optimising assuming UB never happens", especially if said UB is explicitly written in the code, thus contradicting the assumption.
So the compiler simply ignores this possibility and optimizes assuming the input is a valid C program.
Unless you use the sleight of hand that is commonly used in vastly expanding the meaning of the word "valid" to mean "the compiler can assume there is no UB".
Which, again, isn't "ignoring the situation".
You'd probably assume it's a prank, and treat it as a green light by proceeding normally. Compiler code gen around UB is no more complex than this, though the consequences can cause complex chains of reasoning in later optimization passes.
That's not what UB is. UB is "the standard doesn't say anything about this particular situation, because we intentionally left the standard incomplete". It never said "UB cannot happen".
But even assuming your "purple streetlight" analogy, it exactly does not cover the "complex chains of reasoning" that the compilers then do.
I saw a purple streetlight, therefore I can just assume it is green. Nope.
I saw a purple streetlight, therefore I can now run over pedestrians. Nope.
I saw a purple streetlight, therefore this now an airport runway. Nope.
The compiler-writers' justifications don't pass even the most minimal test for coherence.