#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 O_TRUNC;
int O_WRONLY;
int close(int);
int open(char *, int);
int /*<<< orphan*/ puts(char *);
int write(int, char *, int);
void test_open_file_writing() {
  int fd = open(".throwaway-test-output", O_WRONLY | O_TRUNC);
  if (fd == -1) {
    puts("Error opening file for write: BROKEN");
  }
  if (write(fd, "moo", 3) != 3) {
    puts("Did not write 3 bytes");
  }
  if (close(fd) == -1) {
    puts("Error closing");
  }
}


x86 assembly (clang, -O0):
<test_open_file_writing>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    0x2ea0(%rip),%rax
or     (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    117f <test_open_file_writing+0x3f>
lea    0xe9d(%rip),%rdi
call   1030 <puts@plt>
mov    -0x4(%rbp),%edi
lea    0xeb3(%rip),%rsi
mov    $0x3,%edx
call   1040 <write@plt>
cmp    $0x3,%eax
je     11a8 <test_open_file_writing+0x68>
lea    0xe9d(%rip),%rdi
call   1030 <puts@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    11c5 <test_open_file_writing+0x85>
lea    0xe96(%rip),%rdi
call   1030 <puts@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_open_file_writing>:
push   %rbx
mov    0x2e88(%rip),%rax
mov    0x2ea9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xea6(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1172 <test_open_file_writing+0x32>
lea    0xeaa(%rip),%rdi
call   1030 <puts@plt>
lea    0xec3(%rip),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1040 <write@plt>
cmp    $0x3,%eax
je     1196 <test_open_file_writing+0x56>
lea    0xeaf(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     11a4 <test_open_file_writing+0x64>
pop    %rbx
ret
lea    0xeab(%rip),%rdi
pop    %rbx
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [23 x i8] c".throwaway-test-output\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [37 x i8] c"Error opening file for write: BROKEN\00"
@.str.2 = [4 x i8] c"moo\00"
@.str.3 = [22 x i8] c"Did not write 3 bytes\00"
@.str.4 = [14 x i8] c"Error closing\00"
define void @test_open_file_writing() {
%1 = alloca i32
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_TRUNC
%4 = or i32 %2, %3
%5 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %1
%6 = load i32, i32* %1
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @puts(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = load i32, i32* %1
%12 = call i32 @write(i32 %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
%17 = load i32, i32* %1
%18 = call i32 @close(i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
ret void
}

LLVM IR (-O2):
@.str = [23 x i8] c".throwaway-test-output\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [37 x i8] c"Error opening file for write: BROKEN\00"
@.str.2 = [4 x i8] c"moo\00"
@.str.3 = [22 x i8] c"Did not write 3 bytes\00"
@.str.4 = [14 x i8] c"Error closing\00"
define void @test_open_file_writing() {
%1 = load i32, i32* @O_WRONLY
%2 = load i32, i32* @O_TRUNC
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%10 = icmp eq i32 %9, 3
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = tail call i32 @close(i32 %4)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
ret void
}