Does GCC include any flags to prevent this sort of detrimental optimization?
-O0 // Do Not Optimise
This sounds like it should do the trick (but I've not done C coding for quite some time, so I don't know if there's a nuance as to why it wouldn't), but would also presumably kill any other optimisation.
A better option may be to combine that with pragmas (https://gcc.gnu.org/onlinedocs/gcc/Function-Specific-Option-...) to switch optimisation levels within the code.
Anyone who's played with GCC recently know whether this would work or not?
(discussed on HN 8 days ago: https://news.ycombinator.com/item?id=8233484)