gcc inline option
Issue
When defining a function as inline, how to make sure that gcc will integrate its code into the code of its caller without using any of the optimisation options (-O
#include <stdio.h>
inline int function_inline(int a, int b);
int function_inline(int a, int b) {
return (a * b) + 3;
}
int main(int argc, char* argv[])
{
int i = argc;
i = function_inline(i, i+1);
return 0;
}
Compiling the code above create the following assembly code:
$ objdump -d a.out
<output truncated>
00000000004004f6 <function_inline>:
4004f6: 55 push %rbp
4004f7: 48 89 e5 mov %rsp,%rbp
4004fa: 89 7d fc mov %edi,-0x4(%rbp)
4004fd: 89 75 f8 mov %esi,-0x8(%rbp)
400500: 8b 45 fc mov -0x4(%rbp),%eax
400503: 0f af 45 f8 imul -0x8(%rbp),%eax
400507: 83 c0 03 add $0x3,%eax
40050a: 5d pop %rbp
40050b: c3 retq
000000000040050c <main>:
40050c: 55 push %rbp
40050d: 48 89 e5 mov %rsp,%rbp
400510: 48 83 ec 20 sub $0x20,%rsp
400514: 89 7d ec mov %edi,-0x14(%rbp)
400517: 48 89 75 e0 mov %rsi,-0x20(%rbp)
40051b: 8b 45 ec mov -0x14(%rbp),%eax
40051e: 89 45 fc mov %eax,-0x4(%rbp)
400521: 8b 45 fc mov -0x4(%rbp),%eax
400524: 8d 50 01 lea 0x1(%rax),%edx
400527: 8b 45 fc mov -0x4(%rbp),%eax
40052a: 89 d6 mov %edx,%esi
40052c: 89 c7 mov %eax,%edi
40052e: e8 c3 ff ff ff callq 4004f6 <function_inline>
400533: 89 45 fc mov %eax,-0x4(%rbp)
400536: b8 00 00 00 00 mov $0x0,%eax
40053b: c9 leaveq
40053c: c3 retq
40053d: 0f 1f 00 nopl (%rax)
There shouldn't be a call to function_inline() in main() but the code of the function itself.
Environment
- Red Hat Enterprise Linux 6
- gcc-4.4.7-16.el6.x86_64
Subscriber exclusive content
A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.