#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_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
scalar_t__ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
void *map_file(char *file_name, int *size) {
  struct stat st;
  void *map;
  int fd;
  fd = open(file_name, O_RDWR);
  if (fd < 0) {
    perror(file_name);
    return NULL;
  }
  if (fstat(fd, &st)) {
    perror("Could not determine file size");
    close(fd);
    return NULL;
  }
  *size = st.st_size;
  map = mmap(NULL, *size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (map == MAP_FAILED) {
    perror("Mapping to memory failed");
    close(fd);
    return NULL;
  }
  close(fd);
  return map;
}


x86 assembly (clang, -O0):
<map_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1195 <map_file+0x45>
mov    -0x10(%rbp),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    124f <map_file+0xff>
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
je     11cc <map_file+0x7c>
lea    0xe4e(%rip),%rdi
call   1060 <perror@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    124f <map_file+0xff>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
mov    0x2ddd(%rip),%rax
or     (%rax),%edx
mov    0x2df4(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2dab(%rip),%rcx
cmp    (%rcx),%rax
jne    123f <map_file+0xef>
lea    0xdf9(%rip),%rdi
call   1060 <perror@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    124f <map_file+0xff>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     11d4 <map_file+0x84>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
test   %rax,%rax
jne    11e0 <map_file+0x90>
mov    0x8(%rsp),%esi
mov    %esi,(%r14)
mov    0x2e67(%rip),%rax
mov    0x2e30(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e45(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e04(%rip),%rax
cmp    (%rax),%rbx
je     11e9 <map_file+0x99>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rbx,%rdi
call   1060 <perror@plt>
xor    %ebx,%ebx
jmp    11c8 <map_file+0x78>
lea    0xe19(%rip),%rdi
jmp    11f0 <map_file+0xa0>
lea    0xe2e(%rip),%rdi
call   1060 <perror@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %ebx,%ebx
jmp    11c8 <map_file+0x78>

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

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