I have a legacy firmware application that requires new functionality. The size of the application was already near the limited flash capacity of the device and the few new funct
Compiler optimisation that triggers bug ? That's strange. Get a map of your program, and see if you should target data or code. Look for duplicated code. Look for code with similar goal. One example of it is the busybox code, which aims for small memory footprint.
It is favoring size over readability, so it sometimes get quite ugly, with gotos and so on.