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

/* Variables and functions */
void *MAP_FAILED;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ *check_fopen(char *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int, int, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
void *check_mmap_file(char *filename, char mode, int64_t *length) {
  FILE *tf;
  int flags = MAP_SHARED, prot = PROT_READ;
  struct stat ts;
  if (mode == 'r')
    tf = check_fopen(filename, "rb");
  else if (mode == 'w') {
    tf = check_fopen(filename, "r+b");
    prot |= PROT_WRITE;
  } else {
    fprintf(stderr, "[Error] Invalid mode %c passed to check_mmap_file!\n",
            mode);
    exit(1);
  }
  int fd = fileno(tf);
  if (fstat(fd, &ts) != 0) {
    fprintf(stderr, "[Error] Fstat failure on file %s!\n", filename);
    perror("[Error] Reason");
    exit(1);
  }
  void *res = mmap(NULL, ts.st_size, prot, flags, fd, 0);
  if (res == MAP_FAILED) {
    fprintf(stderr, "[Error] Mmap failure on file %s, mode %c!\n", filename,
            mode);
    perror("[Error] Reason");
    exit(1);
  }
  fclose(tf);
  if (length)
    *length = ts.st_size;
  return res;
}


x86 assembly (clang, -O0):
<check_mmap_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movsbl -0x9(%rbp),%eax
cmp    $0x72,%eax
jne    11d4 <check_mmap_file+0x54>
mov    -0x8(%rbp),%rdi
lea    0xe3a(%rip),%rsi
call   1090 <check_fopen@plt>
mov    %rax,-0x20(%rbp)
jmp    1233 <check_mmap_file+0xb3>
movsbl -0x9(%rbp),%eax
cmp    $0x77,%eax
jne    1209 <check_mmap_file+0x89>
mov    -0x8(%rbp),%rdi
lea    0xe17(%rip),%rsi
call   1090 <check_fopen@plt>
mov    %rax,-0x20(%rbp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    122e <check_mmap_file+0xae>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
movsbl -0x9(%rbp),%edx
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1233 <check_mmap_file+0xb3>
mov    -0x20(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0x0,%rax
je     1286 <check_mmap_file+0x106>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdd2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    0xde7(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x30(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    -0x34(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    0x2d12(%rip),%rcx
cmp    (%rcx),%rax
jne    12ec <check_mmap_file+0x16c>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
movsbl -0x9(%rbp),%ecx
lea    0xd9e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    0xd81(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x18(%rbp)
je     1309 <check_mmap_file+0x189>
mov    -0x30(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_mmap_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    0x2e51(%rip),%rax
mov    (%rax),%r13d
mov    0x2e57(%rip),%rax
mov    (%rax),%r12d
cmp    $0x77,%bpl
mov    %rdi,0x8(%rsp)
mov    %rdx,0x10(%rsp)
je     11cf <check_mmap_file+0x4f>
cmp    $0x72,%bpl
jne    12aa <check_mmap_file+0x12a>
lea    0xe3b(%rip),%rsi
call   1090 <check_fopen@plt>
mov    %rax,%rbx
jmp    11e8 <check_mmap_file+0x68>
lea    0xe2d(%rip),%rsi
call   1090 <check_fopen@plt>
mov    %rax,%rbx
mov    0x2de3(%rip),%rax
or     (%rax),%r12d
mov    %ebp,0x4(%rsp)
mov    %rbx,%rdi
call   1060 <fileno@plt>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
test   %rax,%rax
jne    1256 <check_mmap_file+0xd6>
mov    (%rsp),%r14d
xor    %edi,%edi
mov    %r14d,%esi
mov    %r12d,%edx
mov    %r13d,%ecx
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
mov    0x2d96(%rip),%rax
cmp    (%rax),%rbp
je     1274 <check_mmap_file+0xf4>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x10(%rsp),%rax
test   %rax,%rax
je     1244 <check_mmap_file+0xc4>
mov    %r14d,(%rax)
mov    %rbp,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    0xdd5(%rip),%rsi
mov    0x8(%rsp),%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1294 <check_mmap_file+0x114>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
mov    0x8(%rsp),%rdx
mov    0x4(%rsp),%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xdc3(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
lea    0xd4d(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@MAP_SHARED = global i32 0
@PROT_READ = global i32 0
@.str = [3 x i8] c"rb\00"
@.str.1 = [4 x i8] c"r+b\00"
@PROT_WRITE = global i32 0
@stderr = global i32 0
@.str.2 = [52 x i8] c"[Error] Invalid mode %c passed to check_mmap_file!\0A\00"
@.str.3 = [35 x i8] c"[Error] Fstat failure on file %s!\0A\00"
@.str.4 = [15 x i8] c"[Error] Reason\00"
@MAP_FAILED = global i8* null
@.str.5 = [43 x i8] c"[Error] Mmap failure on file %s, mode %c!\0A\00"
define i8* @check_mmap_file(i8* %0, i8 signext %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %4
store i8 %1, i8* %5
store i32* %2, i32** %6
%13 = load i32, i32* @MAP_SHARED
store i32 %13, i32* %8
%14 = load i32, i32* @PROT_READ
store i32 %14, i32* %9
%15 = load i8, i8* %5
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 114
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = load i8*, i8** %4
%20 = call i32* @check_fopen(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %7
br label %38
21: ; preds = %3
%22 = load i8, i8* %5
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 119
br i1 %24, label %25, label %31
25: ; preds = %21
%26 = load i8*, i8** %4
%27 = call i32* @check_fopen(i8* %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %7
%28 = load i32, i32* @PROT_WRITE
%29 = load i32, i32* %9
%30 = or i32 %29, %28
store i32 %30, i32* %9
br label %37
31: ; preds = %21
%32 = load i32, i32* @stderr
%33 = load i8, i8* %5
%34 = sext i8 %33 to i32
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %25
br label %38
38: ; preds = %37, %18
%39 = load i32*, i32** %7
%40 = call i32 @fileno(i32* %39)
store i32 %40, i32* %11
%41 = load i32, i32* %11
%42 = call i64 @fstat(i32 %41, %struct.stat* %10)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %38
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %4
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %38
%51 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%52 = load i32, i32* %51
%53 = load i32, i32* %9
%54 = load i32, i32* %8
%55 = load i32, i32* %11
%56 = call i8* @mmap(i32* null, i32 %52, i32 %53, i32 %54, i32 %55, i32 0)
store i8* %56, i8** %12
%57 = load i8*, i8** %12
%58 = load i8*, i8** @MAP_FAILED
%59 = icmp eq i8* %57, %58
br i1 %59, label %60, label %68
60: ; preds = %50
%61 = load i32, i32* @stderr
%62 = load i8*, i8** %4
%63 = load i8, i8* %5
%64 = sext i8 %63 to i32
%65 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i8* %62, i32 %64)
%66 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %50
%69 = load i32*, i32** %7
%70 = call i32 @fclose(i32* %69)
%71 = load i32*, i32** %6
%72 = icmp ne i32* %71, null
br i1 %72, label %73, label %77
73: ; preds = %68
%74 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%75 = load i32, i32* %74
%76 = load i32*, i32** %6
store i32 %75, i32* %76
br label %77
77: ; preds = %73, %68
%78 = load i8*, i8** %12
ret i8* %78
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@MAP_SHARED = global i32 0
@PROT_READ = global i32 0
@.str = [3 x i8] c"rb\00"
@.str.1 = [4 x i8] c"r+b\00"
@PROT_WRITE = global i32 0
@stderr = global i32 0
@.str.2 = [52 x i8] c"[Error] Invalid mode %c passed to check_mmap_file!\0A\00"
@.str.3 = [35 x i8] c"[Error] Fstat failure on file %s!\0A\00"
@.str.4 = [15 x i8] c"[Error] Reason\00"
@MAP_FAILED = global i8* null
@.str.5 = [43 x i8] c"[Error] Mmap failure on file %s, mode %c!\0A\00"
define i8* @check_mmap_file(i8* %0, i8 signext %1, i32* writeonly %2) {
%4 = alloca %struct.stat
%5 = load i32, i32* @MAP_SHARED
%6 = load i32, i32* @PROT_READ
%7 = bitcast %struct.stat* %4 to i8*
%8 = sext i8 %1 to i32
switch i8 %1, label %15 [
i8 114, label %9
i8 119, label %11
]
9: ; preds = %3
%10 = tail call i32* @check_fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %19
11: ; preds = %3
%12 = tail call i32* @check_fopen(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %13, %6
br label %19
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %11, %9
%20 = phi i32 [ %6, %9 ], [ %14, %11 ]
%21 = phi i32* [ %10, %9 ], [ %12, %11 ]
%22 = tail call i32 @fileno(i32* %21)
%23 = call i64 @fstat(i32 %22, %struct.stat* %4)
%24 = icmp eq i64 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %19
%26 = load i32, i32* @stderr
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%28 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %19
%31 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%32 = load i32, i32* %31
%33 = tail call i8* @mmap(i32* null, i32 %32, i32 %20, i32 %5, i32 %22, i32 0)
%34 = load i8*, i8** @MAP_FAILED
%35 = icmp eq i8* %33, %34
br i1 %35, label %36, label %41
36: ; preds = %30
%37 = load i32, i32* @stderr
%38 = tail call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i8* %0, i32 %8)
%39 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%40 = tail call i32 @exit(i32 1)
unreachable
41: ; preds = %30
%42 = tail call i32 @fclose(i32* %21)
%43 = icmp eq i32* %2, null
br i1 %43, label %45, label %44
44: ; preds = %41
store i32 %32, i32* %2
br label %45
45: ; preds = %44, %41
ret i8* %33
}