#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;
};

/* Variables and functions */
unsigned char const *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CLOEXEC;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ PROT_READ;
unsigned char *__mmap(int /*<<< orphan*/, size_t, int /*<<< orphan*/,
                      int /*<<< orphan*/, int, int /*<<< orphan*/);
int __sys_open(char const *, int);
int /*<<< orphan*/ __syscall(int, int, ...);
const char unsigned *__map_file(const char *pathname, size_t *size) {
  struct stat st;
  const unsigned char *map = MAP_FAILED;
  int fd = __sys_open(pathname, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
  if (fd < 0)
    return 0;
  if (!__syscall(108, fd, &st))
    map = __mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
  __syscall(6, fd);
  *size = st.st_size;
  return map == MAP_FAILED ? 0 : map;
}


x86 assembly (clang, -O0):
<__map_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e79(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    0x2e4e(%rip),%rax
or     (%rax),%esi
mov    0x2e6d(%rip),%rax
or     (%rax),%esi
call   1050 <__sys_open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    118c <__map_file+0x5c>
movq   $0x0,-0x8(%rbp)
jmp    121a <__map_file+0xea>
mov    -0x2c(%rbp),%esi
mov    $0x6c,%edi
lea    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1040 <__syscall@plt>
cmp    $0x0,%eax
jne    11d1 <__map_file+0xa1>
mov    -0x20(%rbp),%rsi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
mov    0x2e2c(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <__mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%esi
mov    $0x6,%edi
mov    $0x0,%al
call   1040 <__syscall@plt>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    0x2dca(%rip),%rcx
cmp    (%rcx),%rax
jne    120a <__map_file+0xda>
xor    %eax,%eax
mov    %rax,-0x38(%rbp)
jmp    1212 <__map_file+0xe2>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__map_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    0x2e7a(%rip),%r12
mov    0x2e83(%rip),%rax
mov    0x2e5c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    (%r12),%r15
mov    0x2e75(%rip),%rax
or     (%rax),%esi
call   1050 <__sys_open@plt>
test   %eax,%eax
js     11d0 <__map_file+0xa0>
mov    %eax,%ebp
lea    0x8(%rsp),%rdx
mov    $0x6c,%edi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <__syscall@plt>
test   %eax,%eax
jne    11ae <__map_file+0x7e>
mov    0x8(%rsp),%rsi
mov    0x2e65(%rip),%rax
mov    (%rax),%edx
mov    0x2e4c(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <__mmap@plt>
mov    %rax,%r15
xor    %ebx,%ebx
mov    $0x6,%edi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <__syscall@plt>
mov    0x8(%rsp),%rax
mov    %rax,(%r14)
cmp    (%r12),%r15
cmovne %r15,%rbx
jmp    11d2 <__map_file+0xa2>
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 }
@MAP_FAILED = global i8* null
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@O_NONBLOCK = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
define i8* @__map_file(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i64* %1, i64** %5
%9 = load i8*, i8** @MAP_FAILED
store i8* %9, i8** %7
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDONLY
%12 = load i32, i32* @O_CLOEXEC
%13 = or i32 %11, %12
%14 = load i32, i32* @O_NONBLOCK
%15 = or i32 %13, %14
%16 = call i32 @__sys_open(i8* %10, i32 %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %2
store i8* null, i8** %3
br label %45
20: ; preds = %2
%21 = load i32, i32* %8
%22 = call i32 (i32, i32, ...) @__syscall(i32 108, i32 %21, %struct.stat* %6)
%23 = icmp ne i32 %22, 0
br i1 %23, label %31, label %24
24: ; preds = %20
%25 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%26 = load i64, i64* %25
%27 = load i32, i32* @PROT_READ
%28 = load i32, i32* @MAP_SHARED
%29 = load i32, i32* %8
%30 = call i8* @__mmap(i32 0, i64 %26, i32 %27, i32 %28, i32 %29, i32 0)
store i8* %30, i8** %7
br label %31
31: ; preds = %24, %20
%32 = load i32, i32* %8
%33 = call i32 (i32, i32, ...) @__syscall(i32 6, i32 %32)
%34 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%35 = load i64, i64* %34
%36 = load i64*, i64** %5
store i64 %35, i64* %36
%37 = load i8*, i8** %7
%38 = load i8*, i8** @MAP_FAILED
%39 = icmp eq i8* %37, %38
br i1 %39, label %40, label %41
40: ; preds = %31
br label %43
41: ; preds = %31
%42 = load i8*, i8** %7
br label %43
43: ; preds = %41, %40
%44 = phi i8* [ null, %40 ], [ %42, %41 ]
store i8* %44, i8** %3
br label %45
45: ; preds = %43, %19
%46 = load i8*, i8** %3
ret i8* %46
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@MAP_FAILED = global i8* null
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@O_NONBLOCK = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
define i8* @__map_file(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i8*, i8** @MAP_FAILED
%6 = load i32, i32* @O_RDONLY
%7 = load i32, i32* @O_CLOEXEC
%8 = or i32 %7, %6
%9 = load i32, i32* @O_NONBLOCK
%10 = or i32 %8, %9
%11 = tail call i32 @__sys_open(i8* %0, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %30, label %13
13: ; preds = %2
%14 = call i32 (i32, i32, ...) @__syscall(i32 108, i32 %11, %struct.stat* %3)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%18 = load i64, i64* %17
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @MAP_SHARED
%21 = call i8* @__mmap(i32 0, i64 %18, i32 %19, i32 %20, i32 %11, i32 0)
br label %22
22: ; preds = %16, %13
%23 = phi i8* [ %5, %13 ], [ %21, %16 ]
%24 = call i32 (i32, i32, ...) @__syscall(i32 6, i32 %11)
%25 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%26 = load i64, i64* %25
store i64 %26, i64* %1
%27 = load i8*, i8** @MAP_FAILED
%28 = icmp eq i8* %23, %27
%29 = select i1 %28, i8* null, i8* %23
br label %30
30: ; preds = %2, %22
%31 = phi i8* [ %29, %22 ], [ null, %2 ]
ret i8* %31
}