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

/* Variables and functions */
unsigned char *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
unsigned char *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/,
                    int /*<<< orphan*/, int /*<<< orphan*/, int,
                    int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int mmap_file(const char *filename, unsigned char **map) {
  int fd;
  struct stat st;
  fd = open(filename, O_RDONLY);
  if (fd < 0)
    return fd;
  if (fstat(fd, &st) == -1)
    return 0;
  *map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (*map == MAP_FAILED)
    return 0;
  close(fd);
  return 1;
}


x86 assembly (clang, -O0):
<mmap_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    117a <mmap_file+0x3a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fc <mmap_file+0xbc>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
jne    119b <mmap_file+0x5b>
movl   $0x0,-0x4(%rbp)
jmp    11fc <mmap_file+0xbc>
mov    -0x20(%rbp),%esi
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
mov    0x2e22(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2df0(%rip),%rcx
cmp    (%rcx),%rax
jne    11ed <mmap_file+0xad>
movl   $0x0,-0x4(%rbp)
jmp    11fc <mmap_file+0xbc>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmap_file>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11b5 <mmap_file+0x75>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
je     11ba <mmap_file+0x7a>
mov    0x8(%rsp),%esi
mov    0x2e7c(%rip),%rax
mov    (%rax),%edx
mov    0x2e4b(%rip),%rax
mov    (%rax),%ecx
xor    %r15d,%r15d
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r14)
mov    0x2e27(%rip),%rcx
cmp    (%rcx),%rax
je     11bd <mmap_file+0x7d>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0x1,%r15d
jmp    11bd <mmap_file+0x7d>
mov    %ebx,%r15d
jmp    11bd <mmap_file+0x7d>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
define i32 @mmap_file(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.stat
store i8* %0, i8** %4
store i8** %1, i8*** %5
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = load i32, i32* %6
store i32 %14, i32* %3
br label %36
15: ; preds = %2
%16 = load i32, i32* %6
%17 = call i32 @fstat(i32 %16, %struct.stat* %7)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 0, i32* %3
br label %36
20: ; preds = %15
%21 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%22 = load i32, i32* %21
%23 = load i32, i32* @PROT_READ
%24 = load i32, i32* @MAP_PRIVATE
%25 = load i32, i32* %6
%26 = call i8* @mmap(i32* null, i32 %22, i32 %23, i32 %24, i32 %25, i32 0)
%27 = load i8**, i8*** %5
store i8* %26, i8** %27
%28 = load i8**, i8*** %5
%29 = load i8*, i8** %28
%30 = load i8*, i8** @MAP_FAILED
%31 = icmp eq i8* %29, %30
br i1 %31, label %32, label %33
32: ; preds = %20
store i32 0, i32* %3
br label %36
33: ; preds = %20
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
store i32 1, i32* %3
br label %36
36: ; preds = %33, %32, %19, %13
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
define i32 @mmap_file(i8* nocapture %0, i8** 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 slt i32 %6, 0
br i1 %7, label %21, label %8
8: ; preds = %2
%9 = call i32 @fstat(i32 %6, %struct.stat* %3)
%10 = icmp eq i32 %9, -1
br i1 %10, label %21, label %11
11: ; preds = %8
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @MAP_PRIVATE
%16 = tail call i8* @mmap(i32* null, i32 %13, i32 %14, i32 %15, i32 %6, i32 0)
store i8* %16, i8** %1
%17 = load i8*, i8** @MAP_FAILED
%18 = icmp eq i8* %16, %17
br i1 %18, label %21, label %19
19: ; preds = %11
%20 = tail call i32 @close(i32 %6)
br label %21
21: ; preds = %11, %8, %2, %19
%22 = phi i32 [ 1, %19 ], [ %6, %2 ], [ 0, %8 ], [ 0, %11 ]
ret i32 %22
}