An interesting article on undefined behavior

In theory, yes, the compiler is free to do anything.

In practice, undefined behavior is generally used for only one thing: optimizing the program, assuming that it is unreachable. And, yes, this optimization can easily go wrong - e.g., if compiler is able to prove that every path in program contains UB, the program can be compiled down to single trap instruction. But in general, if UB is triggered by some inputs, then any other code path must assume that these inputs are impossible - nothing more, nothing less.