#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_SHARED;
int O_APPEND;
int O_RDWR;
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
int get_file_size(int);
void *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int elfi_open_and_map(char *fname, void **data, int *len) {
  int size;
  int fd;
  if ((fd = open(fname, O_APPEND | O_RDWR, 0)) < 0) {
    perror("open:");
    exit(1);
  }
  size = get_file_size(fd);
  if ((*data = mmap(0, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED, fd,
                    0)) == MAP_FAILED) {
    perror("mmap:");
    exit(1);
  }
  *len = size;
  return fd;
}


x86 assembly (clang, -O0):
<elfi_open_and_map>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    0x2e60(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11a3 <elfi_open_and_map+0x53>
lea    0xe6c(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%edi
call   1040 <get_file_size@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
mov    0x2dff(%rip),%rax
or     (%rax),%edx
mov    0x2e06(%rip),%rax
or     (%rax),%edx
mov    0x2e15(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
mov    0x2dc6(%rip),%rcx
cmp    (%rcx),%rax
jne    1211 <elfi_open_and_map+0xc1>
lea    0xe04(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<elfi_open_and_map>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    0x2e44(%rip),%rax
mov    0x2e6d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %edx,%edx
call   1050 <open@plt>
test   %eax,%eax
js     11d7 <elfi_open_and_map+0x87>
mov    %eax,%ebp
mov    %eax,%edi
call   1040 <get_file_size@plt>
mov    %eax,%ebx
mov    0x2e6c(%rip),%rax
mov    0x2e2d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e32(%rip),%rax
or     (%rax),%edx
mov    0x2e41(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r15)
mov    0x2df6(%rip),%rcx
cmp    (%rcx),%rax
je     11e0 <elfi_open_and_map+0x90>
mov    %ebx,(%r14)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe22(%rip),%rdi
jmp    11e7 <elfi_open_and_map+0x97>
lea    0xe1f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@O_APPEND = global i32 0
@O_RDWR = global i32 0
@.str = [6 x i8] c"open:\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [6 x i8] c"mmap:\00"
define i32 @elfi_open_and_map(i8* %0, i8** %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32* %2, i32** %6
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_APPEND
%11 = load i32, i32* @O_RDWR
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %9, i32 %12, i32 0)
store i32 %13, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %3
%19 = load i32, i32* %8
%20 = call i32 @get_file_size(i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @PROT_WRITE
%24 = or i32 %22, %23
%25 = load i32, i32* @PROT_EXEC
%26 = or i32 %24, %25
%27 = load i32, i32* @MAP_SHARED
%28 = load i32, i32* %8
%29 = call i8* @mmap(i32 0, i32 %21, i32 %26, i32 %27, i32 %28, i32 0)
%30 = load i8**, i8*** %5
store i8* %29, i8** %30
%31 = load i8*, i8** @MAP_FAILED
%32 = icmp eq i8* %29, %31
br i1 %32, label %33, label %36
33: ; preds = %18
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %18
%37 = load i32, i32* %7
%38 = load i32*, i32** %6
store i32 %37, i32* %38
%39 = load i32, i32* %8
ret i32 %39
}

LLVM IR (-O2):
@O_APPEND = global i32 0
@O_RDWR = global i32 0
@.str = [6 x i8] c"open:\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [6 x i8] c"mmap:\00"
define i32 @elfi_open_and_map(i8* nocapture %0, i8** nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = load i32, i32* @O_APPEND
%5 = load i32, i32* @O_RDWR
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* %0, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %3
%13 = tail call i32 @get_file_size(i32 %7)
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %15, %14
%17 = load i32, i32* @PROT_EXEC
%18 = or i32 %16, %17
%19 = load i32, i32* @MAP_SHARED
%20 = tail call i8* @mmap(i32 0, i32 %13, i32 %18, i32 %19, i32 %7, i32 0)
store i8* %20, i8** %1
%21 = load i8*, i8** @MAP_FAILED
%22 = icmp eq i8* %20, %21
br i1 %22, label %23, label %26
23: ; preds = %12
%24 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %12
store i32 %13, i32* %2
ret i32 %7
}