The C++ standard's "as-if" rule[1] allows these kinds of transformations if they do not effect the "observable behavior" of the program. Interestingly, if the values are marked volatile the compiler outputs the "correct" short-circuiting code, even with optimizations[2]
> Always assumed this was a requirement of the language, not "up to the compiler".
But the assembly does respect short-circuiting in C++!
The language specification tells you how to figure out the meaning of a program and it tells you which behaviors you are guaranteed to observe or not observe from the program. This is completely abstract – we could ourselves carry out a C++ program in our heads, or with paper and pencil.
A compiler takes a program as input and writes code for some particular machine as output. If the guaranteed behaviors of the program occur when the machine runs the code, the compiler has conformed to the language specification [0]. The language specification says almost nothing about what this code has to look like.
Even if the language specification states that `b == 0` is not evaluated in `a == 0 || b== 0` when a is 0, the corresponding assembly might always involve a `cmp b, 0` because what it means to "evaluate b == 0" has to do with the meaning of the C++ program, not the assembly.
https://cpp.godbolt.org/z/h6sEY8TMz
Always assumed this was a requirement of the language, not "up to the compiler". Perhaps it's only a requirement if the conditions can have side-effects.
Interestingly, every answer in this SO post gets this wrong. https://stackoverflow.com/questions/1799072/c-short-circuiti...