#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
void *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int stat(char const *, struct stat *);
int map_file(const char *filename, void **addr, size_t *size) {
  struct stat info;
  int error = 0;
  int fd;
  error = stat(filename, &info);
  if (!error) {
    *size = info.st_size;
    if (info.st_size > 0) {
      fd = open(filename, O_RDWR);
      if (fd >= 0) {
        *addr = mmap(NULL, *size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
        if (*addr == MAP_FAILED)
          error = -1;
        close(fd);
      } else
        error = fd;
    } else
      *addr = NULL;
  } else {
    perror("can't stat");
  }
  return error;
}


x86 assembly (clang, -O0):
<map_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    123c <map_file+0xec>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x20(%rbp)
jbe    122c <map_file+0xdc>
mov    -0x8(%rbp),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jl     1221 <map_file+0xd1>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    0x2e30(%rip),%rax
mov    (%rax),%edx
mov    0x2dff(%rip),%rax
or     (%rax),%edx
mov    0x2dee(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2dbc(%rip),%rcx
cmp    (%rcx),%rax
jne    1214 <map_file+0xc4>
movl   $0xffffffff,-0x24(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
jmp    1227 <map_file+0xd7>
mov    -0x28(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    1237 <map_file+0xe7>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
jmp    1248 <map_file+0xf8>
lea    0xdbd(%rip),%rdi
call   1070 <perror@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %eax,%eax
jne    11fe <map_file+0xae>
mov    0x8(%rsp),%rax
mov    %rax,(%r15)
test   %rax,%rax
je     11e6 <map_file+0x96>
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11ef <map_file+0x9f>
mov    (%r15),%rsi
mov    0x2e53(%rip),%rax
mov    0x2e24(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e11(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r14)
mov    0x2df0(%rip),%rcx
xor    %ebp,%ebp
cmp    (%rcx),%rax
sete   %bpl
neg    %ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%ebx
jmp    11ef <map_file+0x9f>
movq   $0x0,(%r14)
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebx
lea    0xdf9(%rip),%rdi
call   1070 <perror@plt>
jmp    11ef <map_file+0x9f>

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

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