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

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int strtol(char *, int /*<<< orphan*/ *, int);
int sysctlbyname(char *, char *, size_t *, int /*<<< orphan*/ *,
                 int /*<<< orphan*/);
int get_max_cpufreq_level(int cpu) {
  char buf[512], sysctl_freq[32];
  size_t len = sizeof(buf) / sizeof(buf[0]);
  int r;
  snprintf(sysctl_freq, sizeof(sysctl_freq) / sizeof(sysctl_freq[0]),
           "dev.cpu.%d.freq_levels", cpu);
  r = sysctlbyname(sysctl_freq, buf, &len, NULL, 0);
  if (r < 0)
    return -1;
  r = strtol(buf, NULL, 10);
  return r;
}


x86 assembly (clang, -O0):
<get_max_cpufreq_level>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %edi,-0x8(%rbp)
movq   $0x200,-0x238(%rbp)
lea    -0x230(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x20,%esi
lea    0xea1(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x230(%rbp),%rdi
lea    -0x210(%rbp),%rsi
lea    -0x238(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
xor    %r8d,%r8d
call   1050 <sysctlbyname@plt>
mov    %eax,-0x23c(%rbp)
cmpl   $0x0,-0x23c(%rbp)
jge    11a4 <get_max_cpufreq_level+0x74>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c8 <get_max_cpufreq_level+0x98>
lea    -0x210(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %eax,-0x23c(%rbp)
mov    -0x23c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_max_cpufreq_level>:
push   %rbx
sub    $0x230,%rsp
mov    %edi,%ecx
movq   $0x200,0x8(%rsp)
lea    0xeb6(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1030 <snprintf@plt>
lea    0x30(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <sysctlbyname@plt>
test   %eax,%eax
js     118a <get_max_cpufreq_level+0x5a>
lea    0x30(%rsp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
jmp    118f <get_max_cpufreq_level+0x5f>
mov    $0xffffffff,%eax
add    $0x230,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [23 x i8] c"dev.cpu.%d.freq_levels\00"
define i32 @get_max_cpufreq_level(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [512 x i8]
%5 = alloca [32 x i8]
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %3
store i64 512, i64* %6
%8 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i32 @snprintf(i8* %8, i32 32, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%13 = call i32 @sysctlbyname(i8* %11, i8* %12, i64* %6, i32* null, i32 0)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 -1, i32* %2
br label %21
17: ; preds = %1
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%19 = call i32 @strtol(i8* %18, i32* null, i32 10)
store i32 %19, i32* %7
%20 = load i32, i32* %7
store i32 %20, i32* %2
br label %21
21: ; preds = %17, %16
%22 = load i32, i32* %2
ret i32 %22
}

LLVM IR (-O2):
@.str = [23 x i8] c"dev.cpu.%d.freq_levels\00"
define i32 @get_max_cpufreq_level(i32 %0) {
%2 = alloca [512 x i8]
%3 = alloca [32 x i8]
%4 = alloca i64
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%7 = bitcast i64* %4 to i8*
store i64 512, i64* %4
%8 = call i32 @snprintf(i8* %6, i32 32, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %0)
%9 = call i32 @sysctlbyname(i8* %6, i8* %5, i64* %4, i32* null, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %1
%12 = call i32 @strtol(i8* nocapture %5, i32* null, i32 10)
br label %13
13: ; preds = %1, %11
%14 = phi i32 [ %12, %11 ], [ -1, %1 ]
ret i32 %14
}