#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int sscanf(char *, char *, unsigned long *);
int strlen(char *);
unsigned long proc_get_freq_kernel(unsigned int cpu) {
  FILE *fp;
  char value[255];
  char file[255];
  unsigned long value2;
  snprintf(file, 255, "/proc/sys/cpu/%u/speed", cpu);
  fp = fopen(file, "r");
  if (!fp)
    return 0;
  fgets(value, 255, fp);
  fclose(fp);
  if (strlen(value) > (255 - 10)) {
    return 0;
  }
  if (sscanf(value, "%lu", &value2) != 1)
    return 0;
  return value2;
}


x86 assembly (clang, -O0):
<proc_get_freq_kernel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %edi,-0xc(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0xff,%esi
lea    0xe7c(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x220(%rbp),%rdi
lea    0xe80(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b8 <proc_get_freq_kernel+0x58>
movq   $0x0,-0x8(%rbp)
jmp    1235 <proc_get_freq_kernel+0xd5>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0xff,%esi
call   1060 <fgets@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x120(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0xf5,%eax
jle    11fa <proc_get_freq_kernel+0x9a>
movq   $0x0,-0x8(%rbp)
jmp    1235 <proc_get_freq_kernel+0xd5>
lea    -0x120(%rbp),%rdi
lea    0xe11(%rip),%rsi
lea    -0x228(%rbp),%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
je     122a <proc_get_freq_kernel+0xca>
movq   $0x0,-0x8(%rbp)
jmp    1235 <proc_get_freq_kernel+0xd5>
mov    -0x228(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_get_freq_kernel>:
push   %r14
push   %rbx
sub    $0x218,%rsp
mov    %edi,%ecx
lea    0xe8d(%rip),%rdx
lea    0x110(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
call   1050 <snprintf@plt>
lea    0xe88(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11ed <proc_get_freq_kernel+0x8d>
mov    %rax,%rbx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xff,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    $0xf5,%eax
jg     11ed <proc_get_freq_kernel+0x8d>
lea    0xe47(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    11ed <proc_get_freq_kernel+0x8d>
mov    0x8(%rsp),%rax
jmp    11ef <proc_get_freq_kernel+0x8f>
xor    %eax,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [23 x i8] c"/proc/sys/cpu/%u/speed\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [4 x i8] c"%lu\00"
define i64 @proc_get_freq_kernel(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [255 x i8]
%6 = alloca [255 x i8]
%7 = alloca i64
store i32 %0, i32* %3
%8 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i32 @snprintf(i8* %8, i32 255, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %4
%13 = load i32*, i32** %4
%14 = icmp ne i32* %13, null
br i1 %14, label %16, label %15
15: ; preds = %1
store i64 0, i64* %2
br label %33
16: ; preds = %1
%17 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%18 = load i32*, i32** %4
%19 = call i32 @fgets(i8* %17, i32 255, i32* %18)
%20 = load i32*, i32** %4
%21 = call i32 @fclose(i32* %20)
%22 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%23 = call i32 @strlen(i8* %22)
%24 = icmp sgt i32 %23, 245
br i1 %24, label %25, label %26
25: ; preds = %16
store i64 0, i64* %2
br label %33
26: ; preds = %16
%27 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%28 = call i32 @sscanf(i8* %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %7)
%29 = icmp ne i32 %28, 1
br i1 %29, label %30, label %31
30: ; preds = %26
store i64 0, i64* %2
br label %33
31: ; preds = %26
%32 = load i64, i64* %7
store i64 %32, i64* %2
br label %33
33: ; preds = %31, %30, %25, %15
%34 = load i64, i64* %2
ret i64 %34
}

LLVM IR (-O2):
@.str = [23 x i8] c"/proc/sys/cpu/%u/speed\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [4 x i8] c"%lu\00"
define i64 @proc_get_freq_kernel(i32 %0) {
%2 = alloca [255 x i8]
%3 = alloca [255 x i8]
%4 = alloca i64
%5 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%7 = bitcast i64* %4 to i8*
%8 = call i32 @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %0)
%9 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %21, label %11
11: ; preds = %1
%12 = call i32 @fgets(i8* %5, i32 255, i32* %9)
%13 = call i32 @fclose(i32* %9)
%14 = call i32 @strlen(i8* %5)
%15 = icmp sgt i32 %14, 245
br i1 %15, label %21, label %16
16: ; preds = %11
%17 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %4)
%18 = icmp eq i32 %17, 1
%19 = load i64, i64* %4
%20 = select i1 %18, i64 %19, i64 0
br label %21
21: ; preds = %16, %11, %1
%22 = phi i64 [ 0, %1 ], [ 0, %11 ], [ %20, %16 ]
ret i64 %22
}