#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, unsigned long *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
int get_freq_scales_count(int cpu) {
  FILE *file;
  char filename[64];
  long unsigned freq;
  int count = 0;
  sprintf(filename, "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state",
          cpu);
  file = fopen(filename, "r");
  if (!file) {
    fprintf(stderr, "Could not open %s\n", filename);
    exit(EXIT_FAILURE);
  };
  do {
    freq = 0;
    fscanf(file, "%lu %*d\n", &freq);
    if (freq)
      count++;
  } while (freq);
  fclose(file);
  return count;
}


x86 assembly (clang, -O0):
<get_freq_scales_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x5c(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe80(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x50(%rbp),%rdi
lea    0xeaa(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11cb <get_freq_scales_count+0x6b>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rdx
lea    0xe84(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11d0 <get_freq_scales_count+0x70>
movq   $0x0,-0x58(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe6c(%rip),%rsi
lea    -0x58(%rbp),%rdx
call   1040 <fscanf@plt>
cmpq   $0x0,-0x58(%rbp)
je     1200 <get_freq_scales_count+0xa0>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    1205 <get_freq_scales_count+0xa5>
cmpq   $0x0,-0x58(%rbp)
jne    11d0 <get_freq_scales_count+0x70>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x5c(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_freq_scales_count>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x58,%rsp
mov    %edi,%edx
lea    0xe8d(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
lea    0xeb3(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11e7 <get_freq_scales_count+0x87>
mov    %rax,%rbx
mov    $0xffffffff,%ebp
lea    0xeac(%rip),%r14
lea    0x8(%rsp),%r15
nopl   0x0(%rax,%rax,1)
movq   $0x0,0x8(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <fscanf@plt>
add    $0x1,%ebp
cmpq   $0x0,0x8(%rsp)
jne    11b0 <get_freq_scales_count+0x50>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1050 <fprintf@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [58 x i8] c"/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [19 x i8] c"Could not open %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [9 x i8] c"%lu %*d\0A\00"
define i32 @get_freq_scales_count(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca [64 x i8]
%5 = alloca i64
%6 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %6
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* %2
%9 = call i32 @sprintf(i8* %7, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %3
%12 = load i32*, i32** %3
%13 = icmp ne i32* %12, null
br i1 %13, label %20, label %14
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %1
br label %21
21: ; preds = %30, %20
store i64 0, i64* %5
%22 = load i32*, i32** %3
%23 = call i32 @fscanf(i32* %22, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i64* %5)
%24 = load i64, i64* %5
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
store i32 %28, i32* %6
br label %29
29: ; preds = %26, %21
br label %30
30: ; preds = %29
%31 = load i64, i64* %5
%32 = icmp ne i64 %31, 0
br i1 %32, label %21, label %33
33: ; preds = %30
%34 = load i32*, i32** %3
%35 = call i32 @fclose(i32* %34)
%36 = load i32, i32* %6
ret i32 %36
}

LLVM IR (-O2):
@.str = [58 x i8] c"/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [19 x i8] c"Could not open %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [9 x i8] c"%lu %*d\0A\00"
define i32 @get_freq_scales_count(i32 %0) {
%2 = alloca [64 x i8]
%3 = alloca i64
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%5 = bitcast i64* %3 to i8*
%6 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32* @fopen(i8* %4, 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 %9, label %14
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %1, %14
%15 = phi i32 [ %19, %14 ], [ 0, %1 ]
store i64 0, i64* %3
%16 = call i32 @fscanf(i32* %7, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i64* %3)
%17 = load i64, i64* %3
%18 = icmp eq i64 %17, 0
%19 = add nuw nsw i32 %15, 1
br i1 %18, label %20, label %14
20: ; preds = %14
%21 = call i32 @fclose(i32* %7)
ret i32 %15
}