Earlier quoted context omitted.
> I have programmed in C since ~1986, so please don't try to explain the language to me, and don't assume that my POV comes from a place of ignorance. I apologize for my tone, it was more patronizing that I had intended it to be. > The craziness with undefined behavior is a fairly recent phenomenon. In fact, I started programming in C before there even was a standard, so all behavior was "undefined", yet no compiler…
> The optimizing compiler has a different opinion than you do of "altering behavior". Obviously. And let's be clear: the optimizing compilers of today . This rule used to be inviolable, now it's just something to be scoffed at, see: > If you're looking for something that follows what you're doing exactly, write assembly. Er, no. Compilers used to be able to do this, with optimizations enabled. That this is no longer…
But back to the main point: either you can have optimizations, or you can have code that "does what you want", but you can't have both. OK, I lied, you can have a very small compromise where you do simple things like constant folding and keep with the intent of the programmer, and that's O0. That's what you want. But if you want anything more, even simple things like loop vectorization, you'll need to give up this control.
Really, can you blame the compiler? If you had a conditional that had a branch that was provably false, wouldn't you want the compiler to optimize it out? Should the compiler emit code for
if (false) {
// do something
}
In the security issue you mentioned, that's basically what the compiler's doing: removing a branch that it knows never occurs.