#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
#define EACCES 130
#define EISDIR 129
#define ENOENT 128
int /*<<< orphan*/ EXIT_FAILURE;
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char const *, char const *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ stderr;
FILE *openFile(const char *path, const char *mode) {
  FILE *file = fopen(path, mode);
  if (file == NULL) {
    char *errstr;
    switch (errno) {
    case EACCES:
      errstr = "Permission denied";
      break;
    case ENOENT:
      errstr = "File not found";
      break;
    case EISDIR:
      errstr = "Is a directory";
      break;
    default:
      errstr = "Unknown error";
    }
    fprintf(stderr, "Error opening %s: %s\n", path, errstr);
    exit(EXIT_FAILURE);
  }
  return file;
}


x86 assembly (clang, -O0):
<openFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1204 <openFile+0xd4>
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
sub    $0x80,%eax
je     11ae <openFile+0x7e>
jmp    1178 <openFile+0x48>
mov    -0x24(%rbp),%eax
sub    $0x81,%eax
je     11be <openFile+0x8e>
jmp    118b <openFile+0x5b>
mov    -0x24(%rbp),%eax
sub    $0x82,%eax
jne    11ce <openFile+0x9e>
jmp    119e <openFile+0x6e>
lea    0xe5b(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    11d9 <openFile+0xa9>
lea    0xe5d(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    11d9 <openFile+0xa9>
lea    0xe5c(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    11d9 <openFile+0xa9>
lea    0xe5b(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xe4d(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openFile>:
push   %rbx
mov    %rdi,%rbx
call   1040 <fopen@plt>
test   %rax,%rax
je     1140 <openFile+0x10>
pop    %rbx
ret
mov    0x2e91(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
cmp    $0x2,%eax
ja     1163 <openFile+0x33>
cltq
lea    0xefa(%rip),%rdx
movslq (%rdx,%rax,4),%rcx
add    %rdx,%rcx
jmp    116a <openFile+0x3a>
lea    0xec6(%rip),%rcx
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
lea    0xec4(%rip),%rsi
mov    %rbx,%rdx
call   1030 <fprintf@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@errno = global i32 0
@.str = [18 x i8] c"Permission denied\00"
@.str.1 = [15 x i8] c"File not found\00"
@.str.2 = [15 x i8] c"Is a directory\00"
@.str.3 = [14 x i8] c"Unknown error\00"
@stderr = global i32 0
@.str.4 = [22 x i8] c"Error opening %s: %s\0A\00"
@EXIT_FAILURE = global i32 0
define i32* @openFile(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %7, i8* %8)
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %25
12: ; preds = %2
%13 = load i32, i32* @errno
switch i32 %13, label %17 [
i32 130, label %14
i32 128, label %15
i32 129, label %16
]
14: ; preds = %12
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8** %6
br label %18
15: ; preds = %12
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8** %6
br label %18
16: ; preds = %12
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8** %6
br label %18
17: ; preds = %12
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8** %6
br label %18
18: ; preds = %17, %16, %15, %14
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %3
%21 = load i8*, i8** %6
%22 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %20, i8* %21)
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %2
%26 = load i32*, i32** %5
ret i32* %26
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [18 x i8] c"Permission denied\00"
@.str.1 = [15 x i8] c"File not found\00"
@.str.2 = [15 x i8] c"Is a directory\00"
@.str.3 = [14 x i8] c"Unknown error\00"
@stderr = global i32 0
@.str.4 = [22 x i8] c"Error opening %s: %s\0A\00"
@EXIT_FAILURE = global i32 0
@reltable.openFile = [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([15 x i8]* @.str.1 to i64), i64 ptrtoint ([3 x i32]* @reltable.openFile to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([15 x i8]* @.str.2 to i64), i64 ptrtoint ([3 x i32]* @reltable.openFile to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([18 x i8]* @.str to i64), i64 ptrtoint ([3 x i32]* @reltable.openFile to i64)) to i32)]
define noalias i32* @openFile(i8* %0, i8* nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* %1)
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %19
5: ; preds = %2
%6 = load i32, i32* @errno
%7 = add i32 %6, -128
%8 = icmp ult i32 %7, 3
br i1 %8, label %9, label %13
9: ; preds = %5
%10 = sext i32 %7 to i64
%11 = shl i64 %10, 2
%12 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.openFile to i8*), i64 %11)
br label %13
13: ; preds = %5, %9
%14 = phi i8* [ %12, %9 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), %5 ]
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %14)
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
ret i32* %3
}