#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 */

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ close(int);
int fopen(char *, int /*<<< orphan*/);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int /*<<< orphan*/ write(int, char *, int);
void main(void) {
  char file[] = "mmap.s";
  char *mappedptr;
  int fd, filelen;
  fd = fopen(file, O_RDONLY);
  filelen = lseek(fd, 0, SEEK_END);
  mappedptr = mmap(NULL, filelen, PROT_READ, MAP_SHARED, fd, 0);
  write(1, mappedptr, filelen);
  munmap(mappedptr, filelen);
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0xe92(%rip),%eax
mov    %eax,-0x7(%rbp)
mov    0xe8c(%rip),%ax
mov    %ax,-0x3(%rbp)
mov    0xe84(%rip),%al
mov    %al,-0x1(%rbp)
lea    -0x7(%rbp),%rdi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
call   1080 <fopen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
mov    0x2e25(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1070 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x732e70,0xb(%rsp)
movl   $0x70616d6d,0x8(%rsp)
70
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
call   1080 <fopen@plt>
mov    %eax,%r14d
mov    0x2e43(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,%ebp
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
mov    0x2e35(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%esi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    $0x1,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1070 <munmap@plt>
mov    %r14d,%edi
call   1060 <close@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.file = [7 x i8] c"mmap.s\00"
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
define void @main() {
%1 = alloca [7 x i8]
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = bitcast [7 x i8]* %1 to i8*
%6 = getelementptr inbounds [7 x i8], [7 x i8]* %1, i64 0, i64 0
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @fopen(i8* %6, i32 %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = load i32, i32* @SEEK_END
%11 = call i32 @lseek(i32 %9, i32 0, i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @MAP_SHARED
%15 = load i32, i32* %3
%16 = call i8* @mmap(i32* null, i32 %12, i32 %13, i32 %14, i32 %15, i32 0)
store i8* %16, i8** %2
%17 = load i8*, i8** %2
%18 = load i32, i32* %4
%19 = call i32 @write(i32 1, i8* %17, i32 %18)
%20 = load i8*, i8** %2
%21 = load i32, i32* %4
%22 = call i32 @munmap(i8* %20, i32 %21)
%23 = load i32, i32* %3
%24 = call i32 @close(i32 %23)
ret void
}

LLVM IR (-O2):
@__const.main.file = [7 x i8] c"mmap.s\00"
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
define void @main() {
%1 = alloca [7 x i8]
%2 = getelementptr inbounds [7 x i8], [7 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = call i32 @fopen(i8* %2, i32 %3)
%5 = load i32, i32* @SEEK_END
%6 = call i32 @lseek(i32 %4, i32 0, i32 %5)
%7 = load i32, i32* @PROT_READ
%8 = load i32, i32* @MAP_SHARED
%9 = call i8* @mmap(i32* null, i32 %6, i32 %7, i32 %8, i32 %4, i32 0)
%10 = call i32 @write(i32 1, i8* %9, i32 %6)
%11 = call i32 @munmap(i8* %9, i32 %6)
%12 = call i32 @close(i32 %4)
ret void
}