#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_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ err(int, char *, char const *);
void *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int stat(char const *, struct stat *);
void *map_file(const char *pathname, size_t *filelen) {
  struct stat sb;
  if (stat(pathname, &sb) == -1)
    err(1, "can't stat file %s", pathname);
  *filelen = sb.st_size;
  int fd = open(pathname, O_RDONLY);
  if (fd == -1)
    err(1, "can't open file %s", pathname);
  void *file = mmap(NULL, *filelen, PROT_READ, MAP_SHARED, fd, 0);
  if (file == MAP_FAILED)
    err(1, "can't mmap file %s", pathname);
  return file;
}


x86 assembly (clang, -O0):
<map_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
jne    117b <map_file+0x3b>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe8a(%rip),%rsi
call   1050 <err@plt>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ba <map_file+0x7a>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe5e(%rip),%rsi
call   1050 <err@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
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    1210 <map_file+0xd0>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe1b(%rip),%rsi
call   1050 <err@plt>
mov    -0x28(%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
lea    0x8(%rsp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
jne    1171 <map_file+0x31>
lea    0xe9c(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1050 <err@plt>
mov    0x8(%rsp),%rax
mov    %rax,(%r14)
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11a5 <map_file+0x65>
lea    0xe7b(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1050 <err@plt>
mov    (%r14),%rsi
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
mov    0x2e30(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    0x2df7(%rip),%rax
cmp    (%rax),%rbp
jne    11ea <map_file+0xaa>
lea    0xe49(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1050 <err@plt>
mov    %rbp,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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