#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_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprint_err(char *, char *, char *, int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
extern int open_binary_file(char *filename, int for_write) {
  int flags = 0;
  int filedes;
  if (for_write) {
    flags = flags | O_WRONLY | O_CREAT | O_TRUNC;
    filedes = open(filename, flags, 00777);
  } else {
    flags = flags | O_RDONLY;
    filedes = open(filename, flags);
  }
  if (filedes == -1)
    fprint_err("### Error opening file %s for %s: %s\n", filename,
               (for_write ? "write" : "read"), strerror(errno));
  return filedes;
}


x86 assembly (clang, -O0):
<open_binary_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     118c <open_binary_file+0x5c>
mov    -0x10(%rbp),%eax
mov    0x2e5e(%rip),%rcx
or     (%rcx),%eax
mov    0x2e95(%rip),%rcx
or     (%rcx),%eax
mov    0x2e84(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    $0x1ff,%edx
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
jmp    11ac <open_binary_file+0x7c>
mov    -0x10(%rbp),%eax
mov    0x2e42(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11fe <open_binary_file+0xce>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%edx
lea    0xe64(%rip),%rax
lea    0xe57(%rip),%rcx
cmp    $0x0,%edx
cmovne %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    %eax,%ecx
lea    0xe07(%rip),%rdi
call   1030 <fprint_err@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_binary_file>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
test   %esi,%esi
je     1169 <open_binary_file+0x39>
mov    0x2e74(%rip),%rax
mov    0x2ead(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e9a(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x1ff,%edx
xor    %eax,%eax
call   1040 <open@plt>
jmp    117c <open_binary_file+0x4c>
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11b9 <open_binary_file+0x89>
test   %ebp,%ebp
lea    0xea0(%rip),%rax
lea    0xe93(%rip),%rbp
cmove  %rax,%rbp
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe54(%rip),%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
mov    %eax,%ecx
call   1030 <fprint_err@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_RDONLY = global i32 0
@.str = [38 x i8] c"### Error opening file %s for %s: %s\0A\00"
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [5 x i8] c"read\00"
@errno = global i32 0
define i32 @open_binary_file(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
store i32 0, i32* %5
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %20
9: ; preds = %2
%10 = load i32, i32* %5
%11 = load i32, i32* @O_WRONLY
%12 = or i32 %10, %11
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %14, %15
store i32 %16, i32* %5
%17 = load i8*, i8** %3
%18 = load i32, i32* %5
%19 = call i32 (i8*, i32, ...) @open(i8* %17, i32 %18, i32 511)
store i32 %19, i32* %6
br label %27
20: ; preds = %2
%21 = load i32, i32* %5
%22 = load i32, i32* @O_RDONLY
%23 = or i32 %21, %22
store i32 %23, i32* %5
%24 = load i8*, i8** %3
%25 = load i32, i32* %5
%26 = call i32 (i8*, i32, ...) @open(i8* %24, i32 %25)
store i32 %26, i32* %6
br label %27
27: ; preds = %20, %9
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %39
30: ; preds = %27
%31 = load i8*, i8** %3
%32 = load i32, i32* %4
%33 = icmp ne i32 %32, 0
%34 = zext i1 %33 to i64
%35 = select i1 %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0)
%36 = load i32, i32* @errno
%37 = call i32 @strerror(i32 %36)
%38 = call i32 @fprint_err(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %31, i8* %35, i32 %37)
br label %39
39: ; preds = %30, %27
%40 = load i32, i32* %6
ret i32 %40
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_RDONLY = global i32 0
@.str = [38 x i8] c"### Error opening file %s for %s: %s\0A\00"
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [5 x i8] c"read\00"
@errno = global i32 0
define i32 @open_binary_file(i8* %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %11, label %4
4: ; preds = %2
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %9, i32 511)
br label %14
11: ; preds = %2
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %12)
br label %14
14: ; preds = %11, %4
%15 = phi i32 [ %10, %4 ], [ %13, %11 ]
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = select i1 %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0)
%19 = load i32, i32* @errno
%20 = tail call i32 @strerror(i32 %19)
%21 = tail call i32 @fprint_err(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %18, i32 %20)
br label %22
22: ; preds = %17, %14
ret i32 %15
}