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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sscanf(char *, char *, float *);
char *strstr(char *, char *);
float cpu_clock_speed(void) {
  FILE *fp;
  char buffer[4095 + 1];
  size_t bytes_read;
  char *match;
  float clock_speed;
  fp = fopen("/proc/cpuinfo", "r");
  if (fp == NULL)
    return 0;
  bytes_read = fread(buffer, 1, sizeof(buffer), fp);
  fclose(fp);
  if (bytes_read == 0 || bytes_read == sizeof(buffer))
    return 0;
  buffer[bytes_read] = '\0';
  match = strstr(buffer, "cpu MHz");
  if (match == NULL)
    return 0;
  sscanf(match, "cpu MHz : %f", &clock_speed);
  return clock_speed;
}


x86 assembly (clang, -O0):
<cpu_clock_speed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
lea    0xe9e(%rip),%rdi
lea    0xea5(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    118a <cpu_clock_speed+0x3a>
xorps  %xmm0,%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    124b <cpu_clock_speed+0xfb>
lea    -0x1010(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
mov    %rax,-0x1018(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x1018(%rbp)
je     11d3 <cpu_clock_speed+0x83>
cmpq   $0x1000,-0x1018(%rbp)
jne    11e0 <cpu_clock_speed+0x90>
xorps  %xmm0,%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    124b <cpu_clock_speed+0xfb>
mov    -0x1018(%rbp),%rax
movb   $0x0,-0x1010(%rbp,%rax,1)
lea    -0x1010(%rbp),%rdi
lea    0xe13(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x1020(%rbp)
cmpq   $0x0,-0x1020(%rbp)
jne    1224 <cpu_clock_speed+0xd4>
xorps  %xmm0,%xmm0
movss  %xmm0,-0x4(%rbp)
jmp    124b <cpu_clock_speed+0xfb>
mov    -0x1020(%rbp),%rdi
lea    0xde6(%rip),%rsi
lea    -0x1024(%rbp),%rdx
call   1050 <sscanf@plt>
movss  -0x1024(%rbp),%xmm0
ff
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpu_clock_speed>:
push   %r14
push   %rbx
sub    $0x1018,%rsp
lea    0xe9f(%rip),%rdi
lea    0xea6(%rip),%rsi
call   1060 <fopen@plt>
xorps  %xmm0,%xmm0
test   %rax,%rax
je     11e1 <cpu_clock_speed+0x91>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   $0xffffffffffffefff,%r14
je     11de <cpu_clock_speed+0x8e>
movb   $0x0,0x10(%rsp,%r14,1)
lea    0xe60(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1070 <strstr@plt>
test   %rax,%rax
xorps  %xmm0,%xmm0
je     11e1 <cpu_clock_speed+0x91>
lea    0xe4f(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1050 <sscanf@plt>
movss  0xc(%rsp),%xmm0
jmp    11e1 <cpu_clock_speed+0x91>
xorps  %xmm0,%xmm0
add    $0x1018,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"cpu MHz\00"
@.str.3 = [13 x i8] c"cpu MHz : %f\00"
define float @cpu_clock_speed() {
%1 = alloca [4096 x i8]
%2 = alloca float
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%4 = bitcast float* %2 to i8*
%5 = 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))
%6 = icmp eq i32* %5, null
br i1 %6, label %19, label %7
7: ; preds = %0
%8 = call i64 @fread(i8* %3, i32 1, i32 4096, i32* %5)
%9 = tail call i32 @fclose(i32* %5)
%10 = and i64 %8, -4097
%11 = icmp eq i64 %10, 0
br i1 %11, label %19, label %12
12: ; preds = %7
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 %8
store i8 0, i8* %13
%14 = call i8* @strstr(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i8* %14, null
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = call i32 @sscanf(i8* %14, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), float* %2)
%18 = load float, float* %2
br label %19
19: ; preds = %12, %7, %0, %16
%20 = phi float [ %18, %16 ], [ 0.000000e+00, %0 ], [ 0.000000e+00, %7 ], [ 0.000000e+00, %12 ]
ret float %20
}