On 11/03/2025 07:26, manoj alladawar via Gcc-help wrote:
Dear GCC Community,
I hope this message finds you well.
I am currently working on a Debian-based system running Ubuntu 22.04 and
using GCC version 11.4.0. I have compiled a C source file using GCC with
the -O2 optimization flag, resulting in an optimized binary.
I am now interested in generating high-level C code from this optimized
binary for analysis and understanding. Could you kindly advise me on which
decompiler would be most suitable for this purpose? Specifically, I would
appreciate recommendations for decompilers that perform effectively on
GCC-based optimized code.
Your guidance and insights would be greatly appreciated.
Thank you for your time and support. Kind regards,
Manoj Alladawar
I am not sure what you are trying to get at here.
Suppose we have the function :
int foo(int x) {
int y = 0;
for (int i = 0; i < 5; i++) {
y += x;
}
return y;
}
When optimising, gcc will just multiply "x" by 5.
Are you hoping to be able to run gcc on this, then decompile that output
and get a result :
int foo(int x) {
return x * 5;
}
or even :
int foo(int x) {
return (x << 2) + x;
}
?
If so, I think no such tool is possible in the general case.
There /are/ decompilers that turn assembly into compilable C. But the
structure and details is often lost - an original "for" loop might end
up as a "goto" loop, for example. Once you have fed in normal C code
with optimisation, things like inter-procedural optimisations, constant
propagation, inlining, and other re-arrangements will mean that the
regurgitated C code is incomprehensible. It should be possible to
compile it again and get the same semantics - that's the point of a
decompiler. And it might be possible for security analysis tools to
gather information about vulnerabilities. But it's not going to help
you understand how the optimiser is working.
I think the best tool for looking at optimisation of small sections of
code is the <https://godbolt.org> online compiler explorer. You can put
in C code (or many other languages) and look at the generated assembly -
using an x86 compiler or any other target processor if you prefer.
<https://godbolt.org/z/dG8dd57ej>
Beyond that, gcc has lots of options for dumping information about its
internal trees during many of the optimisation passes. But it is no
small task learning to understand and use that information.
David