#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 */

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strlen(char const *);
int write(int, char const *, int /*<<< orphan*/);
int sys_power_state(const char *state) {
  int rv, fd = open("/sys/power/state", O_WRONLY);
  if (fd < 0) {
    perror("opening /sys/power/state");
    return fd;
  }
  rv = write(fd, state, strlen(state));
  if (rv < 0) {
    perror("writing to /sys/power/state");
    return rv;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<sys_power_state>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
lea    0xe9c(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1195 <sys_power_state+0x45>
lea    0xe7b(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ed <sys_power_state+0x9d>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11de <sys_power_state+0x8e>
lea    0xe4b(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ed <sys_power_state+0x9d>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sys_power_state>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     119a <sys_power_state+0x4a>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
js     11a8 <sys_power_state+0x58>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe5f(%rip),%rdi
call   1070 <perror@plt>
jmp    1193 <sys_power_state+0x43>
mov    %eax,%ebp
lea    0xe68(%rip),%rdi
call   1070 <perror@plt>
mov    %ebp,%ebx
jmp    1193 <sys_power_state+0x43>

LLVM IR (-O0):
@.str = [17 x i8] c"/sys/power/state\00"
@O_WRONLY = global i32 0
@.str.1 = [25 x i8] c"opening /sys/power/state\00"
@.str.2 = [28 x i8] c"writing to /sys/power/state\00"
define i32 @sys_power_state(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i32, i32* @O_WRONLY
%7 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* %5
store i32 %12, i32* %2
br label %27
13: ; preds = %1
%14 = load i32, i32* %5
%15 = load i8*, i8** %3
%16 = load i8*, i8** %3
%17 = call i32 @strlen(i8* %16)
%18 = call i32 @write(i32 %14, i8* %15, i32 %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* %4
store i32 %23, i32* %2
br label %27
24: ; preds = %13
%25 = load i32, i32* %5
%26 = call i32 @close(i32 %25)
store i32 0, i32* %2
br label %27
27: ; preds = %24, %21, %10
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@.str = [17 x i8] c"/sys/power/state\00"
@O_WRONLY = global i32 0
@.str.1 = [25 x i8] c"opening /sys/power/state\00"
@.str.2 = [28 x i8] c"writing to /sys/power/state\00"
define i32 @sys_power_state(i8* %0) {
%2 = load i32, i32* @O_WRONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %15
7: ; preds = %1
%8 = tail call i32 @strlen(i8* %0)
%9 = tail call i32 @write(i32 %3, i8* %0, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %15
13: ; preds = %7
%14 = tail call i32 @close(i32 %3)
br label %15
15: ; preds = %13, %11, %5
%16 = phi i32 [ %3, %5 ], [ %9, %11 ], [ 0, %13 ]
ret i32 %16
}