#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 */
float atof(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ memcmp(char *, char *, int);
unsigned int cpu_speed(void) {
  FILE *f;
  char line[160];
  f = fopen("/proc/cpuinfo", "r");
  if (f == NULL) {
    return 0;
  }
  while (!feof(f)) {
    char *res;
    res = fgets(line, 160, f);
    if (res != NULL) {
      if (memcmp(line, "cpu MHz\t\t: ", 11) == 0) {
        float s;
        s = atof(line + 10);
        fclose(f);
        return s * 1000.0;
      }
    }
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<cpu_speed>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
lea    0xe96(%rip),%rdi
lea    0xe9d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1199 <cpu_speed+0x39>
movl   $0x0,-0x4(%rbp)
jmp    1264 <cpu_speed+0x104>
jmp    119e <cpu_speed+0x3e>
mov    -0x10(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11bc <cpu_speed+0x5c>
jmp    1254 <cpu_speed+0xf4>
lea    -0xb0(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0xa0,%esi
call   1060 <fgets@plt>
mov    %rax,-0xb8(%rbp)
cmpq   $0x0,-0xb8(%rbp)
je     124f <cpu_speed+0xef>
lea    -0xb0(%rbp),%rdi
lea    0xe24(%rip),%rsi
mov    $0xb,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    124a <cpu_speed+0xea>
lea    -0xa6(%rbp),%rdi
call   1030 <atof@plt>
movss  %xmm0,-0xbc(%rbp)
ff
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movss  -0xbc(%rbp),%xmm0
ff
cvtss2sd %xmm0,%xmm0
movsd  0xdc7(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %eax,-0x4(%rbp)
jmp    1264 <cpu_speed+0x104>
jmp    124f <cpu_speed+0xef>
jmp    119e <cpu_speed+0x3e>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpu_speed>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xb8,%rsp
lea    0xe93(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1080 <fopen@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     11a0 <cpu_speed+0x40>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <feof@plt>
test   %eax,%eax
je     11b2 <cpu_speed+0x52>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14d,%eax
add    $0xb8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0x10(%rsp),%r15
lea    0xe5a(%rip),%r12
jmp    11cc <cpu_speed+0x6c>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    1198 <cpu_speed+0x38>
mov    %r15,%rdi
mov    $0xa0,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11c0 <cpu_speed+0x60>
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0xb,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
jne    11c0 <cpu_speed+0x60>
lea    0x1a(%rsp),%rdi
call   1030 <atof@plt>
movss  %xmm0,0xc(%rsp)
mov    %rbx,%rdi
call   1040 <fclose@plt>
movss  0xc(%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
mulsd  0xde0(%rip),%xmm0
cvttsd2si %xmm0,%r14
jmp    11a0 <cpu_speed+0x40>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"cpu MHz\09\09: \00"
define i32 @cpu_speed() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [160 x i8]
%4 = alloca i8*
%5 = alloca float
%6 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 0, i32* %1
br label %41
10: ; preds = %0
br label %11
11: ; preds = %37, %10
%12 = load i32*, i32** %2
%13 = call i32 @feof(i32* %12)
%14 = icmp ne i32 %13, 0
%15 = xor i1 %14, true
br i1 %15, label %16, label %38
16: ; preds = %11
%17 = getelementptr inbounds [160 x i8], [160 x i8]* %3, i64 0, i64 0
%18 = load i32*, i32** %2
%19 = call i8* @fgets(i8* %17, i32 160, i32* %18)
store i8* %19, i8** %4
%20 = load i8*, i8** %4
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %37
22: ; preds = %16
%23 = getelementptr inbounds [160 x i8], [160 x i8]* %3, i64 0, i64 0
%24 = call i64 @memcmp(i8* %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 11)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %22
%27 = getelementptr inbounds [160 x i8], [160 x i8]* %3, i64 0, i64 0
%28 = getelementptr inbounds i8, i8* %27, i64 10
%29 = call float @atof(i8* %28)
store float %29, float* %5
%30 = load i32*, i32** %2
%31 = call i32 @fclose(i32* %30)
%32 = load float, float* %5
%33 = fpext float %32 to double
%34 = fmul double %33, 1.000000e+03
%35 = fptoui double %34 to i32
store i32 %35, i32* %1
br label %41
36: ; preds = %22
br label %37
37: ; preds = %36, %16
br label %11
38: ; preds = %11
%39 = load i32*, i32** %2
%40 = call i32 @fclose(i32* %39)
store i32 0, i32* %1
br label %41
41: ; preds = %38, %26, %9
%42 = load i32, i32* %1
ret i32 %42
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"cpu MHz\09\09: \00"
define i32 @cpu_speed() {
%1 = alloca [160 x i8]
%2 = getelementptr inbounds [160 x i8], [160 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %26, label %5
5: ; preds = %0
%6 = call i32 @feof(i32* %3)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %24
8: ; preds = %5, %21
%9 = call i8* @fgets(i8* %2, i32 160, i32* %3)
%10 = icmp eq i8* %9, null
br i1 %10, label %21, label %11
11: ; preds = %8
%12 = call i64 @memcmp(i8* %2, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %11
%15 = getelementptr inbounds [160 x i8], [160 x i8]* %1, i64 0, i64 10
%16 = call float @atof(i8* %15)
%17 = call i32 @fclose(i32* %3)
%18 = fpext float %16 to double
%19 = fmul double %18, 1.000000e+03
%20 = fptoui double %19 to i32
br label %26
21: ; preds = %8, %11
%22 = call i32 @feof(i32* %3)
%23 = icmp eq i32 %22, 0
br i1 %23, label %8, label %24
24: ; preds = %21, %5
%25 = call i32 @fclose(i32* %3)
br label %26
26: ; preds = %14, %0, %24
%27 = phi i32 [ 0, %24 ], [ 0, %0 ], [ %20, %14 ]
ret i32 %27
}