int probeCPUID() {
  unsigned a, b, c, d, v, reg;
  asm("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "0"(0xa));
  v = a & 0xff;
  printf("Version Identifier: %u \n", v);
  printf("-----------------\n");
  switch (v) {
  case 3:
    printf("-------v3----------\n");
  case 2:
    printf("------v2-----------\n");
    printf("number of fixed counters: %u\n", d & 0xf);
    printf("bit width of fixed counters: %u\n", d >> 5 & 0xff);
  case 1:
    printf("--------v1---------\n");
    printf("number of counters: %u\n", a >> 8 & 0xff);
    printf("bit width of counter: %u\n", a >> 16 & 0xff);
    break;
  default:
    printf("unknown version");
  }
  asm("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "0"(0x1));
  printf("type: %x\n", a >> 12 & 0xc);
  printf("family: %x\n", (a >> 8 & 0xf) + (a >> 20 & 0xff));
  printf("model: %x\n", (a >> 4 & 0xf) + ((a >> 16 & 0xf) << 4));
  if ((c >> 15 & 0x1) == 0x1) {
    printf("perf capabilities enabled\n");
    reg = 0x345;
    asm("rdmsr" : "=a"(a), "=d"(d) : "c"(reg));
    if ((a >> 12 & 0x1) == 0x1)
      printf("freeze while smm enambled");
  }
  return 0;
}


x86 assembly (clang, -O0):
<probeCPUID>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
sub    $0x28,%rsp
mov    $0xa,%eax
cpuid
mov    %eax,-0xc(%rbp)
mov    %ebx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movzbl -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xec3(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x25(%rbp)
call   1030 <printf@plt>
mov    -0x25(%rbp),%al
lea    0xec8(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0x1,%eax
je     11d6 <probeCPUID+0xc6>
jmp    116a <probeCPUID+0x5a>
mov    -0x24(%rbp),%eax
sub    $0x2,%eax
je     119a <probeCPUID+0x8a>
jmp    117b <probeCPUID+0x6b>
mov    -0x24(%rbp),%eax
sub    $0x3,%eax
jne    121d <probeCPUID+0x10d>
jmp    118c <probeCPUID+0x7c>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xea0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%esi
and    $0xf,%esi
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%esi
shr    $0x5,%esi
and    $0xff,%esi
lea    0xea5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeb8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%esi
shr    $0x8,%esi
and    $0xff,%esi
lea    0xeb3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%esi
shr    $0x10,%esi
and    $0xff,%esi
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    122b <probeCPUID+0x11b>
lea    0xeb8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%eax
cpuid
mov    %eax,-0xc(%rbp)
mov    %ebx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0xc(%rbp),%esi
shr    $0xc,%esi
and    $0xc,%esi
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%esi
shr    $0x8,%esi
and    $0xf,%esi
mov    -0xc(%rbp),%eax
shr    $0x14,%eax
and    $0xff,%eax
add    %eax,%esi
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%esi
shr    $0x4,%esi
and    $0xf,%esi
mov    -0xc(%rbp),%eax
shr    $0x10,%eax
and    $0xf,%eax
shl    $0x4,%eax
add    %eax,%esi
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
shr    $0xf,%eax
and    $0x1,%eax
cmp    $0x1,%eax
jne    12f5 <probeCPUID+0x1e5>
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x345,-0x20(%rbp)
mov    -0x20(%rbp),%ecx
rdmsr
mov    %eax,-0xc(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
shr    $0xc,%eax
and    $0x1,%eax
cmp    $0x1,%eax
jne    12f0 <probeCPUID+0x1e0>
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12f5 <probeCPUID+0x1e5>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

x86 assembly (clang, -O2):
<probeCPUID>:
push   %rbp
push   %r14
push   %rbx
mov    $0xa,%eax
cpuid
mov    %edx,%r14d
mov    %eax,%ebx
movzbl %bl,%ebp
lea    0xec6(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xf8b(%rip),%rdi
call   1030 <puts@plt>
cmp    $0x1,%bpl
je     11a1 <probeCPUID+0x81>
cmp    $0x2,%bl
je     116b <probeCPUID+0x4b>
cmp    $0x3,%bl
jne    11d4 <probeCPUID+0xb4>
lea    0xfc3(%rip),%rdi
call   1030 <puts@plt>
lea    0xfa3(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%esi
and    $0xf,%esi
lea    0xe95(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
shr    $0x5,%r14d
movzbl %r14b,%esi
lea    0xe9d(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xf3f(%rip),%rdi
call   1030 <puts@plt>
movzbl %bh,%esi
lea    0xea1(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
shr    $0x10,%ebx
movzbl %bl,%esi
lea    0xea5(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11e2 <probeCPUID+0xc2>
lea    0xeaf(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%eax
cpuid
mov    %ecx,%r14d
mov    %eax,%ebp
mov    %eax,%ebx
shr    $0xc,%ebx
mov    %ebx,%esi
and    $0xc,%esi
lea    0xe9b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%eax
shr    $0x8,%eax
and    $0xf,%eax
mov    %ebp,%ecx
shr    $0x14,%ecx
movzbl %cl,%esi
add    %eax,%esi
lea    0xe85(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
shr    $0x4,%ebp
and    $0xf,%ebp
and    $0xf0,%ebx
or     %ebp,%ebx
lea    0xe75(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   $0x8000,%r14d
je     1275 <probeCPUID+0x155>
lea    0xea7(%rip),%rdi
call   1030 <puts@plt>
mov    $0x345,%ecx
rdmsr
test   $0x1000,%eax
je     1275 <probeCPUID+0x155>
lea    0xe4d(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"Version Identifier: %u \0A\00"
@.str.1 = [19 x i8] c"-----------------\0A\00"
@.str.2 = [21 x i8] c"-------v3----------\0A\00"
@.str.3 = [21 x i8] c"------v2-----------\0A\00"
@.str.4 = [30 x i8] c"number of fixed counters: %u\0A\00"
@.str.5 = [33 x i8] c"bit width of fixed counters: %u\0A\00"
@.str.6 = [21 x i8] c"--------v1---------\0A\00"
@.str.7 = [24 x i8] c"number of counters: %u\0A\00"
@.str.8 = [26 x i8] c"bit width of counter: %u\0A\00"
@.str.9 = [16 x i8] c"unknown version\00"
@.str.10 = [10 x i8] c"type: %x\0A\00"
@.str.11 = [12 x i8] c"family: %x\0A\00"
@.str.12 = [11 x i8] c"model: %x\0A\00"
@.str.13 = [27 x i8] c"perf capabilities enabled\0A\00"
@.str.14 = [26 x i8] c"freeze while smm enambled\00"
define i32 @probeCPUID() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = call { i32, i32, i32, i32 } asm "cpuid", "={ax},={bx},={cx},={dx},0,~{dirflag},~{fpsr},~{flags}"(i32 10)
%8 = extractvalue { i32, i32, i32, i32 } %7, 0
%9 = extractvalue { i32, i32, i32, i32 } %7, 1
%10 = extractvalue { i32, i32, i32, i32 } %7, 2
%11 = extractvalue { i32, i32, i32, i32 } %7, 3
store i32 %8, i32* %1
store i32 %9, i32* %2
store i32 %10, i32* %3
store i32 %11, i32* %4
%12 = load i32, i32* %1
%13 = and i32 %12, 255
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* %5
switch i32 %17, label %39 [
i32 3, label %18
i32 2, label %20
i32 1, label %29
]
18: ; preds = %0
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %0, %18
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%22 = load i32, i32* %4
%23 = and i32 %22, 15
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %4
%26 = lshr i32 %25, 5
%27 = and i32 %26, 255
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %0, %20
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
%31 = load i32, i32* %1
%32 = lshr i32 %31, 8
%33 = and i32 %32, 255
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %1
%36 = lshr i32 %35, 16
%37 = and i32 %36, 255
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0), i32 %37)
br label %41
39: ; preds = %0
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
br label %41
41: ; preds = %39, %29
%42 = call { i32, i32, i32, i32 } asm "cpuid", "={ax},={bx},={cx},={dx},0,~{dirflag},~{fpsr},~{flags}"(i32 1)
%43 = extractvalue { i32, i32, i32, i32 } %42, 0
%44 = extractvalue { i32, i32, i32, i32 } %42, 1
%45 = extractvalue { i32, i32, i32, i32 } %42, 2
%46 = extractvalue { i32, i32, i32, i32 } %42, 3
store i32 %43, i32* %1
store i32 %44, i32* %2
store i32 %45, i32* %3
store i32 %46, i32* %4
%47 = load i32, i32* %1
%48 = lshr i32 %47, 12
%49 = and i32 %48, 12
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0), i32 %49)
%51 = load i32, i32* %1
%52 = lshr i32 %51, 8
%53 = and i32 %52, 15
%54 = load i32, i32* %1
%55 = lshr i32 %54, 20
%56 = and i32 %55, 255
%57 = add i32 %53, %56
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.11, i64 0, i64 0), i32 %57)
%59 = load i32, i32* %1
%60 = lshr i32 %59, 4
%61 = and i32 %60, 15
%62 = load i32, i32* %1
%63 = lshr i32 %62, 16
%64 = and i32 %63, 15
%65 = shl i32 %64, 4
%66 = add i32 %61, %65
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.12, i64 0, i64 0), i32 %66)
%68 = load i32, i32* %3
%69 = lshr i32 %68, 15
%70 = and i32 %69, 1
%71 = icmp eq i32 %70, 1
br i1 %71, label %72, label %85
72: ; preds = %41
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.13, i64 0, i64 0))
store i32 837, i32* %6
%74 = load i32, i32* %6
%75 = call { i32, i32 } asm "rdmsr", "={ax},={dx},{cx},~{dirflag},~{fpsr},~{flags}"(i32 %74)
%76 = extractvalue { i32, i32 } %75, 0
%77 = extractvalue { i32, i32 } %75, 1
store i32 %76, i32* %1
store i32 %77, i32* %4
%78 = load i32, i32* %1
%79 = lshr i32 %78, 12
%80 = and i32 %79, 1
%81 = icmp eq i32 %80, 1
br i1 %81, label %82, label %84
82: ; preds = %72
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.14, i64 0, i64 0))
br label %84
84: ; preds = %82, %72
br label %85
85: ; preds = %84, %41
ret i32 0
}

LLVM IR (-O2):
@.str = [25 x i8] c"Version Identifier: %u \0A\00"
@.str.4 = [30 x i8] c"number of fixed counters: %u\0A\00"
@.str.5 = [33 x i8] c"bit width of fixed counters: %u\0A\00"
@.str.7 = [24 x i8] c"number of counters: %u\0A\00"
@.str.8 = [26 x i8] c"bit width of counter: %u\0A\00"
@.str.9 = [16 x i8] c"unknown version\00"
@.str.10 = [10 x i8] c"type: %x\0A\00"
@.str.11 = [12 x i8] c"family: %x\0A\00"
@.str.12 = [11 x i8] c"model: %x\0A\00"
@.str.14 = [26 x i8] c"freeze while smm enambled\00"
@str = [18 x i8] c"-----------------\00"
@str.15 = [20 x i8] c"--------v1---------\00"
@str.16 = [26 x i8] c"perf capabilities enabled\00"
@str.17 = [20 x i8] c"------v2-----------\00"
@str.18 = [20 x i8] c"-------v3----------\00"
define i32 @probeCPUID() {
%1 = tail call { i32, i32, i32, i32 } asm "cpuid", "={ax},={bx},={cx},={dx},0,~{dirflag},~{fpsr},~{flags}"(i32 10)
%2 = extractvalue { i32, i32, i32, i32 } %1, 0
%3 = extractvalue { i32, i32, i32, i32 } %1, 3
%4 = and i32 %2, 255
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%7 = trunc i32 %2 to i8
switch i8 %7, label %25 [
i8 3, label %8
i8 2, label %10
i8 1, label %17
]
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.18, i64 0, i64 0))
br label %10
10: ; preds = %0, %8
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.17, i64 0, i64 0))
%12 = and i32 %3, 15
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %12)
%14 = lshr i32 %3, 5
%15 = and i32 %14, 255
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %15)
br label %17
17: ; preds = %0, %10
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.15, i64 0, i64 0))
%19 = lshr i32 %2, 8
%20 = and i32 %19, 255
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %20)
%22 = lshr i32 %2, 16
%23 = and i32 %22, 255
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0), i32 %23)
br label %27
25: ; preds = %0
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
br label %27
27: ; preds = %25, %17
%28 = tail call { i32, i32, i32, i32 } asm "cpuid", "={ax},={bx},={cx},={dx},0,~{dirflag},~{fpsr},~{flags}"(i32 1)
%29 = extractvalue { i32, i32, i32, i32 } %28, 0
%30 = extractvalue { i32, i32, i32, i32 } %28, 2
%31 = lshr i32 %29, 12
%32 = and i32 %31, 12
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0), i32 %32)
%34 = lshr i32 %29, 8
%35 = and i32 %34, 15
%36 = lshr i32 %29, 20
%37 = and i32 %36, 255
%38 = add nuw nsw i32 %35, %37
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.11, i64 0, i64 0), i32 %38)
%40 = lshr i32 %29, 4
%41 = and i32 %40, 15
%42 = and i32 %31, 240
%43 = or i32 %41, %42
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.12, i64 0, i64 0), i32 %43)
%45 = and i32 %30, 32768
%46 = icmp eq i32 %45, 0
br i1 %46, label %55, label %47
47: ; preds = %27
%48 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.16, i64 0, i64 0))
%49 = tail call { i32, i32 } asm "rdmsr", "={ax},={dx},{cx},~{dirflag},~{fpsr},~{flags}"(i32 837)
%50 = extractvalue { i32, i32 } %49, 0
%51 = and i32 %50, 4096
%52 = icmp eq i32 %51, 0
br i1 %52, label %55, label %53
53: ; preds = %47
%54 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.14, i64 0, i64 0))
br label %55
55: ; preds = %47, %53, %27
ret i32 0
}