I noticed that GCC is very smart about optimizing printf. For example, printf("") is completely removed from the resulting code. printf("\n") is replaced with putchar('\n').
I noticed when I compile a very small module like this:
extern "C" int printf(const char *__restrict __format, ...); void f() { printf("\n"); } with gcc -O2 the call to printf is replaced with putchar('\n') (don't even need to include any header files).
Generated code:
f(): mov edi, 10 jmp putchar What is the mechanism that allows those optimizations to be performed? As far as I know standard C++ does not provide any features that allows such optimizations.
Could it be extended for the user functions or is it "hardcoded" in the compiler?
This also means if I wanted to redefine printf with my own implementation the compiler might mess it up. Is it true?