double get_dram_unit(long unsigned cpu) {
  static double dram_unit = -1.0;
  if (dram_unit > 0.0)
    return dram_unit;
  unsigned int eax = 1, ebx = 0, ecx = 0, edx = 0;
  cpuid(&eax, &ebx, &ecx, &edx);
  if (((eax >> 8) & 0xF) != 6) {
    x86_energy_set_error_string(
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/tud-zih-energy/x86_energy/refs/heads/master/"
        "src/access/msr.c",
        __func__, 193, "Unknown CPU family %x", ((eax >> 8) & 0xF));
    return 0;
  }
  switch ((((eax >> 16) & 0xF) << 4) + ((eax >> 4) & 0xF)) {
  case 0x3f:
  case 0x4e:
  case 0x55:
    dram_unit = 1.0 / pow(2.0, 16.0);
    break;
  default:
    dram_unit = get_default_unit(cpu);
  }
  return dram_unit;
}


x86 assembly (clang, -O0):
<get_dram_unit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movsd  0x2eec(%rip),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1173 <get_dram_unit+0x33>
movsd  0x2ed7(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    128f <get_dram_unit+0x14f>
movl   $0x1,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
lea    -0x14(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%rcx
mov    $0x0,%al
call   1060 <cpuid@plt>
mov    -0x14(%rbp),%eax
shr    $0x8,%eax
and    $0xf,%eax
cmp    $0x6,%eax
je     11f2 <get_dram_unit+0xb2>
mov    -0x14(%rbp),%r8d
shr    $0x8,%r8d
and    $0xf,%r8d
lea    0xe4d(%rip),%rdi
lea    0xedd(%rip),%rsi
mov    $0xc1,%edx
lea    0xedf(%rip),%rcx
mov    $0x0,%al
call   1030 <x86_energy_set_error_string@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    128f <get_dram_unit+0x14f>
mov    -0x14(%rbp),%ecx
mov    %ecx,%eax
shr    $0xc,%eax
and    $0xf0,%eax
shr    $0x4,%ecx
and    $0xf,%ecx
or     %ecx,%eax
mov    %eax,-0x24(%rbp)
sub    $0x3f,%eax
je     123a <get_dram_unit+0xfa>
jmp    1218 <get_dram_unit+0xd8>
mov    -0x24(%rbp),%eax
sub    $0x4e,%eax
je     123a <get_dram_unit+0xfa>
jmp    1229 <get_dram_unit+0xe9>
mov    -0x24(%rbp),%eax
sub    $0x55,%eax
jne    126b <get_dram_unit+0x12b>
jmp    123a <get_dram_unit+0xfa>
movsd  0xdc6(%rip),%xmm0
movsd  0xdc6(%rip),%xmm1
call   1040 <pow@plt>
movaps %xmm0,%xmm1
movsd  0xda6(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,0x2dda(%rip)
jmp    1282 <get_dram_unit+0x142>
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1050 <get_default_unit@plt>
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x2dbe(%rip)
movsd  0x2db6(%rip),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_dram_unit>:
push   %rbx
sub    $0x10,%rsp
movsd  0x2efb(%rip),%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
ja     120b <get_dram_unit+0xdb>
mov    %rdi,%rbx
movl   $0x1,(%rsp)
movl   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
movl   $0x0,0x4(%rsp)
mov    %rsp,%rdi
lea    0xc(%rsp),%rsi
lea    0x8(%rsp),%rdx
lea    0x4(%rsp),%rcx
xor    %eax,%eax
call   1050 <cpuid@plt>
mov    (%rsp),%eax
mov    %eax,%r8d
shr    $0x8,%r8d
and    $0xf,%r8d
cmp    $0x6,%r8d
jne    11cb <get_dram_unit+0x9b>
mov    %eax,%ecx
shr    $0xc,%ecx
and    $0xf0,%ecx
shr    $0x4,%eax
and    $0xf,%eax
or     %ecx,%eax
add    $0xc1,%al
cmp    $0x16,%al
ja     11f2 <get_dram_unit+0xc2>
movzbl %al,%eax
mov    $0x408001,%ecx
bt     %rax,%rcx
jae    11f2 <get_dram_unit+0xc2>
movsd  0xe37(%rip),%xmm0
jmp    1203 <get_dram_unit+0xd3>
lea    0xe36(%rip),%rdi
lea    0xec6(%rip),%rsi
lea    0xecd(%rip),%rcx
mov    $0xc1,%edx
xor    %eax,%eax
call   1030 <x86_energy_set_error_string@plt>
xorpd  %xmm0,%xmm0
jmp    120b <get_dram_unit+0xdb>
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <get_default_unit@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x2e2d(%rip)
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@get_dram_unit.dram_unit = internal global double -1.000000e+00
@.str = [151 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/tud-zih-energy/x86_energy/refs/heads/master/src/access/msr.c\00"
@__func__.get_dram_unit = [14 x i8] c"get_dram_unit\00"
@.str.1 = [22 x i8] c"Unknown CPU family %x\00"
define double @get_dram_unit(i64 %0) {
%2 = alloca double
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i64 %0, i64* %3
%8 = load double, double* @get_dram_unit.dram_unit
%9 = fcmp ogt double %8, 0.000000e+00
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load double, double* @get_dram_unit.dram_unit
store double %11, double* %2
br label %41
12: ; preds = %1
store i32 1, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
%13 = call i32 (i32*, i32*, i32*, i32*, ...) bitcast (i32 (...)* @cpuid to i32 (i32*, i32*, i32*, i32*, ...)*)(i32* %4, i32* %5, i32* %6, i32* %7)
%14 = load i32, i32* %4
%15 = lshr i32 %14, 8
%16 = and i32 %15, 15
%17 = icmp ne i32 %16, 6
br i1 %17, label %18, label %23
18: ; preds = %12
%19 = load i32, i32* %4
%20 = lshr i32 %19, 8
%21 = and i32 %20, 15
%22 = call i32 (i8*, i8*, i32, i8*, i32, ...) bitcast (i32 (...)* @x86_energy_set_error_string to i32 (i8*, i8*, i32, i8*, i32, ...)*)(i8* getelementptr inbounds ([151 x i8], [151 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__func__.get_dram_unit, i64 0, i64 0), i32 193, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %21)
store double 0.000000e+00, double* %2
br label %41
23: ; preds = %12
%24 = load i32, i32* %4
%25 = lshr i32 %24, 16
%26 = and i32 %25, 15
%27 = shl i32 %26, 4
%28 = load i32, i32* %4
%29 = lshr i32 %28, 4
%30 = and i32 %29, 15
%31 = add i32 %27, %30
switch i32 %31, label %35 [
i32 63, label %32
i32 78, label %32
i32 85, label %32
]
32: ; preds = %23, %23, %23
%33 = call double @pow(double 2.000000e+00, double 1.600000e+01)
%34 = fdiv double 1.000000e+00, %33
store double %34, double* @get_dram_unit.dram_unit
br label %39
35: ; preds = %23
%36 = load i64, i64* %3
%37 = call i32 (i64, ...) bitcast (i32 (...)* @get_default_unit to i32 (i64, ...)*)(i64 %36)
%38 = sitofp i32 %37 to double
store double %38, double* @get_dram_unit.dram_unit
br label %39
39: ; preds = %35, %32
%40 = load double, double* @get_dram_unit.dram_unit
store double %40, double* %2
br label %41
41: ; preds = %39, %18, %10
%42 = load double, double* %2
ret double %42
}

LLVM IR (-O2):
@get_dram_unit.dram_unit = internal unnamed_addr global double -1.000000e+00
@.str = [151 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/tud-zih-energy/x86_energy/refs/heads/master/src/access/msr.c\00"
@__func__.get_dram_unit = [14 x i8] c"get_dram_unit\00"
@.str.1 = [22 x i8] c"Unknown CPU family %x\00"
define double @get_dram_unit(i64 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = load double, double* @get_dram_unit.dram_unit
%7 = fcmp ogt double %6, 0.000000e+00
br i1 %7, label %34, label %8
8: ; preds = %1
%9 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%10 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%11 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%12 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%13 = call i32 (i32*, i32*, i32*, i32*, ...) bitcast (i32 (...)* @cpuid to i32 (i32*, i32*, i32*, i32*, ...)*)(i32* %2, i32* %3, i32* %4, i32* %5)
%14 = load i32, i32* %2
%15 = lshr i32 %14, 8
%16 = and i32 %15, 15
%17 = icmp eq i32 %16, 6
br i1 %17, label %20, label %18
18: ; preds = %8
%19 = call i32 (i8*, i8*, i32, i8*, i32, ...) bitcast (i32 (...)* @x86_energy_set_error_string to i32 (i8*, i8*, i32, i8*, i32, ...)*)(i8* getelementptr inbounds ([151 x i8], [151 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @__func__.get_dram_unit, i64 0, i64 0), i32 193, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %16)
br label %32
20: ; preds = %8
%21 = lshr i32 %14, 12
%22 = and i32 %21, 240
%23 = lshr i32 %14, 4
%24 = and i32 %23, 15
%25 = or i32 %22, %24
%26 = trunc i32 %25 to i8
switch i8 %26, label %27 [
i8 63, label %30
i8 78, label %30
i8 85, label %30
]
27: ; preds = %20
%28 = call i32 (i64, ...) bitcast (i32 (...)* @get_default_unit to i32 (i64, ...)*)(i64 %0)
%29 = sitofp i32 %28 to double
br label %30
30: ; preds = %20, %20, %20, %27
%31 = phi double [ %29, %27 ], [ 0x3EF0000000000000, %20 ], [ 0x3EF0000000000000, %20 ], [ 0x3EF0000000000000, %20 ]
store double %31, double* @get_dram_unit.dram_unit
br label %32
32: ; preds = %30, %18
%33 = phi double [ 0.000000e+00, %18 ], [ %31, %30 ]
br label %34
34: ; preds = %1, %32
%35 = phi double [ %33, %32 ], [ %6, %1 ]
ret double %35
}