#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 ssize_t;
typedef int /*<<< orphan*/ path;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int, unsigned int,
                            char const *);
unsigned int sysfs_idlestate_read_file(unsigned int cpu, unsigned int idlestate,
                                       const char *fname, char *buf,
                                       size_t buflen) {
  char path[255];
  int fd;
  ssize_t numread;
  snprintf(path, sizeof(path),
           "/sys/devices/system/cpu/"
           "cpu%u/cpuidle/state%u/%s",
           cpu, idlestate, fname);
  fd = open(path, O_RDONLY);
  if (fd == -1)
    return 0;
  numread = read(fd, buf, buflen - 1);
  if (numread < 1) {
    close(fd);
    return 0;
  }
  buf[numread] = '\0';
  close(fd);
  return (unsigned int)numread;
}


x86 assembly (clang, -O0):
<sysfs_idlestate_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    -0xc(%rbp),%r8d
mov    -0x18(%rbp),%r9
mov    $0xff,%esi
lea    0xe85(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x130(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0xffffffff,-0x134(%rbp)
jne    11b4 <sysfs_idlestate_read_file+0x74>
movl   $0x0,-0x4(%rbp)
jmp    1218 <sysfs_idlestate_read_file+0xd8>
mov    -0x134(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
sub    $0x1,%rdx
call   1050 <read@plt>
mov    %eax,-0x138(%rbp)
cmpl   $0x1,-0x138(%rbp)
jge    11f5 <sysfs_idlestate_read_file+0xb5>
mov    -0x134(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1218 <sysfs_idlestate_read_file+0xd8>
mov    -0x20(%rbp),%rax
movslq -0x138(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x134(%rbp),%edi
call   1040 <close@plt>
mov    -0x138(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_idlestate_read_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %r8,%rbx
mov    %rcx,%r14
mov    %rdx,%r9
mov    %esi,%r8d
mov    %edi,%ecx
lea    0xea0(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0xff,%esi
call   1030 <snprintf@plt>
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11a8 <sysfs_idlestate_read_file+0x68>
mov    %eax,%ebp
add    $0xffffffffffffffff,%rbx
mov    %eax,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <read@plt>
test   %eax,%eax
jle    11ac <sysfs_idlestate_read_file+0x6c>
mov    %eax,%ebx
mov    %eax,%eax
movb   $0x0,(%r14,%rax,1)
jmp    11ae <sysfs_idlestate_read_file+0x6e>
xor    %ebx,%ebx
jmp    11b5 <sysfs_idlestate_read_file+0x75>
xor    %ebx,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [49 x i8] c"/sys/devices/system/cpu/cpu%u/cpuidle/state%u/%s\00"
@O_RDONLY = global i32 0
define i32 @sysfs_idlestate_read_file(i32 %0, i32 %1, i8* %2, i8* %3, i64 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca [255 x i8]
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i64 %4, i64* %11
%15 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%16 = load i32, i32* %7
%17 = load i32, i32* %8
%18 = load i8*, i8** %9
%19 = call i32 @snprintf(i8* %15, i32 255, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %17, i8* %18)
%20 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %13
%23 = load i32, i32* %13
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %5
store i32 0, i32* %6
br label %45
26: ; preds = %5
%27 = load i32, i32* %13
%28 = load i8*, i8** %10
%29 = load i64, i64* %11
%30 = sub i64 %29, 1
%31 = call i32 @read(i32 %27, i8* %28, i64 %30)
store i32 %31, i32* %14
%32 = load i32, i32* %14
%33 = icmp slt i32 %32, 1
br i1 %33, label %34, label %37
34: ; preds = %26
%35 = load i32, i32* %13
%36 = call i32 @close(i32 %35)
store i32 0, i32* %6
br label %45
37: ; preds = %26
%38 = load i8*, i8** %10
%39 = load i32, i32* %14
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
store i8 0, i8* %41
%42 = load i32, i32* %13
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* %14
store i32 %44, i32* %6
br label %45
45: ; preds = %37, %34, %25
%46 = load i32, i32* %6
ret i32 %46
}

LLVM IR (-O2):
@.str = [49 x i8] c"/sys/devices/system/cpu/cpu%u/cpuidle/state%u/%s\00"
@O_RDONLY = global i32 0
define i32 @sysfs_idlestate_read_file(i32 %0, i32 %1, i8* %2, i8* nocapture %3, i64 %4) {
%6 = alloca [255 x i8]
%7 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%8 = call i32 @snprintf(i8* %7, i32 255, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1, i8* %2)
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %7, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %22, label %12
12: ; preds = %5
%13 = add i64 %4, -1
%14 = call i32 @read(i32 %10, i8* %3, i64 %13)
%15 = icmp slt i32 %14, 1
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = zext i32 %14 to i64
%18 = getelementptr inbounds i8, i8* %3, i64 %17
store i8 0, i8* %18
br label %19
19: ; preds = %12, %16
%20 = phi i32 [ %14, %16 ], [ 0, %12 ]
%21 = call i32 @close(i32 %10)
br label %22
22: ; preds = %19, %5
%23 = phi i32 [ 0, %5 ], [ %20, %19 ]
ret i32 %23
}