#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 BUFSIZ;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ sscanf(char *, char *, int *);
int system_get_cpu_speed(int num, int speed[]) {
  FILE *f;
  char buf[BUFSIZ];
  int i;
  int ok = 0;
  f = fopen("/proc/cpuinfo", "rt");
  if (f == NULL) {
    return 0;
  }
  for (i = 0; i < num; i++) {
    speed[i] = -1;
  }
  while (fgets(buf, BUFSIZ, f)) {
    sscanf(buf, "processor : %d", &i);
    if (i < num) {
      ok += sscanf(buf, "cpu MHz : %d", &speed[i]);
    }
  }
  fclose(f);
  return ok;
}


x86 assembly (clang, -O0):
<system_get_cpu_speed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
lea    0xe77(%rip),%rdi
lea    0xe7e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b7 <system_get_cpu_speed+0x77>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1273 <system_get_cpu_speed+0x133>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11e7 <system_get_cpu_speed+0xa7>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movl   $0xffffffff,(%rax,%rcx,4)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11be <system_get_cpu_speed+0x7e>
jmp    11ec <system_get_cpu_speed+0xac>
mov    -0x40(%rbp),%rdi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     125d <system_get_cpu_speed+0x11d>
mov    -0x40(%rbp),%rdi
lea    0xdfa(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1050 <sscanf@plt>
mov    -0x2c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1258 <system_get_cpu_speed+0x118>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rdx
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xdda(%rip),%rsi
call   1050 <sscanf@plt>
mov    %rax,%rcx
movslq -0x30(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x30(%rbp)
jmp    11ec <system_get_cpu_speed+0xac>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<system_get_cpu_speed>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,-0x38(%rbp)
mov    %edi,%r15d
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe77(%rip),%rdi
lea    0xe7e(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     124a <system_get_cpu_speed+0xfa>
mov    %rax,%r13
xor    %r12d,%r12d
mov    $0x0,%eax
test   %r15d,%r15d
jle    11c6 <system_get_cpu_speed+0x76>
mov    %r15d,%edx
shl    $0x2,%rdx
mov    -0x38(%rbp),%rdi
mov    $0xff,%esi
call   1040 <memset@plt>
mov    %r15d,%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    %r13,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1240 <system_get_cpu_speed+0xf0>
xor    %r12d,%r12d
lea    -0x2c(%rbp),%r14
jmp    1209 <system_get_cpu_speed+0xb9>
nopl   0x0(%rax,%rax,1)
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    %r13,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1240 <system_get_cpu_speed+0xf0>
mov    %rbx,%rdi
lea    0xdfe(%rip),%rsi
mov    %r14,%rdx
call   1060 <sscanf@plt>
movslq -0x2c(%rbp),%rax
cmp    %r15d,%eax
jge    11f0 <system_get_cpu_speed+0xa0>
mov    -0x38(%rbp),%rcx
lea    (%rcx,%rax,4),%rdx
mov    %rbx,%rdi
lea    0xdea(%rip),%rsi
call   1060 <sscanf@plt>
add    %eax,%r12d
jmp    11f0 <system_get_cpu_speed+0xa0>
mov    %r13,%rdi
call   1030 <fclose@plt>
jmp    124d <system_get_cpu_speed+0xfd>
xor    %r12d,%r12d
mov    %r12d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [15 x i8] c"processor : %d\00"
@.str.3 = [13 x i8] c"cpu MHz : %d\00"
define i32 @system_get_cpu_speed(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
%12 = load i32, i32* @BUFSIZ
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %7
%15 = alloca i8, i64 %13
store i64 %13, i64* %8
store i32 0, i32* %10
%16 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %6
%17 = load i32*, i32** %6
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 0, i32* %3
store i32 1, i32* %11
br label %59
20: ; preds = %2
store i32 0, i32* %9
br label %21
21: ; preds = %30, %20
%22 = load i32, i32* %9
%23 = load i32, i32* %4
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %33
25: ; preds = %21
%26 = load i32*, i32** %5
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %26, i64 %28
store i32 -1, i32* %29
br label %30
30: ; preds = %25
%31 = load i32, i32* %9
%32 = add nsw i32 %31, 1
store i32 %32, i32* %9
br label %21
33: ; preds = %21
br label %34
34: ; preds = %54, %33
%35 = load i32, i32* @BUFSIZ
%36 = load i32*, i32** %6
%37 = call i64 @fgets(i8* %15, i32 %35, i32* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %55
39: ; preds = %34
%40 = call i64 @sscanf(i8* %15, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32* %9)
%41 = load i32, i32* %9
%42 = load i32, i32* %4
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %54
44: ; preds = %39
%45 = load i32*, i32** %5
%46 = load i32, i32* %9
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
%49 = call i64 @sscanf(i8* %15, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32* %48)
%50 = load i32, i32* %10
%51 = sext i32 %50 to i64
%52 = add nsw i64 %51, %49
%53 = trunc i64 %52 to i32
store i32 %53, i32* %10
br label %54
54: ; preds = %44, %39
br label %34
55: ; preds = %34
%56 = load i32*, i32** %6
%57 = call i32 @fclose(i32* %56)
%58 = load i32, i32* %10
store i32 %58, i32* %3
store i32 1, i32* %11
br label %59
59: ; preds = %55, %19
%60 = load i8*, i8** %7
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [15 x i8] c"processor : %d\00"
@.str.3 = [13 x i8] c"cpu MHz : %d\00"
define i32 @system_get_cpu_speed(i32 %0, i32* %1) {
%3 = bitcast i32* %1 to i8*
%4 = alloca i32
%5 = load i32, i32* @BUFSIZ
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = bitcast i32* %4 to i8*
%9 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %40, label %11
11: ; preds = %2
%12 = icmp sgt i32 %0, 0
br i1 %12, label %13, label %16
13: ; preds = %11
%14 = zext i32 %0 to i64
%15 = shl nuw nsw i64 %14, 2
br label %16
16: ; preds = %13, %11
%17 = phi i32 [ 0, %11 ], [ %0, %13 ]
store i32 %17, i32* %4
%18 = load i32, i32* @BUFSIZ
%19 = call i64 @fgets(i8* %7, i32 %18, i32* %9)
%20 = icmp eq i64 %19, 0
br i1 %20, label %37, label %21
21: ; preds = %16, %32
%22 = phi i32 [ %33, %32 ], [ 0, %16 ]
%23 = call i64 @sscanf(i8* %7, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%24 = load i32, i32* %4
%25 = icmp slt i32 %24, %0
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = sext i32 %24 to i64
%28 = getelementptr inbounds i32, i32* %1, i64 %27
%29 = call i64 @sscanf(i8* %7, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32* %28)
%30 = trunc i64 %29 to i32
%31 = add i32 %22, %30
br label %32
32: ; preds = %26, %21
%33 = phi i32 [ %31, %26 ], [ %22, %21 ]
%34 = load i32, i32* @BUFSIZ
%35 = call i64 @fgets(i8* %7, i32 %34, i32* %9)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %21
37: ; preds = %32, %16
%38 = phi i32 [ 0, %16 ], [ %33, %32 ]
%39 = call i32 @fclose(i32* %9)
br label %40
40: ; preds = %2, %37
%41 = phi i32 [ %38, %37 ], [ 0, %2 ]
ret i32 %41
}