The C Standard explicitly specifies signed integer overflow as having undefined behavior. Yet most CPUs implement signed arithmetics with defined semantics
Not quite an example of optimization, but one useful consequence of undefined behaviour is -ftrapv
command line switch of GCC/clang. It inserts code which crashes your program on integer overflow.
It won't work on unsigned integers, in accordance with the idea that unsigned overflow is intentional.
The Standard's wording on signed integer overflow ensures that people won't write overflowing code on purpose, so ftrapv
is a useful tool to discover unintentional overflow.