https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78676
--- Comment #7 from mecej4 at operamail dot com --- (In reply to Andrew Pinski from comment #3) > Try -fwrapv . Thanks! Using that flag with -O2 does make the program give correct results, but I am a little confused about what that flag does and when it should be used. Obviously, it would be safe to use -fwrapv with any bit-twiddling Fortran routine, but it is not obvious which optimizations are impacted by the use of this option. The online documentation at https://gcc.gnu.org/onlinedocs/gcc-6.2.0/gcc/Optimize-Options.html#Optimize-Options says "See also the -fwrapv option" as part of the description of -fstrict-overflow, but I cannot find a separate entry that explains -fwrapv, at least not on that page. I did find a description of -fwrapv on a page for an older version of GCC, namely, 3.0, at http://www.cs.fsu.edu/~baker/ada/gnat/html/gcc_3.html#IDX1319 , but it says "This flag enables some optimizations and disables other." and I was not able to find which ones. Anyway, I am doubtful whether the old manual page is applicable to GCC 6.2.