#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*/ uint64_t;

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int PROT_EXEC;
int PROT_READ;
int /*<<< orphan*/ err(int, char *);
char *mmap(int /*<<< orphan*/, int /*<<< orphan*/, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
char *map_file(const char *path, uint64_t size) {
  int fd = open(path, O_RDONLY);
  if (fd == -1)
    err(1, "open");
  char *mem = mmap(0, size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);
  if (mem == MAP_FAILED)
    err(1, "mmap");
  return mem;
}


x86 assembly (clang, -O0):
<map_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    116f <map_file+0x3f>
mov    $0x1,%edi
lea    0xe96(%rip),%rsi
call   1040 <err@plt>
mov    -0xc(%rbp),%esi
mov    0x2e7f(%rip),%rax
mov    (%rax),%edx
mov    0x2e56(%rip),%rax
or     (%rax),%edx
mov    0x2e3d(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e15(%rip),%rcx
cmp    (%rcx),%rax
jne    11c5 <map_file+0x95>
mov    $0x1,%edi
lea    0xe45(%rip),%rsi
call   1040 <err@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_file>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    115b <map_file+0x2b>
lea    0xeaf(%rip),%rsi
mov    $0x1,%edi
call   1040 <err@plt>
mov    0x2e96(%rip),%rax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e54(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e31(%rip),%rax
cmp    (%rax),%rbx
jne    11a5 <map_file+0x75>
lea    0xe6a(%rip),%rsi
mov    $0x1,%edi
call   1040 <err@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_EXEC = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
define i8* @map_file(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = load i32, i32* %4
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @PROT_EXEC
%18 = or i32 %16, %17
%19 = load i32, i32* @MAP_PRIVATE
%20 = load i32, i32* %5
%21 = call i8* @mmap(i32 0, i32 %15, i32 %18, i32 %19, i32 %20, i32 0)
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = load i8*, i8** @MAP_FAILED
%24 = icmp eq i8* %22, %23
br i1 %24, label %25, label %27
25: ; preds = %14
%26 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %14
%28 = load i8*, i8** %6
ret i8* %28
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_EXEC = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
define i8* @map_file(i8* nocapture %0, i32 %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %2
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_EXEC
%11 = or i32 %10, %9
%12 = load i32, i32* @MAP_PRIVATE
%13 = tail call i8* @mmap(i32 0, i32 %1, i32 %11, i32 %12, i32 %4, i32 0)
%14 = load i8*, i8** @MAP_FAILED
%15 = icmp eq i8* %13, %14
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %8
ret i8* %13
}