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

/* Variables and functions */
scalar_t__ atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int get_frequency_from_brand();
char *strstr(char *, char *);
uint32_t get_cpufreq(void) {
  char line[128];
  uint32_t freq = 0;
  char *match;
  freq = get_frequency_from_brand();
  if (freq > 0)
    return freq;
  FILE *fp =
      fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r");
  if (fp != NULL) {
    if (fgets(line, sizeof(line), fp) != NULL) {
      freq = (uint32_t)atoi(line) / 1000;
    }
    fclose(fp);
  } else if ((fp = fopen("/proc/cpuinfo", "r"))) {
    while (fgets(line, sizeof(line), fp) != NULL) {
      match = strstr(line, "cpu MHz");
      if (match != NULL) {
        while (((*match < '0') || (*match > '9')) && (*match != '\0'))
          match++;
        freq = (uint32_t)atoi(match);
        break;
      }
    }
    fclose(fp);
  }
  return freq;
}


x86 assembly (clang, -O0):
<get_cpufreq>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x94(%rbp)
mov    $0x0,%al
call   1060 <get_frequency_from_brand@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0x0,-0x94(%rbp)
jle    119d <get_cpufreq+0x3d>
mov    -0x94(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    132e <get_cpufreq+0x1ce>
lea    0xe5c(%rip),%rdi
lea    0xe8b(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0xa8(%rbp)
cmpq   $0x0,-0xa8(%rbp)
je     1212 <get_cpufreq+0xb2>
lea    -0x90(%rbp),%rdi
mov    -0xa8(%rbp),%rdx
mov    $0x80,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1201 <get_cpufreq+0xa1>
lea    -0x90(%rbp),%rdi
call   1070 <atoi@plt>
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x94(%rbp)
mov    -0xa8(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1325 <get_cpufreq+0x1c5>
lea    0xe1f(%rip),%rdi
lea    0xe16(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0xa8(%rbp)
cmp    $0x0,%rax
je     1320 <get_cpufreq+0x1c0>
jmp    123b <get_cpufreq+0xdb>
lea    -0x90(%rbp),%rdi
mov    -0xa8(%rbp),%rdx
mov    $0x80,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1314 <get_cpufreq+0x1b4>
lea    -0x90(%rbp),%rdi
lea    0xddb(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,-0xa0(%rbp)
cmpq   $0x0,-0xa0(%rbp)
je     130f <get_cpufreq+0x1af>
jmp    128a <get_cpufreq+0x12a>
mov    -0xa0(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jl     12b8 <get_cpufreq+0x158>
mov    -0xa0(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x39,%ecx
mov    %al,-0xa9(%rbp)
jle    12ce <get_cpufreq+0x16e>
mov    -0xa0(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0xa9(%rbp)
mov    -0xa9(%rbp),%al
test   $0x1,%al
jne    12e1 <get_cpufreq+0x181>
jmp    12f8 <get_cpufreq+0x198>
mov    -0xa0(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0xa0(%rbp)
jmp    128a <get_cpufreq+0x12a>
mov    -0xa0(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x94(%rbp)
jmp    1314 <get_cpufreq+0x1b4>
jmp    123b <get_cpufreq+0xdb>
mov    -0xa8(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1325 <get_cpufreq+0x1c5>
mov    -0x94(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cpufreq>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x88,%rsp
xor    %eax,%eax
call   1060 <get_frequency_from_brand@plt>
mov    %eax,%r15d
test   %eax,%eax
jg     1257 <get_cpufreq+0xf7>
lea    0xe79(%rip),%rdi
lea    0xea8(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11d8 <get_cpufreq+0x78>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     124f <get_cpufreq+0xef>
mov    %rsp,%rdi
call   1070 <atoi@plt>
cltq
imul   $0x10624dd3,%rax,%r15
mov    %r15,%rax
shr    $0x3f,%rax
sar    $0x26,%r15
add    %eax,%r15d
jmp    124f <get_cpufreq+0xef>
lea    0xe59(%rip),%rdi
lea    0xe50(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1257 <get_cpufreq+0xf7>
mov    %rax,%r14
mov    %rsp,%rbx
lea    0xe49(%rip),%r12
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     124f <get_cpufreq+0xef>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     1200 <get_cpufreq+0xa0>
add    $0xffffffffffffffff,%rax
nopl   0x0(%rax)
movzbl 0x1(%rax),%ecx
add    $0x1,%rax
test   %cl,%cl
je     1244 <get_cpufreq+0xe4>
add    $0xd0,%cl
cmp    $0x9,%cl
ja     1230 <get_cpufreq+0xd0>
mov    %rax,%rdi
call   1070 <atoi@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %r15d,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [54 x i8] c"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [14 x i8] c"/proc/cpuinfo\00"
@.str.3 = [8 x i8] c"cpu MHz\00"
define i32 @get_cpufreq() {
%1 = alloca i32
%2 = alloca [128 x i8]
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
store i32 0, i32* %3
%6 = call i32 (...) @get_frequency_from_brand()
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = load i32, i32* %3
store i32 %10, i32* %1
br label %74
11: ; preds = %0
%12 = call i32* @fopen(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %5
%13 = load i32*, i32** %5
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %28
15: ; preds = %11
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%17 = load i32*, i32** %5
%18 = call i32* @fgets(i8* %16, i32 128, i32* %17)
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %25
20: ; preds = %15
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%22 = call i64 @atoi(i8* %21)
%23 = trunc i64 %22 to i32
%24 = sdiv i32 %23, 1000
store i32 %24, i32* %3
br label %25
25: ; preds = %20, %15
%26 = load i32*, i32** %5
%27 = call i32 @fclose(i32* %26)
br label %72
28: ; preds = %11
%29 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %29, i32** %5
%30 = icmp ne i32* %29, null
br i1 %30, label %31, label %71
31: ; preds = %28
br label %32
32: ; preds = %67, %31
%33 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%34 = load i32*, i32** %5
%35 = call i32* @fgets(i8* %33, i32 128, i32* %34)
%36 = icmp ne i32* %35, null
br i1 %36, label %37, label %68
37: ; preds = %32
%38 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%39 = call i8* @strstr(i8* %38, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i8* %39, i8** %4
%40 = load i8*, i8** %4
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %67
42: ; preds = %37
br label %43
43: ; preds = %60, %42
%44 = load i8*, i8** %4
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp slt i32 %46, 48
br i1 %47, label %53, label %48
48: ; preds = %43
%49 = load i8*, i8** %4
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp sgt i32 %51, 57
br i1 %52, label %53, label %58
53: ; preds = %48, %43
%54 = load i8*, i8** %4
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp ne i32 %56, 0
br label %58
58: ; preds = %53, %48
%59 = phi i1 [ false, %48 ], [ %57, %53 ]
br i1 %59, label %60, label %63
60: ; preds = %58
%61 = load i8*, i8** %4
%62 = getelementptr inbounds i8, i8* %61, i32 1
store i8* %62, i8** %4
br label %43
63: ; preds = %58
%64 = load i8*, i8** %4
%65 = call i64 @atoi(i8* %64)
%66 = trunc i64 %65 to i32
store i32 %66, i32* %3
br label %68
67: ; preds = %37
br label %32
68: ; preds = %63, %32
%69 = load i32*, i32** %5
%70 = call i32 @fclose(i32* %69)
br label %71
71: ; preds = %68, %28
br label %72
72: ; preds = %71, %25
%73 = load i32, i32* %3
store i32 %73, i32* %1
br label %74
74: ; preds = %72, %9
%75 = load i32, i32* %1
ret i32 %75
}

LLVM IR (-O2):
@.str = [54 x i8] c"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [14 x i8] c"/proc/cpuinfo\00"
@.str.3 = [8 x i8] c"cpu MHz\00"
define i32 @get_cpufreq() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @get_frequency_from_brand()
%4 = icmp sgt i32 %3, 0
br i1 %4, label %39, label %5
5: ; preds = %0
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %15, label %8
8: ; preds = %5
%9 = call i32* @fgets(i8* %2, i32 128, i32* %6)
%10 = icmp eq i32* %9, null
br i1 %10, label %35, label %11
11: ; preds = %8
%12 = call i64 @atoi(i8* %2)
%13 = trunc i64 %12 to i32
%14 = sdiv i32 %13, 1000
br label %35
15: ; preds = %5
%16 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %39, label %18
18: ; preds = %15, %21
%19 = call i32* @fgets(i8* %2, i32 128, i32* %16)
%20 = icmp eq i32* %19, null
br i1 %20, label %35, label %21
21: ; preds = %18
%22 = call i8* @strstr(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%23 = icmp eq i8* %22, null
br i1 %23, label %18, label %24
24: ; preds = %21, %24
%25 = phi i8* [ %31, %24 ], [ %22, %21 ]
%26 = load i8, i8* %25
%27 = add i8 %26, -48
%28 = icmp ult i8 %27, 10
%29 = icmp eq i8 %26, 0
%30 = or i1 %29, %28
%31 = getelementptr inbounds i8, i8* %25, i64 1
br i1 %30, label %32, label %24
32: ; preds = %24
%33 = call i64 @atoi(i8* %25)
%34 = trunc i64 %33 to i32
br label %35
35: ; preds = %18, %32, %8, %11
%36 = phi i32* [ %6, %11 ], [ %6, %8 ], [ %16, %32 ], [ %16, %18 ]
%37 = phi i32 [ %14, %11 ], [ %3, %8 ], [ %34, %32 ], [ %3, %18 ]
%38 = call i32 @fclose(i32* %36)
br label %39
39: ; preds = %35, %15, %0
%40 = phi i32 [ %3, %0 ], [ %3, %15 ], [ %37, %35 ]
ret i32 %40
}