#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 */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
scalar_t__ mmap(int /*<<< orphan*/ *, scalar_t__, int /*<<< orphan*/,
                int /*<<< orphan*/, int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int stat(char const *, struct stat *);
const char *mmap_by_filename(const char *filename, size_t *size) {
  struct stat st;
  int status, handle;
  const char *data;
  status = stat(filename, &st);
  if (status < 0) {
    return NULL;
  }
  handle = open(filename, O_RDONLY);
  if (handle < 0) {
    return NULL;
  }
  data =
      (const char *)mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, handle, 0);
  if (data == (const char *)-1) {
    return NULL;
  } else {
    *size = (size_t)st.st_size;
  }
  close(handle);
  return data;
}


x86 assembly (clang, -O0):
<mmap_by_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1177 <mmap_by_filename+0x37>
movq   $0x0,-0x8(%rbp)
jmp    1206 <mmap_by_filename+0xc6>
mov    -0x10(%rbp),%rdi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11a3 <mmap_by_filename+0x63>
movq   $0x0,-0x8(%rbp)
jmp    1206 <mmap_by_filename+0xc6>
mov    -0x20(%rbp),%rsi
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
mov    0x2e19(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x30(%rbp)
jne    11eb <mmap_by_filename+0xab>
movq   $0x0,-0x8(%rbp)
jmp    1206 <mmap_by_filename+0xc6>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%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_by_filename>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %eax,%eax
js     11b8 <mmap_by_filename+0x78>
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11b8 <mmap_by_filename+0x78>
mov    %eax,%ebp
mov    0x8(%rsp),%r15
mov    0x2e75(%rip),%rax
mov    (%rax),%edx
mov    0x2e44(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     11ba <mmap_by_filename+0x7a>
mov    %rax,%r12
mov    %r15,(%r14)
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r12,%rbx
jmp    11ba <mmap_by_filename+0x7a>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
define i8* @mmap_by_filename(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %4
store i64* %1, i64** %5
%10 = load i8*, i8** %4
%11 = call i32 @stat(i8* %10, %struct.stat* %6)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i8* null, i8** %3
br label %41
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
store i8* null, i8** %3
br label %41
22: ; preds = %15
%23 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%24 = load i64, i64* %23
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @MAP_PRIVATE
%27 = load i32, i32* %8
%28 = call i64 @mmap(i32* null, i64 %24, i32 %25, i32 %26, i32 %27, i32 0)
%29 = inttoptr i64 %28 to i8*
store i8* %29, i8** %9
%30 = load i8*, i8** %9
%31 = icmp eq i8* %30, inttoptr (i64 -1 to i8*)
br i1 %31, label %32, label %33
32: ; preds = %22
store i8* null, i8** %3
br label %41
33: ; preds = %22
%34 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%35 = load i64, i64* %34
%36 = load i64*, i64** %5
store i64 %35, i64* %36
br label %37
37: ; preds = %33
%38 = load i32, i32* %8
%39 = call i32 @close(i32 %38)
%40 = load i8*, i8** %9
store i8* %40, i8** %3
br label %41
41: ; preds = %37, %32, %21, %14
%42 = load i8*, i8** %3
ret i8* %42
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
define i8* @mmap_by_filename(i8* nocapture %0, i64* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @stat(i8* %0, %struct.stat* %3)
%6 = icmp slt i32 %5, 0
br i1 %6, label %21, label %7
7: ; preds = %2
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* %0, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %21, label %11
11: ; preds = %7
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i64, i64* %12
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @MAP_PRIVATE
%16 = tail call i64 @mmap(i32* null, i64 %13, i32 %14, i32 %15, i32 %9, i32 0)
%17 = inttoptr i64 %16 to i8*
%18 = icmp eq i8* %17, inttoptr (i64 -1 to i8*)
br i1 %18, label %21, label %19
19: ; preds = %11
store i64 %13, i64* %1
%20 = tail call i32 @close(i32 %9)
br label %21
21: ; preds = %11, %7, %2, %19
%22 = phi i8* [ %17, %19 ], [ null, %2 ], [ null, %7 ], [ null, %11 ]
ret i8* %22
}