#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_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int, unsigned int,
                            char const *);
int write(int, char const *, size_t);
unsigned int cpuidle_state_write_file(unsigned int cpu, unsigned int idlestate,
                                      const char *fname, const char *value,
                                      size_t len) {
  char path[255];
  int fd;
  ssize_t numwrite;
  snprintf(path, sizeof(path),
           "/sys/devices/system/cpu/"
           "cpu%u/cpuidle/state%u/%s",
           cpu, idlestate, fname);
  fd = open(path, O_WRONLY);
  if (fd == -1)
    return 0;
  numwrite = write(fd, value, len);
  if (numwrite < 1) {
    close(fd);
    return 0;
  }
  close(fd);
  return (unsigned int)numwrite;
}


x86 assembly (clang, -O0):
<cpuidle_state_write_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   1040 <snprintf@plt>
lea    -0x130(%rbp),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0xffffffff,-0x134(%rbp)
jne    11b4 <cpuidle_state_write_file+0x74>
movl   $0x0,-0x4(%rbp)
jmp    1205 <cpuidle_state_write_file+0xc5>
mov    -0x134(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x138(%rbp)
cmpl   $0x1,-0x138(%rbp)
jge    11f1 <cpuidle_state_write_file+0xb1>
mov    -0x134(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1205 <cpuidle_state_write_file+0xc5>
mov    -0x134(%rbp),%edi
call   1050 <close@plt>
mov    -0x138(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpuidle_state_write_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %r8,%r14
mov    %rcx,%rbx
mov    %rdx,%r9
mov    %esi,%r8d
mov    %edi,%ecx
lea    0xea0(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0xff,%esi
call   1040 <snprintf@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11a7 <cpuidle_state_write_file+0x67>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
test   %ebx,%ebx
cmovns %ebx,%eax
jmp    11a9 <cpuidle_state_write_file+0x69>
xor    %eax,%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_WRONLY = global i32 0
define i32 @cpuidle_state_write_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_WRONLY
%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 %40
26: ; preds = %5
%27 = load i32, i32* %13
%28 = load i8*, i8** %10
%29 = load i64, i64* %11
%30 = call i32 @write(i32 %27, i8* %28, i64 %29)
store i32 %30, i32* %14
%31 = load i32, i32* %14
%32 = icmp slt i32 %31, 1
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = load i32, i32* %13
%35 = call i32 @close(i32 %34)
store i32 0, i32* %6
br label %40
36: ; preds = %26
%37 = load i32, i32* %13
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %14
store i32 %39, i32* %6
br label %40
40: ; preds = %36, %33, %25
%41 = load i32, i32* %6
ret i32 %41
}

LLVM IR (-O2):
@.str = [49 x i8] c"/sys/devices/system/cpu/cpu%u/cpuidle/state%u/%s\00"
@O_WRONLY = global i32 0
define i32 @cpuidle_state_write_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_WRONLY
%10 = call i32 @open(i8* %7, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %17, label %12
12: ; preds = %5
%13 = call i32 @write(i32 %10, i8* %3, i64 %4)
%14 = call i32 @close(i32 %10)
%15 = icmp sgt i32 %13, 0
%16 = select i1 %15, i32 %13, i32 0
br label %17
17: ; preds = %12, %5
%18 = phi i32 [ 0, %5 ], [ %16, %12 ]
ret i32 %18
}