#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 /*<<< orphan*/ st_size;
};

/* Variables and functions */
int /*<<< orphan*/ MADV_RANDOM;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ madvise(char *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
                int /*<<< orphan*/, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stat(char *, struct stat *);
char *file_to_mmap(char *filename) {
  struct stat sb;
  stat(filename, &sb);
  int fd = open(filename, O_RDONLY);
  assert(fd != -1);
  char *map;
  map = (char *)mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
  madvise(map, sb.st_size, MADV_RANDOM);
  return map;
}


x86 assembly (clang, -O0):
<file_to_mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <stat@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%esi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
mov    0x2e46(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
call   1060 <madvise@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_to_mmap>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1050 <stat@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%ebx
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1040 <assert@plt>
mov    (%rsp),%ebp
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
mov    0x2e56(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <madvise@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MADV_RANDOM = global i32 0
define i8* @file_to_mmap(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @stat(i8* %6, %struct.stat* %3)
%8 = load i8*, i8** %2
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, -1
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @MAP_SHARED
%19 = load i32, i32* %4
%20 = call i64 @mmap(i32* null, i32 %16, i32 %17, i32 %18, i32 %19, i32 0)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %5
%22 = load i8*, i8** %5
%23 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i32, i32* @MADV_RANDOM
%26 = call i32 @madvise(i8* %22, i32 %24, i32 %25)
%27 = load i8*, i8** %5
ret i8* %27
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MADV_RANDOM = global i32 0
define i8* @file_to_mmap(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp ne i32 %6, -1
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @MAP_SHARED
%14 = tail call i64 @mmap(i32* null, i32 %11, i32 %12, i32 %13, i32 %6, i32 0)
%15 = inttoptr i64 %14 to i8*
%16 = load i32, i32* @MADV_RANDOM
%17 = tail call i32 @madvise(i8* %15, i32 %11, i32 %16)
ret i8* %15
}