https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88896
--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> --- The compiler optimizes the program with the assumption that undefined behavior doesn't happen. So, e.g. it can remove loop condition if it proves that undefined behavior happens before the last iteration and many other possibilities. Use -fsanitize=undefined to discover the UB if unsure (though that doesn't catch e.g. aliasing bugs).