Code segfaults with clang -O1, works with gcc49 and clang -O2

Bruce Cran bruce at
Wed Oct 30 10:04:24 UTC 2013

The following code works (on FreeBSD 11-CURRENT) with gcc49 or clang -O2 
or higher, but segfaults in do_cpuid() with clang -O1 or lower. Is there 
a bug in the asm statement, or is clang doing something wrong?

#include <stdio.h>

static inline void do_cpuid(unsigned int *eax, unsigned int *ebx,
                             unsigned int *ecx, unsigned int *edx)
         asm volatile("cpuid"
                 : "=a" (*eax), "=b" (*ebx), "=r" (*ecx), "=d" (*edx)
                 : "0" (*eax), "2" (*ecx)
                 : "memory");

int main(int argc, char **argv)
     unsigned int a = 0, b = 0, c = 0, d = 0;
     do_cpuid(&a, &b, &c, &d);
     printf("%d %d %d %d\n", a, b, c, d);
     return 0;

Bruce Cran

More information about the freebsd-questions mailing list