#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 */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ PROT_WRITE;
scalar_t__ fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, size_t, size_t, char *, char const *);
char *map_file_to_memory(size_t *len, int fd, const char *file) {
  struct stat sb;
  char *addr;
  if (fstat(fd, &sb) < 0) {
    perror("fstat");
    return 0;
  }
  *len = sb.st_size;
  addr = mmap(NULL, *len, PROT_WRITE, MAP_SHARED, fd, 0);
  if (addr == MAP_FAILED) {
    perror("mmap");
    return 0;
  }
  printf("Mapped %zu (%#zx) bytes starting at address %p of file %s\n", *len,
         *len, addr, file);
  return addr;
}


x86 assembly (clang, -O0):
<map_file_to_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0x0,%rax
jge    1182 <map_file_to_memory+0x42>
lea    0xe90(%rip),%rdi
call   1050 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1211 <map_file_to_memory+0xd1>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
mov    0x2e4c(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2e0b(%rip),%rcx
cmp    (%rcx),%rax
jne    11e7 <map_file_to_memory+0xa7>
lea    0xe31(%rip),%rdi
call   1050 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1211 <map_file_to_memory+0xd1>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xe07(%rip),%rdi
call   1040 <printf@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_file_to_memory>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
mov    %rdi,%r15
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1060 <fstat@plt>
test   %rax,%rax
js     11bd <map_file_to_memory+0x7d>
mov    0x8(%rsp),%rsi
mov    %rsi,(%r15)
mov    0x2e67(%rip),%rax
mov    (%rax),%edx
mov    0x2e76(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e3d(%rip),%rax
cmp    (%rax),%rbx
je     11c6 <map_file_to_memory+0x86>
mov    (%r15),%rdx
lea    0xe69(%rip),%rdi
mov    %rdx,%rsi
mov    %rbx,%rcx
mov    %r14,%r8
call   1040 <printf@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe3c(%rip),%rdi
jmp    11cd <map_file_to_memory+0x8d>
lea    0xe39(%rip),%rdi
call   1050 <perror@plt>
xor    %ebx,%ebx
jmp    11b0 <map_file_to_memory+0x70>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [6 x i8] c"fstat\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
@.str.2 = [59 x i8] c"Mapped %zu (%#zx) bytes starting at address %p of file %s\0A\00"
define i8* @map_file_to_memory(i64* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.stat
%9 = alloca i8*
store i64* %0, i64** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%10 = load i32, i32* %6
%11 = call i64 @fstat(i32 %10, %struct.stat* %8)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %4
br label %39
15: ; preds = %3
%16 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%17 = load i64, i64* %16
%18 = load i64*, i64** %5
store i64 %17, i64* %18
%19 = load i64*, i64** %5
%20 = load i64, i64* %19
%21 = load i32, i32* @PROT_WRITE
%22 = load i32, i32* @MAP_SHARED
%23 = load i32, i32* %6
%24 = call i8* @mmap(i32* null, i64 %20, i32 %21, i32 %22, i32 %23, i32 0)
store i8* %24, i8** %9
%25 = load i8*, i8** %9
%26 = load i8*, i8** @MAP_FAILED
%27 = icmp eq i8* %25, %26
br i1 %27, label %28, label %30
28: ; preds = %15
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %4
br label %39
30: ; preds = %15
%31 = load i64*, i64** %5
%32 = load i64, i64* %31
%33 = load i64*, i64** %5
%34 = load i64, i64* %33
%35 = load i8*, i8** %9
%36 = load i8*, i8** %7
%37 = call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i64 %32, i64 %34, i8* %35, i8* %36)
%38 = load i8*, i8** %9
store i8* %38, i8** %4
br label %39
39: ; preds = %30, %28, %13
%40 = load i8*, i8** %4
ret i8* %40
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [6 x i8] c"fstat\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
@.str.2 = [59 x i8] c"Mapped %zu (%#zx) bytes starting at address %p of file %s\0A\00"
define i8* @map_file_to_memory(i64* nocapture %0, i32 %1, i8* %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i64 @fstat(i32 %1, %struct.stat* %4)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %23
10: ; preds = %3
%11 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%12 = load i64, i64* %11
store i64 %12, i64* %0
%13 = load i32, i32* @PROT_WRITE
%14 = load i32, i32* @MAP_SHARED
%15 = tail call i8* @mmap(i32* null, i64 %12, i32 %13, i32 %14, i32 %1, i32 0)
%16 = load i8*, i8** @MAP_FAILED
%17 = icmp eq i8* %15, %16
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %23
20: ; preds = %10
%21 = load i64, i64* %0
%22 = tail call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i64 %21, i64 %21, i8* %15, i8* %2)
br label %23
23: ; preds = %20, %18, %8
%24 = phi i8* [ null, %8 ], [ null, %18 ], [ %15, %20 ]
ret i8* %24
}