Re:Falsehoods programmers believe about undefined behavior
by skitter on Monday November 28, 2022 @11:34AM
(#33775715)
If UB happens¹, anything can happen. So if the check can only happen after UB, or leads to UB, it can be removed. That isn't the case in your example – testSomeState() can return false (and if it can't, it getting optimized out is unrelated do the code it guards). Points 14-16 from the article are just nonsense, UB is a runtime concept and dead code doesn't run unless you already triggered UB¹.
¹or is guaranteed to happen in the future