title: Why is memcmp(a, b, 4) only sometimes optimized to a uint32 comparison?
body: <p>Given this code:</p>
  
  <pre><code>#include &lt;string.h&gt;
  
  int equal4(const char* a, const char* b)
  {
      return memcmp(a, b, 4) == 0;
  }
  
  int less4(const char* a, const char* b)
  {
      return memcmp(a, b, 4) &lt; 0;
  }
  </code></pre>
  
  <p>GCC 7 on x86_64 introduced an optimization for the first case (Clang has done it for a long time):</p>
  
  <pre><code>    mov     eax, DWORD PTR [rsi]
      cmp     DWORD PTR [rdi], eax
      sete    al
      movzx   eax, al
  </code></pre>
  
  <p>But the second case still calls <code>memcmp()</code>:</p>
  
  <pre><code>    sub     rsp, 8
      mov     edx, 4
      call    memcmp
      add     rsp, 8
      shr     eax, 31
  </code></pre>
  
  <p>Could a similar optimization be applied to the second case?  What's the best assembly for this, and is there any clear reason why it isn't being done (by GCC or Clang)?</p>
  
  <p>See it on Godbolt's Compiler Explorer: <a href="https://godbolt.org/g/jv8fcf" rel="noreferrer">https://godbolt.org/g/jv8fcf</a></p>