#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ file_exists(char const *);
long file_size(char const *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
void *mmap(int /*<<< orphan*/ *, long, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *get_file_handle(const char *filename, long *fsz) {
  int fd;
  void *handle;
  if (!file_exists(filename)) {
    fprintf(stderr, "%s does not exist\n", filename);
    exit(1);
  }
  *fsz = file_size(filename);
  fd = open(filename, O_RDONLY);
  assert(fd > 0);
  handle = mmap(NULL, *fsz, PROT_READ, MAP_PRIVATE, fd, 0);
  if (handle == MAP_FAILED) {
    fprintf(stderr, "Error mapping %s to memory", filename);
    exit(1);
  }
  close(fd);
  return (char *)handle;
}


x86 assembly (clang, -O0):
<get_file_handle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <file_exists@plt>
cmp    $0x0,%eax
jne    11c5 <get_file_handle+0x45>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe4a(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1060 <file_size@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rdi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
mov    0x2db3(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d8a(%rip),%rcx
cmp    (%rcx),%rax
jne    1262 <get_file_handle+0xe2>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdc0(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_handle>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
call   1030 <file_exists@plt>
test   %eax,%eax
je     11fe <get_file_handle+0x7e>
mov    %r14,%rdi
call   1060 <file_size@plt>
mov    %rax,(%rbx)
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1090 <open@plt>
mov    %eax,%ebp
xor    %edi,%edi
test   %eax,%eax
setg   %dil
call   1070 <assert@plt>
mov    (%rbx),%rsi
mov    0x2e30(%rip),%rax
mov    (%rax),%edx
mov    0x2df7(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2dd6(%rip),%rax
cmp    (%rax),%rbx
je     1210 <get_file_handle+0x90>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
jmp    1220 <get_file_handle+0xa0>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
lea    0xdf3(%rip),%rsi
mov    %r14,%rdx
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [19 x i8] c"%s does not exist\0A\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [27 x i8] c"Error mapping %s to memory\00"
define i8* @get_file_handle(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
store i64* %1, i64** %4
%7 = load i8*, i8** %3
%8 = call i32 @file_exists(i8* %7)
%9 = icmp ne i32 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %3
%13 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i8*, i8** %3
%17 = call i64 @file_size(i8* %16)
%18 = load i64*, i64** %4
store i64 %17, i64* %18
%19 = load i8*, i8** %3
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp sgt i32 %22, 0
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = load i64*, i64** %4
%27 = load i64, i64* %26
%28 = load i32, i32* @PROT_READ
%29 = load i32, i32* @MAP_PRIVATE
%30 = load i32, i32* %5
%31 = call i8* @mmap(i32* null, i64 %27, i32 %28, i32 %29, i32 %30, i32 0)
store i8* %31, i8** %6
%32 = load i8*, i8** %6
%33 = load i8*, i8** @MAP_FAILED
%34 = icmp eq i8* %32, %33
br i1 %34, label %35, label %40
35: ; preds = %15
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %3
%38 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %15
%41 = load i32, i32* %5
%42 = call i32 @close(i32 %41)
%43 = load i8*, i8** %6
ret i8* %43
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"%s does not exist\0A\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [27 x i8] c"Error mapping %s to memory\00"
define i8* @get_file_handle(i8* %0, i64* nocapture %1) {
%3 = tail call i32 @file_exists(i8* %0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = tail call i64 @file_size(i8* %0)
store i64 %10, i64* %1
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %0, i32 %11)
%13 = icmp sgt i32 %12, 0
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = load i64, i64* %1
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @MAP_PRIVATE
%19 = tail call i8* @mmap(i32* null, i64 %16, i32 %17, i32 %18, i32 %12, i32 0)
%20 = load i8*, i8** @MAP_FAILED
%21 = icmp eq i8* %19, %20
br i1 %21, label %22, label %26
22: ; preds = %9
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %9
%27 = tail call i32 @close(i32 %12)
ret i8* %19
}