#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;
  if (!fgets(value, 255, fp)) {
    fclose(fp);
    return 0;
  }
  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    1254 <proc_get_freq_kernel+0xf4>
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0xff,%esi
call   1060 <fgets@plt>
cmp    $0x0,%eax
jne    11ec <proc_get_freq_kernel+0x8c>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1254 <proc_get_freq_kernel+0xf4>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x120(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0xf5,%eax
jle    1219 <proc_get_freq_kernel+0xb9>
movq   $0x0,-0x8(%rbp)
jmp    1254 <proc_get_freq_kernel+0xf4>
lea    -0x120(%rbp),%rdi
lea    0xdf2(%rip),%rsi
lea    -0x228(%rbp),%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
je     1249 <proc_get_freq_kernel+0xe9>
movq   $0x0,-0x8(%rbp)
jmp    1254 <proc_get_freq_kernel+0xf4>
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   %rbp
push   %rbx
sub    $0x218,%rsp
mov    %edi,%ecx
lea    0xe8e(%rip),%rdx
lea    0x110(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
call   1050 <snprintf@plt>
lea    0xe89(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f1 <proc_get_freq_kernel+0x91>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0xff,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %ebp,%ebp
je     11f1 <proc_get_freq_kernel+0x91>
lea    0x10(%rsp),%rdi
call   1040 <strlen@plt>
cmp    $0xf5,%eax
jg     11f1 <proc_get_freq_kernel+0x91>
lea    0xe43(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    11f1 <proc_get_freq_kernel+0x91>
mov    0x8(%rsp),%rax
jmp    11f3 <proc_get_freq_kernel+0x93>
xor    %eax,%eax
add    $0x218,%rsp
pop    %rbx
pop    %rbp
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 %38
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 = icmp ne i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = load i32*, i32** %4
%23 = call i32 @fclose(i32* %22)
store i64 0, i64* %2
br label %38
24: ; preds = %16
%25 = load i32*, i32** %4
%26 = call i32 @fclose(i32* %25)
%27 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%28 = call i32 @strlen(i8* %27)
%29 = icmp sgt i32 %28, 245
br i1 %29, label %30, label %31
30: ; preds = %24
store i64 0, i64* %2
br label %38
31: ; preds = %24
%32 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%33 = call i32 @sscanf(i8* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %7)
%34 = icmp ne i32 %33, 1
br i1 %34, label %35, label %36
35: ; preds = %31
store i64 0, i64* %2
br label %38
36: ; preds = %31
%37 = load i64, i64* %7
store i64 %37, i64* %2
br label %38
38: ; preds = %36, %35, %30, %21, %15
%39 = load i64, i64* %2
ret i64 %39
}

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 %23, label %11
11: ; preds = %1
%12 = call i32 @fgets(i8* %5, i32 255, i32* %9)
%13 = icmp eq i32 %12, 0
%14 = call i32 @fclose(i32* %9)
br i1 %13, label %23, label %15
15: ; preds = %11
%16 = call i32 @strlen(i8* %5)
%17 = icmp sgt i32 %16, 245
br i1 %17, label %23, label %18
18: ; preds = %15
%19 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64* %4)
%20 = icmp eq i32 %19, 1
%21 = load i64, i64* %4
%22 = select i1 %20, i64 %21, i64 0
br label %23
23: ; preds = %11, %18, %15, %1
%24 = phi i64 [ 0, %1 ], [ 0, %15 ], [ %22, %18 ], [ 0, %11 ]
ret i64 %24
}