#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 size_t ps;

/* Variables and functions */
int MAP_PRIVATE;
int O_WRONLY;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGE_SIZE;
int /*<<< orphan*/ close(int);
scalar_t__ fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/ *, size_t, int, int, int, int /*<<< orphan*/);
int open(char *, int, int);
size_t sysconf(int /*<<< orphan*/);
char *mapfile_flag(char *file, int oflags, size_t *size, int flag) {
  int fd = open(file, oflags, 0644);
  if (fd < 0)
    return NULL;
  struct stat st;
  if (fstat(fd, &st) >= 0 && (*size = st.st_size) > 0) {
    size_t ps = sysconf(_SC_PAGE_SIZE);
    *size = (((st.st_size) + (ps)-1) & ~((ps)-1));
    int prot = PROT_READ;
    if ((oflags & O_WRONLY) || (flag & MAP_PRIVATE))
      prot |= PROT_WRITE;
    char *map = mmap(NULL, *size, prot, flag, fd, 0);
    close(fd);
    if (map == (char *)-1)
      return NULL;
    *size = st.st_size;
    return map;
  }
  close(fd);
  return NULL;
}


x86 assembly (clang, -O0):
<mapfile_flag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    $0x1a4,%edx
call   1050 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1191 <mapfile_flag+0x41>
movq   $0x0,-0x8(%rbp)
jmp    12a4 <mapfile_flag+0x154>
mov    -0x28(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
jl     1294 <mapfile_flag+0x144>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jbe    1294 <mapfile_flag+0x144>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rcx
add    -0x38(%rbp),%rcx
sub    $0x1,%rcx
mov    -0x38(%rbp),%rax
sub    $0x1,%rax
xor    $0xffffffffffffffff,%rax
and    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2db2(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1226 <mapfile_flag+0xd6>
mov    -0x24(%rbp),%eax
mov    0x2dad(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1235 <mapfile_flag+0xe5>
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
or     -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x3c(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    -0x28(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x48(%rbp)
jne    127c <mapfile_flag+0x12c>
movq   $0x0,-0x8(%rbp)
jmp    12a4 <mapfile_flag+0x154>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12a4 <mapfile_flag+0x154>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mapfile_flag>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %esi,%ebx
mov    $0x1a4,%edx
call   1050 <open@plt>
test   %eax,%eax
js     1216 <mapfile_flag+0xc6>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
test   %rax,%rax
js     120f <mapfile_flag+0xbf>
mov    0x8(%rsp),%r12
mov    %r12,(%r15)
test   %r12,%r12
je     120f <mapfile_flag+0xbf>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
lea    (%r12,%rax,1),%rcx
add    $0xffffffffffffffff,%rcx
neg    %rax
and    %rcx,%rax
mov    %rax,(%r15)
mov    0x2e38(%rip),%rcx
mov    (%rcx),%edx
mov    0x2def(%rip),%rcx
test   %ebx,(%rcx)
jne    11d9 <mapfile_flag+0x89>
mov    0x2df4(%rip),%rcx
test   %r14d,(%rcx)
je     11e2 <mapfile_flag+0x92>
mov    0x2df8(%rip),%rcx
or     (%rcx),%edx
xor    %ebx,%ebx
xor    %edi,%edi
mov    %rax,%rsi
mov    %r14d,%ecx
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffffffffffff,%r14
je     1218 <mapfile_flag+0xc8>
mov    %r12,(%r15)
mov    %r14,%rbx
jmp    1218 <mapfile_flag+0xc8>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@_SC_PAGE_SIZE = global i32 0
@PROT_READ = global i32 0
@O_WRONLY = global i32 0
@MAP_PRIVATE = global i32 0
@PROT_WRITE = global i32 0
define i8* @mapfile_flag(i8* %0, i32 %1, i64* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.stat
%12 = alloca i64
%13 = alloca i32
%14 = alloca i8*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i64* %2, i64** %8
store i32 %3, i32* %9
%15 = load i8*, i8** %6
%16 = load i32, i32* %7
%17 = call i32 @open(i8* %15, i32 %16, i32 420)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %4
store i8* null, i8** %5
br label %77
21: ; preds = %4
%22 = load i32, i32* %10
%23 = call i64 @fstat(i32 %22, %struct.stat* %11)
%24 = icmp sge i64 %23, 0
br i1 %24, label %25, label %74
25: ; preds = %21
%26 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%27 = load i64, i64* %26
%28 = load i64*, i64** %8
store i64 %27, i64* %28
%29 = icmp ugt i64 %27, 0
br i1 %29, label %30, label %74
30: ; preds = %25
%31 = load i32, i32* @_SC_PAGE_SIZE
%32 = call i64 @sysconf(i32 %31)
store i64 %32, i64* %12
%33 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%34 = load i64, i64* %33
%35 = load i64, i64* %12
%36 = add i64 %34, %35
%37 = sub i64 %36, 1
%38 = load i64, i64* %12
%39 = sub i64 %38, 1
%40 = xor i64 %39, -1
%41 = and i64 %37, %40
%42 = load i64*, i64** %8
store i64 %41, i64* %42
%43 = load i32, i32* @PROT_READ
store i32 %43, i32* %13
%44 = load i32, i32* %7
%45 = load i32, i32* @O_WRONLY
%46 = and i32 %44, %45
%47 = icmp ne i32 %46, 0
br i1 %47, label %53, label %48
48: ; preds = %30
%49 = load i32, i32* %9
%50 = load i32, i32* @MAP_PRIVATE
%51 = and i32 %49, %50
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %48, %30
%54 = load i32, i32* @PROT_WRITE
%55 = load i32, i32* %13
%56 = or i32 %55, %54
store i32 %56, i32* %13
br label %57
57: ; preds = %53, %48
%58 = load i64*, i64** %8
%59 = load i64, i64* %58
%60 = load i32, i32* %13
%61 = load i32, i32* %9
%62 = load i32, i32* %10
%63 = call i8* @mmap(i32* null, i64 %59, i32 %60, i32 %61, i32 %62, i32 0)
store i8* %63, i8** %14
%64 = load i32, i32* %10
%65 = call i32 @close(i32 %64)
%66 = load i8*, i8** %14
%67 = icmp eq i8* %66, inttoptr (i64 -1 to i8*)
br i1 %67, label %68, label %69
68: ; preds = %57
store i8* null, i8** %5
br label %77
69: ; preds = %57
%70 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%71 = load i64, i64* %70
%72 = load i64*, i64** %8
store i64 %71, i64* %72
%73 = load i8*, i8** %14
store i8* %73, i8** %5
br label %77
74: ; preds = %25, %21
%75 = load i32, i32* %10
%76 = call i32 @close(i32 %75)
store i8* null, i8** %5
br label %77
77: ; preds = %74, %69, %68, %20
%78 = load i8*, i8** %5
ret i8* %78
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@_SC_PAGE_SIZE = global i32 0
@PROT_READ = global i32 0
@O_WRONLY = global i32 0
@MAP_PRIVATE = global i32 0
@PROT_WRITE = global i32 0
define i8* @mapfile_flag(i8* nocapture %0, i32 %1, i64* nocapture %2, i32 %3) {
%5 = alloca %struct.stat
%6 = tail call i32 @open(i8* %0, i32 %1, i32 420)
%7 = icmp slt i32 %6, 0
br i1 %7, label %44, label %8
8: ; preds = %4
%9 = bitcast %struct.stat* %5 to i8*
%10 = call i64 @fstat(i32 %6, %struct.stat* %5)
%11 = icmp sgt i64 %10, -1
br i1 %11, label %12, label %40
12: ; preds = %8
%13 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%14 = load i64, i64* %13
store i64 %14, i64* %2
%15 = icmp eq i64 %14, 0
br i1 %15, label %40, label %16
16: ; preds = %12
%17 = load i32, i32* @_SC_PAGE_SIZE
%18 = tail call i64 @sysconf(i32 %17)
%19 = add i64 %14, -1
%20 = add i64 %19, %18
%21 = sub i64 0, %18
%22 = and i64 %20, %21
store i64 %22, i64* %2
%23 = load i32, i32* @PROT_READ
%24 = load i32, i32* @O_WRONLY
%25 = and i32 %24, %1
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %16
%28 = load i32, i32* @MAP_PRIVATE
%29 = and i32 %28, %3
%30 = icmp eq i32 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %27, %16
%32 = load i32, i32* @PROT_WRITE
%33 = or i32 %32, %23
br label %34
34: ; preds = %31, %27
%35 = phi i32 [ %33, %31 ], [ %23, %27 ]
%36 = tail call i8* @mmap(i32* null, i64 %22, i32 %35, i32 %3, i32 %6, i32 0)
%37 = tail call i32 @close(i32 %6)
%38 = icmp eq i8* %36, inttoptr (i64 -1 to i8*)
br i1 %38, label %42, label %39
39: ; preds = %34
store i64 %14, i64* %2
br label %42
40: ; preds = %12, %8
%41 = tail call i32 @close(i32 %6)
br label %42
42: ; preds = %39, %34, %40
%43 = phi i8* [ null, %40 ], [ %36, %39 ], [ null, %34 ]
br label %44
44: ; preds = %4, %42
%45 = phi i8* [ %43, %42 ], [ null, %4 ]
ret i8* %45
}