Search⌘ K

Disassembly Output in Code Optimization Mode

Explore how code optimization flags like -O2 affect the disassembly of C/C++ programs using GDB. Learn to interpret reordered instructions and understand the impact of separate compilation on variable initialization. Practice compiling, running, and disassembling optimized code to improve debugging and reconstruction skills.

Disassembly output with optimization

We can add flags to optimize the code while compiling our source file. The flag -O is used for the optimization of code, and the -O1 flag further optimizes the code size and execution time. There are other flags for optimization, such as -O2 and -O3, which reduce the code size and execution time even further.

Source code

Below is a C/C++ code that will help us reconstruct a program through the GDB disassembly output in optimization mode.

C++
int a, b;
int *pa, *pb;
int main(int argc, char* argv[])
{
pa = &a;
pb = &b;
*pa = 1;
*pb = 1;
*pb = *pb + *pa;
++*pa;
*pb = *pb * *pa;
return 0;
}

Compilation and execution of code

...