title: Which inline assembly code is correct for rdtscp?
body: <p>Disclaimer: Words cannot describe how much I detest AT&amp;T style syntax</p>
  
  <p>I have a problem that I hope is caused by register clobbering. If not, I have a much bigger problem.</p>
  
  <p>The first version I used was</p>
  
  <pre><code>static unsigned long long rdtscp(void)
  {
      unsigned int hi, lo;
      __asm__ __volatile__("rdtscp" : "=a"(lo), "=d"(hi));
      return (unsigned long long)lo | ((unsigned long long)hi &lt;&lt; 32);
  }
  </code></pre>
  
  <p>I notice there is no 'clobbering' stuff in this version. Whether or not this is a problem I don't know... I suppose it depends if the compiler inlines the function or not. Using this version causes me problems <em>that aren't always reproducible</em>.</p>
  
  <p>The next version I found is</p>
  
  <pre><code>static unsigned long long rdtscp(void)
  {
      unsigned long long tsc;
      __asm__ __volatile__(
          "rdtscp;"
          "shl $32, %%rdx;"
          "or %%rdx, %%rax"
          : "=a"(tsc)
          :
          : "%rcx", "%rdx");
  
      return tsc;
  }
  </code></pre>
  
  <p>This is reassuringly unreadable and official looking, but like I said my issue isn't always reproducible so I'm merely trying to rule out one possible cause of my problem.</p>
  
  <p>The reason I <em>believe</em> the first version is a problem is that it is overwriting a register that previously held a function parameter.</p>
  
  <p>What's correct... version 1, or version 2, or both?</p>