#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 {
  scalar_t__ st_size;
};

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, scalar_t__, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
void *mapfile(const char *file, int *size) {
  int fd;
  struct stat st;
  void *p;
  if ((fd = open(file, O_RDONLY)) == -1)
    return NULL;
  if (fstat(fd, &st) == -1 || st.st_size <= 0) {
    close(fd);
    return NULL;
  }
  p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
  close(fd);
  if (p == (void *)-1)
    return NULL;
  *size = (int)st.st_size;
  return p;
}


x86 assembly (clang, -O0):
<mapfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    117b <mapfile+0x3b>
movq   $0x0,-0x8(%rbp)
jmp    1214 <mapfile+0xd4>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
je     119b <mapfile+0x5b>
cmpq   $0x0,-0x28(%rbp)
jg     11b0 <mapfile+0x70>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1214 <mapfile+0xd4>
mov    -0x28(%rbp),%rsi
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
mov    0x2e24(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x30(%rbp)
jne    1200 <mapfile+0xc0>
movq   $0x0,-0x8(%rbp)
jmp    1214 <mapfile+0xd4>
mov    -0x28(%rbp),%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mapfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11c2 <mapfile+0x82>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
je     11bb <mapfile+0x7b>
mov    0x8(%rsp),%r15
test   %r15,%r15
jle    11bb <mapfile+0x7b>
mov    0x2e72(%rip),%rax
mov    (%rax),%edx
mov    0x2e59(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%r12
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffffffffffff,%r12
je     11c4 <mapfile+0x84>
mov    %r15d,(%r14)
mov    %r12,%rbx
jmp    11c4 <mapfile+0x84>
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 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
define i8* @mapfile(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %2
store i8* null, i8** %3
br label %43
14: ; preds = %2
%15 = load i32, i32* %6
%16 = call i32 @fstat(i32 %15, %struct.stat* %7)
%17 = icmp eq i32 %16, -1
br i1 %17, label %22, label %18
18: ; preds = %14
%19 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%20 = load i64, i64* %19
%21 = icmp sle i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18, %14
%23 = load i32, i32* %6
%24 = call i32 @close(i32 %23)
store i8* null, i8** %3
br label %43
25: ; preds = %18
%26 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%27 = load i64, i64* %26
%28 = load i32, i32* @PROT_READ
%29 = load i32, i32* @MAP_SHARED
%30 = load i32, i32* %6
%31 = call i8* @mmap(i32* null, i64 %27, i32 %28, i32 %29, i32 %30, i32 0)
store i8* %31, i8** %8
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
%34 = load i8*, i8** %8
%35 = icmp eq i8* %34, inttoptr (i64 -1 to i8*)
br i1 %35, label %36, label %37
36: ; preds = %25
store i8* null, i8** %3
br label %43
37: ; preds = %25
%38 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%39 = load i64, i64* %38
%40 = trunc i64 %39 to i32
%41 = load i32*, i32** %5
store i32 %40, i32* %41
%42 = load i8*, i8** %8
store i8* %42, i8** %3
br label %43
43: ; preds = %37, %36, %22, %13
%44 = load i8*, i8** %3
ret i8* %44
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
define i8* @mapfile(i8* nocapture %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %25, label %8
8: ; preds = %2
%9 = call i32 @fstat(i32 %6, %struct.stat* %3)
%10 = icmp eq i32 %9, -1
%11 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%12 = load i64, i64* %11
%13 = icmp slt i64 %12, 1
%14 = select i1 %10, i1 true, i1 %13
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = tail call i32 @close(i32 %6)
br label %25
17: ; preds = %8
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @MAP_SHARED
%20 = tail call i8* @mmap(i32* null, i64 %12, i32 %18, i32 %19, i32 %6, i32 0)
%21 = tail call i32 @close(i32 %6)
%22 = icmp eq i8* %20, inttoptr (i64 -1 to i8*)
br i1 %22, label %25, label %23
23: ; preds = %17
%24 = trunc i64 %12 to i32
store i32 %24, i32* %1
br label %25
25: ; preds = %17, %2, %23, %15
%26 = phi i8* [ null, %15 ], [ %20, %23 ], [ null, %2 ], [ null, %17 ]
ret i8* %26
}