#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 {
  size_t st_size;
};
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int MAP_FILE;
int MAP_SHARED;
int O_RDONLY;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fstat(int, struct stat *);
void *mmap(long, size_t, int, int, int, int /*<<< orphan*/);
int open(char const *, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
void *mapFile(const char *filename, size_t *length, char mode) {
  void *ptr = 0L;
  struct stat sb;
  int f;
  int openMode = O_RDONLY | 0;
  int mapMode = O_RDWR | 0;
  switch (mode) {
  case 'r':
    openMode = O_RDONLY | 0;
    mapMode = PROT_READ;
    break;
  case 'w':
    openMode = O_RDWR | 0;
    mapMode = PROT_READ | PROT_WRITE;
    break;
  default:
    fprintf(stderr, "Invalid mode to mapFile; must be 'r' or 'w'\n");
    exit(1);
    break;
  }
  errno = 0;
  f = open(filename, openMode);
  if (errno) {
    fprintf(stderr, "Couldn't open() '%s'\n%s\n", filename, strerror(errno));
    exit(1);
  }
  fstat(f, &sb);
  if (errno) {
    fprintf(stderr, "Couldn't fstat() '%s'\n%s\n", filename, strerror(errno));
    exit(1);
  }
  *length = sb.st_size;
  ptr = mmap(0L, *length, mapMode, (MAP_FILE | MAP_SHARED), f, (off_t)0);
  if (errno) {
    fprintf(stderr, "Couldn't mmap() '%s'\n%s\n", filename, strerror(errno));
    exit(1);
  }
  close(f);
  return (ptr);
}


x86 assembly (clang, -O0):
<mapFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
movsbl -0x11(%rbp),%eax
mov    %eax,-0x38(%rbp)
sub    $0x72,%eax
je     11cb <mapFile+0x5b>
jmp    11ba <mapFile+0x4a>
mov    -0x38(%rbp),%eax
sub    $0x77,%eax
je     11eb <mapFile+0x7b>
jmp    1214 <mapFile+0xa4>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
or     $0x0,%eax
mov    %eax,-0x30(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    1235 <mapFile+0xc5>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
or     $0x0,%eax
mov    %eax,-0x30(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    0x2d9e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x34(%rbp)
jmp    1235 <mapFile+0xc5>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xddc(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2d74(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%esi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2d57(%rip),%rax
cmpq   $0x0,(%rax)
je     12a8 <mapFile+0x138>
mov    0x2d4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d32(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xd96(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1090 <fstat@plt>
mov    0x2cf5(%rip),%rax
cmpq   $0x0,(%rax)
je     130a <mapFile+0x19a>
mov    0x2cec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2cd0(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xd4d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x34(%rbp),%edx
mov    0x2ca2(%rip),%rax
mov    (%rax),%ecx
mov    0x2cb9(%rip),%rax
or     (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    0x2c64(%rip),%rax
cmpq   $0x0,(%rax)
je     139b <mapFile+0x22b>
mov    0x2c5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2c3f(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    %rax,%rcx
lea    0xcd6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mapFile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
cmp    $0x77,%edx
je     11a2 <mapFile+0x32>
cmp    $0x72,%edx
jne    129c <mapFile+0x12c>
mov    0x2e61(%rip),%rax
mov    (%rax),%ebp
mov    0x2e30(%rip),%rax
jmp    11bb <mapFile+0x4b>
mov    0x2e4f(%rip),%rax
mov    0x2df8(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    0x2dec(%rip),%r12
movq   $0x0,(%r12)
mov    %r14,%rdi
call   1060 <open@plt>
mov    (%r12),%rdi
test   %rdi,%rdi
jne    1240 <mapFile+0xd0>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
mov    (%r12),%rdi
test   %rdi,%rdi
jne    1257 <mapFile+0xe7>
mov    0x8(%rsp),%rsi
mov    %rsi,(%r15)
mov    0x2dc5(%rip),%rax
mov    0x2dde(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%edx
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    (%r12),%rdi
test   %rdi,%rdi
jne    126e <mapFile+0xfe>
mov    %rax,%rbp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %rbp,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d71(%rip),%rax
mov    (%rax),%ebx
call   1080 <strerror@plt>
lea    0xdd8(%rip),%rsi
jmp    1283 <mapFile+0x113>
mov    0x2d5a(%rip),%rax
mov    (%rax),%ebx
call   1080 <strerror@plt>
lea    0xdda(%rip),%rsi
jmp    1283 <mapFile+0x113>
mov    0x2d43(%rip),%rax
mov    (%rax),%ebx
call   1080 <strerror@plt>
lea    0xddd(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
lea    0xd54(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@stderr = global i32 0
@.str = [45 x i8] c"Invalid mode to mapFile; must be 'r' or 'w'\0A\00"
@errno = global i64 0
@.str.1 = [25 x i8] c"Couldn't open() '%s'\0A%s\0A\00"
@.str.2 = [26 x i8] c"Couldn't fstat() '%s'\0A%s\0A\00"
@MAP_FILE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [25 x i8] c"Couldn't mmap() '%s'\0A%s\0A\00"
define i8* @mapFile(i8* %0, i64* %1, i8 signext %2) {
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i8
%7 = alloca i8*
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i64* %1, i64** %5
store i8 %2, i8* %6
store i8* null, i8** %7
%12 = load i32, i32* @O_RDONLY
%13 = or i32 %12, 0
store i32 %13, i32* %10
%14 = load i32, i32* @O_RDWR
%15 = or i32 %14, 0
store i32 %15, i32* %11
%16 = load i8, i8* %6
%17 = sext i8 %16 to i32
switch i32 %17, label %28 [
i32 114, label %18
i32 119, label %22
]
18: ; preds = %3
%19 = load i32, i32* @O_RDONLY
%20 = or i32 %19, 0
store i32 %20, i32* %10
%21 = load i32, i32* @PROT_READ
store i32 %21, i32* %11
br label %32
22: ; preds = %3
%23 = load i32, i32* @O_RDWR
%24 = or i32 %23, 0
store i32 %24, i32* %10
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @PROT_WRITE
%27 = or i32 %25, %26
store i32 %27, i32* %11
br label %32
28: ; preds = %3
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %22, %18
store i64 0, i64* @errno
%33 = load i8*, i8** %4
%34 = load i32, i32* %10
%35 = call i32 @open(i8* %33, i32 %34)
store i32 %35, i32* %9
%36 = load i64, i64* @errno
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %45
38: ; preds = %32
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %4
%41 = load i64, i64* @errno
%42 = call i8* @strerror(i64 %41)
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %40, i8* %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %32
%46 = load i32, i32* %9
%47 = call i32 @fstat(i32 %46, %struct.stat* %8)
%48 = load i64, i64* @errno
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %57
50: ; preds = %45
%51 = load i32, i32* @stderr
%52 = load i8*, i8** %4
%53 = load i64, i64* @errno
%54 = call i8* @strerror(i64 %53)
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %52, i8* %54)
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %45
%58 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%59 = load i64, i64* %58
%60 = load i64*, i64** %5
store i64 %59, i64* %60
%61 = load i64*, i64** %5
%62 = load i64, i64* %61
%63 = load i32, i32* %11
%64 = load i32, i32* @MAP_FILE
%65 = load i32, i32* @MAP_SHARED
%66 = or i32 %64, %65
%67 = load i32, i32* %9
%68 = call i8* @mmap(i64 0, i64 %62, i32 %63, i32 %66, i32 %67, i32 0)
store i8* %68, i8** %7
%69 = load i64, i64* @errno
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %78
71: ; preds = %57
%72 = load i32, i32* @stderr
%73 = load i8*, i8** %4
%74 = load i64, i64* @errno
%75 = call i8* @strerror(i64 %74)
%76 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %73, i8* %75)
%77 = call i32 @exit(i32 1)
unreachable
78: ; preds = %57
%79 = load i32, i32* %9
%80 = call i32 @close(i32 %79)
%81 = load i8*, i8** %7
ret i8* %81
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@stderr = global i32 0
@.str = [45 x i8] c"Invalid mode to mapFile; must be 'r' or 'w'\0A\00"
@errno = global i64 0
@.str.1 = [25 x i8] c"Couldn't open() '%s'\0A%s\0A\00"
@.str.2 = [26 x i8] c"Couldn't fstat() '%s'\0A%s\0A\00"
@MAP_FILE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [25 x i8] c"Couldn't mmap() '%s'\0A%s\0A\00"
define i8* @mapFile(i8* %0, i64* nocapture writeonly %1, i8 signext %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = sext i8 %2 to i32
switch i32 %6, label %13 [
i32 114, label %7
i32 119, label %9
]
7: ; preds = %3
%8 = load i32, i32* @PROT_READ
br label %17
9: ; preds = %3
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %11, %10
br label %17
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %9, %7
%18 = phi i32* [ @O_RDWR, %9 ], [ @O_RDONLY, %7 ]
%19 = phi i32 [ %12, %9 ], [ %8, %7 ]
%20 = load i32, i32* %18
store i64 0, i64* @errno
%21 = tail call i32 @open(i8* %0, i32 %20)
%22 = load i64, i64* @errno
%23 = icmp eq i64 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = tail call i8* @strerror(i64 %22)
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %26)
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %17
%30 = call i32 @fstat(i32 %21, %struct.stat* %4)
%31 = load i64, i64* @errno
%32 = icmp eq i64 %31, 0
br i1 %32, label %38, label %33
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = tail call i8* @strerror(i64 %31)
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %35)
%37 = tail call i32 @exit(i32 1)
unreachable
38: ; preds = %29
%39 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%40 = load i64, i64* %39
store i64 %40, i64* %1
%41 = load i32, i32* @MAP_FILE
%42 = load i32, i32* @MAP_SHARED
%43 = or i32 %42, %41
%44 = tail call i8* @mmap(i64 0, i64 %40, i32 %19, i32 %43, i32 %21, i32 0)
%45 = load i64, i64* @errno
%46 = icmp eq i64 %45, 0
br i1 %46, label %52, label %47
47: ; preds = %38
%48 = load i32, i32* @stderr
%49 = tail call i8* @strerror(i64 %45)
%50 = tail call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %49)
%51 = tail call i32 @exit(i32 1)
unreachable
52: ; preds = %38
%53 = tail call i32 @close(i32 %21)
ret i8* %44
}