#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*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int sched_getcpu();
int /*<<< orphan*/ sprintf(char *, char *, int);
int sscanf(char *, char *, unsigned long *);
int /*<<< orphan*/ stderr;
double get_core_freq(void) {
  int cpu;
  FILE *f;
  char buf[256];
  unsigned long khz = 0.0;
  int rc;
  cpu = sched_getcpu();
  if (cpu < 0) {
    perror("sched_getcpu");
    return 0.0;
  }
  sprintf(buf, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", cpu);
  f = fopen(buf, "r");
  if (!f) {
    return 0.0;
  }
  while (fgets(buf, sizeof(buf), f)) {
    rc = sscanf(buf, "%lu", &khz);
    if (rc != 1) {
      fclose(f);
      return 0;
    }
    fclose(f);
    return khz / 1000.0;
  }
  fprintf(stderr, "Empty cpufreq\n");
  fclose(f);
  return 0.0;
}


x86 assembly (clang, -O0):
<get_core_freq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movq   $0x0,-0x128(%rbp)
mov    $0x0,%al
call   1090 <sched_getcpu@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11c3 <get_core_freq+0x43>
lea    0xe7f(%rip),%rdi
call   1080 <perror@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12de <get_core_freq+0x15e>
lea    -0x120(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe69(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x120(%rbp),%rdi
lea    0xe8d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1208 <get_core_freq+0x88>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12de <get_core_freq+0x15e>
jmp    120d <get_core_freq+0x8d>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     12b8 <get_core_freq+0x138>
lea    -0x120(%rbp),%rdi
lea    0xe3c(%rip),%rsi
lea    -0x128(%rbp),%rdx
call   1060 <sscanf@plt>
mov    %eax,-0x12c(%rbp)
cmpl   $0x1,-0x12c(%rbp)
je     126f <get_core_freq+0xef>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12de <get_core_freq+0x15e>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movaps 0xd91(%rip),%xmm1
movq   -0x128(%rbp),%xmm0
ff
punpckldq %xmm1,%xmm0
movapd 0xd8d(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xd56(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12de <get_core_freq+0x15e>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
lea    0xdb2(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_core_freq>:
push   %rbp
push   %rbx
sub    $0x118,%rsp
movq   $0x0,0x8(%rsp)
xor    %eax,%eax
call   1090 <sched_getcpu@plt>
test   %eax,%eax
js     1260 <get_core_freq+0xe0>
lea    0xe8d(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   10a0 <sprintf@plt>
lea    0xeae(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
xorpd  %xmm0,%xmm0
test   %rax,%rax
je     1270 <get_core_freq+0xf0>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1241 <get_core_freq+0xc1>
lea    0xe7a(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rdx
call   1060 <sscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
cmp    $0x1,%ebp
xorpd  %xmm0,%xmm0
jne    1270 <get_core_freq+0xf0>
movsd  0x8(%rsp),%xmm1
unpcklps 0xddd(%rip),%xmm1
subpd  0xde5(%rip),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
addsd  %xmm1,%xmm0
divsd  0xde1(%rip),%xmm0
jmp    1270 <get_core_freq+0xf0>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
lea    0xe21(%rip),%rsi
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    126c <get_core_freq+0xec>
lea    0xdc1(%rip),%rdi
call   1080 <perror@plt>
xorpd  %xmm0,%xmm0
add    $0x118,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"sched_getcpu\00"
@.str.1 = [55 x i8] c"/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [4 x i8] c"%lu\00"
@stderr = global i32 0
@.str.4 = [15 x i8] c"Empty cpufreq\0A\00"
define double @get_core_freq() {
%1 = alloca double
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [256 x i8]
%5 = alloca i64
%6 = alloca i32
store i64 0, i64* %5
%7 = call i32 (...) @sched_getcpu()
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store double 0.000000e+00, double* %1
br label %46
12: ; preds = %0
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%14 = load i32, i32* %2
%15 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %17, i32** %3
%18 = load i32*, i32** %3
%19 = icmp ne i32* %18, null
br i1 %19, label %21, label %20
20: ; preds = %12
store double 0.000000e+00, double* %1
br label %46
21: ; preds = %12
br label %22
22: ; preds = %21
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%24 = load i32*, i32** %3
%25 = call i64 @fgets(i8* %23, i32 256, i32* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %41
27: ; preds = %22
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%29 = call i32 @sscanf(i8* %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64* %5)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp ne i32 %30, 1
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = load i32*, i32** %3
%34 = call i32 @fclose(i32* %33)
store double 0.000000e+00, double* %1
br label %46
35: ; preds = %27
%36 = load i32*, i32** %3
%37 = call i32 @fclose(i32* %36)
%38 = load i64, i64* %5
%39 = uitofp i64 %38 to double
%40 = fdiv double %39, 1.000000e+03
store double %40, double* %1
br label %46
41: ; preds = %22
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%44 = load i32*, i32** %3
%45 = call i32 @fclose(i32* %44)
store double 0.000000e+00, double* %1
br label %46
46: ; preds = %41, %35, %32, %20, %10
%47 = load double, double* %1
ret double %47
}

LLVM IR (-O2):
@.str = [13 x i8] c"sched_getcpu\00"
@.str.1 = [55 x i8] c"/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [4 x i8] c"%lu\00"
@stderr = global i32 0
@.str.4 = [15 x i8] c"Empty cpufreq\0A\00"
define double @get_core_freq() {
%1 = alloca [256 x i8]
%2 = alloca i64
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%5 = tail call i32 (...) @sched_getcpu()
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %28
9: ; preds = %0
%10 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%11 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %28, label %13
13: ; preds = %9
%14 = call i64 @fgets(i8* %3, i32 256, i32* %11)
%15 = icmp eq i64 %14, 0
br i1 %15, label %24, label %16
16: ; preds = %13
%17 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64* %2)
%18 = icmp eq i32 %17, 1
%19 = call i32 @fclose(i32* %11)
br i1 %18, label %20, label %28
20: ; preds = %16
%21 = load i64, i64* %2
%22 = uitofp i64 %21 to double
%23 = fdiv double %22, 1.000000e+03
br label %28
24: ; preds = %13
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%27 = call i32 @fclose(i32* %11)
br label %28
28: ; preds = %16, %9, %24, %20, %7
%29 = phi double [ 0.000000e+00, %7 ], [ %23, %20 ], [ 0.000000e+00, %24 ], [ 0.000000e+00, %9 ], [ 0.000000e+00, %16 ]
ret double %29
}