#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 {
  scalar_t__ st_size;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
scalar_t__ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
void *mmap_file(size_t *len, const char *filename) {
  int fd = open(filename, O_RDONLY);
  if (fd < 0) {
    perror("Error opening file");
  }
  struct stat st[1];
  if (fstat(fd, st)) {
    perror("Error while getting file information");
  }
  *len = (size_t)st->st_size;
  if (!*len) {
    close(fd);
    return NULL;
  }
  void *map = mmap(NULL, *len, PROT_READ, MAP_PRIVATE, fd, 0);
  if (MAP_FAILED == map) {
    perror("mmap failed");
  }
  close(fd);
  return map;
}


x86 assembly (clang, -O0):
<mmap_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    118b <mmap_file+0x3b>
lea    0xe7a(%rip),%rdi
call   1060 <perror@plt>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
je     11ad <mmap_file+0x5d>
lea    0xe6b(%rip),%rdi
call   1060 <perror@plt>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11db <mmap_file+0x8b>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1238 <mmap_file+0xe8>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
mov    0x2dde(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    0x2db9(%rip),%rax
mov    (%rax),%rax
cmp    -0x30(%rbp),%rax
jne    1228 <mmap_file+0xd8>
lea    0xe15(%rip),%rdi
call   1060 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmap_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rax
mov    %rdi,%r14
mov    0x2e7b(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11e4 <mmap_file+0x94>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   1070 <fstat@plt>
test   %rax,%rax
jne    11f2 <mmap_file+0xa2>
mov    0x8(%rsp),%rsi
mov    %rsi,(%r14)
test   %rsi,%rsi
je     11cf <mmap_file+0x7f>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edx
mov    0x2e2d(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e0c(%rip),%rax
cmp    %rbx,(%rax)
jne    11d1 <mmap_file+0x81>
lea    0xe70(%rip),%rdi
call   1060 <perror@plt>
jmp    11d1 <mmap_file+0x81>
xor    %ebx,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe15(%rip),%rdi
call   1060 <perror@plt>
jmp    1175 <mmap_file+0x25>
lea    0xe1a(%rip),%rdi
call   1060 <perror@plt>
jmp    1186 <mmap_file+0x36>

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

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