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

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ sysfs__read_int(char *, int *);
int sysfs__read_ull(char *, unsigned long long *);
int cpu__get_max_freq(unsigned long long *freq) {
  char entry[4096];
  int cpu;
  if (sysfs__read_int("devices/system/cpu/online", &cpu) < 0) {
    return -1;
  }
  snprintf(entry, sizeof(entry),
           "devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpu);
  return sysfs__read_ull(entry, freq);
}


x86 assembly (clang, -O0):
<cpu__get_max_freq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xeba(%rip),%rdi
lea    -0x1014(%rbp),%rsi
call   1030 <sysfs__read_int@plt>
cmp    $0x0,%rax
jge    1168 <cpu__get_max_freq+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    1199 <cpu__get_max_freq+0x69>
lea    -0x1010(%rbp),%rdi
mov    -0x1014(%rbp),%ecx
mov    $0x1000,%esi
lea    0xe99(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x1010(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <sysfs__read_ull@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpu__get_max_freq>:
push   %r14
push   %rbx
sub    $0x1018,%rsp
mov    %rdi,%rbx
lea    0xebc(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1030 <sysfs__read_int@plt>
test   %rax,%rax
js     117d <cpu__get_max_freq+0x4d>
mov    0xc(%rsp),%ecx
lea    0xebc(%rip),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x1000,%esi
call   1040 <snprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <sysfs__read_ull@plt>
jmp    1182 <cpu__get_max_freq+0x52>
mov    $0xffffffff,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [26 x i8] c"devices/system/cpu/online\00"
@.str.1 = [50 x i8] c"devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq\00"
define i32 @cpu__get_max_freq(i64* %0) {
%2 = alloca i32
%3 = alloca i64*
%4 = alloca [4096 x i8]
%5 = alloca i32
store i64* %0, i64** %3
%6 = call i64 @sysfs__read_int(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 -1, i32* %2
br label %16
9: ; preds = %1
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%11 = load i32, i32* %5
%12 = call i32 @snprintf(i8* %10, i32 4096, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%14 = load i64*, i64** %3
%15 = call i32 @sysfs__read_ull(i8* %13, i64* %14)
store i32 %15, i32* %2
br label %16
16: ; preds = %9, %8
%17 = load i32, i32* %2
ret i32 %17
}

LLVM IR (-O2):
@.str = [26 x i8] c"devices/system/cpu/online\00"
@.str.1 = [50 x i8] c"devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq\00"
define i32 @cpu__get_max_freq(i64* %0) {
%2 = alloca [4096 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%5 = bitcast i32* %3 to i8*
%6 = call i64 @sysfs__read_int(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32* %3)
%7 = icmp slt i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %1
%9 = load i32, i32* %3
%10 = call i32 @snprintf(i8* %4, i32 4096, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = call i32 @sysfs__read_ull(i8* %4, i64* %0)
br label %12
12: ; preds = %1, %8
%13 = phi i32 [ %11, %8 ], [ -1, %1 ]
ret i32 %13
}