#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 */
typedef int /*<<< orphan*/ uint8_t;
struct stat {
  int /*<<< orphan*/ st_size;
};

/* Variables and functions */
int MAP_FILE;
int MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ log_perror(char *);
int /*<<< orphan*/ *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/, int, int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int map_file(const char *filename, struct stat *st_out, uint8_t **map_out) {
  int fd;
  if ((fd = open(filename, O_RDONLY)) < 0) {
    log_perror("open()");
    goto err;
  }
  if (fstat(fd, st_out) < 0) {
    log_perror("fstat()");
    goto err;
  }
  if (!(*map_out = mmap(NULL, st_out->st_size, PROT_READ, MAP_FILE | MAP_SHARED,
                        fd, 0))) {
    log_perror("mmap()");
    goto err;
  }
  close(fd);
  return 0;
err:
  if (fd >= 0)
    close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<map_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1193 <map_file+0x43>
lea    0xe77(%rip),%rdi
call   1060 <log_perror@plt>
jmp    1221 <map_file+0xd1>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
jge    11ba <map_file+0x6a>
lea    0xe57(%rip),%rdi
call   1060 <log_perror@plt>
jmp    1221 <map_file+0xd1>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
mov    0x2e00(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0f(%rip),%rax
or     (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    -0x20(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    120d <map_file+0xbd>
lea    0xe0c(%rip),%rdi
call   1060 <log_perror@plt>
jmp    1221 <map_file+0xd1>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    123a <map_file+0xea>
cmpl   $0x0,-0x24(%rbp)
jl     1233 <map_file+0xe3>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_file>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     11bd <map_file+0x6d>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
call   1070 <fstat@plt>
test   %rax,%rax
js     11cb <map_file+0x7b>
mov    (%r15),%esi
mov    0x2e70(%rip),%rax
mov    (%rax),%edx
mov    0x2e3f(%rip),%rax
mov    0x2e50(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %r15d,%r15d
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11d4 <map_file+0x84>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    11ed <map_file+0x9d>
lea    0xe3c(%rip),%rdi
call   1060 <log_perror@plt>
jmp    11e7 <map_file+0x97>
lea    0xe35(%rip),%rdi
jmp    11db <map_file+0x8b>
lea    0xe34(%rip),%rdi
call   1060 <log_perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [7 x i8] c"open()\00"
@.str.1 = [8 x i8] c"fstat()\00"
@PROT_READ = global i32 0
@MAP_FILE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [7 x i8] c"mmap()\00"
define i32 @map_file(i8* %0, %struct.stat* %1, i32** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.stat*
%7 = alloca i32**
%8 = alloca i32
store i8* %0, i8** %5
store %struct.stat* %1, %struct.stat** %6
store i32** %2, i32*** %7
%9 = load i8*, i8** %5
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %8
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 @log_perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %39
15: ; preds = %3
%16 = load i32, i32* %8
%17 = load %struct.stat*, %struct.stat** %6
%18 = call i64 @fstat(i32 %16, %struct.stat* %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @log_perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %39
22: ; preds = %15
%23 = load %struct.stat*, %struct.stat** %6
%24 = getelementptr inbounds %struct.stat, %struct.stat* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @MAP_FILE
%28 = load i32, i32* @MAP_SHARED
%29 = or i32 %27, %28
%30 = load i32, i32* %8
%31 = call i32* @mmap(i32* null, i32 %25, i32 %26, i32 %29, i32 %30, i32 0)
%32 = load i32**, i32*** %7
store i32* %31, i32** %32
%33 = icmp ne i32* %31, null
br i1 %33, label %36, label %34
34: ; preds = %22
%35 = call i32 @log_perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %39
36: ; preds = %22
%37 = load i32, i32* %8
%38 = call i32 @close(i32 %37)
store i32 0, i32* %4
br label %46
39: ; preds = %34, %20, %13
%40 = load i32, i32* %8
%41 = icmp sge i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i32, i32* %8
%44 = call i32 @close(i32 %43)
br label %45
45: ; preds = %42, %39
store i32 -1, i32* %4
br label %46
46: ; preds = %45, %36
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [7 x i8] c"open()\00"
@.str.1 = [8 x i8] c"fstat()\00"
@PROT_READ = global i32 0
@MAP_FILE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [7 x i8] c"mmap()\00"
define i32 @map_file(i8* nocapture %0, %struct.stat* nocapture %1, i32** nocapture writeonly %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %21, label %7
7: ; preds = %3
%8 = tail call i64 @fstat(i32 %5, %struct.stat* %1)
%9 = icmp slt i64 %8, 0
br i1 %9, label %23, label %10
10: ; preds = %7
%11 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%12 = load i32, i32* %11
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @MAP_FILE
%15 = load i32, i32* @MAP_SHARED
%16 = or i32 %15, %14
%17 = tail call i32* @mmap(i32* null, i32 %12, i32 %13, i32 %16, i32 %5, i32 0)
store i32* %17, i32** %2
%18 = icmp eq i32* %17, null
br i1 %18, label %23, label %19
19: ; preds = %10
%20 = tail call i32 @close(i32 %5)
br label %27
21: ; preds = %3
%22 = tail call i32 @log_perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %27
23: ; preds = %10, %7
%24 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), %7 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %10 ]
%25 = tail call i32 @log_perror(i8* %24)
%26 = tail call i32 @close(i32 %5)
br label %27
27: ; preds = %21, %23, %19
%28 = phi i32 [ 0, %19 ], [ -1, %23 ], [ -1, %21 ]
ret i32 %28
}