title: What does cltq do in assembly?
body: <pre><code>0x0000000000400553 &lt;main+59&gt;:   mov    -0x4(%rbp),%eax
  0x0000000000400556 &lt;main+62&gt;:   cltq   
  0x0000000000400558 &lt;main+64&gt;:   shl    $0x3,%rax
  0x000000000040055c &lt;main+68&gt;:   mov    %rax,%rdx
  </code></pre>
  
  <p>In fact my programe is as simple as :</p>
  
  <pre><code>5   int main(int argc, char *argv[]) { 
  6     int i = 0;
  7     while(environ[i]) {
  8       printf("%s\n", environ[i++]);
  9     }
  10    return 0;
  </code></pre>
  
  <p>But the assembly output is pretty long:</p>
  
  <pre><code>Dump of assembler code for function main:
  0x0000000000400518 &lt;main+0&gt;:    push   %rbp
  0x0000000000400519 &lt;main+1&gt;:    mov    %rsp,%rbp
  0x000000000040051c &lt;main+4&gt;:    sub    $0x20,%rsp
  0x0000000000400520 &lt;main+8&gt;:    mov    %edi,-0x14(%rbp)
  0x0000000000400523 &lt;main+11&gt;:   mov    %rsi,-0x20(%rbp)
  0x0000000000400527 &lt;main+15&gt;:   movl   $0x0,-0x4(%rbp)
  0x000000000040052e &lt;main+22&gt;:   jmp    0x400553 &lt;main+59&gt;
  0x0000000000400530 &lt;main+24&gt;:   mov    -0x4(%rbp),%eax
  0x0000000000400533 &lt;main+27&gt;:   cltq   
  0x0000000000400535 &lt;main+29&gt;:   shl    $0x3,%rax
  0x0000000000400539 &lt;main+33&gt;:   mov    %rax,%rdx
  0x000000000040053c &lt;main+36&gt;:   mov    0x2003e5(%rip),%rax        # 0x600928 &lt;environ@@GLIBC_2.2.5&gt;
  0x0000000000400543 &lt;main+43&gt;:   lea    (%rdx,%rax,1),%rax
  0x0000000000400547 &lt;main+47&gt;:   mov    (%rax),%rdi
  0x000000000040054a &lt;main+50&gt;:   addl   $0x1,-0x4(%rbp)
  0x000000000040054e &lt;main+54&gt;:   callq  0x400418 &lt;puts@plt&gt;
  0x0000000000400553 &lt;main+59&gt;:   mov    -0x4(%rbp),%eax
  0x0000000000400556 &lt;main+62&gt;:   cltq   
  0x0000000000400558 &lt;main+64&gt;:   shl    $0x3,%rax
  0x000000000040055c &lt;main+68&gt;:   mov    %rax,%rdx
  0x000000000040055f &lt;main+71&gt;:   mov    0x2003c2(%rip),%rax        # 0x600928 &lt;environ@@GLIBC_2.2.5&gt;
  0x0000000000400566 &lt;main+78&gt;:   lea    (%rdx,%rax,1),%rax
  0x000000000040056a &lt;main+82&gt;:   mov    (%rax),%rax
  0x000000000040056d &lt;main+85&gt;:   test   %rax,%rax
  0x0000000000400570 &lt;main+88&gt;:   jne    0x400530 &lt;main+24&gt;
  0x0000000000400572 &lt;main+90&gt;:   mov    $0x0,%eax
  0x0000000000400577 &lt;main+95&gt;:   leaveq 
  0x0000000000400578 &lt;main+96&gt;:   retq   
  End of assembler dump.
  </code></pre>
  
  <p>What I don't understand is this block:</p>
  
  <pre><code>0x000000000040052e &lt;main+22&gt;:   jmp    0x400553 &lt;main+59&gt;
  0x0000000000400530 &lt;main+24&gt;:   mov    -0x4(%rbp),%eax
  0x0000000000400533 &lt;main+27&gt;:   cltq   
  0x0000000000400535 &lt;main+29&gt;:   shl    $0x3,%rax
  0x0000000000400539 &lt;main+33&gt;:   mov    %rax,%rdx
  0x000000000040053c &lt;main+36&gt;:   mov    0x2003e5(%rip),%rax        # 0x600928 &lt;environ@@GLIBC_2.2.5&gt;
  0x0000000000400543 &lt;main+43&gt;:   lea    (%rdx,%rax,1),%rax
  0x0000000000400547 &lt;main+47&gt;:   mov    (%rax),%rdi
  0x000000000040054a &lt;main+50&gt;:   addl   $0x1,-0x4(%rbp)
  0x000000000040054e &lt;main+54&gt;:   callq  0x400418 &lt;puts@plt&gt;
  0x0000000000400553 &lt;main+59&gt;:   mov    -0x4(%rbp),%eax
  0x0000000000400556 &lt;main+62&gt;:   cltq   
  0x0000000000400558 &lt;main+64&gt;:   shl    $0x3,%rax
  0x000000000040055c &lt;main+68&gt;:   mov    %rax,%rdx
  0x000000000040055f &lt;main+71&gt;:   mov    0x2003c2(%rip),%rax        # 0x600928 &lt;environ@@GLIBC_2.2.5&gt;
  0x0000000000400566 &lt;main+78&gt;:   lea    (%rdx,%rax,1),%rax
  0x000000000040056a &lt;main+82&gt;:   mov    (%rax),%rax
  0x000000000040056d &lt;main+85&gt;:   test   %rax,%rax
  0x0000000000400570 &lt;main+88&gt;:   jne    0x400530 &lt;main+24&gt;
  </code></pre>