#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_CREAT;
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_creating() {
  int fd = open(".throwaway-test-output", O_WRONLY | O_CREAT | O_TRUNC);
  if (fd == -1) {
    puts("Error opening file for read: 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_creating>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    0x2ea0(%rip),%rax
or     (%rax),%esi
mov    0x2e8f(%rip),%rax
or     (%rax),%esi
lea    0xe96(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1188 <test_open_file_creating+0x48>
lea    0xe94(%rip),%rdi
call   1030 <puts@plt>
mov    -0x4(%rbp),%edi
lea    0xea9(%rip),%rsi
mov    $0x3,%edx
call   1040 <write@plt>
cmp    $0x3,%eax
je     11b1 <test_open_file_creating+0x71>
lea    0xe93(%rip),%rdi
call   1030 <puts@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    11ce <test_open_file_creating+0x8e>
lea    0xe8c(%rip),%rdi
call   1030 <puts@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_open_file_creating>:
push   %rbx
mov    0x2e80(%rip),%rax
mov    0x2ea9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e96(%rip),%rax
or     (%rax),%esi
lea    0xe9d(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    117b <test_open_file_creating+0x3b>
lea    0xea1(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb9(%rip),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1040 <write@plt>
cmp    $0x3,%eax
je     119f <test_open_file_creating+0x5f>
lea    0xea5(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     11ad <test_open_file_creating+0x6d>
pop    %rbx
ret
lea    0xea1(%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_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [36 x i8] c"Error opening file for read: 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_creating() {
%1 = alloca i32
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_CREAT
%4 = or i32 %2, %3
%5 = load i32, i32* @O_TRUNC
%6 = or i32 %4, %5
%7 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %1
%8 = load i32, i32* %1
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @puts(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = load i32, i32* %1
%14 = call i32 @write(i32 %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = load i32, i32* %1
%20 = call i32 @close(i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
ret void
}

LLVM IR (-O2):
@.str = [23 x i8] c".throwaway-test-output\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [36 x i8] c"Error opening file for read: 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_creating() {
%1 = load i32, i32* @O_WRONLY
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = load i32, i32* @O_TRUNC
%5 = or i32 %3, %4
%6 = tail call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%12 = icmp eq i32 %11, 3
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = tail call i32 @close(i32 %6)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %20
20: ; preds = %18, %15
ret void
}