title: gcc function parameter alignment on stack frame
body: <p>I have this test.c on my Ubuntu14.04 x86_64 system.</p>
  
  <pre><code>void foo(int a, long b, int c) {
  }
  
  int main() {
      foo(0x1, 0x2, 0x3);
  }
  </code></pre>
  
  <p>I compiled this with <code>gcc --no-stack-protector -g test.c -o test</code> and got the assembly code with <code>objdump -dS test -j .text</code></p>
  
  <pre><code>00000000004004ed &lt;_Z3fooili&gt;:
  void foo(int a, long b, int c) {
    4004ed:       55                      push   %rbp
    4004ee:       48 89 e5                mov    %rsp,%rbp
    4004f1:       89 7d fc                mov    %edi,-0x4(%rbp)
    4004f4:       48 89 75 f0             mov    %rsi,-0x10(%rbp)
    4004f8:       89 55 f8                mov    %edx,-0x8(%rbp) // !!Attention here!!
  }
    4004fb:       5d                      pop    %rbp
    4004fc:       c3                      retq   
  
  00000000004004fd &lt;main&gt;:
  
  int main() {
    4004fd:       55                      push   %rbp
    4004fe:       48 89 e5                mov    %rsp,%rbp
      foo(0x1, 0x2, 0x3);
    400501:       ba 03 00 00 00          mov    $0x3,%edx
    400506:       be 02 00 00 00          mov    $0x2,%esi
    40050b:       bf 01 00 00 00          mov    $0x1,%edi
    400510:       e8 d8 ff ff ff          callq  4004ed &lt;_Z3fooili&gt;
  }
    400515:       b8 00 00 00 00          mov    $0x0,%eax
    40051a:       5d                      pop    %rbp
    40051b:       c3                      retq   
    40051c:       0f 1f 40 00             nopl   0x0(%rax)
  </code></pre>
  
  <p>I know that the function parameters should be pushed to stack from right to left in sequence. So I was expecting this</p>
  
  <pre><code>void foo(int a, long b, int c) {
        push   %rbp
        mov    %rsp,%rbp
        mov    %edi,-0x4(%rbp)
        mov    %rsi,-0x10(%rbp)
        mov    %edx,-0x14(%rbp) // c should be push on stack after b, not after a
  </code></pre>
  
  <p>But gcc seemed clever enough to push parameter c(0x3) right after a(0x1) to save the four bytes which should be reserved for data alignment of b(0x2). Can someone please explain this and show me some documentation on why gcc did this?</p>