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

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
void mmap_cat(char *argv[]) {
  write(1, "MMAP\n\n", 6);
  int fd;
  struct stat stat_buffer;
  char *file_buffer;
  fd = open(argv[1], O_RDONLY);
  fstat(fd, &stat_buffer);
  file_buffer = mmap(NULL, stat_buffer.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  close(fd);
  write(1, file_buffer, stat_buffer.st_size);
  munmap(file_buffer, stat_buffer.st_size);
  close(1);
}


x86 assembly (clang, -O0):
<mmap_cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x1,%edi
lea    0xe88(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1080 <fstat@plt>
mov    -0x10(%rbp),%esi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
mov    0x2e16(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1060 <munmap@plt>
mov    $0x1,%edi
call   1050 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmap_cat>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0xe8e(%rip),%rsi
mov    $0x1,%edi
mov    $0x6,%edx
call   1030 <write@plt>
mov    0x8(%rbx),%rdi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
mov    0x8(%rsp),%r14d
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
mov    0x2e1a(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0x1,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1060 <munmap@plt>
mov    $0x1,%edi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <close@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [7 x i8] c"MMAP\0A\0A\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
define void @mmap_cat(i8** %0) {
%2 = alloca i8**
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca i8*
store i8** %0, i8*** %2
%6 = call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%7 = load i8**, i8*** %2
%8 = getelementptr inbounds i8*, i8** %7, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = call i32 @fstat(i32 %12, %struct.stat* %4)
%14 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @MAP_PRIVATE
%18 = load i32, i32* %3
%19 = call i8* @mmap(i32* null, i32 %15, i32 %16, i32 %17, i32 %18, i32 0)
store i8* %19, i8** %5
%20 = load i32, i32* %3
%21 = call i32 @close(i32 %20)
%22 = load i8*, i8** %5
%23 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @write(i32 1, i8* %22, i32 %24)
%26 = load i8*, i8** %5
%27 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @munmap(i8* %26, i32 %28)
%30 = call i32 @close(i32 1)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [7 x i8] c"MMAP\0A\0A\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
define void @mmap_cat(i8** nocapture %0) {
%2 = alloca %struct.stat
%3 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%4 = bitcast %struct.stat* %2 to i8*
%5 = getelementptr inbounds i8*, i8** %0, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %6, i32 %7)
%9 = call i32 @fstat(i32 %8, %struct.stat* %2)
%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_PRIVATE
%14 = tail call i8* @mmap(i32* null, i32 %11, i32 %12, i32 %13, i32 %8, i32 0)
%15 = tail call i32 @close(i32 %8)
%16 = tail call i32 @write(i32 1, i8* %14, i32 %11)
%17 = tail call i32 @munmap(i8* %14, i32 %11)
%18 = tail call i32 @close(i32 1)
ret void
}