#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 int /*<<< orphan*/ off_t;
typedef scalar_t__ int32_t;

/* Variables and functions */
void *MAP_FAILED;
scalar_t__ MAP_FILE;
scalar_t__ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
scalar_t__ PROT_READ;
scalar_t__ PROT_WRITE;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ close(scalar_t__);
scalar_t__ lseek(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/, scalar_t__, scalar_t__, scalar_t__, scalar_t__,
           int /*<<< orphan*/);
scalar_t__ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, scalar_t__, char *, ...);
void *map_file(char *fname, uint64_t *filesizep, int32_t enablewrite) {
  void *mmap64(void *addr, size_t len, int32_t prot, int32_t flags,
               int32_t fildes, off_t off);
  int32_t fd, rwflags, flags = MAP_FILE | MAP_SHARED;
  uint64_t filesize;
  void *ptr = 0;
  *filesizep = 0;
  if (enablewrite != 0)
    fd = open(fname, O_RDWR);
  else
    fd = open(fname, O_RDONLY);
  if (fd < 0) {
    printf("map_file: error opening enablewrite.%d %s\n", enablewrite, fname);
    return (0);
  }
  if (*filesizep == 0)
    filesize = (uint64_t)lseek(fd, 0, SEEK_END);
  else
    filesize = *filesizep;
  rwflags = PROT_READ;
  if (enablewrite != 0)
    rwflags |= PROT_WRITE;
  ptr = mmap(0, filesize, rwflags, flags, fd, 0);
  close(fd);
  if (ptr == 0 || ptr == MAP_FAILED) {
    printf("map_file.write%d: mapping %s failed? mp %p\n", enablewrite, fname,
           ptr);
    return (0);
  }
  *filesizep = filesize;
  return (ptr);
}


x86 assembly (clang, -O0):
<map_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%rax
mov    0x2e73(%rip),%rcx
or     (%rcx),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     11b5 <map_file+0x65>
mov    -0x10(%rbp),%rdi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %rax,-0x28(%rbp)
jmp    11cb <map_file+0x7b>
mov    -0x10(%rbp),%rdi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11f9 <map_file+0xa9>
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12db <map_file+0x18b>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1224 <map_file+0xd4>
mov    -0x28(%rbp),%rdi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %rax,-0x40(%rbp)
jmp    122f <map_file+0xdf>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     125a <map_file+0x10a>
mov    0x2d61(%rip),%rax
mov    (%rax),%rax
or     -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x28(%rbp),%r8
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <close@plt>
cmpq   $0x0,-0x48(%rbp)
je     12a1 <map_file+0x151>
mov    -0x48(%rbp),%rax
mov    0x2d10(%rip),%rcx
cmp    (%rcx),%rax
jne    12c8 <map_file+0x178>
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x48(%rbp),%rcx
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12db <map_file+0x18b>
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%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,%r12
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    0x2e55(%rip),%rax
mov    0x2e76(%rip),%rcx
mov    (%rcx),%r13
or     (%rax),%r13
movq   $0x0,(%rsi)
test   %rdx,%rdx
jne    118d <map_file+0x3d>
mov    0x2e3d(%rip),%rax
jmp    1194 <map_file+0x44>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %rax,%rax
js     121c <map_file+0xcc>
mov    %rax,%r15
mov    %rbx,(%rsp)
mov    %rbp,%rbx
mov    0x0(%rbp),%r14
test   %r14,%r14
jne    11cc <map_file+0x7c>
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %rax,%r14
mov    0x2e25(%rip),%rax
mov    %r12,%rdx
test   %r12,%r12
je     11e5 <map_file+0x95>
mov    0x2dce(%rip),%rcx
mov    (%rcx),%rdx
or     (%rax),%rdx
xor    %edi,%edi
mov    %r14,%rsi
mov    %r13,%rcx
mov    %r15,%r8
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    %r15,%rdi
call   1060 <close@plt>
test   %rbp,%rbp
je     1234 <map_file+0xe4>
mov    0x2d96(%rip),%rax
cmp    (%rax),%rbp
je     1234 <map_file+0xe4>
mov    %r14,(%rbx)
jmp    124e <map_file+0xfe>
lea    0xddd(%rip),%rdi
xor    %ebp,%ebp
mov    %r12,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    124e <map_file+0xfe>
lea    0xdf0(%rip),%rdi
mov    %r12,%rsi
mov    (%rsp),%rdx
mov    %rbp,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MAP_FILE = global i64 0
@MAP_SHARED = global i64 0
@O_RDWR = global i32 0
@O_RDONLY = global i32 0
@.str = [43 x i8] c"map_file: error opening enablewrite.%d %s\0A\00"
@SEEK_END = global i32 0
@PROT_READ = global i64 0
@PROT_WRITE = global i64 0
@MAP_FAILED = global i8* null
@.str.1 = [44 x i8] c"map_file.write%d: mapping %s failed? mp %p\0A\00"
define i8* @map_file(i8* %0, i64* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8*
store i8* %0, i8** %5
store i64* %1, i64** %6
store i64 %2, i64* %7
%13 = load i64, i64* @MAP_FILE
%14 = load i64, i64* @MAP_SHARED
%15 = or i64 %13, %14
store i64 %15, i64* %10
store i8* null, i8** %12
%16 = load i64*, i64** %6
store i64 0, i64* %16
%17 = load i64, i64* %7
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %3
%20 = load i8*, i8** %5
%21 = load i32, i32* @O_RDWR
%22 = call i64 @open(i8* %20, i32 %21)
store i64 %22, i64* %8
br label %27
23: ; preds = %3
%24 = load i8*, i8** %5
%25 = load i32, i32* @O_RDONLY
%26 = call i64 @open(i8* %24, i32 %25)
store i64 %26, i64* %8
br label %27
27: ; preds = %23, %19
%28 = load i64, i64* %8
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i64, i64* %7
%32 = load i8*, i8** %5
%33 = call i32 (i8*, i64, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i64 %31, i8* %32)
store i8* null, i8** %4
br label %76
34: ; preds = %27
%35 = load i64*, i64** %6
%36 = load i64, i64* %35
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i64, i64* %8
%40 = load i32, i32* @SEEK_END
%41 = call i64 @lseek(i64 %39, i32 0, i32 %40)
store i64 %41, i64* %11
br label %45
42: ; preds = %34
%43 = load i64*, i64** %6
%44 = load i64, i64* %43
store i64 %44, i64* %11
br label %45
45: ; preds = %42, %38
%46 = load i64, i64* @PROT_READ
store i64 %46, i64* %9
%47 = load i64, i64* %7
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %45
%50 = load i64, i64* @PROT_WRITE
%51 = load i64, i64* %9
%52 = or i64 %51, %50
store i64 %52, i64* %9
br label %53
53: ; preds = %49, %45
%54 = load i64, i64* %11
%55 = load i64, i64* %9
%56 = load i64, i64* %10
%57 = load i64, i64* %8
%58 = call i8* @mmap(i32 0, i64 %54, i64 %55, i64 %56, i64 %57, i32 0)
store i8* %58, i8** %12
%59 = load i64, i64* %8
%60 = call i32 @close(i64 %59)
%61 = load i8*, i8** %12
%62 = icmp eq i8* %61, null
br i1 %62, label %67, label %63
63: ; preds = %53
%64 = load i8*, i8** %12
%65 = load i8*, i8** @MAP_FAILED
%66 = icmp eq i8* %64, %65
br i1 %66, label %67, label %72
67: ; preds = %63, %53
%68 = load i64, i64* %7
%69 = load i8*, i8** %5
%70 = load i8*, i8** %12
%71 = call i32 (i8*, i64, i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i64 %68, i8* %69, i8* %70)
store i8* null, i8** %4
br label %76
72: ; preds = %63
%73 = load i64, i64* %11
%74 = load i64*, i64** %6
store i64 %73, i64* %74
%75 = load i8*, i8** %12
store i8* %75, i8** %4
br label %76
76: ; preds = %72, %67, %30
%77 = load i8*, i8** %4
ret i8* %77
}

