#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*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
scalar_t__ strncmp(char *, char *, int /*<<< orphan*/);
char *strtok(char *, char *);
int valid_gov(char *gov) {
  FILE *f = NULL;
  const char fname[] =
      "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors";
  char delimiter[] = " ";
  char buff[1024];
  char *ptr = NULL, *eptr = NULL;
  f = fopen(fname, "r");
  if (f == NULL) {
    fprintf(stderr, "Cannot open file %s for reading!\n", fname);
    return 0;
  }
  eptr = fgets(buff, 1024, f);
  if (eptr == NULL) {
    fprintf(stderr, "Cannot read content of file %s!\n", fname);
    fclose(f);
    return 0;
  }
  ptr = strtok(buff, delimiter);
  while (ptr != NULL) {
    if (strncmp(ptr, gov, strlen(ptr)) == 0) {
      fclose(f);
      return 1;
    }
    ptr = strtok(NULL, delimiter);
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<valid_gov>:
push   %rbp
mov    %rsp,%rbp
sub    $0x490,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
lea    0xe5e(%rip),%rsi
mov    $0x41,%edx
call   1080 <memcpy@plt>
mov    0xe8e(%rip),%ax
mov    %ax,-0x62(%rbp)
movq   $0x0,-0x478(%rbp)
movq   $0x0,-0x480(%rbp)
lea    -0x60(%rbp),%rdi
lea    0xe6b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1211 <valid_gov+0x91>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rdx
lea    0xe45(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1310 <valid_gov+0x190>
lea    -0x470(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
mov    %rax,-0x480(%rbp)
cmpq   $0x0,-0x480(%rbp)
jne    1269 <valid_gov+0xe9>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rdx
lea    0xe18(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1310 <valid_gov+0x190>
lea    -0x470(%rbp),%rdi
lea    -0x62(%rbp),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x478(%rbp)
cmpq   $0x0,-0x478(%rbp)
je     1300 <valid_gov+0x180>
mov    -0x478(%rbp),%rax
mov    %rax,-0x490(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x488(%rbp)
mov    -0x478(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x490(%rbp),%rdi
mov    -0x488(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12e7 <valid_gov+0x167>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1310 <valid_gov+0x190>
lea    -0x62(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <strtok@plt>
mov    %rax,-0x478(%rbp)
jmp    1280 <valid_gov+0x100>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x490,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<valid_gov>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x468,%rsp
mov    %rdi,%r12
movaps 0xea8(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xe8c(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe70(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe54(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movb   $0x0,0x50(%rsp)
movw   $0x20,0xe(%rsp)
lea    0xe7d(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     123e <valid_gov+0xce>
mov    %rax,%r14
lea    0x60(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     125a <valid_gov+0xea>
lea    0x60(%rsp),%rdi
lea    0xe(%rsp),%rsi
call   1090 <strtok@plt>
test   %rax,%rax
je     1274 <valid_gov+0x104>
mov    %rax,%rbx
lea    0xe(%rsp),%r15
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     128d <valid_gov+0x11d>
xor    %edi,%edi
mov    %r15,%rsi
call   1090 <strtok@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1210 <valid_gov+0xa0>
jmp    1274 <valid_gov+0x104>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1070 <fprintf@plt>
jmp    127c <valid_gov+0x10c>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1070 <fprintf@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x468,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    $0x1,%eax
jmp    127e <valid_gov+0x10e>

LLVM IR (-O0):
@__const.valid_gov.fname = [65 x i8] c"/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors\00"
@__const.valid_gov.delimiter = [2 x i8] c" \00"
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [34 x i8] c"Cannot open file %s for reading!\0A\00"
@.str.2 = [33 x i8] c"Cannot read content of file %s!\0A\00"
define i32 @valid_gov(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [65 x i8]
%6 = alloca [2 x i8]
%7 = alloca [1024 x i8]
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %3
store i32* null, i32** %4
%10 = bitcast [65 x i8]* %5 to i8*
%11 = bitcast [2 x i8]* %6 to i8*
store i8* null, i8** %8
store i8* null, i8** %9
%12 = getelementptr inbounds [65 x i8], [65 x i8]* %5, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %4
%14 = load i32*, i32** %4
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = getelementptr inbounds [65 x i8], [65 x i8]* %5, i64 0, i64 0
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %18)
store i32 0, i32* %2
br label %55
20: ; preds = %1
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%22 = load i32*, i32** %4
%23 = call i8* @fgets(i8* %21, i32 1024, i32* %22)
store i8* %23, i8** %9
%24 = load i8*, i8** %9
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %32
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = getelementptr inbounds [65 x i8], [65 x i8]* %5, i64 0, i64 0
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = load i32*, i32** %4
%31 = call i32 @fclose(i32* %30)
store i32 0, i32* %2
br label %55
32: ; preds = %20
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%34 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%35 = call i8* @strtok(i8* %33, i8* %34)
store i8* %35, i8** %8
br label %36
36: ; preds = %49, %32
%37 = load i8*, i8** %8
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %52
39: ; preds = %36
%40 = load i8*, i8** %8
%41 = load i8*, i8** %3
%42 = load i8*, i8** %8
%43 = call i32 @strlen(i8* %42)
%44 = call i64 @strncmp(i8* %40, i8* %41, i32 %43)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %39
%47 = load i32*, i32** %4
%48 = call i32 @fclose(i32* %47)
store i32 1, i32* %2
br label %55
49: ; preds = %39
%50 = getelementptr inbounds [2 x i8], [2 x i8]* %6, i64 0, i64 0
%51 = call i8* @strtok(i8* null, i8* %50)
store i8* %51, i8** %8
br label %36
52: ; preds = %36
%53 = load i32*, i32** %4
%54 = call i32 @fclose(i32* %53)
store i32 0, i32* %2
br label %55
55: ; preds = %52, %46, %26, %16
%56 = load i32, i32* %2
ret i32 %56
}

LLVM IR (-O2):
@__const.valid_gov.fname = [65 x i8] c"/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors\00"
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [34 x i8] c"Cannot open file %s for reading!\0A\00"
@.str.2 = [33 x i8] c"Cannot read content of file %s!\0A\00"
define i32 @valid_gov(i8* %0) {
%2 = alloca [65 x i8]
%3 = alloca i16
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [65 x i8], [65 x i8]* %2, i64 0, i64 0
%6 = bitcast i16* %3 to i8*
store i16 32, i16* %3
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %35
13: ; preds = %1
%14 = call i8* @fgets(i8* %7, i32 1024, i32* %8)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%19 = call i32 @fclose(i32* %8)
br label %35
20: ; preds = %13
%21 = call i8* @strtok(i8* %7, i8* %6)
%22 = icmp eq i8* %21, null
br i1 %22, label %33, label %23
23: ; preds = %20, %30
%24 = phi i8* [ %31, %30 ], [ %21, %20 ]
%25 = call i32 @strlen(i8* %24)
%26 = call i64 @strncmp(i8* %24, i8* %0, i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @fclose(i32* %8)
br label %35
30: ; preds = %23
%31 = call i8* @strtok(i8* null, i8* %6)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %23
33: ; preds = %30, %20
%34 = call i32 @fclose(i32* %8)
br label %35
35: ; preds = %33, %28, %16, %10
%36 = phi i32 [ 0, %10 ], [ 0, %16 ], [ 1, %28 ], [ 0, %33 ]
ret i32 %36
}