I'll think about your principle, though. My first instinct is that it's not quite right. Actually there probably exist some simple-but-flagrantly-UB programs that "just so happen" to work at -O0 on some implementation, then get caught out at a higher level. The correct fix is to make the program more complex, but nevertheless
for that task the optimisations are still optimisations, even though
for that cack-handed program they aren't. I can't immediately construct such a cack-handed program to prove it, but I expect there are some on "code review" :-) —
Steve Jessop 45 secs ago