#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 */
struct stat {
  scalar_t__ st_size;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *, char const *);
int fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
void map_file(const char *path, char **pmap, size_t *psz) {
  int fd;
  struct stat st;
  void *map;
  size_t sz;
  fd = open(path, O_RDONLY);
  if (fd == -1)
    die("cannot open %s: %m", path);
  if (fstat(fd, &st) == -1)
    die("cannot stat %s: %m", path);
  sz = (size_t)st.st_size;
  map = mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0);
  if (map == MAP_FAILED)
    die("cannot map %s: %m", path);
  close(fd);
  *pmap = map;
  *psz = sz;
}


x86 assembly (clang, -O0):
<map_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1193 <map_file+0x43>
mov    -0x8(%rbp),%rsi
lea    0xe72(%rip),%rdi
call   1060 <die@plt>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11b8 <map_file+0x68>
mov    -0x8(%rbp),%rsi
lea    0xe60(%rip),%rdi
call   1060 <die@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
mov    0x2e14(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2dd3(%rip),%rcx
cmp    (%rcx),%rax
jne    120e <map_file+0xbe>
mov    -0x8(%rbp),%rsi
lea    0xe1d(%rip),%rdi
call   1060 <die@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1188 <map_file+0x38>
lea    0xe80(%rip),%rdi
mov    %r12,%rsi
call   1060 <die@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11a6 <map_file+0x56>
lea    0xe75(%rip),%rdi
mov    %r12,%rsi
call   1060 <die@plt>
mov    (%rsp),%r13
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
mov    0x2e2e(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r13,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2df2(%rip),%rax
cmp    (%rax),%rbx
jne    11ea <map_file+0x9a>
lea    0xe44(%rip),%rdi
mov    %r12,%rsi
call   1060 <die@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,(%r15)
mov    %r13,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [19 x i8] c"cannot open %s: %m\00"
@.str.1 = [19 x i8] c"cannot stat %s: %m\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [18 x i8] c"cannot map %s: %m\00"
define void @map_file(i8* %0, i8** %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i64* %2, i64** %6
%11 = load i8*, i8** %4
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = load i8*, i8** %4
%18 = call i32 @die(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %16, %3
%20 = load i32, i32* %7
%21 = call i32 @fstat(i32 %20, %struct.stat* %8)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i8*, i8** %4
%25 = call i32 @die(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %23, %19
%27 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%28 = load i64, i64* %27
store i64 %28, i64* %10
%29 = load i64, i64* %10
%30 = load i32, i32* @PROT_READ
%31 = load i32, i32* @MAP_SHARED
%32 = load i32, i32* %7
%33 = call i8* @mmap(i32* null, i64 %29, i32 %30, i32 %31, i32 %32, i32 0)
store i8* %33, i8** %9
%34 = load i8*, i8** %9
%35 = load i8*, i8** @MAP_FAILED
%36 = icmp eq i8* %34, %35
br i1 %36, label %37, label %40
37: ; preds = %26
%38 = load i8*, i8** %4
%39 = call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %37, %26
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
%43 = load i8*, i8** %9
%44 = load i8**, i8*** %5
store i8* %43, i8** %44
%45 = load i64, i64* %10
%46 = load i64*, i64** %6
store i64 %45, i64* %46
ret void
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [19 x i8] c"cannot open %s: %m\00"
@.str.1 = [19 x i8] c"cannot stat %s: %m\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [18 x i8] c"cannot map %s: %m\00"
define void @map_file(i8* %0, i8** nocapture writeonly %1, i64* nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @die(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %11
11: ; preds = %9, %3
%12 = call i32 @fstat(i32 %7, %struct.stat* %4)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @die(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %16
16: ; preds = %14, %11
%17 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%18 = load i64, i64* %17
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @MAP_SHARED
%21 = tail call i8* @mmap(i32* null, i64 %18, i32 %19, i32 %20, i32 %7, i32 0)
%22 = load i8*, i8** @MAP_FAILED
%23 = icmp eq i8* %21, %22
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = tail call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %26
26: ; preds = %24, %16
%27 = tail call i32 @close(i32 %7)
store i8* %21, i8** %1
store i64 %18, i64* %2
ret void
}