#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ write(int, char *, int);
main() {
  int fd, i, j, oflag;
  char buf[1000];
  oflag = (O_TRUNC | O_CREAT | O_WRONLY);
  fd = open("tempfile", oflag, 0x1c0);
  if (fd < 0) {
    printf("Error opening\n");
    exit(1);
  }
  sleep(4);
  printf("writer: writting to file\n");
  write(fd, "This is a test", 15);
  sleep(4);
  printf("writer: writting to file\n");
  write(fd, "Second test", 12);
  close(fd);
  printf("writer: done\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400,%rsp
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    0x2e7d(%rip),%rcx
or     (%rcx),%eax
mov    0x2e44(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe6d(%rip),%rdi
mov    $0x1c0,%edx
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11c0 <main+0x60>
lea    0xe58(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    $0x4,%edi
call   1080 <sleep@plt>
lea    0xe47(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
lea    0xe52(%rip),%rsi
mov    $0xf,%edx
call   1030 <write@plt>
mov    $0x4,%edi
call   1080 <sleep@plt>
lea    0xe1d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
lea    0xe37(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
lea    0xe2a(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e85(%rip),%rax
mov    0x2e86(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e4b(%rip),%rax
or     (%rax),%esi
lea    0xe7a(%rip),%rdi
mov    $0x1c0,%edx
call   1060 <open@plt>
test   %eax,%eax
js     1204 <main+0xa4>
mov    %eax,%ebx
mov    $0x4,%edi
call   1080 <sleep@plt>
lea    0xe7d(%rip),%r14
mov    %r14,%rdi
call   1030 <puts@plt>
lea    0xe53(%rip),%rsi
mov    %ebx,%edi
mov    $0xf,%edx
call   1040 <write@plt>
mov    $0x4,%edi
call   1080 <sleep@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
lea    0xe3d(%rip),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
lea    0xe48(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe3f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str = [9 x i8] c"tempfile\00"
@.str.1 = [15 x i8] c"Error opening\0A\00"
@.str.2 = [26 x i8] c"writer: writting to file\0A\00"
@.str.3 = [15 x i8] c"This is a test\00"
@.str.4 = [12 x i8] c"Second test\00"
@.str.5 = [14 x i8] c"writer: done\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1000 x i8]
%6 = load i32, i32* @O_TRUNC
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = load i32, i32* @O_WRONLY
%10 = or i32 %8, %9
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %11, i32 448)
store i32 %12, i32* %1
%13 = load i32, i32* %1
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %0
%19 = call i32 @sleep(i32 4)
%20 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* %1
%22 = call i32 @write(i32 %21, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 15)
%23 = call i32 @sleep(i32 4)
%24 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* %1
%26 = call i32 @write(i32 %25, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 12)
%27 = load i32, i32* %1
%28 = call i32 @close(i32 %27)
%29 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str = [9 x i8] c"tempfile\00"
@.str.3 = [15 x i8] c"This is a test\00"
@.str.4 = [12 x i8] c"Second test\00"
@str.6 = [25 x i8] c"writer: writting to file\00"
@str.7 = [13 x i8] c"writer: done\00"
@str.8 = [14 x i8] c"Error opening\00"
define i32 @main() {
%1 = load i32, i32* @O_TRUNC
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = load i32, i32* @O_WRONLY
%5 = or i32 %3, %4
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5, i32 448)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.8, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = tail call i32 @sleep(i32 4)
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.6, i64 0, i64 0))
%14 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 15)
%15 = tail call i32 @sleep(i32 4)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.6, i64 0, i64 0))
%17 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 12)
%18 = tail call i32 @close(i32 %6)
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.7, i64 0, i64 0))
ret i32 0
}