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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int /*<<< orphan*/ stderr;
int strcmp(char const *, char *);
size_t strlen(char *);
int cpu_governor_cmp(unsigned int cpu, const char *governor) {
  FILE *fp;
  char buffer[128];
  int governor_cmp = -1;
  size_t len;
  snprintf(buffer, sizeof(buffer),
           "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor", cpu);
  fp = fopen(buffer, "r");
  if (fp == NULL) {
    fprintf(stderr, "cpu_governor_cmp: Failed to open %s\n", buffer);
    return -1;
  }
  if (fgets(buffer, sizeof(buffer), fp) != NULL) {
    len = strlen(buffer);
    if (len > 0 && buffer[len - 1] == '\n') {
      buffer[len - 1] = '\0';
    }
    governor_cmp = strcmp(governor, buffer);
  }
  fclose(fp);
  return governor_cmp;
}


x86 assembly (clang, -O0):
<cpu_governor_cmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0xa4(%rbp)
ff ff ff
lea    -0xa0(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x80,%esi
lea    0xe5e(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xe80(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f1 <cpu_governor_cmp+0x81>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    -0xa0(%rbp),%rdx
lea    0xe57(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <cpu_governor_cmp+0x118>
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x80,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1276 <cpu_governor_cmp+0x106>
lea    -0xa0(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0xb0(%rbp)
cmpq   $0x0,-0xb0(%rbp)
jbe    1260 <cpu_governor_cmp+0xf0>
mov    -0xb0(%rbp),%rax
sub    $0x1,%rax
movsbl -0xa0(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    1260 <cpu_governor_cmp+0xf0>
mov    -0xb0(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,-0xa0(%rbp,%rax,1)
mov    -0x10(%rbp),%rdi
lea    -0xa0(%rbp),%rsi
call   1070 <strcmp@plt>
mov    %eax,-0xa4(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0xa4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpu_governor_cmp>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %rsi,%r14
mov    %edi,%ecx
lea    0xe79(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
call   1050 <snprintf@plt>
lea    0xe97(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     11ee <cpu_governor_cmp+0x7e>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     120d <cpu_governor_cmp+0x9d>
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
test   %rax,%rax
je     11df <cpu_governor_cmp+0x6f>
cmpb   $0xa,-0x1(%rax,%rbp,1)
jne    11df <cpu_governor_cmp+0x6f>
movb   $0x0,-0x1(%rsp,%rax,1)
mov    %rsp,%rsi
mov    %r14,%rdi
call   1070 <strcmp@plt>
mov    %eax,%ebp
jmp    1212 <cpu_governor_cmp+0xa2>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
mov    %rsp,%rdx
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    121a <cpu_governor_cmp+0xaa>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [55 x i8] c"/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [37 x i8] c"cpu_governor_cmp: Failed to open %s\0A\00"
define i32 @cpu_governor_cmp(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [128 x i8]
%8 = alloca i32
%9 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 -1, i32* %8
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%11 = load i32, i32* %4
%12 = call i32 @snprintf(i8* %10, i32 128, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %19)
store i32 -1, i32* %3
br label %50
21: ; preds = %2
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%23 = load i32*, i32** %6
%24 = call i32* @fgets(i8* %22, i32 128, i32* %23)
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %46
26: ; preds = %21
%27 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%28 = call i64 @strlen(i8* %27)
store i64 %28, i64* %9
%29 = load i64, i64* %9
%30 = icmp ugt i64 %29, 0
br i1 %30, label %31, label %42
31: ; preds = %26
%32 = load i64, i64* %9
%33 = sub i64 %32, 1
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 %33
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 10
br i1 %37, label %38, label %42
38: ; preds = %31
%39 = load i64, i64* %9
%40 = sub i64 %39, 1
%41 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 %40
store i8 0, i8* %41
br label %42
42: ; preds = %38, %31, %26
%43 = load i8*, i8** %5
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%45 = call i32 @strcmp(i8* %43, i8* %44)
store i32 %45, i32* %8
br label %46
46: ; preds = %42, %21
%47 = load i32*, i32** %6
%48 = call i32 @fclose(i32* %47)
%49 = load i32, i32* %8
store i32 %49, i32* %3
br label %50
50: ; preds = %46, %17
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@.str = [55 x i8] c"/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [37 x i8] c"cpu_governor_cmp: Failed to open %s\0A\00"
define i32 @cpu_governor_cmp(i32 %0, i8* nocapture %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 128, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i32* @fopen(i8* %4, 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 %8, label %11
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %28
11: ; preds = %2
%12 = call i32* @fgets(i8* %4, i32 128, i32* %6)
%13 = icmp eq i32* %12, null
br i1 %13, label %25, label %14
14: ; preds = %11
%15 = call i64 @strlen(i8* %4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %14
%18 = add i64 %15, -1
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 10
br i1 %21, label %22, label %23
22: ; preds = %17
store i8 0, i8* %19
br label %23
23: ; preds = %22, %17, %14
%24 = call i32 @strcmp(i8* dereferenceable(1) %1, i8* %4)
br label %25
25: ; preds = %23, %11
%26 = phi i32 [ %24, %23 ], [ -1, %11 ]
%27 = call i32 @fclose(i32* %6)
br label %28
28: ; preds = %25, %8
%29 = phi i32 [ -1, %8 ], [ %26, %25 ]
ret i32 %29
}