#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDONLY;
int O_RDWR;
int O_TRUNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
size_t get_file_size(char *);
int /*<<< orphan*/ lseek(int, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
char *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fdin, fdout;
  char *src, *dst;
  size_t file_size;
  if (argc != 3) {
    printf("Usage: mmap2 <src> <dest>\n");
    exit(EXIT_FAILURE);
  }
  file_size = get_file_size(argv[1]);
  fdin = open(argv[1], O_RDONLY, 0);
  fdout = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 0666);
  lseek(fdout, file_size - 1, SEEK_SET);
  write(fdout, "", 1);
  src = mmap(NULL, file_size, PROT_READ, MAP_SHARED, fdin, 0);
  assert(src != MAP_FAILED);
  dst = mmap(NULL, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fdout, 0);
  assert(src != MAP_FAILED);
  memcpy(dst, src, file_size);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11ca <main+0x3a>
lea    0xe49(%rip),%rdi
call   1050 <printf@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <get_file_size@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
mov    0x2dea(%rip),%rax
or     (%rax),%esi
mov    0x2dd9(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x30(%rbp),%rsi
sub    $0x1,%rsi
mov    0x2d78(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
mov    -0x18(%rbp),%edi
lea    0xdd1(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x30(%rbp),%rsi
mov    0x2d8a(%rip),%rax
mov    (%rax),%edx
mov    0x2d71(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d08(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x30(%rbp),%rsi
mov    0x2d45(%rip),%rax
mov    (%rax),%edx
mov    0x2cf4(%rip),%rax
or     (%rax),%edx
mov    0x2d23(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2cba(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1080 <memcpy@plt>
mov    0x2c9a(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    12a8 <main+0x118>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1090 <get_file_size@plt>
mov    %rax,%r15
mov    0x8(%rbx),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <open@plt>
mov    %eax,%r14d
mov    0x2df7(%rip),%rax
mov    0x2e20(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%rbx),%rdi
mov    0x2e09(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   10a0 <open@plt>
mov    %eax,%ebp
lea    -0x1(%r15),%rsi
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1060 <lseek@plt>
lea    0xe09(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <write@plt>
mov    0x2dc5(%rip),%r12
mov    (%r12),%edx
mov    0x2daa(%rip),%r13
mov    0x0(%r13),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2d44(%rip),%r14
xor    %edi,%edi
cmp    (%r14),%rax
setne  %dil
call   1070 <assert@plt>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edx
or     (%r12),%edx
mov    0x0(%r13),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbp
xor    %edi,%edi
cmp    (%r14),%rbx
setne  %dil
call   1070 <assert@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1080 <memcpy@plt>
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xd51(%rip),%rdi
call   1030 <puts@plt>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"Usage: mmap2 <src> <dest>\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [1 x i8] zeroinitializer
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@PROT_WRITE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i64 @get_file_size(i8* %20)
store i64 %21, i64* %10
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %24, i32 %25, i32 0)
store i32 %26, i32* %6
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = load i32, i32* @O_RDWR
%31 = load i32, i32* @O_CREAT
%32 = or i32 %30, %31
%33 = load i32, i32* @O_TRUNC
%34 = or i32 %32, %33
%35 = call i32 @open(i8* %29, i32 %34, i32 438)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = load i64, i64* %10
%38 = sub i64 %37, 1
%39 = load i32, i32* @SEEK_SET
%40 = call i32 @lseek(i32 %36, i64 %38, i32 %39)
%41 = load i32, i32* %7
%42 = call i32 @write(i32 %41, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i32 1)
%43 = load i64, i64* %10
%44 = load i32, i32* @PROT_READ
%45 = load i32, i32* @MAP_SHARED
%46 = load i32, i32* %6
%47 = call i8* @mmap(i32* null, i64 %43, i32 %44, i32 %45, i32 %46, i32 0)
store i8* %47, i8** %8
%48 = load i8*, i8** %8
%49 = load i8*, i8** @MAP_FAILED
%50 = icmp ne i8* %48, %49
%51 = zext i1 %50 to i32
%52 = call i32 @assert(i32 %51)
%53 = load i64, i64* %10
%54 = load i32, i32* @PROT_READ
%55 = load i32, i32* @PROT_WRITE
%56 = or i32 %54, %55
%57 = load i32, i32* @MAP_SHARED
%58 = load i32, i32* %7
%59 = call i8* @mmap(i32* null, i64 %53, i32 %56, i32 %57, i32 %58, i32 0)
store i8* %59, i8** %9
%60 = load i8*, i8** %8
%61 = load i8*, i8** @MAP_FAILED
%62 = icmp ne i8* %60, %61
%63 = zext i1 %62 to i32
%64 = call i32 @assert(i32 %63)
%65 = load i8*, i8** %9
%66 = load i8*, i8** %8
%67 = load i64, i64* %10
%68 = call i32 @memcpy(i8* %65, i8* %66, i64 %67)
%69 = load i32, i32* @EXIT_SUCCESS
%70 = call i32 @exit(i32 %69)
unreachable
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [1 x i8] zeroinitializer
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@PROT_WRITE = global i32 0
@EXIT_SUCCESS = global i32 0
@str = [26 x i8] c"Usage: mmap2 <src> <dest>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i64 @get_file_size(i8* %10)
%12 = load i8*, i8** %9
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @open(i8* %12, i32 %13, i32 0)
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_CREAT
%19 = or i32 %18, %17
%20 = load i32, i32* @O_TRUNC
%21 = or i32 %19, %20
%22 = tail call i32 @open(i8* %16, i32 %21, i32 438)
%23 = add i64 %11, -1
%24 = load i32, i32* @SEEK_SET
%25 = tail call i32 @lseek(i32 %22, i64 %23, i32 %24)
%26 = tail call i32 @write(i32 %22, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i32 1)
%27 = load i32, i32* @PROT_READ
%28 = load i32, i32* @MAP_SHARED
%29 = tail call i8* @mmap(i32* null, i64 %11, i32 %27, i32 %28, i32 %14, i32 0)
%30 = load i8*, i8** @MAP_FAILED
%31 = icmp ne i8* %29, %30
%32 = zext i1 %31 to i32
%33 = tail call i32 @assert(i32 %32)
%34 = load i32, i32* @PROT_READ
%35 = load i32, i32* @PROT_WRITE
%36 = or i32 %35, %34
%37 = load i32, i32* @MAP_SHARED
%38 = tail call i8* @mmap(i32* null, i64 %11, i32 %36, i32 %37, i32 %22, i32 0)
%39 = load i8*, i8** @MAP_FAILED
%40 = icmp ne i8* %29, %39
%41 = zext i1 %40 to i32
%42 = tail call i32 @assert(i32 %41)
%43 = tail call i32 @memcpy(i8* %38, i8* %29, i64 %11)
%44 = load i32, i32* @EXIT_SUCCESS
%45 = tail call i32 @exit(i32 %44)
unreachable
}