#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 scalar_t__ uint64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
int sscanf(char *, char *, float *);
int /*<<< orphan*/ stderr;
uint64_t get_cpu_freq(void) {
  FILE *fd;
  uint64_t freq = 0;
  float freqf = 0;
  char *line = NULL;
  size_t len = 0;
  fd = fopen("/proc/cpuinfo", "r");
  if (!fd) {
    fprintf(stderr, "failed to get cpu frequecy\n");
    perror(NULL);
    return freq;
  }
  while (getline(&line, &len, fd) != EOF) {
    if (sscanf(line, "cpu MHz\t: %f", &freqf) == 1) {
      freqf = freqf * 1000000UL;
      freq = (uint64_t)freqf;
      break;
    }
  }
  fclose(fd);
  return freq;
}


x86 assembly (clang, -O0):
<get_cpu_freq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x0,-0x18(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
lea    0xe75(%rip),%rdi
lea    0xe7c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d5 <get_cpu_freq+0x75>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    0xe5a(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    124f <get_cpu_freq+0xef>
jmp    11da <get_cpu_freq+0x7a>
mov    -0x10(%rbp),%rdx
lea    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <getline@plt>
mov    0x2df6(%rip),%rcx
cmp    (%rcx),%rax
je     123e <get_cpu_freq+0xde>
mov    -0x28(%rbp),%rdi
lea    0xe2a(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
jne    1239 <get_cpu_freq+0xd9>
movss  0xde0(%rip),%xmm0
mulss  -0x1c(%rbp),%xmm0
movss  %xmm0,-0x1c(%rbp)
cvttss2si -0x1c(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    123e <get_cpu_freq+0xde>
jmp    11da <get_cpu_freq+0x7a>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cpu_freq>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movl   $0x0,0x4(%rsp)
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0xe75(%rip),%rdi
lea    0xe7c(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     121f <get_cpu_freq+0xbf>
mov    %rax,%rbx
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r12
mov    0x2e34(%rip),%r14
lea    0xe75(%rip),%r13
lea    0x4(%rsp),%rbp
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1080 <getline@plt>
cmp    (%r14),%rax
je     1203 <get_cpu_freq+0xa3>
mov    0x8(%rsp),%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1050 <sscanf@plt>
cmp    $0x1,%eax
jne    11c0 <get_cpu_freq+0x60>
movss  0x4(%rsp),%xmm0
mulss  0xe0a(%rip),%xmm0
movss  %xmm0,0x4(%rsp)
cvttss2si %xmm0,%rbp
jmp    1205 <get_cpu_freq+0xa5>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %rbp,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
call   1040 <fprintf@plt>
xor    %ebp,%ebp
xor    %edi,%edi
call   1070 <perror@plt>
jmp    120d <get_cpu_freq+0xad>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [28 x i8] c"failed to get cpu frequecy\0A\00"
@EOF = global i64 0
@.str.3 = [13 x i8] c"cpu MHz\09: %f\00"
define i64 @get_cpu_freq() {
%1 = alloca i64
%2 = alloca i32*
%3 = alloca i64
%4 = alloca float
%5 = alloca i8*
%6 = alloca i64
store i64 0, i64* %3
store float 0.000000e+00, float* %4
store i8* null, i8** %5
store i64 0, i64* %6
%7 = 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* %7, i32** %2
%8 = load i32*, i32** %2
%9 = icmp ne i32* %8, null
br i1 %9, label %15, label %10
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 @perror(i32* null)
%14 = load i64, i64* %3
store i64 %14, i64* %1
br label %35
15: ; preds = %0
br label %16
16: ; preds = %30, %15
%17 = load i32*, i32** %2
%18 = call i64 @getline(i8** %5, i64* %6, i32* %17)
%19 = load i64, i64* @EOF
%20 = icmp ne i64 %18, %19
br i1 %20, label %21, label %31
21: ; preds = %16
%22 = load i8*, i8** %5
%23 = call i32 @sscanf(i8* %22, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), float* %4)
%24 = icmp eq i32 %23, 1
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = load float, float* %4
%27 = fmul float %26, 1.000000e+06
store float %27, float* %4
%28 = load float, float* %4
%29 = fptosi float %28 to i64
store i64 %29, i64* %3
br label %31
30: ; preds = %21
br label %16
31: ; preds = %25, %16
%32 = load i32*, i32** %2
%33 = call i32 @fclose(i32* %32)
%34 = load i64, i64* %3
store i64 %34, i64* %1
br label %35
35: ; preds = %31, %10
%36 = load i64, i64* %1
ret i64 %36
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [28 x i8] c"failed to get cpu frequecy\0A\00"
@EOF = global i64 0
@.str.3 = [13 x i8] c"cpu MHz\09: %f\00"
define i64 @get_cpu_freq() {
%1 = alloca float
%2 = alloca i8*
%3 = alloca i64
%4 = bitcast float* %1 to i8*
store float 0.000000e+00, float* %1
%5 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%6 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%7 = 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))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%12 = tail call i32 @perror(i32* null)
br label %28
13: ; preds = %0, %17
%14 = call i64 @getline(i8** %2, i64* %3, i32* %7)
%15 = load i64, i64* @EOF
%16 = icmp eq i64 %14, %15
br i1 %16, label %25, label %17
17: ; preds = %13
%18 = load i8*, i8** %2
%19 = call i32 @sscanf(i8* %18, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), float* %1)
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %13
21: ; preds = %17
%22 = load float, float* %1
%23 = fmul float %22, 1.000000e+06
store float %23, float* %1
%24 = fptosi float %23 to i64
br label %25
25: ; preds = %13, %21
%26 = phi i64 [ %24, %21 ], [ 0, %13 ]
%27 = call i32 @fclose(i32* %7)
br label %28
28: ; preds = %25, %9
%29 = phi i64 [ %26, %25 ], [ 0, %9 ]
ret i64 %29
}