#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_SYNC;
int O_WRONLY;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stop_and_wait();
char *strerror(int /*<<< orphan*/);
int open_write(char *filename, int sync_mode) {
  int fd;
  int flag = sync_mode ? O_SYNC : 0;
  do {
    fd = open(filename, O_WRONLY | 0 | flag);
    if (fd < 0) {
      fprintf(stderr, "Error opening output: %s\n", strerror(errno));
      stop_and_wait();
    }
  } while (fd < 0);
  return fd;
}


x86 assembly (clang, -O0):
<open_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     116a <open_write+0x2a>
mov    0x2e88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    1174 <open_write+0x34>
xor    %eax,%eax
mov    %eax,-0x18(%rbp)
jmp    1174 <open_write+0x34>
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
or     -0x14(%rbp),%esi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11d2 <open_write+0x92>
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe3a(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x0,%al
call   1030 <stop_and_wait@plt>
jmp    11d7 <open_write+0x97>
cmpl   $0x0,-0x10(%rbp)
jl     117a <open_write+0x3a>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r15
test   %esi,%esi
je     115d <open_write+0x1d>
mov    0x2e8d(%rip),%rax
mov    (%rax),%ebp
mov    0x2e5c(%rip),%r13
mov    0x0(%r13),%esi
or     %ebp,%esi
mov    %r15,%rdi
call   1050 <open@plt>
test   %eax,%eax
jns    11c8 <open_write+0x88>
mov    0x2e53(%rip),%r12
lea    0xe7c(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x2e41(%rip),%rax
mov    (%rax),%ebx
mov    (%r12),%edi
call   1060 <strerror@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1040 <fprintf@plt>
xor    %eax,%eax
call   1030 <stop_and_wait@plt>
mov    0x0(%r13),%esi
or     %ebp,%esi
mov    %r15,%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1190 <open_write+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_SYNC = global i32 0
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"Error opening output: %s\0A\00"
@errno = global i32 0
define i32 @open_write(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = load i32, i32* @O_SYNC
br label %12
11: ; preds = %2
br label %12
12: ; preds = %11, %9
%13 = phi i32 [ %10, %9 ], [ 0, %11 ]
store i32 %13, i32* %6
br label %14
14: ; preds = %30, %12
%15 = load i8*, i8** %3
%16 = load i32, i32* @O_WRONLY
%17 = or i32 %16, 0
%18 = load i32, i32* %6
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %15, i32 %19)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %14
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %26)
%28 = call i32 (...) @stop_and_wait()
br label %29
29: ; preds = %23, %14
br label %30
30: ; preds = %29
%31 = load i32, i32* %5
%32 = icmp slt i32 %31, 0
br i1 %32, label %14, label %33
33: ; preds = %30
%34 = load i32, i32* %5
ret i32 %34
}

LLVM IR (-O2):
@O_SYNC = global i32 0
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"Error opening output: %s\0A\00"
@errno = global i32 0
define i32 @open_write(i8* nocapture %0, i32 %1) {
%3 = icmp eq i32 %1, 0
%4 = load i32, i32* @O_SYNC
%5 = select i1 %3, i32 0, i32 %4
%6 = load i32, i32* @O_WRONLY
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %2, %10
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = tail call i32 (...) @stop_and_wait()
%16 = load i32, i32* @O_WRONLY
%17 = or i32 %16, %5
%18 = tail call i32 @open(i8* %0, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %10, label %20
20: ; preds = %10, %2
%21 = phi i32 [ %8, %2 ], [ %18, %10 ]
ret i32 %21
}