#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ msr_path;
typedef int /*<<< orphan*/ cpuid_path;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ apicid;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ cpuID(int, int, unsigned int *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ pread(int, ...);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ pwrite(int, int *, int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int topology(int core) {
  unsigned int regs[4];
  int i;
  char vendor[12];
  cpuID(0, 0, regs);
  ((unsigned *)vendor)[0] = regs[1];
  ((unsigned *)vendor)[1] = regs[3];
  ((unsigned *)vendor)[2] = regs[2];
  printf("%s\n", vendor);
  int max_leaf = regs[0];
  cpuID(1, 0, regs);
  int stepping = regs[0] & 0xF;
  int model = ((regs[0] >> 16) & 0xF) << 4 | ((regs[0] >> 4) & 0xF);
  int family = ((regs[0] >> 20) & 0xFF) << 4 | ((regs[0] >> 8) & 0xF);
  printf("%02x_%x %x\n", family, model, stepping);
  cpuID(0x0A, 0, regs);
  int nr_ctrs = (regs[0] >> 8) & 0xFF;
  int ctr_width = (regs[0] >> 16) & 0xFF;
  int nr_fixed_ctrs = regs[3] & 0x1F;
  int fixed_ctr_width = (regs[3] >> 5) & 0xFF;
  printf("nr_ctrs %d, ctr_width %d, nr_fixed_ctrs %d, fixed_ctr_width %d\n",
         nr_ctrs, ctr_width, nr_fixed_ctrs, fixed_ctr_width);
  cpuID(0xB, 0, regs);
  unsigned int x2APIC_ID = regs[3] & 0xFFFFFFFF;
  printf("APIC ID %X\n", x2APIC_ID);
  char msr_path[80];
  int msr_fd = -1;
  int apicid = 0;
  snprintf(msr_path, sizeof(msr_path), "/dev/cpu/%d/msr", core);
  msr_fd = open(msr_path, O_RDONLY);
  apicid = 1 << 11 | 1 << 10;
  pwrite(msr_fd, &apicid, sizeof(apicid), 0x1B);
  apicid = 0;
  pread(msr_fd, &apicid, sizeof(apicid), 0x802);
  printf("msr %d\n", apicid);
  char cpuid_path[80];
  int cpuid_fd = -1;
  snprintf(cpuid_path, sizeof(cpuid_path), "/dev/cpu/%d/cpuid", core);
  cpuid_fd = open(cpuid_path, O_RDONLY);
  if (regs[1] != 0) {
    int SMT_Mask_Width;
    int SMT_Select_Mask;
    int CorePlus_Mask_Width;
    int CoreOnly_Select_Mask;
    int Pkg_Select_Mask;
    int SMT_ID, Core_ID, Pkg_ID;
    int logical_cores, physical_cores;
    for (i = 0; i <= max_leaf; i++) {
      unsigned int buf[4];
      pread(cpuid_fd, buf, sizeof(buf), i * 0x100000000 | 0xB);
      printf("%s %d %d %X %d\n", cpuid_path, buf[0], buf[1], buf[2], buf[3]);
      cpuID(0xB, i, regs);
      printf("%d %d %X %d\n", regs[0], regs[1], regs[2], regs[3]);
      if ((regs[1] & 0xFFFF) == 0)
        break;
      if (((regs[2] >> 8) & 0xFF) == 1) {
        logical_cores = regs[1];
        SMT_Mask_Width = regs[0] & 0xF;
        SMT_Select_Mask = ~((-1) << SMT_Mask_Width);
        SMT_ID = x2APIC_ID & SMT_Select_Mask;
      } else if (((regs[2] >> 8) & 0xFF) == 2) {
        printf("logical cores at smt level %d %d\n", logical_cores, regs[1]);
        physical_cores = regs[1] / logical_cores;
        CorePlus_Mask_Width = regs[0] & 0xF;
        CoreOnly_Select_Mask = ~((-1) << CorePlus_Mask_Width) ^ SMT_Select_Mask;
        Core_ID = (x2APIC_ID & CoreOnly_Select_Mask) >> SMT_Mask_Width;
        Pkg_Select_Mask = (-1) << CorePlus_Mask_Width;
        Pkg_ID = (x2APIC_ID & Pkg_Select_Mask) >> CorePlus_Mask_Width;
        printf("Pkg_ID Core_ID SMT_ID %d %d %d %d\n", Pkg_ID, Core_ID, SMT_ID,
               physical_cores);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<topology>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x20(%rbp),%rdx
xor    %esi,%esi
mov    %esi,%edi
call   1050 <cpuID@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x28(%rbp)
lea    -0x30(%rbp),%rsi
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
lea    -0x20(%rbp),%rdx
mov    $0x1,%edi
xor    %esi,%esi
call   1050 <cpuID@plt>
mov    -0x20(%rbp),%eax
and    $0xf,%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%eax
shr    $0x10,%eax
and    $0xf,%eax
shl    $0x4,%eax
mov    -0x20(%rbp),%ecx
shr    $0x4,%ecx
and    $0xf,%ecx
or     %ecx,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%eax
shr    $0x14,%eax
and    $0xff,%eax
shl    $0x4,%eax
mov    -0x20(%rbp),%ecx
shr    $0x8,%ecx
and    $0xf,%ecx
or     %ecx,%eax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%esi
mov    -0x3c(%rbp),%edx
mov    -0x38(%rbp),%ecx
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x20(%rbp),%rdx
mov    $0xa,%edi
xor    %esi,%esi
call   1050 <cpuID@plt>
mov    -0x20(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%eax
shr    $0x10,%eax
and    $0xff,%eax
mov    %eax,-0x48(%rbp)
mov    -0x14(%rbp),%eax
and    $0x1f,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x14(%rbp),%eax
shr    $0x5,%eax
and    $0xff,%eax
mov    %eax,-0x50(%rbp)
mov    -0x44(%rbp),%esi
mov    -0x48(%rbp),%edx
mov    -0x4c(%rbp),%ecx
mov    -0x50(%rbp),%r8d
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x20(%rbp),%rdx
mov    $0xb,%edi
xor    %esi,%esi
call   1050 <cpuID@plt>
mov    -0x14(%rbp),%eax
and    $0xffffffff,%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%esi
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0xb4(%rbp)
ff ff ff
movl   $0x0,-0xb8(%rbp)
lea    -0xb0(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x50,%esi
lea    0xd9f(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0xb0(%rbp),%rdi
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xb4(%rbp)
movl   $0xc00,-0xb8(%rbp)
mov    -0xb4(%rbp),%edi
lea    -0xb8(%rbp),%rsi
mov    $0x4,%edx
mov    $0x1b,%ecx
call   1060 <pwrite@plt>
movl   $0x0,-0xb8(%rbp)
mov    -0xb4(%rbp),%edi
lea    -0xb8(%rbp),%rsi
mov    $0x4,%edx
mov    $0x802,%ecx
mov    $0x0,%al
call   1080 <pread@plt>
mov    -0xb8(%rbp),%esi
lea    0xd34(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x114(%rbp)
ff ff ff
lea    -0x110(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x50,%esi
lea    0xd15(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x110(%rbp),%rdi
mov    0x2c76(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     156e <topology+0x40e>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x34(%rbp),%eax
jg     1569 <topology+0x409>
mov    -0x114(%rbp),%edi
lea    -0x150(%rbp),%rsi
movslq -0x24(%rbp),%rcx
shl    $0x20,%rcx
or     $0xb,%rcx
mov    $0x10,%edx
mov    $0x0,%al
call   1080 <pread@plt>
lea    -0x110(%rbp),%rsi
mov    -0x150(%rbp),%edx
mov    -0x14c(%rbp),%ecx
mov    -0x148(%rbp),%r8d
mov    -0x144(%rbp),%r9d
lea    0xc9d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%esi
lea    -0x20(%rbp),%rdx
mov    $0xb,%edi
call   1050 <cpuID@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%r8d
lea    0xc74(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
and    $0xffff,%eax
cmp    $0x0,%eax
jne    1432 <topology+0x2d2>
jmp    1569 <topology+0x409>
mov    -0x18(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
cmp    $0x1,%eax
jne    1485 <topology+0x325>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x138(%rbp)
mov    -0x20(%rbp),%eax
and    $0xf,%eax
mov    %eax,-0x118(%rbp)
mov    -0x118(%rbp),%ecx
mov    $0xffffffff,%eax
shl    %cl,%eax
xor    $0xffffffff,%eax
mov    %eax,-0x11c(%rbp)
mov    -0x54(%rbp),%eax
and    -0x11c(%rbp),%eax
mov    %eax,-0x12c(%rbp)
jmp    1556 <topology+0x3f6>
mov    -0x18(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
cmp    $0x2,%eax
jne    1551 <topology+0x3f1>
mov    -0x138(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xbed(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
xor    %edx,%edx
divl   -0x138(%rbp)
mov    %eax,-0x13c(%rbp)
mov    -0x20(%rbp),%eax
and    $0xf,%eax
mov    %eax,-0x120(%rbp)
mov    -0x120(%rbp),%ecx
mov    $0xffffffff,%eax
shl    %cl,%eax
xor    $0xffffffff,%eax
xor    -0x11c(%rbp),%eax
mov    %eax,-0x124(%rbp)
mov    -0x54(%rbp),%eax
and    -0x124(%rbp),%eax
mov    -0x118(%rbp),%ecx
shr    %cl,%eax
mov    %eax,-0x130(%rbp)
mov    -0x120(%rbp),%ecx
mov    $0xffffffff,%eax
shl    %cl,%eax
mov    %eax,-0x128(%rbp)
mov    -0x54(%rbp),%eax
and    -0x128(%rbp),%eax
mov    -0x120(%rbp),%ecx
shr    %cl,%eax
mov    %eax,-0x134(%rbp)
mov    -0x134(%rbp),%esi
mov    -0x130(%rbp),%edx
mov    -0x12c(%rbp),%ecx
mov    -0x13c(%rbp),%r8d
lea    0xb6e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1556 <topology+0x3f6>
jmp    155b <topology+0x3fb>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1390 <topology+0x230>
jmp    156e <topology+0x40e>
xor    %eax,%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<topology>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xf8,%rsp
mov    %edi,%r14d
mov    %rsp,%rbp
xor    %edi,%edi
xor    %esi,%esi
mov    %rbp,%rdx
call   1060 <cpuID@plt>
mov    0x4(%rsp),%eax
mov    %eax,0x44(%rsp)
mov    0x8(%rsp),%rax
rol    $0x20,%rax
mov    %rax,0x48(%rsp)
lea    0x44(%rsp),%rdi
call   1030 <puts@plt>
mov    (%rsp),%r12d
mov    $0x1,%edi
xor    %esi,%esi
mov    %rbp,%rdx
call   1060 <cpuID@plt>
mov    (%rsp),%esi
mov    %esi,%ecx
and    $0xf,%ecx
mov    %esi,%eax
shr    $0xc,%eax
and    $0xf0,%eax
mov    %esi,%edx
shr    $0x4,%edx
and    $0xf,%edx
or     %eax,%edx
mov    %esi,%eax
shr    $0x10,%eax
and    $0xff0,%eax
shr    $0x8,%esi
and    $0xf,%esi
or     %eax,%esi
lea    0xe05(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
xor    %esi,%esi
mov    %rbp,%rdx
call   1060 <cpuID@plt>
mov    (%rsp),%eax
mov    0xc(%rsp),%edi
movzbl %ah,%esi
shr    $0x10,%eax
movzbl %al,%edx
mov    %edi,%ecx
and    $0x1f,%ecx
shr    $0x5,%edi
movzbl %dil,%r8d
lea    0xdd8(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xb,%edi
xor    %esi,%esi
mov    %rbp,%rdx
call   1060 <cpuID@plt>
mov    0xc(%rsp),%esi
lea    0xdf7(%rip),%rdi
mov    %esi,0x20(%rsp)
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdf1(%rip),%rdx
lea    0xa0(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x50,%esi
mov    %r14d,%ecx
call   1050 <snprintf@plt>
mov    0x2d62(%rip),%r15
mov    (%r15),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
mov    %eax,%ebp
movl   $0xc00,0x1c(%rsp)
lea    0x1c(%rsp),%rbx
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
mov    $0x1b,%ecx
call   1070 <pwrite@plt>
movl   $0x0,0x1c(%rsp)
mov    $0x4,%edx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x802,%ecx
xor    %eax,%eax
call   1090 <pread@plt>
mov    0x1c(%rsp),%esi
lea    0xd8b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xd85(%rip),%rdx
lea    0x50(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x50,%esi
mov    %r14d,%ecx
call   1050 <snprintf@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
mov    %r12,%rbx
call   1080 <open@plt>
mov    %eax,0x2c(%rsp)
cmpl   $0x0,0x4(%rsp)
je     1470 <topology+0x300>
test   %ebx,%ebx
js     1470 <topology+0x300>
add    $0x1,%rbx
mov    $0xb,%ebp
xor    %r13d,%r13d
jmp    13bc <topology+0x24c>
cs nopw 0x0(%rax,%rax,1)
lea    0xd4b(%rip),%rdi
mov    0x28(%rsp),%r14d
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x4(%rsp),%eax
xor    %edx,%edx
div    %r14d
mov    %rbx,%r8
movzbl (%rsp),%ebx
and    $0xf,%bl
mov    $0xffffffff,%esi
mov    %ebx,%ecx
shl    %cl,%esi
mov    %r12d,%edx
xor    %esi,%edx
not    %edx
mov    0x20(%rsp),%edi
and    %edi,%edx
mov    %r15d,%ecx
shr    %cl,%edx
and    %edi,%esi
mov    %ebx,%ecx
mov    %r8,%rbx
shr    %cl,%esi
lea    0xd20(%rip),%rdi
mov    0x24(%rsp),%ecx
mov    %eax,%r8d
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%r13
movabs $0x100000000,%rax
add    %rax,%rbp
cmp    %r13,%rbx
je     1470 <topology+0x300>
mov    $0x10,%edx
mov    0x2c(%rsp),%edi
lea    0x30(%rsp),%rsi
mov    %rbp,%rcx
xor    %eax,%eax
call   1090 <pread@plt>
mov    0x30(%rsp),%edx
mov    0x34(%rsp),%ecx
mov    0x38(%rsp),%r8d
mov    0x3c(%rsp),%r9d
lea    0xc95(%rip),%rdi
lea    0x50(%rsp),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xb,%edi
mov    %r13d,%esi
mov    %rsp,%rdx
call   1060 <cpuID@plt>
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
mov    0x8(%rsp),%ecx
mov    0xc(%rsp),%r8d
lea    0xc65(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x4(%rsp),%edx
test   %dx,%dx
je     1470 <topology+0x300>
movzbl 0x9(%rsp),%eax
cmp    $0x2,%al
je     1340 <topology+0x1d0>
cmp    $0x1,%al
jne    13a2 <topology+0x232>
mov    (%rsp),%r15d
and    $0xf,%r15d
mov    $0xffffffff,%r12d
mov    %r15d,%ecx
shl    %cl,%r12d
not    %r12d
mov    0x20(%rsp),%eax
and    %r12d,%eax
mov    %eax,0x24(%rsp)
mov    %edx,0x28(%rsp)
jmp    13a2 <topology+0x232>
xor    %eax,%eax
add    $0xf8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [12 x i8] c"%02x_%x %x\0A\00"
@.str.2 = [64 x i8] c"nr_ctrs %d, ctr_width %d, nr_fixed_ctrs %d, fixed_ctr_width %d\0A\00"
@.str.3 = [12 x i8] c"APIC ID %X\0A\00"
@.str.4 = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDONLY = global i32 0
@.str.5 = [8 x i8] c"msr %d\0A\00"
@.str.6 = [18 x i8] c"/dev/cpu/%d/cpuid\00"
@.str.7 = [16 x i8] c"%s %d %d %X %d\0A\00"
@.str.8 = [13 x i8] c"%d %d %X %d\0A\00"
@.str.9 = [34 x i8] c"logical cores at smt level %d %d\0A\00"
@.str.10 = [35 x i8] c"Pkg_ID Core_ID SMT_ID %d %d %d %d\0A\00"
define i32 @topology(i32 %0) {
%2 = alloca i32
%3 = alloca [4 x i32]
%4 = alloca i32
%5 = alloca [12 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca [80 x i8]
%16 = alloca i32
%17 = alloca i32
%18 = alloca [80 x i8]
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
%22 = alloca i32
%23 = alloca i32
%24 = alloca i32
%25 = alloca i32
%26 = alloca i32
%27 = alloca i32
%28 = alloca i32
%29 = alloca i32
%30 = alloca [4 x i32]
store i32 %0, i32* %2
%31 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%32 = call i32 @cpuID(i32 0, i32 0, i32* %31)
%33 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 1
%34 = load i32, i32* %33
%35 = getelementptr inbounds [12 x i8], [12 x i8]* %5, i64 0, i64 0
%36 = bitcast i8* %35 to i32*
%37 = getelementptr inbounds i32, i32* %36, i64 0
store i32 %34, i32* %37
%38 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 3
%39 = load i32, i32* %38
%40 = getelementptr inbounds [12 x i8], [12 x i8]* %5, i64 0, i64 0
%41 = bitcast i8* %40 to i32*
%42 = getelementptr inbounds i32, i32* %41, i64 1
store i32 %39, i32* %42
%43 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 2
%44 = load i32, i32* %43
%45 = getelementptr inbounds [12 x i8], [12 x i8]* %5, i64 0, i64 0
%46 = bitcast i8* %45 to i32*
%47 = getelementptr inbounds i32, i32* %46, i64 2
store i32 %44, i32* %47
%48 = getelementptr inbounds [12 x i8], [12 x i8]* %5, i64 0, i64 0
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %48)
%50 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%51 = load i32, i32* %50
store i32 %51, i32* %6
%52 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%53 = call i32 @cpuID(i32 1, i32 0, i32* %52)
%54 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%55 = load i32, i32* %54
%56 = and i32 %55, 15
store i32 %56, i32* %7
%57 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%58 = load i32, i32* %57
%59 = lshr i32 %58, 16
%60 = and i32 %59, 15
%61 = shl i32 %60, 4
%62 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%63 = load i32, i32* %62
%64 = lshr i32 %63, 4
%65 = and i32 %64, 15
%66 = or i32 %61, %65
store i32 %66, i32* %8
%67 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%68 = load i32, i32* %67
%69 = lshr i32 %68, 20
%70 = and i32 %69, 255
%71 = shl i32 %70, 4
%72 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%73 = load i32, i32* %72
%74 = lshr i32 %73, 8
%75 = and i32 %74, 15
%76 = or i32 %71, %75
store i32 %76, i32* %9
%77 = load i32, i32* %9
%78 = load i32, i32* %8
%79 = load i32, i32* %7
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %77, i32 %78, i32 %79)
%81 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%82 = call i32 @cpuID(i32 10, i32 0, i32* %81)
%83 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%84 = load i32, i32* %83
%85 = lshr i32 %84, 8
%86 = and i32 %85, 255
store i32 %86, i32* %10
%87 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%88 = load i32, i32* %87
%89 = lshr i32 %88, 16
%90 = and i32 %89, 255
store i32 %90, i32* %11
%91 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 3
%92 = load i32, i32* %91
%93 = and i32 %92, 31
store i32 %93, i32* %12
%94 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 3
%95 = load i32, i32* %94
%96 = lshr i32 %95, 5
%97 = and i32 %96, 255
store i32 %97, i32* %13
%98 = load i32, i32* %10
%99 = load i32, i32* %11
%100 = load i32, i32* %12
%101 = load i32, i32* %13
%102 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.2, i64 0, i64 0), i32 %98, i32 %99, i32 %100, i32 %101)
%103 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%104 = call i32 @cpuID(i32 11, i32 0, i32* %103)
%105 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 3
%106 = load i32, i32* %105
%107 = and i32 %106, -1
store i32 %107, i32* %14
%108 = load i32, i32* %14
%109 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %108)
store i32 -1, i32* %16
store i32 0, i32* %17
%110 = getelementptr inbounds [80 x i8], [80 x i8]* %15, i64 0, i64 0
%111 = load i32, i32* %2
%112 = call i32 @snprintf(i8* %110, i32 80, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %111)
%113 = getelementptr inbounds [80 x i8], [80 x i8]* %15, i64 0, i64 0
%114 = load i32, i32* @O_RDONLY
%115 = call i32 @open(i8* %113, i32 %114)
store i32 %115, i32* %16
store i32 3072, i32* %17
%116 = load i32, i32* %16
%117 = call i32 @pwrite(i32 %116, i32* %17, i32 4, i32 27)
store i32 0, i32* %17
%118 = load i32, i32* %16
%119 = call i32 (i32, ...) @pread(i32 %118, i32* %17, i64 4, i32 2050)
%120 = load i32, i32* %17
%121 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %120)
store i32 -1, i32* %19
%122 = getelementptr inbounds [80 x i8], [80 x i8]* %18, i64 0, i64 0
%123 = load i32, i32* %2
%124 = call i32 @snprintf(i8* %122, i32 80, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %123)
%125 = getelementptr inbounds [80 x i8], [80 x i8]* %18, i64 0, i64 0
%126 = load i32, i32* @O_RDONLY
%127 = call i32 @open(i8* %125, i32 %126)
store i32 %127, i32* %19
%128 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 1
%129 = load i32, i32* %128
%130 = icmp ne i32 %129, 0
br i1 %130, label %131, label %235
131: ; preds = %1
store i32 0, i32* %4
br label %132
132: ; preds = %231, %131
%133 = load i32, i32* %4
%134 = load i32, i32* %6
%135 = icmp sle i32 %133, %134
br i1 %135, label %136, label %234
136: ; preds = %132
%137 = load i32, i32* %19
%138 = getelementptr inbounds [4 x i32], [4 x i32]* %30, i64 0, i64 0
%139 = load i32, i32* %4
%140 = sext i32 %139 to i64
%141 = mul nsw i64 %140, 4294967296
%142 = or i64 %141, 11
%143 = call i32 (i32, ...) @pread(i32 %137, i32* %138, i64 16, i64 %142)
%144 = getelementptr inbounds [80 x i8], [80 x i8]* %18, i64 0, i64 0
%145 = getelementptr inbounds [4 x i32], [4 x i32]* %30, i64 0, i64 0
%146 = load i32, i32* %145
%147 = getelementptr inbounds [4 x i32], [4 x i32]* %30, i64 0, i64 1
%148 = load i32, i32* %147
%149 = getelementptr inbounds [4 x i32], [4 x i32]* %30, i64 0, i64 2
%150 = load i32, i32* %149
%151 = getelementptr inbounds [4 x i32], [4 x i32]* %30, i64 0, i64 3
%152 = load i32, i32* %151
%153 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i8* %144, i32 %146, i32 %148, i32 %150, i32 %152)
%154 = load i32, i32* %4
%155 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%156 = call i32 @cpuID(i32 11, i32 %154, i32* %155)
%157 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%158 = load i32, i32* %157
%159 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 1
%160 = load i32, i32* %159
%161 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 2
%162 = load i32, i32* %161
%163 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 3
%164 = load i32, i32* %163
%165 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i32 %158, i32 %160, i32 %162, i32 %164)
%166 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 1
%167 = load i32, i32* %166
%168 = and i32 %167, 65535
%169 = icmp eq i32 %168, 0
br i1 %169, label %170, label %171
170: ; preds = %136
br label %234
171: ; preds = %136
%172 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 2
%173 = load i32, i32* %172
%174 = lshr i32 %173, 8
%175 = and i32 %174, 255
%176 = icmp eq i32 %175, 1
br i1 %176, label %177, label %189
177: ; preds = %171
%178 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 1
%179 = load i32, i32* %178
store i32 %179, i32* %28
%180 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%181 = load i32, i32* %180
%182 = and i32 %181, 15
store i32 %182, i32* %20
%183 = load i32, i32* %20
%184 = shl i32 -1, %183
%185 = xor i32 %184, -1
store i32 %185, i32* %21
%186 = load i32, i32* %14
%187 = load i32, i32* %21
%188 = and i32 %186, %187
store i32 %188, i32* %25
br label %230
189: ; preds = %171
%190 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 2
%191 = load i32, i32* %190
%192 = lshr i32 %191, 8
%193 = and i32 %192, 255
%194 = icmp eq i32 %193, 2
br i1 %194, label %195, label %229
195: ; preds = %189
%196 = load i32, i32* %28
%197 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 1
%198 = load i32, i32* %197
%199 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.9, i64 0, i64 0), i32 %196, i32 %198)
%200 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 1
%201 = load i32, i32* %200
%202 = load i32, i32* %28
%203 = udiv i32 %201, %202
store i32 %203, i32* %29
%204 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%205 = load i32, i32* %204
%206 = and i32 %205, 15
store i32 %206, i32* %22
%207 = load i32, i32* %22
%208 = shl i32 -1, %207
%209 = xor i32 %208, -1
%210 = load i32, i32* %21
%211 = xor i32 %209, %210
store i32 %211, i32* %23
%212 = load i32, i32* %14
%213 = load i32, i32* %23
%214 = and i32 %212, %213
%215 = load i32, i32* %20
%216 = lshr i32 %214, %215
store i32 %216, i32* %26
%217 = load i32, i32* %22
%218 = shl i32 -1, %217
store i32 %218, i32* %24
%219 = load i32, i32* %14
%220 = load i32, i32* %24
%221 = and i32 %219, %220
%222 = load i32, i32* %22
%223 = lshr i32 %221, %222
store i32 %223, i32* %27
%224 = load i32, i32* %27
%225 = load i32, i32* %26
%226 = load i32, i32* %25
%227 = load i32, i32* %29
%228 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.10, i64 0, i64 0), i32 %224, i32 %225, i32 %226, i32 %227)
br label %229
229: ; preds = %195, %189
br label %230
230: ; preds = %229, %177
br label %231
231: ; preds = %230
%232 = load i32, i32* %4
%233 = add nsw i32 %232, 1
store i32 %233, i32* %4
br label %132
234: ; preds = %170, %132
br label %235
235: ; preds = %234, %1
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [12 x i8] c"%02x_%x %x\0A\00"
@.str.2 = [64 x i8] c"nr_ctrs %d, ctr_width %d, nr_fixed_ctrs %d, fixed_ctr_width %d\0A\00"
@.str.3 = [12 x i8] c"APIC ID %X\0A\00"
@.str.4 = [16 x i8] c"/dev/cpu/%d/msr\00"
@O_RDONLY = global i32 0
@.str.5 = [8 x i8] c"msr %d\0A\00"
@.str.6 = [18 x i8] c"/dev/cpu/%d/cpuid\00"
@.str.7 = [16 x i8] c"%s %d %d %X %d\0A\00"
@.str.8 = [13 x i8] c"%d %d %X %d\0A\00"
@.str.9 = [34 x i8] c"logical cores at smt level %d %d\0A\00"
@.str.10 = [35 x i8] c"Pkg_ID Core_ID SMT_ID %d %d %d %d\0A\00"
define i32 @topology(i32 %0) {
%2 = alloca [4 x i32]
%3 = alloca [12 x i8]
%4 = alloca [80 x i8]
%5 = alloca i32
%6 = alloca [80 x i8]
%7 = alloca [4 x i32]
%8 = bitcast [4 x i32]* %2 to i8*
%9 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%10 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 0
%11 = call i32 @cpuID(i32 0, i32 0, i32* %10)
%12 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 1
%13 = load i32, i32* %12
%14 = bitcast [12 x i8]* %3 to i32*
store i32 %13, i32* %14
%15 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 3
%16 = load i32, i32* %15
%17 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 4
%18 = bitcast i8* %17 to i32*
store i32 %16, i32* %18
%19 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 2
%20 = load i32, i32* %19
%21 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 8
%22 = bitcast i8* %21 to i32*
store i32 %20, i32* %22
%23 = call i32 @puts(i8* %9)
%24 = load i32, i32* %10
%25 = call i32 @cpuID(i32 1, i32 0, i32* %10)
%26 = load i32, i32* %10
%27 = and i32 %26, 15
%28 = lshr i32 %26, 12
%29 = and i32 %28, 240
%30 = lshr i32 %26, 4
%31 = and i32 %30, 15
%32 = or i32 %29, %31
%33 = lshr i32 %26, 16
%34 = and i32 %33, 4080
%35 = lshr i32 %26, 8
%36 = and i32 %35, 15
%37 = or i32 %34, %36
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %37, i32 %32, i32 %27)
%39 = call i32 @cpuID(i32 10, i32 0, i32* %10)
%40 = load i32, i32* %10
%41 = lshr i32 %40, 8
%42 = and i32 %41, 255
%43 = lshr i32 %40, 16
%44 = and i32 %43, 255
%45 = load i32, i32* %15
%46 = and i32 %45, 31
%47 = lshr i32 %45, 5
%48 = and i32 %47, 255
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @.str.2, i64 0, i64 0), i32 %42, i32 %44, i32 %46, i32 %48)
%50 = call i32 @cpuID(i32 11, i32 0, i32* %10)
%51 = load i32, i32* %15
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %51)
%53 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%54 = bitcast i32* %5 to i8*
%55 = call i32 @snprintf(i8* %53, i32 80, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %0)
%56 = load i32, i32* @O_RDONLY
%57 = call i32 @open(i8* %53, i32 %56)
store i32 3072, i32* %5
%58 = call i32 @pwrite(i32 %57, i32* %5, i32 4, i32 27)
store i32 0, i32* %5
%59 = call i32 (i32, ...) @pread(i32 %57, i32* %5, i64 4, i32 2050)
%60 = load i32, i32* %5
%61 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %60)
%62 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%63 = call i32 @snprintf(i8* %62, i32 80, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %0)
%64 = load i32, i32* @O_RDONLY
%65 = call i32 @open(i8* %62, i32 %64)
%66 = load i32, i32* %12
%67 = icmp eq i32 %66, 0
%68 = icmp slt i32 %24, 0
%69 = select i1 %67, i1 true, i1 %68
br i1 %69, label %134, label %70
70: ; preds = %1
%71 = bitcast [4 x i32]* %7 to i8*
%72 = getelementptr inbounds [4 x i32], [4 x i32]* %7, i64 0, i64 0
%73 = getelementptr inbounds [4 x i32], [4 x i32]* %7, i64 0, i64 1
%74 = getelementptr inbounds [4 x i32], [4 x i32]* %7, i64 0, i64 2
%75 = getelementptr inbounds [4 x i32], [4 x i32]* %7, i64 0, i64 3
%76 = add nuw i32 %24, 1
%77 = zext i32 %76 to i64
br label %78
78: ; preds = %70, %127
%79 = phi i64 [ 0, %70 ], [ %132, %127 ]
%80 = phi i32 [ undef, %70 ], [ %131, %127 ]
%81 = phi i32 [ undef, %70 ], [ %130, %127 ]
%82 = phi i32 [ undef, %70 ], [ %129, %127 ]
%83 = phi i32 [ undef, %70 ], [ %128, %127 ]
%84 = shl nuw nsw i64 %79, 32
%85 = or i64 %84, 11
%86 = call i32 (i32, ...) @pread(i32 %65, i32* %72, i64 16, i64 %85)
%87 = load i32, i32* %72
%88 = load i32, i32* %73
%89 = load i32, i32* %74
%90 = load i32, i32* %75
%91 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i8* %62, i32 %87, i32 %88, i32 %89, i32 %90)
%92 = trunc i64 %79 to i32
%93 = call i32 @cpuID(i32 11, i32 %92, i32* %10)
%94 = load i32, i32* %10
%95 = load i32, i32* %12
%96 = load i32, i32* %19
%97 = load i32, i32* %15
%98 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), i32 %94, i32 %95, i32 %96, i32 %97)
%99 = load i32, i32* %12
%100 = and i32 %99, 65535
%101 = icmp eq i32 %100, 0
br i1 %101, label %126, label %102
102: ; preds = %78
%103 = load i32, i32* %19
%104 = lshr i32 %103, 8
%105 = trunc i32 %104 to i8
switch i8 %105, label %127 [
i8 1, label %106
i8 2, label %112
]
106: ; preds = %102
%107 = load i32, i32* %10
%108 = and i32 %107, 15
%109 = shl nsw i32 -1, %108
%110 = xor i32 %109, -1
%111 = and i32 %51, %110
br label %127
112: ; preds = %102
%113 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.9, i64 0, i64 0), i32 %80, i32 %99)
%114 = load i32, i32* %12
%115 = udiv i32 %114, %80
%116 = load i32, i32* %10
%117 = and i32 %116, 15
%118 = shl nsw i32 -1, %117
%119 = xor i32 %83, %118
%120 = xor i32 %119, -1
%121 = and i32 %51, %120
%122 = lshr i32 %121, %81
%123 = and i32 %118, %51
%124 = lshr i32 %123, %117
%125 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.10, i64 0, i64 0), i32 %124, i32 %122, i32 %82, i32 %115)
br label %127
126: ; preds = %78
br label %134
127: ; preds = %102, %112, %106
%128 = phi i32 [ %83, %102 ], [ %83, %112 ], [ %110, %106 ]
%129 = phi i32 [ %82, %102 ], [ %82, %112 ], [ %111, %106 ]
%130 = phi i32 [ %81, %102 ], [ %81, %112 ], [ %108, %106 ]
%131 = phi i32 [ %80, %102 ], [ %80, %112 ], [ %99, %106 ]
%132 = add nuw nsw i64 %79, 1
%133 = icmp eq i64 %132, %77
br i1 %133, label %134, label %78
134: ; preds = %127, %126, %1
ret i32 0
}