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

/* Variables and functions */
void *MAP_FAILED;
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*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int perror(char const *);
int map_file(void **map, const char *filename) {
  int fd;
  struct stat sb;
  void *addr;
  fd = open(filename, O_RDONLY);
  if (fd == -1)
    return (perror(filename), -1);
  if (fstat(fd, &sb) == -1)
    return (perror(filename), -1);
  addr = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (addr == MAP_FAILED)
    return (perror("mmap"), -1);
  (void)close(fd);
  *map = addr;
  return (sb.st_size);
}


x86 assembly (clang, -O0):
<map_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1194 <map_file+0x44>
mov    -0x18(%rbp),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122c <map_file+0xdc>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11be <map_file+0x6e>
mov    -0x18(%rbp),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122c <map_file+0xdc>
mov    -0x20(%rbp),%esi
mov    0x2e30(%rip),%rax
mov    (%rax),%edx
mov    0x2e17(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2dd6(%rip),%rcx
cmp    (%rcx),%rax
jne    1213 <map_file+0xc3>
lea    0xdfe(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122c <map_file+0xdc>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11d1 <map_file+0x81>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
je     11d1 <map_file+0x81>
mov    (%rsp),%r15d
mov    0x2e69(%rip),%rax
mov    (%rax),%edx
mov    0x2e50(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e14(%rip),%rax
cmp    (%rax),%rbx
je     11d6 <map_file+0x86>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,(%r14)
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
jmp    11dd <map_file+0x8d>
lea    0xe23(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%r15d
jmp    11c3 <map_file+0x73>

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

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