#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 /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int main(void) {
  FILE *fp = NULL;
  fp = fopen("/sys/power/state", "w");
  if (fp == NULL) {
    perror("zzz: fopen");
    return 1;
  }
  if (fputs("mem", fp) == EOF) {
    perror("zzz: fputs");
    fclose(fp);
    return 1;
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
lea    0xea2(%rip),%rdi
lea    0xeac(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1191 <main+0x51>
lea    0xe93(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e2 <main+0xa2>
mov    -0x10(%rbp),%rsi
lea    0xe82(%rip),%rdi
call   1040 <fputs@plt>
mov    0x2e40(%rip),%rcx
cmp    (%rcx),%rax
jne    11d2 <main+0x92>
lea    0xe6a(%rip),%rdi
call   1060 <perror@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e2 <main+0xa2>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xeb8(%rip),%rdi
lea    0xec2(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1183 <main+0x43>
mov    %rax,%rbx
lea    0xebb(%rip),%rdi
mov    %rax,%rsi
call   1040 <fputs@plt>
mov    0x2e76(%rip),%rcx
cmp    (%rcx),%rax
je     1191 <main+0x51>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe89(%rip),%rdi
call   1060 <perror@plt>
jmp    11a5 <main+0x65>
lea    0xe8a(%rip),%rdi
call   1060 <perror@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    $0x1,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/sys/power/state\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [11 x i8] c"zzz: fopen\00"
@.str.3 = [4 x i8] c"mem\00"
@EOF = global i64 0
@.str.4 = [11 x i8] c"zzz: fputs\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
store i32 0, i32* %1
store i32* null, i32** %2
%3 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %3, i32** %2
%4 = load i32*, i32** %2
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %20
8: ; preds = %0
%9 = load i32*, i32** %2
%10 = call i64 @fputs(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %9)
%11 = load i64, i64* @EOF
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %17
13: ; preds = %8
%14 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%15 = load i32*, i32** %2
%16 = call i32 @fclose(i32* %15)
store i32 1, i32* %1
br label %20
17: ; preds = %8
%18 = load i32*, i32** %2
%19 = call i32 @fclose(i32* %18)
store i32 0, i32* %1
br label %20
20: ; preds = %17, %13, %6
%21 = load i32, i32* %1
ret i32 %21
}

LLVM IR (-O2):
@.str = [17 x i8] c"/sys/power/state\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [11 x i8] c"zzz: fopen\00"
@.str.3 = [4 x i8] c"mem\00"
@EOF = global i64 0
@.str.4 = [11 x i8] c"zzz: fputs\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %14
5: ; preds = %0
%6 = tail call i64 @fputs(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32* %1)
%7 = load i64, i64* @EOF
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %12
9: ; preds = %5
%10 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%11 = tail call i32 @fclose(i32* %1)
br label %14
12: ; preds = %5
%13 = tail call i32 @fclose(i32* %1)
br label %14
14: ; preds = %12, %9, %3
%15 = phi i32 [ 1, %3 ], [ 1, %9 ], [ 0, %12 ]
ret i32 %15
}