#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_EXCL;
int O_RDWR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int open(char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int openFile(char *fileName, bool shouldCreate) {
  int descriptor = -1;
  if (shouldCreate) {
    descriptor = open(fileName, O_RDWR | O_CREAT | O_EXCL);
  } else {
    descriptor = open(fileName, O_RDWR);
  }
  if (descriptor == -1) {
    fprintf(stderr, "Error opening file at path: %s\nError: %s\n", fileName,
            strerror(errno));
    exit(errno);
  }
  return descriptor;
}


x86 assembly (clang, -O0):
<openFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     118c <openFile+0x4c>
mov    -0x8(%rbp),%rdi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
mov    0x2e84(%rip),%rax
or     (%rax),%esi
mov    0x2e4b(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x10(%rbp)
jmp    11a1 <openFile+0x61>
mov    -0x8(%rbp),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11f1 <openFile+0xb1>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rdx
mov    %rax,%rcx
lea    0xe22(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openFile>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e95(%rip),%rax
mov    (%rax),%ecx
mov    0x2ea4(%rip),%rax
mov    (%rax),%eax
or     %ecx,%eax
mov    0x2e69(%rip),%rdx
or     (%rdx),%eax
mov    %rdi,%rbx
test   %esi,%esi
cmove  %ecx,%eax
mov    %eax,%esi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     117a <openFile+0x3a>
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e4f(%rip),%rax
mov    (%rax),%ebp
mov    0x2e36(%rip),%r14
mov    (%r14),%edi
call   1060 <strerror@plt>
lea    0xe67(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1030 <fprintf@plt>
mov    (%r14),%edi
call   1050 <exit@plt>

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

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@stderr = global i32 0
@.str = [42 x i8] c"Error opening file at path: %s\0AError: %s\0A\00"
@errno = global i32 0
define i32 @openFile(i8* %0, i32 %1) {
%3 = icmp eq i32 %1, 0
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_EXCL
%8 = or i32 %6, %7
%9 = select i1 %3, i32 %4, i32 %8
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %15)
%17 = load i32, i32* @errno
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
ret i32 %10
}