#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;
  if ((error = stat(filename, &info)) == -1) {
    perror("can't stat");
    return 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;
  }
  return error;
}


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)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    119b <map_file+0x4b>
lea    0xe75(%rip),%rdi
call   1070 <perror@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1253 <map_file+0x103>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x28(%rbp)
jbe    1242 <map_file+0xf2>
mov    -0x10(%rbp),%rdi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jl     1237 <map_file+0xe7>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
mov    0x2de9(%rip),%rax
or     (%rax),%edx
mov    0x2dd8(%rip),%rax
mov    (%rax),%ecx
mov    -0x30(%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    0x2da6(%rip),%rcx
cmp    (%rcx),%rax
jne    122a <map_file+0xda>
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
jmp    123d <map_file+0xed>
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    124d <map_file+0xfd>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x2c(%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    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbp
mov    %rsp,%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
je     11f5 <map_file+0xa5>
mov    %eax,%ebx
mov    (%rsp),%rax
mov    %rax,(%r15)
test   %rax,%rax
je     11df <map_file+0x8f>
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11e8 <map_file+0x98>
mov    (%r15),%rsi
mov    0x2e58(%rip),%rax
mov    0x2e29(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e16(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r14)
mov    0x2df5(%rip),%rcx
cmp    (%rcx),%rax
mov    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11e6 <map_file+0x96>
movq   $0x0,(%r14)
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe04(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11e8 <map_file+0x98>

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

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