#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 /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, unsigned int *);
unsigned int getCoreNumber();
int /*<<< orphan*/ *openCPUFreqFile(unsigned int, char *, char *);
unsigned int getCurFreq(unsigned int coreID) {
  assert(coreID < getCoreNumber());
  unsigned int freq = 0;
  FILE *pFreqFile = openCPUFreqFile(coreID, "cpuinfo_cur_freq", "r");
  if (pFreqFile != NULL) {
    fscanf(pFreqFile, "%u", &freq);
    fclose(pFreqFile);
  }
  return freq;
}


x86 assembly (clang, -O0):
<getCurFreq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1070 <getCoreNumber@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
cmp    %ecx,%eax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    0xe73(%rip),%rsi
lea    0xe7d(%rip),%rdx
call   1060 <openCPUFreqFile@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11c5 <getCurFreq+0x75>
mov    -0x10(%rbp),%rdi
lea    0xe60(%rip),%rsi
lea    -0x8(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getCurFreq>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <getCoreNumber@plt>
xor    %edi,%edi
cmp    %ebx,%eax
seta   %dil
call   1050 <assert@plt>
movl   $0x0,0x4(%rsp)
lea    0xe86(%rip),%rsi
lea    0xe90(%rip),%rdx
mov    %ebx,%edi
call   1060 <openCPUFreqFile@plt>
test   %rax,%rax
je     11b0 <getCurFreq+0x60>
mov    %rax,%rbx
lea    0xe7c(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x4(%rsp),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"cpuinfo_cur_freq\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%u\00"
define i32 @getCurFreq(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i32 (...) @getCoreNumber()
%7 = icmp ult i32 %5, %6
%8 = zext i1 %7 to i32
%9 = call i32 @assert(i32 %8)
store i32 0, i32* %3
%10 = load i32, i32* %2
%11 = call i32* @openCPUFreqFile(i32 %10, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %4
%12 = load i32*, i32** %4
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %19
14: ; preds = %1
%15 = load i32*, i32** %4
%16 = call i32 @fscanf(i32* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
%17 = load i32*, i32** %4
%18 = call i32 @fclose(i32* %17)
br label %19
19: ; preds = %14, %1
%20 = load i32, i32* %3
ret i32 %20
}

LLVM IR (-O2):
@.str = [17 x i8] c"cpuinfo_cur_freq\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%u\00"
define i32 @getCurFreq(i32 %0) {
%2 = alloca i32
%3 = tail call i32 (...) @getCoreNumber()
%4 = icmp ugt i32 %3, %0
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%8 = tail call i32* @openCPUFreqFile(i32 %0, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %14, label %10
10: ; preds = %1
%11 = call i32 @fscanf(i32* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%12 = call i32 @fclose(i32* %8)
%13 = load i32, i32* %2
br label %14
14: ; preds = %10, %1
%15 = phi i32 [ %13, %10 ], [ 0, %1 ]
ret i32 %15
}