LLVM IR (-O2):
@MAP_FILE = global i64 0
@MAP_SHARED = global i64 0
@O_RDWR = global i32 0
@O_RDONLY = global i32 0
@.str = [43 x i8] c"map_file: error opening enablewrite.%d %s\0A\00"
@SEEK_END = global i32 0
@PROT_READ = global i64 0
@PROT_WRITE = global i64 0
@MAP_FAILED = global i8* null
@.str.1 = [44 x i8] c"map_file.write%d: mapping %s failed? mp %p\0A\00"
define i8* @map_file(i8* %0, i64* nocapture %1, i64 %2) {
%4 = load i64, i64* @MAP_FILE
%5 = load i64, i64* @MAP_SHARED
%6 = or i64 %5, %4
store i64 0, i64* %1
%7 = icmp eq i64 %2, 0
%8 = load i32, i32* @O_RDONLY
%9 = load i32, i32* @O_RDWR
%10 = select i1 %7, i32 %8, i32 %9
%11 = tail call i64 @open(i8* %0, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = tail call i32 (i8*, i64, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i64 %2, i8* %0)
br label %36
15: ; preds = %3
%16 = load i64, i64* %1
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @SEEK_END
%20 = tail call i64 @lseek(i64 %11, i32 0, i32 %19)
br label %21
21: ; preds = %15, %18
%22 = phi i64 [ %20, %18 ], [ %16, %15 ]
%23 = load i64, i64* @PROT_READ
%24 = load i64, i64* @PROT_WRITE
%25 = select i1 %7, i64 0, i64 %24
%26 = or i64 %25, %23
%27 = tail call i8* @mmap(i32 0, i64 %22, i64 %26, i64 %6, i64 %11, i32 0)
%28 = tail call i32 @close(i64 %11)
%29 = icmp eq i8* %27, null
%30 = load i8*, i8** @MAP_FAILED
%31 = icmp eq i8* %27, %30
%32 = select i1 %29, i1 true, i1 %31
br i1 %32, label %33, label %35
33: ; preds = %21
%34 = tail call i32 (i8*, i64, i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i64 %2, i8* %0, i8* %27)
br label %36
35: ; preds = %21
store i64 %22, i64* %1
br label %36
36: ; preds = %35, %33, %13
%37 = phi i8* [ null, %13 ], [ null, %33 ], [ %27, %35 ]
ret i8* %37
}