#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(char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char const *);
int getCoreNumber();
int /*<<< orphan*/ *openCPUFreqFile(int, char *, char *);
char setCPUGovernor(const char *newPolicy) {
  int nbCore = getCoreNumber();
  int i = 0;
  assert(newPolicy);
  for (i = 0; i < nbCore; i++) {
    FILE *pGovernorFile = openCPUFreqFile(i, "scaling_governor", "w");
    if (pGovernorFile != NULL) {
      fprintf(pGovernorFile, "%s", newPolicy);
      fclose(pGovernorFile);
    } else {
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<setCPUGovernor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <getCoreNumber@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <assert@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11ec <setCPUGovernor+0x9c>
mov    -0x18(%rbp),%edi
lea    0xe6d(%rip),%rsi
lea    0xe77(%rip),%rdx
call   1060 <openCPUFreqFile@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11d0 <setCPUGovernor+0x80>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe56(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
jmp    11d9 <setCPUGovernor+0x89>
movb   $0xff,-0x1(%rbp)
jmp    11f0 <setCPUGovernor+0xa0>
jmp    11de <setCPUGovernor+0x8e>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    117d <setCPUGovernor+0x2d>
movb   $0x0,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setCPUGovernor>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <getCoreNumber@plt>
mov    %eax,%r13d
mov    %r14,%rdi
call   1050 <assert@plt>
test   %r13d,%r13d
jle    11c6 <setCPUGovernor+0x76>
lea    0xe82(%rip),%r15
lea    0xe8c(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <openCPUFreqFile@plt>
test   %rax,%rax
je     11c4 <setCPUGovernor+0x74>
mov    %rax,%rbp
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <fputs@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
add    $0x1,%ebx
cmp    %ebx,%r13d
jne    1190 <setCPUGovernor+0x40>
xor    %ebx,%ebx
jmp    11c6 <setCPUGovernor+0x76>
mov    $0xff,%bl
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"scaling_governor\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [3 x i8] c"%s\00"
define signext i8 @setCPUGovernor(i8* %0) {
%2 = alloca i8
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %3
%7 = call i32 (...) @getCoreNumber()
store i32 %7, i32* %4
store i32 0, i32* %5
%8 = load i8*, i8** %3
%9 = call i32 @assert(i8* %8)
store i32 0, i32* %5
br label %10
10: ; preds = %27, %1
%11 = load i32, i32* %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %30
14: ; preds = %10
%15 = load i32, i32* %5
%16 = call i32* @openCPUFreqFile(i32 %15, 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* %16, i32** %6
%17 = load i32*, i32** %6
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %25
19: ; preds = %14
%20 = load i32*, i32** %6
%21 = load i8*, i8** %3
%22 = call i32 @fprintf(i32* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = load i32*, i32** %6
%24 = call i32 @fclose(i32* %23)
br label %26
25: ; preds = %14
store i8 -1, i8* %2
br label %31
26: ; preds = %19
br label %27
27: ; preds = %26
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
br label %10
30: ; preds = %10
store i8 0, i8* %2
br label %31
31: ; preds = %30, %25
%32 = load i8, i8* %2
ret i8 %32
}

LLVM IR (-O2):
@.str = [17 x i8] c"scaling_governor\00"
@.str.1 = [2 x i8] c"w\00"
define signext i8 @setCPUGovernor(i8* %0) {
%2 = tail call i32 (...) @getCoreNumber()
%3 = tail call i32 @assert(i8* %0)
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %14
5: ; preds = %1, %9
%6 = phi i32 [ %12, %9 ], [ 0, %1 ]
%7 = tail call i32* @openCPUFreqFile(i32 %6, 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))
%8 = icmp eq i32* %7, null
br i1 %8, label %14, label %9
9: ; preds = %5
%10 = tail call i32 @fputs(i8* %0, i32* %7)
%11 = tail call i32 @fclose(i32* %7)
%12 = add nuw nsw i32 %6, 1
%13 = icmp eq i32 %12, %2
br i1 %13, label %14, label %5
14: ; preds = %9, %5, %1
%15 = phi i8 [ 0, %1 ], [ -1, %5 ], [ 0, %9 ]
ret i8 %15
}