https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123873
--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> --- Note, gcc doesn't try hard to produce bad code for -fsanitize=undefined, but it needs to find a balance. The intent is that for -O0 -fsanitize=undefined it catches as much UB as possible (from the sanitized ones), for -O2 -fsanitize=undefined it can miss some UB but not too much, but still e.g. various match.pd transformations need to be disabled because they result in UBs not being diagnosed. The sanitizers are meant to diagnose code with UB and optimizations are meant to optimize code on the assumption that UB does not happen...
