#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*/ FILE;

/* Variables and functions */
int asprintf(char **, char *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int cpu_scaling_max_freq(int cpu) {
  int max_freq;
  FILE *fp;
  char *filename = NULL;
  if (asprintf(&filename,
               "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq",
               cpu) == -1) {
    return -1;
  }
  fp = fopen(filename, "r");
  free(filename);
  if (!fp)
    return -1;
  if (fscanf(fp, "%d", &max_freq) != 1)
    max_freq = -1;
  fclose(fp);
  return max_freq;
}


x86 assembly (clang, -O0):
<cpu_scaling_max_freq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edx
lea    -0x20(%rbp),%rdi
lea    0xe8f(%rip),%rsi
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    118b <cpu_scaling_max_freq+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <cpu_scaling_max_freq+0xa2>
mov    -0x20(%rbp),%rdi
lea    0xea1(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x18(%rbp)
jne    11bf <cpu_scaling_max_freq+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <cpu_scaling_max_freq+0xa2>
mov    -0x18(%rbp),%rdi
lea    0xe6f(%rip),%rsi
lea    -0xc(%rbp),%rdx
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     11e3 <cpu_scaling_max_freq+0x93>
movl   $0xffffffff,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpu_scaling_max_freq>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%edx
movq   $0x0,0x10(%rsp)
lea    0xe98(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1060 <asprintf@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11cc <cpu_scaling_max_freq+0x7c>
mov    0x10(%rsp),%rdi
lea    0xeaf(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
test   %rbx,%rbx
je     11cc <cpu_scaling_max_freq+0x7c>
lea    0xe93(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     11c0 <cpu_scaling_max_freq+0x70>
movl   $0xffffffff,0xc(%rsp)
ff
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0xc(%rsp),%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [55 x i8] c"/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @cpu_scaling_max_freq(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
store i32 %0, i32* %3
store i8* null, i8** %6
%7 = load i32, i32* %3
%8 = call i32 @asprintf(i8** %6, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %28
11: ; preds = %1
%12 = load i8*, i8** %6
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %5
%14 = load i8*, i8** %6
%15 = call i32 @free(i8* %14)
%16 = load i32*, i32** %5
%17 = icmp ne i32* %16, null
br i1 %17, label %19, label %18
18: ; preds = %11
store i32 -1, i32* %2
br label %28
19: ; preds = %11
%20 = load i32*, i32** %5
%21 = call i32 @fscanf(i32* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%22 = icmp ne i32 %21, 1
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 -1, i32* %4
br label %24
24: ; preds = %23, %19
%25 = load i32*, i32** %5
%26 = call i32 @fclose(i32* %25)
%27 = load i32, i32* %4
store i32 %27, i32* %2
br label %28
28: ; preds = %24, %18, %10
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@.str = [55 x i8] c"/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @cpu_scaling_max_freq(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = bitcast i32* %2 to i8*
%5 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%6 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %21, label %8
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = load i8*, i8** %3
%12 = call i32 @free(i8* %11)
%13 = icmp eq i32* %10, null
br i1 %13, label %21, label %14
14: ; preds = %8
%15 = call i32 @fscanf(i32* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%16 = icmp eq i32 %15, 1
br i1 %16, label %18, label %17
17: ; preds = %14
store i32 -1, i32* %2
br label %18
18: ; preds = %17, %14
%19 = call i32 @fclose(i32* %10)
%20 = load i32, i32* %2
br label %21
21: ; preds = %8, %1, %18
%22 = phi i32 [ %20, %18 ], [ -1, %1 ], [ -1, %8 ]
ret i32 %22
}