#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;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int sprintf(char *, char *, char const *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int write(int, char *, int);
void suspend_system(const char *suspend) {
  char buf[20];
  int f = open("/sys/power/state", O_WRONLY);
  int len;
  ssize_t n;
  if (f < 0) {
    perror("open /sys/power/state");
    return;
  }
  len = sprintf(buf, "%s\n", suspend) - 1;
  len = strlen(buf);
  n = write(f, buf, len);
  if (n < 0)
    perror("write /sys/power/state");
  else if (n != len)
    fprintf(stderr, "short write to %s\n", "/sys/power/state");
  close(f);
}


x86 assembly (clang, -O0):
<suspend_system>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
lea    0xe90(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11af <suspend_system+0x3f>
lea    0xe5b(%rip),%rdi
call   1080 <perror@plt>
jmp    1237 <suspend_system+0xc7>
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe7e(%rip),%rsi
call   1090 <sprintf@plt>
sub    $0x1,%eax
mov    %eax,-0x28(%rbp)
lea    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1202 <suspend_system+0x92>
lea    0xe1e(%rip),%rdi
call   1080 <perror@plt>
jmp    122f <suspend_system+0xbf>
mov    -0x2c(%rbp),%eax
cmp    -0x28(%rbp),%eax
je     122a <suspend_system+0xba>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
lea    0xdf7(%rip),%rdx
call   1060 <fprintf@plt>
jmp    122f <suspend_system+0xbf>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<suspend_system>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11f9 <suspend_system+0x89>
mov    %eax,%ebx
lea    0xe9d(%rip),%rsi
mov    %rsp,%r15
mov    %r15,%rdi
mov    %r14,%rdx
call   1090 <sprintf@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
js     120f <suspend_system+0x9f>
cmp    %ebp,%eax
je     11e7 <suspend_system+0x77>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
lea    0xe3a(%rip),%rdx
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe00(%rip),%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <perror@plt>
lea    0xe00(%rip),%rdi
call   1080 <perror@plt>
jmp    11e7 <suspend_system+0x77>

LLVM IR (-O0):
@.str = [17 x i8] c"/sys/power/state\00"
@O_WRONLY = global i32 0
@.str.1 = [22 x i8] c"open /sys/power/state\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [23 x i8] c"write /sys/power/state\00"
@stderr = global i32 0
@.str.4 = [19 x i8] c"short write to %s\0A\00"
define void @suspend_system(i8* %0) {
%2 = alloca i8*
%3 = alloca [20 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i32, i32* @O_WRONLY
%8 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %39
13: ; preds = %1
%14 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%15 = load i8*, i8** %2
%16 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%17 = sub nsw i32 %16, 1
store i32 %17, i32* %5
%18 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%19 = call i32 @strlen(i8* %18)
store i32 %19, i32* %5
%20 = load i32, i32* %4
%21 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%22 = load i32, i32* %5
%23 = call i32 @write(i32 %20, i8* %21, i32 %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %13
%27 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %36
28: ; preds = %13
%29 = load i32, i32* %6
%30 = load i32, i32* %5
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %35
35: ; preds = %32, %28
br label %36
36: ; preds = %35, %26
%37 = load i32, i32* %4
%38 = call i32 @close(i32 %37)
br label %39
39: ; preds = %36, %11
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"/sys/power/state\00"
@O_WRONLY = global i32 0
@.str.1 = [22 x i8] c"open /sys/power/state\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@.str.3 = [23 x i8] c"write /sys/power/state\00"
@stderr = global i32 0
@.str.4 = [19 x i8] c"short write to %s\0A\00"
define void @suspend_system(i8* %0) {
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_WRONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %23
9: ; preds = %1
%10 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%11 = call i32 @strlen(i8* %3)
%12 = call i32 @write(i32 %5, i8* %3, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %21
16: ; preds = %9
%17 = icmp eq i32 %12, %11
br i1 %17, label %21, label %18
18: ; preds = %16
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %16, %18, %14
%22 = call i32 @close(i32 %5)
br label %23
23: ; preds = %21, %7
ret void
}