#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 */
void *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*/ exit(int /*<<< orphan*/);
scalar_t__ fstat(int, struct stat *);
int lseek(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(void *, void *, scalar_t__);
void *mmap(int /*<<< orphan*/, scalar_t__, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fdin;
  int fdout;
  void *src;
  void *dst;
  struct stat statbuf;
  if (3 != argc) {
    printf("usage: %s <fromfile> <tofile>\n", argv[0]);
    return -1;
  }
  if ((fdin = open(argv[1], O_RDONLY)) < 0) {
    printf("can't create %s for reading\n", argv[1]);
    return -1;
  }
  if ((fdout = open(argv[2], O_RDWR | O_CREAT | O_TRUNC)) < 0) {
    printf("can't create %s for writing\n", argv[2]);
    return -1;
  }
  if (fstat(fdin, &statbuf) < 0) {
    printf("fstat error\n");
    return -1;
  }
  if (lseek(fdout, statbuf.st_size - 1, SEEK_SET) == -1) {
    printf("lseek error\n");
    return -1;
  }
  if (write(fdout, "", 1) != 1) {
    printf("write error\n");
    return -1;
  }
  if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0)) ==
      MAP_FAILED) {
    printf("mmap error for input\n");
    return -1;
  }
  if ((dst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fdout,
                  0)) == MAP_FAILED) {
    printf("mmap error for output\n");
    return -1;
  }
  memcpy(dst, src, statbuf.st_size);
  exit(0);
}


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)
mov    $0x3,%eax
cmp    -0x8(%rbp),%eax
je     11c5 <main+0x45>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c9 <main+0x249>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1209 <main+0x89>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c9 <main+0x249>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
mov    0x2dd7(%rip),%rax
or     (%rax),%esi
mov    0x2dc6(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    125f <main+0xdf>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c9 <main+0x249>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0x0,%rax
jge    128f <main+0x10f>
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c9 <main+0x249>
mov    -0x18(%rbp),%edi
mov    -0x30(%rbp),%rsi
sub    $0x1,%rsi
mov    0x2d0f(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
jne    12cb <main+0x14b>
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c9 <main+0x249>
mov    -0x18(%rbp),%edi
lea    0xd49(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     1302 <main+0x182>
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c9 <main+0x249>
mov    -0x30(%rbp),%rsi
mov    0x2cdb(%rip),%rax
mov    (%rax),%edx
mov    0x2cc2(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    0x2c6e(%rip),%rcx
cmp    (%rcx),%rax
jne    1355 <main+0x1d5>
lea    0xd3e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c9 <main+0x249>
mov    -0x30(%rbp),%rsi
mov    0x2c88(%rip),%rax
mov    (%rax),%edx
mov    0x2c3f(%rip),%rax
or     (%rax),%edx
mov    0x2c66(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    0x2c12(%rip),%rcx
cmp    (%rcx),%rax
jne    13b1 <main+0x231>
lea    0xcf8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c9 <main+0x249>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1070 <memcpy@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    12ae <main+0x11e>
mov    0x8(%rbx),%rdi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     12ba <main+0x12a>
mov    %eax,%ebp
mov    0x2dfe(%rip),%rax
mov    0x2e27(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%rbx),%rdi
mov    0x2e10(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     12c7 <main+0x137>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %ebp,%edi
call   10b0 <fstat@plt>
test   %rax,%rax
js     12ed <main+0x15d>
mov    (%rsp),%r14
lea    -0x1(%r14),%rsi
mov    0x2d9c(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
je     12f6 <main+0x166>
lea    0xdf0(%rip),%rsi
mov    %r15d,%edi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    12ff <main+0x16f>
mov    0x2d9d(%rip),%r12
mov    (%r12),%edx
mov    0x2d82(%rip),%r13
mov    0x0(%r13),%ecx
xor    %edi,%edi
mov    %r14,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    0x2d2f(%rip),%rbp
cmp    0x0(%rbp),%rax
je     1308 <main+0x178>
mov    %rax,%rbx
mov    0x2d23(%rip),%rax
mov    (%rax),%edx
or     (%r12),%edx
mov    0x0(%r13),%ecx
xor    %edi,%edi
mov    %r14,%rsi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
cmp    0x0(%rbp),%rax
jne    1316 <main+0x186>
lea    0xdad(%rip),%rdi
jmp    130f <main+0x17f>
mov    (%rbx),%rsi
lea    0xd48(%rip),%rdi
jmp    12d2 <main+0x142>
mov    0x8(%rbx),%rsi
lea    0xd5a(%rip),%rdi
jmp    12d2 <main+0x142>
mov    0x10(%rbx),%rsi
lea    0xd6a(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda8(%rip),%rdi
jmp    130f <main+0x17f>
lea    0xd93(%rip),%rdi
jmp    130f <main+0x17f>
lea    0xd7e(%rip),%rdi
jmp    130f <main+0x17f>
lea    0xd60(%rip),%rdi
call   1030 <puts@plt>
jmp    12d9 <main+0x149>
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1080 <memcpy@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [31 x i8] c"usage: %s <fromfile> <tofile>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [29 x i8] c"can't create %s for reading\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [29 x i8] c"can't create %s for writing\0A\00"
@.str.3 = [13 x i8] c"fstat error\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [13 x i8] c"lseek error\0A\00"
@.str.5 = [1 x i8] zeroinitializer
@.str.6 = [13 x i8] c"write error\0A\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.7 = [22 x i8] c"mmap error for input\0A\00"
@PROT_WRITE = global i32 0
@.str.8 = [23 x i8] c"mmap error for output\0A\00"
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 %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 3, %11
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %99
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %18
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %28)
store i32 -1, i32* %3
br label %99
30: ; preds = %18
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = load i32, i32* @O_RDWR
%35 = load i32, i32* @O_CREAT
%36 = or i32 %34, %35
%37 = load i32, i32* @O_TRUNC
%38 = or i32 %36, %37
%39 = call i32 @open(i8* %33, i32 %38)
store i32 %39, i32* %7
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %30
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 2
%44 = load i8*, i8** %43
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %44)
store i32 -1, i32* %3
br label %99
46: ; preds = %30
%47 = load i32, i32* %6
%48 = call i64 @fstat(i32 %47, %struct.stat* %10)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %99
52: ; preds = %46
%53 = load i32, i32* %7
%54 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%55 = load i64, i64* %54
%56 = sub nsw i64 %55, 1
%57 = load i32, i32* @SEEK_SET
%58 = call i32 @lseek(i32 %53, i64 %56, i32 %57)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %52
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %99
62: ; preds = %52
%63 = load i32, i32* %7
%64 = call i32 @write(i32 %63, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.5, i64 0, i64 0), i32 1)
%65 = icmp ne i32 %64, 1
br i1 %65, label %66, label %68
66: ; preds = %62
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %3
br label %99
68: ; preds = %62
%69 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%70 = load i64, i64* %69
%71 = load i32, i32* @PROT_READ
%72 = load i32, i32* @MAP_SHARED
%73 = load i32, i32* %6
%74 = call i8* @mmap(i32 0, i64 %70, i32 %71, i32 %72, i32 %73, i32 0)
store i8* %74, i8** %8
%75 = load i8*, i8** @MAP_FAILED
%76 = icmp eq i8* %74, %75
br i1 %76, label %77, label %79
77: ; preds = %68
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %3
br label %99
79: ; preds = %68
%80 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%81 = load i64, i64* %80
%82 = load i32, i32* @PROT_READ
%83 = load i32, i32* @PROT_WRITE
%84 = or i32 %82, %83
%85 = load i32, i32* @MAP_SHARED
%86 = load i32, i32* %7
%87 = call i8* @mmap(i32 0, i64 %81, i32 %84, i32 %85, i32 %86, i32 0)
store i8* %87, i8** %9
%88 = load i8*, i8** @MAP_FAILED
%89 = icmp eq i8* %87, %88
br i1 %89, label %90, label %92
90: ; preds = %79
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %3
br label %99
92: ; preds = %79
%93 = load i8*, i8** %9
%94 = load i8*, i8** %8
%95 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%96 = load i64, i64* %95
%97 = call i32 @memcpy(i8* %93, i8* %94, i64 %96)
%98 = call i32 @exit(i32 0)
unreachable
99: ; preds = %90, %77, %66, %60, %50, %41, %25, %13
%100 = load i32, i32* %3
ret i32 %100
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [31 x i8] c"usage: %s <fromfile> <tofile>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [29 x i8] c"can't create %s for reading\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [29 x i8] c"can't create %s for writing\0A\00"
@SEEK_SET = global i32 0
@.str.5 = [1 x i8] zeroinitializer
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@PROT_WRITE = global i32 0
@str = [22 x i8] c"mmap error for output\00"
@str.9 = [21 x i8] c"mmap error for input\00"
@str.10 = [12 x i8] c"write error\00"
@str.11 = [12 x i8] c"lseek error\00"
@str.12 = [12 x i8] c"fstat error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %71
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* %11, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = load i8*, i8** %10
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %71
18: ; preds = %9
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDWR
%22 = load i32, i32* @O_CREAT
%23 = or i32 %22, %21
%24 = load i32, i32* @O_TRUNC
%25 = or i32 %23, %24
%26 = tail call i32 @open(i8* %20, i32 %25)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %18
%29 = load i8*, i8** %19
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %29)
br label %71
31: ; preds = %18
%32 = call i64 @fstat(i32 %13, %struct.stat* %3)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.12, i64 0, i64 0))
br label %71
36: ; preds = %31
%37 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%38 = load i64, i64* %37
%39 = add nsw i64 %38, -1
%40 = load i32, i32* @SEEK_SET
%41 = tail call i32 @lseek(i32 %26, i64 %39, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.11, i64 0, i64 0))
br label %71
45: ; preds = %36
%46 = tail call i32 @write(i32 %26, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.5, i64 0, i64 0), i32 1)
%47 = icmp eq i32 %46, 1
br i1 %47, label %50, label %48
48: ; preds = %45
%49 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.10, i64 0, i64 0))
br label %71
50: ; preds = %45
%51 = load i32, i32* @PROT_READ
%52 = load i32, i32* @MAP_SHARED
%53 = tail call i8* @mmap(i32 0, i64 %38, i32 %51, i32 %52, i32 %13, i32 0)
%54 = load i8*, i8** @MAP_FAILED
%55 = icmp eq i8* %53, %54
br i1 %55, label %56, label %58
56: ; preds = %50
%57 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.9, i64 0, i64 0))
br label %71
58: ; preds = %50
%59 = load i32, i32* @PROT_READ
%60 = load i32, i32* @PROT_WRITE
%61 = or i32 %60, %59
%62 = load i32, i32* @MAP_SHARED
%63 = tail call i8* @mmap(i32 0, i64 %38, i32 %61, i32 %62, i32 %26, i32 0)
%64 = load i8*, i8** @MAP_FAILED
%65 = icmp eq i8* %63, %64
br i1 %65, label %66, label %68
66: ; preds = %58
%67 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %71
68: ; preds = %58
%69 = tail call i32 @memcpy(i8* %63, i8* %53, i64 %38)
%70 = tail call i32 @exit(i32 0)
unreachable
71: ; preds = %66, %56, %48, %43, %34, %28, %15, %6
ret i32 -1
}