Sometimes you can fool the (C) optimizer by using the 'volatile' keyword in front of a variable in code that would otherwise be optimized out.
https://www.embeddedrelated.com/thread/4749/when-and-how-to-...
https://www.embeddedrelated.com/thread/4749/when-and-how-to-...