#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*/ snprintf(char *, int, char *, unsigned long);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
scalar_t__ strncmp(char *, char *, int /*<<< orphan*/);
char *strtok(char *, char *);
int valid_freq(unsigned long freq) {
  FILE *f = NULL;
  const char fname[] =
      "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies";
  char delimiter[] = " ";
  char buff[1024];
  char freqstr[25];
  char *ptr = NULL, *eptr = NULL;
  snprintf(freqstr, 24, "%lu", freq);
  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, freqstr, strlen(ptr)) == 0) {
      fclose(f);
      return 1;
    }
    ptr = strtok(NULL, delimiter);
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<valid_freq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4b0,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
lea    0xe4e(%rip),%rsi
mov    $0x43,%edx
call   1090 <memcpy@plt>
mov    0xe80(%rip),%ax
mov    %ax,-0x62(%rbp)
movq   $0x0,-0x498(%rbp)
movq   $0x0,-0x4a0(%rbp)
lea    -0x490(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x18,%esi
lea    0xe51(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x60(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    123d <valid_freq+0xad>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rdx
lea    0xe1f(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    133f <valid_freq+0x1af>
lea    -0x470(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1070 <fgets@plt>
mov    %rax,-0x4a0(%rbp)
cmpq   $0x0,-0x4a0(%rbp)
jne    1295 <valid_freq+0x105>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rdx
lea    0xdf2(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    133f <valid_freq+0x1af>
lea    -0x470(%rbp),%rdi
lea    -0x62(%rbp),%rsi
call   10b0 <strtok@plt>
mov    %rax,-0x498(%rbp)
cmpq   $0x0,-0x498(%rbp)
je     132f <valid_freq+0x19f>
mov    -0x498(%rbp),%rax
mov    %rax,-0x4b0(%rbp)
lea    -0x490(%rbp),%rax
mov    %rax,-0x4a8(%rbp)
mov    -0x498(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x4b0(%rbp),%rdi
mov    -0x4a8(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1316 <valid_freq+0x186>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    133f <valid_freq+0x1af>
lea    -0x62(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <strtok@plt>
mov    %rax,-0x498(%rbp)
jmp    12ac <valid_freq+0x11c>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<valid_freq>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x488,%rsp
mov    %rdi,%rcx
movaps 0xe98(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xe7c(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe60(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe44(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x736569,0x4f(%rsp)
movw   $0x20,0xe(%rsp)
lea    0xe6c(%rip),%rdx
lea    0x60(%rsp),%rdi
mov    $0x18,%esi
call   1060 <snprintf@plt>
lea    0xe5a(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     126e <valid_freq+0xee>
mov    %rax,%r14
lea    0x80(%rsp),%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     128a <valid_freq+0x10a>
lea    0x80(%rsp),%rdi
lea    0xe(%rsp),%rsi
call   10a0 <strtok@plt>
test   %rax,%rax
je     12a4 <valid_freq+0x124>
mov    %rax,%rbx
lea    0x60(%rsp),%r15
lea    0xe(%rsp),%r12
nopl   (%rax)
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     12bd <valid_freq+0x13d>
xor    %edi,%edi
mov    %r12,%rsi
call   10a0 <strtok@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1240 <valid_freq+0xc0>
jmp    12a4 <valid_freq+0x124>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1080 <fprintf@plt>
jmp    12ac <valid_freq+0x12c>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1080 <fprintf@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x488,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    $0x1,%eax
jmp    12ae <valid_freq+0x12e>

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

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