title: Difference between clang and gcc
body: <p>I used both of these compilers in different projects.</p>
  
  <p>How they are different in terms of code processing and output generations? For example both <code>gcc</code> and <code>clang</code> has <code>-O2</code> options for optimization. Are they operating the same way (high level) in terms of optimizing code? I did a little test , for example if I have the following code:</p>
  
  <pre><code>int foo(int num) {
      if(num % 2 == 1)
          return num * num;
      else
          return num * num +1;  
  }
  </code></pre>
  
  <p>the following are the output assemblies with clang and gcc with -O2:</p>
  
  <pre><code>----gcc 5.3.0-----                              ----clang 3.8.0----
  foo(int):                                       foo(int):
          movl    %edi, %edx                              movl    %edi, %eax
          shrl    $31, %edx                               shrl    $31, %eax
          leal    (%rdi,%rdx), %eax                       addl    %edi, %eax
          andl    $1, %eax                                andl    $-2, %eax
          subl    %edx, %eax                              movl    %edi, %ecx
          cmpl    $1, %eax                                subl    %eax, %ecx
          je      .L5                                     imull   %edi, %edi
          imull   %edi, %edi                              cmpl    $1, %ecx
          leal    1(%rdi), %eax                           setne   %al
          ret                                             movzbl  %al, %eax
  .L5:                                                    addl    %edi, %eax
          movl    %edi, %eax                              retq
          imull   %edi, %eax
          ret
  </code></pre>
  
  <p>as it can be seen the output has different instructions. So my question is does one of them has advantage over another in different projects?</p>