#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, unsigned int *);
unsigned int getCoreNumber();
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int, char *);
void waitCurFreq(unsigned int coreID, unsigned int targetFreq) {
  assert(coreID < getCoreNumber());
  unsigned int freq = 0;
  char filePathBuffer[100] = {'\0'};
  snprintf(filePathBuffer, 100, "/sys/devices/system/cpu/cpu%d/cpufreq/%s",
           coreID, "cpuinfo_cur_freq");
  do {
    FILE *pFreqFile = fopen(filePathBuffer, "r");
    if (pFreqFile != NULL) {
      fscanf(pFreqFile, "%u", &freq);
      fclose(pFreqFile);
    }
  } while (freq != targetFreq);
}


x86 assembly (clang, -O0):
<waitCurFreq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x7c(%rbp)
mov    $0x0,%al
call   1080 <getCoreNumber@plt>
mov    %eax,%ecx
mov    -0x7c(%rbp),%eax
cmp    %ecx,%eax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
movl   $0x0,-0xc(%rbp)
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
lea    -0x70(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x64,%esi
lea    0xe34(%rip),%rdx
lea    0xe56(%rip),%r8
call   1040 <snprintf@plt>
lea    -0x70(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x78(%rbp)
je     1214 <waitCurFreq+0xa4>
mov    -0x78(%rbp),%rdi
lea    0xe3a(%rip),%rsi
lea    -0xc(%rbp),%rdx
call   1060 <fscanf@plt>
mov    -0x78(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1219 <waitCurFreq+0xa9>
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    11d8 <waitCurFreq+0x68>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitCurFreq>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
xor    %eax,%eax
call   1070 <getCoreNumber@plt>
xor    %edi,%edi
cmp    %ebp,%eax
seta   %dil
call   1060 <assert@plt>
movl   $0x0,0xc(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x70(%rsp)
lea    0xe42(%rip),%rdx
lea    0xe64(%rip),%r8
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x64,%esi
mov    %ebp,%ecx
call   1040 <snprintf@plt>
lea    0xe5a(%rip),%r13
lea    0xe55(%rip),%r15
lea    0xc(%rsp),%r12
jmp    11f6 <waitCurFreq+0x96>
xchg   %ax,%ax
cmp    %ebx,0xc(%rsp)
je     1221 <waitCurFreq+0xc1>
mov    %r14,%rdi
mov    %r13,%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f0 <waitCurFreq+0x90>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1050 <fscanf@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
jmp    11f0 <waitCurFreq+0x90>
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 x i8] c"/sys/devices/system/cpu/cpu%d/cpufreq/%s\00"
@.str.1 = [17 x i8] c"cpuinfo_cur_freq\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [3 x i8] c"%u\00"
define void @waitCurFreq(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [100 x i8]
%7 = alloca i32*
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* %3
%9 = call i32 (...) @getCoreNumber()
%10 = icmp ult i32 %8, %9
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
store i32 0, i32* %5
%13 = bitcast [100 x i8]* %6 to i8*
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%15 = load i32, i32* %3
%16 = call i32 @snprintf(i8* %14, i32 100, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %15, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %28, %2
%18 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %19, i32** %7
%20 = load i32*, i32** %7
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = load i32*, i32** %7
%24 = call i32 @fscanf(i32* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %5)
%25 = load i32*, i32** %7
%26 = call i32 @fclose(i32* %25)
br label %27
27: ; preds = %22, %17
br label %28
28: ; preds = %27
%29 = load i32, i32* %5
%30 = load i32, i32* %4
%31 = icmp ne i32 %29, %30
br i1 %31, label %17, label %32
32: ; preds = %28
ret void
}

LLVM IR (-O2):
@.str = [41 x i8] c"/sys/devices/system/cpu/cpu%d/cpufreq/%s\00"
@.str.1 = [17 x i8] c"cpuinfo_cur_freq\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [3 x i8] c"%u\00"
define void @waitCurFreq(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = tail call i32 (...) @getCoreNumber()
%6 = icmp ugt i32 %5, %0
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%11 = call i32 @snprintf(i8* %10, i32 100, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %0, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %18, %2
%13 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%17 = call i32 @fclose(i32* %13)
br label %18
18: ; preds = %15, %12
%19 = load i32, i32* %3
%20 = icmp eq i32 %19, %1
br i1 %20, label %21, label %12
21: ; preds = %18
ret void
}