https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69612
--- Comment #2 from Roar Lauritzsen <roarl at pvv dot org> --- Thanks a lot for the quick analysis. Now that I know what it is I can fix my program, and the -fsanitize=undefined will come in handy for localizing problem areas. For future googlers, I am planning to fix my program along these lines: unsigned test2(unsigned a) { if ((int)a < 0) return 1; a++; // 0x7FFFFFFF + 1 is not undefined anymore if ((int)a < 0) return 2; return a; } This code executes the way I want also with -O2, and there are no errors from -fsanitize=undefined. Personally, I find it somewhat incongruent that integer overflow is undefined, while integer casting is not, and I also find it very disturbing that optimized and non-optimized code may behave differently.