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

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close_tb(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int open_tb(char const *, char const *);
int /*<<< orphan*/ stderr;
void *map_file(const char *name, const char *suffix, uint64_t *mapping) {
  int fd = open_tb(name, suffix);
  if (fd == -1)
    return NULL;
  struct stat statbuf;
  fstat(fd, &statbuf);
  *mapping = statbuf.st_size;
  void *data = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (data == MAP_FAILED) {
    fprintf(stderr, "Could not mmap() %s.\n", name);
    exit(1);
  }
  close_tb(fd);
  return data;
}


x86 assembly (clang, -O0):
<map_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <open_tb@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    119b <map_file+0x3b>
movq   $0x0,-0x8(%rbp)
jmp    1220 <map_file+0xc0>
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1080 <fstat@plt>
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%esi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
mov    0x2e25(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2de4(%rip),%rcx
cmp    (%rcx),%rax
jne    1210 <map_file+0xb0>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdff(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x24(%rbp),%edi
call   1070 <close_tb@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rbx
mov    %rdi,%r14
call   1030 <open_tb@plt>
cmp    $0xffffffff,%eax
je     11c3 <map_file+0x63>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
mov    0x8(%rsp),%esi
mov    %esi,(%rbx)
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
mov    0x2e4c(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2e13(%rip),%rax
cmp    (%rax),%rbx
je     11d1 <map_file+0x71>
mov    %ebp,%edi
call   1070 <close_tb@plt>
jmp    11c5 <map_file+0x65>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@stderr = global i32 0
@.str = [22 x i8] c"Could not mmap() %s.\0A\00"
define i8* @map_file(i8* %0, i8* %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = tail call i32 @open_tb(i8* %0, i8* %1)
%6 = icmp eq i32 %5, -1
br i1 %6, label %23, label %7
7: ; preds = %3
%8 = bitcast %struct.stat* %4 to i8*
%9 = call i32 @fstat(i32 %5, %struct.stat* %4)
%10 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%11 = load i32, i32* %10
store i32 %11, i32* %2
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @MAP_SHARED
%14 = tail call i8* @mmap(i32* null, i32 %11, i32 %12, i32 %13, i32 %5, i32 0)
%15 = load i8*, i8** @MAP_FAILED
%16 = icmp eq i8* %14, %15
br i1 %16, label %17, label %21
17: ; preds = %7
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %7
%22 = tail call i32 @close_tb(i32 %5)
br label %23
23: ; preds = %3, %21
%24 = phi i8* [ %14, %21 ], [ null, %3 ]
ret i8* %24
}