#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 scalar_t__ uint64_t;
typedef scalar_t__ off_t;

/* Variables and functions */
int /*<<< orphan*/ MADV_SEQUENTIAL;
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ madvise(void *, scalar_t__, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/ *, scalar_t__, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void *map_file(const char *filepath, int readonly, uint64_t *filelen) {
  int fd;
  off_t len;
  void *data;
  fd = open(filepath, readonly ? O_RDONLY : O_RDWR);
  if (fd < 0) {
    fprintf(stderr, "Failed to open file %s: %s\n", filepath, strerror(errno));
    return NULL;
  }
  len = lseek(fd, 0, SEEK_END);
  if (len < 0 || lseek(fd, 0, SEEK_SET)) {
    fprintf(stderr, "Failed to calculate file length of %s: %s\n", filepath,
            strerror(errno));
    close(fd);
    return NULL;
  }
  data = mmap(NULL, len, readonly ? PROT_READ : (PROT_READ | PROT_WRITE),
              readonly ? MAP_PRIVATE : 0, fd, 0);
  close(fd);
  if (data == MAP_FAILED) {
    fprintf(stderr, "Failed to MMAP file %s: %s\n", filepath, strerror(errno));
    return NULL;
  }
  madvise(data, len, MADV_SEQUENTIAL);
  *filelen = len;
  return data;
}


x86 assembly (clang, -O0):
<map_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11a9 <map_file+0x39>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
jmp    11b5 <map_file+0x45>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1213 <map_file+0xa3>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xdff(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    13b6 <map_file+0x246>
mov    -0x24(%rbp),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jl     1252 <map_file+0xe2>
mov    -0x24(%rbp),%edi
mov    0x2d71(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0x0,%rax
je     129f <map_file+0x12f>
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    %rax,%rcx
lea    0xd97(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    13b6 <map_file+0x246>
mov    -0x30(%rbp),%rax
mov    %rax,-0x70(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12c2 <map_file+0x152>
mov    0x2d40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
jmp    12d7 <map_file+0x167>
mov    0x2d2f(%rip),%rax
mov    (%rax),%eax
mov    0x2cd6(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x74(%rbp)
mov    -0x74(%rbp),%eax
mov    %eax,-0x78(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12f8 <map_file+0x188>
mov    0x2cb2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
jmp    1302 <map_file+0x192>
xor    %eax,%eax
mov    %eax,-0x7c(%rbp)
jmp    1302 <map_file+0x192>
mov    -0x78(%rbp),%edx
mov    -0x70(%rbp),%rsi
mov    -0x7c(%rbp),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x38(%rbp),%rax
mov    0x2c65(%rip),%rcx
cmp    (%rcx),%rax
jne    138d <map_file+0x21d>
mov    0x2c7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    0x2c5b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x8c(%rbp),%edi
mov    -0x88(%rbp),%rdx
mov    %rax,%rcx
lea    0xccc(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    13b6 <map_file+0x246>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    0x2bec(%rip),%rax
mov    (%rax),%edx
call   1070 <madvise@plt>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x90,%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    %esi,%ebx
mov    %rdi,%r15
test   %esi,%esi
jne    1190 <map_file+0x20>
mov    0x2e52(%rip),%rax
jmp    1197 <map_file+0x27>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     120f <map_file+0x9f>
mov    %eax,%ebp
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   %rax,%rax
js     11db <map_file+0x6b>
mov    %rax,%r12
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
xor    %r13d,%r13d
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   %rax,%rax
je     124e <map_file+0xde>
mov    0x2dde(%rip),%rax
mov    (%rax),%ebx
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe23(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    123a <map_file+0xca>
mov    0x2daa(%rip),%rax
mov    (%rax),%ebx
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdd3(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
call   1060 <fprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
test   %ebx,%ebx
mov    0x2d49(%rip),%rax
cmovne (%rax),%ebx
mov    0x2d97(%rip),%rax
mov    0x2d40(%rip),%rcx
mov    (%rcx),%edx
cmovne %r13d,%edx
or     (%rax),%edx
xor    %edi,%edi
mov    %r12,%rsi
mov    %ebx,%ecx
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x2d05(%rip),%rax
cmp    (%rax),%rbx
je     12b1 <map_file+0x141>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <madvise@plt>
mov    %r12,(%r14)
jmp    123c <map_file+0xcc>
mov    0x2d08(%rip),%rax
mov    (%rax),%ebx
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd78(%rip),%rsi
jmp    122d <map_file+0xbd>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Failed to open file %s: %s\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [43 x i8] c"Failed to calculate file length of %s: %s\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [28 x i8] c"Failed to MMAP file %s: %s\0A\00"
@MADV_SEQUENTIAL = global i32 0
define i8* @map_file(i8* %0, i32 %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i64* %2, i64** %7
%11 = load i8*, i8** %5
%12 = load i32, i32* %6
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = load i32, i32* @O_RDONLY
br label %18
16: ; preds = %3
%17 = load i32, i32* @O_RDWR
br label %18
18: ; preds = %16, %14
%19 = phi i32 [ %15, %14 ], [ %17, %16 ]
%20 = call i32 @open(i8* %11, i32 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %5
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %25, i8* %27)
store i8* null, i8** %4
br label %88
29: ; preds = %18
%30 = load i32, i32* %8
%31 = load i32, i32* @SEEK_END
%32 = call i64 @lseek(i32 %30, i32 0, i32 %31)
store i64 %32, i64* %9
%33 = load i64, i64* %9
%34 = icmp slt i64 %33, 0
br i1 %34, label %40, label %35
35: ; preds = %29
%36 = load i32, i32* %8
%37 = load i32, i32* @SEEK_SET
%38 = call i64 @lseek(i32 %36, i32 0, i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %48
40: ; preds = %35, %29
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %5
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8* %42, i8* %44)
%46 = load i32, i32* %8
%47 = call i32 @close(i32 %46)
store i8* null, i8** %4
br label %88
48: ; preds = %35
%49 = load i64, i64* %9
%50 = load i32, i32* %6
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = load i32, i32* @PROT_READ
br label %58
54: ; preds = %48
%55 = load i32, i32* @PROT_READ
%56 = load i32, i32* @PROT_WRITE
%57 = or i32 %55, %56
br label %58
58: ; preds = %54, %52
%59 = phi i32 [ %53, %52 ], [ %57, %54 ]
%60 = load i32, i32* %6
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = load i32, i32* @MAP_PRIVATE
br label %65
64: ; preds = %58
br label %65
65: ; preds = %64, %62
%66 = phi i32 [ %63, %62 ], [ 0, %64 ]
%67 = load i32, i32* %8
%68 = call i8* @mmap(i32* null, i64 %49, i32 %59, i32 %66, i32 %67, i32 0)
store i8* %68, i8** %10
%69 = load i32, i32* %8
%70 = call i32 @close(i32 %69)
%71 = load i8*, i8** %10
%72 = load i8*, i8** @MAP_FAILED
%73 = icmp eq i8* %71, %72
br i1 %73, label %74, label %80
74: ; preds = %65
%75 = load i32, i32* @stderr
%76 = load i8*, i8** %5
%77 = load i32, i32* @errno
%78 = call i8* @strerror(i32 %77)
%79 = call i32 @fprintf(i32 %75, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %76, i8* %78)
store i8* null, i8** %4
br label %88
80: ; preds = %65
%81 = load i8*, i8** %10
%82 = load i64, i64* %9
%83 = load i32, i32* @MADV_SEQUENTIAL
%84 = call i32 @madvise(i8* %81, i64 %82, i32 %83)
%85 = load i64, i64* %9
%86 = load i64*, i64** %7
store i64 %85, i64* %86
%87 = load i8*, i8** %10
store i8* %87, i8** %4
br label %88
88: ; preds = %80, %74, %40, %23
%89 = load i8*, i8** %4
ret i8* %89
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Failed to open file %s: %s\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [43 x i8] c"Failed to calculate file length of %s: %s\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [28 x i8] c"Failed to MMAP file %s: %s\0A\00"
@MADV_SEQUENTIAL = global i32 0
define i8* @map_file(i8* %0, i32 %1, i64* nocapture writeonly %2) {
%4 = icmp eq i32 %1, 0
%5 = load i32, i32* @O_RDONLY
%6 = load i32, i32* @O_RDWR
%7 = select i1 %4, i32 %6, i32 %5
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %13)
br label %48
15: ; preds = %3
%16 = load i32, i32* @SEEK_END
%17 = tail call i64 @lseek(i32 %8, i32 0, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = load i32, i32* @SEEK_SET
%21 = tail call i64 @lseek(i32 %8, i32 0, i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %19, %15
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %26)
%28 = tail call i32 @close(i32 %8)
br label %48
29: ; preds = %19
%30 = load i32, i32* @PROT_READ
%31 = load i32, i32* @PROT_WRITE
%32 = select i1 %4, i32 %31, i32 0
%33 = or i32 %32, %30
%34 = load i32, i32* @MAP_PRIVATE
%35 = select i1 %4, i32 0, i32 %34
%36 = tail call i8* @mmap(i32* null, i64 %17, i32 %33, i32 %35, i32 %8, i32 0)
%37 = tail call i32 @close(i32 %8)
%38 = load i8*, i8** @MAP_FAILED
%39 = icmp eq i8* %36, %38
br i1 %39, label %40, label %45
40: ; preds = %29
%41 = load i32, i32* @stderr
%42 = load i32, i32* @errno
%43 = tail call i8* @strerror(i32 %42)
%44 = tail call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %43)
br label %48
45: ; preds = %29
%46 = load i32, i32* @MADV_SEQUENTIAL
%47 = tail call i32 @madvise(i8* %36, i64 %17, i32 %46)
store i64 %17, i64* %2
br label %48
48: ; preds = %45, %40, %23, %10
%49 = phi i8* [ null, %10 ], [ null, %23 ], [ null, %40 ], [ %36, %45 ]
ret i8* %49
}