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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int /*<<< orphan*/ stderr;
unsigned long strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
unsigned long get_current_cpu_frequency(unsigned int cpu) {
  FILE *fp;
  char buffer[128];
  unsigned long curr_freq = 0;
  snprintf(buffer, sizeof(buffer),
           "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq", cpu);
  fp = fopen(buffer, "r");
  if (fp == NULL) {
    fprintf(stderr, "get_current_cpu_frequency: Failed to open %s\n", buffer);
    return 0;
  }
  if (fgets(buffer, sizeof(buffer), fp) != NULL) {
    curr_freq = strtoul(buffer, NULL, 0);
  }
  fclose(fp);
  return curr_freq;
}


x86 assembly (clang, -O0):
<get_current_cpu_frequency>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0xc(%rbp)
movq   $0x0,-0xa8(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x80,%esi
lea    0xe71(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11df <get_current_cpu_frequency+0x7f>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    -0xa0(%rbp),%rdx
lea    0xe6c(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    122b <get_current_cpu_frequency+0xcb>
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x80,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1217 <get_current_cpu_frequency+0xb7>
lea    -0xa0(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %rax,-0xa8(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0xa8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_current_cpu_frequency>:
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %edi,%ecx
lea    0xe8d(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
call   1040 <snprintf@plt>
lea    0xead(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c0 <get_current_cpu_frequency+0x60>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11dd <get_current_cpu_frequency+0x7d>
mov    %rsp,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %rax,%r14
jmp    11e0 <get_current_cpu_frequency+0x80>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe69(%rip),%rsi
mov    %rsp,%rdx
call   1060 <fprintf@plt>
xor    %r14d,%r14d
jmp    11e8 <get_current_cpu_frequency+0x88>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r14,%rax
add    $0x88,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [55 x i8] c"/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [46 x i8] c"get_current_cpu_frequency: Failed to open %s\0A\00"
define i64 @get_current_cpu_frequency(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [128 x i8]
%6 = alloca i64
store i32 %0, i32* %3
store i64 0, i64* %6
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @snprintf(i8* %7, i32 128, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %4
%12 = load i32*, i32** %4
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i8* %16)
store i64 0, i64* %2
br label %30
18: ; preds = %1
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%20 = load i32*, i32** %4
%21 = call i32* @fgets(i8* %19, i32 128, i32* %20)
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%25 = call i64 @strtoul(i8* %24, i32* null, i32 0)
store i64 %25, i64* %6
br label %26
26: ; preds = %23, %18
%27 = load i32*, i32** %4
%28 = call i32 @fclose(i32* %27)
%29 = load i64, i64* %6
store i64 %29, i64* %2
br label %30
30: ; preds = %26, %14
%31 = load i64, i64* %2
ret i64 %31
}

LLVM IR (-O2):
@.str = [55 x i8] c"/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [46 x i8] c"get_current_cpu_frequency: Failed to open %s\0A\00"
define i64 @get_current_cpu_frequency(i32 %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 128, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i8* %3)
br label %18
10: ; preds = %1
%11 = call i32* @fgets(i8* %3, i32 128, i32* %5)
%12 = icmp eq i32* %11, null
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = call i64 @strtoul(i8* nocapture %3, i32* null, i32 0)
br label %15
15: ; preds = %13, %10
%16 = phi i64 [ %14, %13 ], [ 0, %10 ]
%17 = call i32 @fclose(i32* %5)
br label %18
18: ; preds = %15, %7
%19 = phi i64 [ 0, %7 ], [ %16, %15 ]
ret i64 %19
}