title: Understanding assembly of a simple C program
body: <p>I am trying to understand the assembly of this simple C program.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;fcntl.h&gt;
  #include&lt;string.h&gt;
  void foobar(char *a){
      char c = a[0];
  }
  int main(){
      int fd = open("file.txt", O_RDONLY);
          char buf1[100]="\0";
      char buf[100];
      int aa=0,b=1,c=2,d=3,f=2,g=3;
      read(fd,buf1,104);
      if(strlen(buf1) &gt; 100){
  
      }else{
          strcpy(buf,buf1);
      }
      //strcpy(buf,buf1);
      foobar(buf1);
  }
  </code></pre>
  
  <p>The disassembly of the executable using gdb which i got was foobar disassembly.</p>
  
  <pre><code>   0x000000000040067d &lt;+0&gt;: push   rbp
     0x000000000040067e &lt;+1&gt;: mov    rbp,rsp
     0x0000000000400681 &lt;+4&gt;: mov    QWORD PTR [rbp-0x18],rdi
     0x0000000000400685 &lt;+8&gt;: mov    rax,QWORD PTR [rbp-0x18]
     0x0000000000400689 &lt;+12&gt;:    movzx  eax,BYTE PTR [rax]
     0x000000000040068c &lt;+15&gt;:    mov    BYTE PTR [rbp-0x1],al
     0x000000000040068f &lt;+18&gt;:    pop    rbp
  </code></pre>
  
  <p>main disassembly just before foobar</p>
  
  <pre><code>   0x0000000000400784 &lt;+243&gt;:   lea    rax,[rbp-0xf0]
     0x000000000040078b &lt;+250&gt;:   mov    rdi,rax
     0x000000000040078e &lt;+253&gt;:   call   0x40067d &lt;foobar&gt;
     0x0000000000400793 &lt;+258&gt;:   mov    rbx,QWORD PTR [rbp-0x18]
     0x0000000000400797 &lt;+262&gt;:   xor    rbx,QWORD PTR fs:0x28
     0x00000000004007a0 &lt;+271&gt;:   je     0x4007a7 &lt;main+278&gt;
     0x0000000000400690 &lt;+19&gt;:    ret   
  </code></pre>
  
  <p>Now, i have a question regarding the disassembly of foobar</p>
  
  <pre><code>0x0000000000400681 &lt;+4&gt;:    mov    QWORD PTR [rbp-0x18],rdi
  0x0000000000400685 &lt;+8&gt;:    mov    rax,QWORD PTR [rbp-0x18]
  </code></pre>
  
  <p>Wouldn't the instruction </p>
  
  <pre><code>mov rax, rdi
  </code></pre>
  
  <p>would do the work required by the above two instruction. Why using extra memory location <code>rbp - 0x18</code> for rdi ? 
  Is it related to pass by reference?</p>
  
  <p>Edit:
  Another question which i want to ask is why the foobar function is accessing something<code>(rbp - 0x18)</code> which is not in the frame of foobar.?</p>
  
  <p>My gcc version is gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2</p>
  
  <p>Edit:
  After using -O1 -O2 and -O3 optimization flag while compiling, the foobar assembly changes to </p>
  
  <pre><code>   0x0000000000400670 &lt;+0&gt;: repz ret 
  </code></pre>
  
  <p>and while using -O3 flag some of the disassembly of main is</p>
  
  <pre><code>   0x0000000000400551 &lt;+81&gt;:    rep stos QWORD PTR es:[rdi],rax
     0x0000000000400554 &lt;+84&gt;:    mov    DWORD PTR [rdi],0x0
     0x000000000040055a &lt;+90&gt;:    mov    cl,0x64
     0x000000000040055c &lt;+92&gt;:    mov    edi,r8d
     0x000000000040055f &lt;+95&gt;:    call   0x4004b0 &lt;__read_chk@plt&gt;
     0x0000000000400564 &lt;+100&gt;:   mov    rdx,QWORD PTR [rsp+0x68]
     0x0000000000400569 &lt;+105&gt;:   xor    rdx,QWORD PTR fs:0x28
     0x0000000000400572 &lt;+114&gt;:   jne    0x400579 &lt;main+121&gt;
     0x0000000000400574 &lt;+116&gt;:   add    rsp,0x78
     0x0000000000400578 &lt;+120&gt;:   ret    
     0x0000000000400579 &lt;+121&gt;:   call   0x4004c0 &lt;__stack_chk_fail@plt&gt;
  </code></pre>
  
  <p>I can't find any call to foobar in main .</p>