#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int ENODEV;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
scalar_t__ stat(char *, struct stat *);
int sysfs_get_idlestate_count(unsigned int cpu) {
  char file[255];
  struct stat statbuf;
  int idlestates = 1;
  snprintf(file, 255,
           "/sys/devices/system/cpu/"
           "cpuidle");
  if (stat(file, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode))
    return -ENODEV;
  snprintf(file, 255,
           "/sys/devices/system/cpu/"
           "cpu%u/cpuidle/state0",
           cpu);
  if (stat(file, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode))
    return 0;
  while (stat(file, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) {
    snprintf(file, 255,
             "/sys/devices/system/cpu/"
             "cpu%u/cpuidle/state%d",
             cpu, idlestates);
    idlestates++;
  }
  idlestates--;
  return idlestates;
}


x86 assembly (clang, -O0):
<sysfs_get_idlestate_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x1,-0x11c(%rbp)
lea    -0x110(%rbp),%rdi
mov    $0xff,%esi
lea    0xea5(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
lea    -0x110(%rbp),%rdi
lea    -0x118(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    1194 <sysfs_get_idlestate_count+0x64>
mov    -0x118(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
jne    11a7 <sysfs_get_idlestate_count+0x77>
xor    %eax,%eax
mov    0x2e43(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12aa <sysfs_get_idlestate_count+0x17a>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0xff,%esi
lea    0xe63(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
lea    -0x110(%rbp),%rdi
lea    -0x118(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    11f6 <sysfs_get_idlestate_count+0xc6>
mov    -0x118(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
jne    1202 <sysfs_get_idlestate_count+0xd2>
movl   $0x0,-0x4(%rbp)
jmp    12aa <sysfs_get_idlestate_count+0x17a>
jmp    1207 <sysfs_get_idlestate_count+0xd7>
lea    -0x110(%rbp),%rdi
lea    -0x118(%rbp),%rsi
call   1050 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x11d(%rbp)
jne    1247 <sysfs_get_idlestate_count+0x117>
mov    -0x118(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x11d(%rbp)
mov    -0x11d(%rbp),%al
test   $0x1,%al
jne    125a <sysfs_get_idlestate_count+0x12a>
jmp    1292 <sysfs_get_idlestate_count+0x162>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    -0x11c(%rbp),%r8d
mov    $0xff,%esi
lea    0xdd6(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x11c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x11c(%rbp)
jmp    1207 <sysfs_get_idlestate_count+0xd7>
mov    -0x11c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x11c(%rbp)
mov    -0x11c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_get_idlestate_count>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %edi,%r13d
lea    0xeb5(%rip),%rdx
xor    %ebx,%ebx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xff,%esi
xor    %eax,%eax
call   1040 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1050 <stat@plt>
test   %rax,%rax
je     1190 <sysfs_get_idlestate_count+0x60>
mov    0x2e66(%rip),%rax
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
test   %rax,%rax
je     1173 <sysfs_get_idlestate_count+0x43>
lea    0xe7b(%rip),%rdx
xor    %ebx,%ebx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xff,%esi
mov    %r13d,%ecx
xor    %eax,%eax
call   1040 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1050 <stat@plt>
test   %rax,%rax
jne    117c <sysfs_get_idlestate_count+0x4c>
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
test   %rax,%rax
je     117c <sysfs_get_idlestate_count+0x4c>
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
mov    $0x1,%ebx
test   %rax,%rax
jne    1249 <sysfs_get_idlestate_count+0x119>
mov    $0x1,%ebx
lea    0xe4a(%rip),%r14
lea    0x10(%rsp),%r15
lea    0x8(%rsp),%r12
nopl   (%rax)
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
test   %rax,%rax
je     1249 <sysfs_get_idlestate_count+0x119>
mov    %r15,%rdi
mov    $0xff,%esi
mov    %r14,%rdx
mov    %r13d,%ecx
mov    %ebx,%r8d
xor    %eax,%eax
call   1040 <snprintf@plt>
add    $0x1,%ebx
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     1210 <sysfs_get_idlestate_count+0xe0>
add    $0xffffffff,%ebx
jmp    117c <sysfs_get_idlestate_count+0x4c>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [32 x i8] c"/sys/devices/system/cpu/cpuidle\00"
@ENODEV = global i32 0
@.str.1 = [45 x i8] c"/sys/devices/system/cpu/cpu%u/cpuidle/state0\00"
@.str.2 = [46 x i8] c"/sys/devices/system/cpu/cpu%u/cpuidle/state%d\00"
define i32 @sysfs_get_idlestate_count(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [255 x i8]
%5 = alloca %struct.stat
%6 = alloca i32
store i32 %0, i32* %3
store i32 1, i32* %6
%7 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%8 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %7, i32 255, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%10 = call i64 @stat(i8* %9, %struct.stat* %5)
%11 = icmp ne i64 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %1
%13 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i64 @S_ISDIR(i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %12, %1
%18 = load i32, i32* @ENODEV
%19 = sub nsw i32 0, %18
store i32 %19, i32* %2
br label %56
20: ; preds = %12
%21 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%22 = load i32, i32* %3
%23 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %21, i32 255, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%25 = call i64 @stat(i8* %24, %struct.stat* %5)
%26 = icmp ne i64 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %20
%28 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i64 @S_ISDIR(i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %27, %20
store i32 0, i32* %2
br label %56
33: ; preds = %27
br label %34
34: ; preds = %45, %33
%35 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%36 = call i64 @stat(i8* %35, %struct.stat* %5)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %34
%39 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%40 = load i32, i32* %39
%41 = call i64 @S_ISDIR(i32 %40)
%42 = icmp ne i64 %41, 0
br label %43
43: ; preds = %38, %34
%44 = phi i1 [ false, %34 ], [ %42, %38 ]
br i1 %44, label %45, label %52
45: ; preds = %43
%46 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%47 = load i32, i32* %3
%48 = load i32, i32* %6
%49 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %46, i32 255, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %47, i32 %48)
%50 = load i32, i32* %6
%51 = add nsw i32 %50, 1
store i32 %51, i32* %6
br label %34
52: ; preds = %43
%53 = load i32, i32* %6
%54 = add nsw i32 %53, -1
store i32 %54, i32* %6
%55 = load i32, i32* %6
store i32 %55, i32* %2
br label %56
56: ; preds = %52, %32, %17
%57 = load i32, i32* %2
ret i32 %57
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [32 x i8] c"/sys/devices/system/cpu/cpuidle\00"
@ENODEV = global i32 0
@.str.1 = [45 x i8] c"/sys/devices/system/cpu/cpu%u/cpuidle/state0\00"
@.str.2 = [46 x i8] c"/sys/devices/system/cpu/cpu%u/cpuidle/state%d\00"
define i32 @sysfs_get_idlestate_count(i32 %0) {
%2 = alloca [255 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.stat* %3 to i8*
%6 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %4, i32 255, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%7 = call i64 @stat(i8* %4, %struct.stat* %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%11 = load i32, i32* %10
%12 = call i64 @S_ISDIR(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %9, %1
%15 = load i32, i32* @ENODEV
%16 = sub nsw i32 0, %15
br label %41
17: ; preds = %9
%18 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %4, i32 255, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%19 = call i64 @stat(i8* %4, %struct.stat* %3)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %41
21: ; preds = %17
%22 = load i32, i32* %10
%23 = call i64 @S_ISDIR(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %41, label %25
25: ; preds = %21
%26 = call i64 @stat(i8* %4, %struct.stat* %3)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %38
28: ; preds = %25, %33
%29 = phi i32 [ %35, %33 ], [ 1, %25 ]
%30 = load i32, i32* %10
%31 = call i64 @S_ISDIR(i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %38, label %33
33: ; preds = %28
%34 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %4, i32 255, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %29)
%35 = add nuw nsw i32 %29, 1
%36 = call i64 @stat(i8* %4, %struct.stat* %3)
%37 = icmp eq i64 %36, 0
br i1 %37, label %28, label %38
38: ; preds = %28, %33, %25
%39 = phi i32 [ 1, %25 ], [ %35, %33 ], [ %29, %28 ]
%40 = add nsw i32 %39, -1
br label %41
41: ; preds = %17, %21, %38, %14
%42 = phi i32 [ %16, %14 ], [ %40, %38 ], [ 0, %21 ], [ 0, %17 ]
ret i32 %42
}