#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 */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
scalar_t__ stat(char *, struct stat *);
unsigned int cpuidle_state_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 0;
  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):
<cpuidle_state_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 <cpuidle_state_count+0x64>
mov    -0x118(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
jne    11a0 <cpuidle_state_count+0x70>
movl   $0x0,-0x4(%rbp)
jmp    12a3 <cpuidle_state_count+0x173>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0xff,%esi
lea    0xe6a(%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    11ef <cpuidle_state_count+0xbf>
mov    -0x118(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
jne    11fb <cpuidle_state_count+0xcb>
movl   $0x0,-0x4(%rbp)
jmp    12a3 <cpuidle_state_count+0x173>
jmp    1200 <cpuidle_state_count+0xd0>
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    1240 <cpuidle_state_count+0x110>
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    1253 <cpuidle_state_count+0x123>
jmp    128b <cpuidle_state_count+0x15b>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    -0x11c(%rbp),%r8d
mov    $0xff,%esi
lea    0xddd(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x11c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x11c(%rbp)
jmp    1200 <cpuidle_state_count+0xd0>
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):
<cpuidle_state_count>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %edi,%r13d
lea    0xeb5(%rip),%rdx
xor    %ebp,%ebp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
xor    %eax,%eax
call   1040 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %rax,%rax
je     1187 <cpuidle_state_count+0x57>
mov    %ebp,%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 <cpuidle_state_count+0x43>
lea    0xe84(%rip),%rdx
xor    %ebp,%ebp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %r13d,%ecx
xor    %eax,%eax
call   1040 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %rax,%rax
jne    1173 <cpuidle_state_count+0x43>
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
test   %rax,%rax
je     1173 <cpuidle_state_count+0x43>
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    1249 <cpuidle_state_count+0x119>
mov    $0x1,%ebp
lea    0xe53(%rip),%r14
lea    0x10(%rsp),%r15
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
test   %rax,%rax
je     1249 <cpuidle_state_count+0x119>
mov    %r15,%rdi
mov    $0xff,%esi
mov    %r14,%rdx
mov    %r13d,%ecx
mov    %ebp,%r8d
xor    %eax,%eax
call   1040 <snprintf@plt>
add    $0x1,%ebp
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     1210 <cpuidle_state_count+0xe0>
add    $0xffffffff,%ebp
jmp    1173 <cpuidle_state_count+0x43>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [32 x i8] c"/sys/devices/system/cpu/cpuidle\00"
@.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 @cpuidle_state_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 %18, label %17
17: ; preds = %12, %1
store i32 0, i32* %2
br label %54
18: ; preds = %12
%19 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%20 = load i32, i32* %3
%21 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %19, i32 255, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%23 = call i64 @stat(i8* %22, %struct.stat* %5)
%24 = icmp ne i64 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %18
%26 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i64 @S_ISDIR(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %25, %18
store i32 0, i32* %2
br label %54
31: ; preds = %25
br label %32
32: ; preds = %43, %31
%33 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%34 = call i64 @stat(i8* %33, %struct.stat* %5)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%38 = load i32, i32* %37
%39 = call i64 @S_ISDIR(i32 %38)
%40 = icmp ne i64 %39, 0
br label %41
41: ; preds = %36, %32
%42 = phi i1 [ false, %32 ], [ %40, %36 ]
br i1 %42, label %43, label %50
43: ; preds = %41
%44 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%45 = load i32, i32* %3
%46 = load i32, i32* %6
%47 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %44, i32 255, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %45, i32 %46)
%48 = load i32, i32* %6
%49 = add nsw i32 %48, 1
store i32 %49, i32* %6
br label %32
50: ; preds = %41
%51 = load i32, i32* %6
%52 = add nsw i32 %51, -1
store i32 %52, i32* %6
%53 = load i32, i32* %6
store i32 %53, i32* %2
br label %54
54: ; preds = %50, %30, %17
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [32 x i8] c"/sys/devices/system/cpu/cpuidle\00"
@.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 @cpuidle_state_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 %38
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 %38, label %14
14: ; preds = %9
%15 = 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)
%16 = call i64 @stat(i8* %4, %struct.stat* %3)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %38
18: ; preds = %14
%19 = load i32, i32* %10
%20 = call i64 @S_ISDIR(i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %38, label %22
22: ; preds = %18
%23 = call i64 @stat(i8* %4, %struct.stat* %3)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %35
25: ; preds = %22, %30
%26 = phi i32 [ %32, %30 ], [ 1, %22 ]
%27 = load i32, i32* %10
%28 = call i64 @S_ISDIR(i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %25
%31 = 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 %26)
%32 = add nuw nsw i32 %26, 1
%33 = call i64 @stat(i8* %4, %struct.stat* %3)
%34 = icmp eq i64 %33, 0
br i1 %34, label %25, label %35
35: ; preds = %25, %30, %22
%36 = phi i32 [ 1, %22 ], [ %32, %30 ], [ %26, %25 ]
%37 = add nsw i32 %36, -1
br label %38
38: ; preds = %14, %18, %1, %9, %35
%39 = phi i32 [ %37, %35 ], [ 0, %9 ], [ 0, %1 ], [ 0, %18 ], [ 0, %14 ]
ret i32 %39
}