#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 {
  size_t st_size;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ PROT_READ;
scalar_t__ close(int);
int /*<<< orphan*/ exit(int);
void *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
scalar_t__ munmap(void *, size_t);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int stat(char *, struct stat *);
scalar_t__ write(int, void *, size_t);
int main(int argc, char *argv[]) {
  int fdi, fdo;
  struct stat st;
  void *src;
  size_t size;
  ssize_t offset;
  if (argc != 3) {
    printf("E: Need 2 filenames as arguments.\n");
    return EXIT_FAILURE;
  }
  if ((fdi = open(argv[1], O_RDONLY)) < 0) {
    perror("E: Can not open input file");
    exit(EXIT_FAILURE);
  }
  if (stat(argv[1], &st) == -1) {
    perror("E: Can not stat input file");
    exit(EXIT_FAILURE);
  }
  size = st.st_size;
  src = mmap(NULL, size, PROT_READ, MAP_SHARED, fdi, 0);
  if (src == (void *)-1) {
    perror("E: Can not map input file");
    exit(EXIT_FAILURE);
  }
  if ((fdo = open(argv[2], O_CREAT | O_RDWR | O_TRUNC, 00666)) < 0) {
    perror("E: Can not open output file");
    exit(EXIT_FAILURE);
  }
  offset = write(fdo, src, size);
  if (offset == (ssize_t)-1) {
    perror("E: write() error");
    exit(EXIT_FAILURE);
  }
  if (munmap(src, size)) {
    perror("E: Can not unmap input file");
    exit(EXIT_FAILURE);
  }
  if (close(fdi)) {
    perror("E: Can not close input file");
    exit(EXIT_FAILURE);
  }
  if (close(fdo)) {
    perror("E: Can not close output file");
    exit(EXIT_FAILURE);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11cd <main+0x3d>
lea    0xe49(%rip),%rdi
call   1050 <printf@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13bf <main+0x22f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    120b <main+0x7b>
lea    0xe2b(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    123f <main+0xaf>
lea    0xe12(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
mov    0x2d96(%rip),%rax
mov    (%rax),%edx
mov    0x2d7d(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x28(%rbp)
jne    129c <main+0x10c>
lea    0xdd0(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
mov    0x2d14(%rip),%rax
or     (%rax),%esi
mov    0x2d33(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    12f1 <main+0x161>
lea    0xd95(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0xffffffffffffffff,-0x38(%rbp)
jne    132a <main+0x19a>
lea    0xd78(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c8d(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1080 <munmap@plt>
cmp    $0x0,%rax
je     135b <main+0x1cb>
lea    0xd58(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c5c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
je     1387 <main+0x1f7>
lea    0xd48(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c30(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
je     13b3 <main+0x223>
lea    0xd38(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c04(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2bee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    1284 <main+0xf4>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1090 <open@plt>
test   %eax,%eax
js     12a4 <main+0x114>
mov    %eax,%r14d
mov    0x8(%rbx),%rdi
mov    %rsp,%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     12ad <main+0x11d>
mov    (%rsp),%r15
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
mov    0x2ded(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     12b6 <main+0x126>
mov    %rax,%rbp
mov    0x2de7(%rip),%rax
mov    0x2db0(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%rbx),%rdi
mov    0x2dc9(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1090 <open@plt>
test   %eax,%eax
js     12bf <main+0x12f>
mov    %eax,%ebx
mov    %eax,%edi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1040 <write@plt>
cmp    $0xffffffffffffffff,%rax
je     12c8 <main+0x138>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1080 <munmap@plt>
test   %rax,%rax
jne    12d1 <main+0x141>
mov    %r14d,%edi
call   1060 <close@plt>
test   %rax,%rax
jne    12da <main+0x14a>
mov    %ebx,%edi
call   1060 <close@plt>
test   %rax,%rax
jne    12e3 <main+0x153>
mov    0x2d26(%rip),%rax
jmp    1297 <main+0x107>
lea    0xe47(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d19(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd55(%rip),%rdi
jmp    12ea <main+0x15a>
lea    0xd67(%rip),%rdi
jmp    12ea <main+0x15a>
lea    0xd79(%rip),%rdi
jmp    12ea <main+0x15a>
lea    0xd8a(%rip),%rdi
jmp    12ea <main+0x15a>
lea    0xd9d(%rip),%rdi
jmp    12ea <main+0x15a>
lea    0xda5(%rip),%rdi
jmp    12ea <main+0x15a>
lea    0xdb8(%rip),%rdi
jmp    12ea <main+0x15a>
lea    0xdcb(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cba(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [35 x i8] c"E: Need 2 filenames as arguments.\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [27 x i8] c"E: Can not open input file\00"
@.str.2 = [27 x i8] c"E: Can not stat input file\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [26 x i8] c"E: Can not map input file\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@.str.4 = [28 x i8] c"E: Can not open output file\00"
@.str.5 = [17 x i8] c"E: write() error\00"
@.str.6 = [28 x i8] c"E: Can not unmap input file\00"
@.str.7 = [28 x i8] c"E: Can not close input file\00"
@.str.8 = [29 x i8] c"E: Can not close output file\00"
@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 %struct.stat
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
store i32 %16, i32* %3
br label %105
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDONLY
%22 = call i32 (i8*, i32, ...) @open(i8* %20, i32 %21)
store i32 %22, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %17
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @stat(i8* %31, %struct.stat* %8)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %28
%39 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%40 = load i64, i64* %39
store i64 %40, i64* %10
%41 = load i64, i64* %10
%42 = load i32, i32* @PROT_READ
%43 = load i32, i32* @MAP_SHARED
%44 = load i32, i32* %6
%45 = call i8* @mmap(i32* null, i64 %41, i32 %42, i32 %43, i32 %44, i32 0)
store i8* %45, i8** %9
%46 = load i8*, i8** %9
%47 = icmp eq i8* %46, inttoptr (i64 -1 to i8*)
br i1 %47, label %48, label %52
48: ; preds = %38
%49 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %38
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 2
%55 = load i8*, i8** %54
%56 = load i32, i32* @O_CREAT
%57 = load i32, i32* @O_RDWR
%58 = or i32 %56, %57
%59 = load i32, i32* @O_TRUNC
%60 = or i32 %58, %59
%61 = call i32 (i8*, i32, ...) @open(i8* %55, i32 %60, i32 438)
store i32 %61, i32* %7
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %52
%64 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
%66 = call i32 @exit(i32 %65)
unreachable
67: ; preds = %52
%68 = load i32, i32* %7
%69 = load i8*, i8** %9
%70 = load i64, i64* %10
%71 = call i64 @write(i32 %68, i8* %69, i64 %70)
store i64 %71, i64* %11
%72 = load i64, i64* %11
%73 = icmp eq i64 %72, -1
br i1 %73, label %74, label %78
74: ; preds = %67
%75 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%76 = load i32, i32* @EXIT_FAILURE
%77 = call i32 @exit(i32 %76)
unreachable
78: ; preds = %67
%79 = load i8*, i8** %9
%80 = load i64, i64* %10
%81 = call i64 @munmap(i8* %79, i64 %80)
%82 = icmp ne i64 %81, 0
br i1 %82, label %83, label %87
83: ; preds = %78
%84 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%85 = load i32, i32* @EXIT_FAILURE
%86 = call i32 @exit(i32 %85)
unreachable
87: ; preds = %78
%88 = load i32, i32* %6
%89 = call i64 @close(i32 %88)
%90 = icmp ne i64 %89, 0
br i1 %90, label %91, label %95
91: ; preds = %87
%92 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0))
%93 = load i32, i32* @EXIT_FAILURE
%94 = call i32 @exit(i32 %93)
unreachable
95: ; preds = %87
%96 = load i32, i32* %7
%97 = call i64 @close(i32 %96)
%98 = icmp ne i64 %97, 0
br i1 %98, label %99, label %103
99: ; preds = %95
%100 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0))
%101 = load i32, i32* @EXIT_FAILURE
%102 = call i32 @exit(i32 %101)
unreachable
103: ; preds = %95
%104 = load i32, i32* @EXIT_SUCCESS
store i32 %104, i32* %3
br label %105
105: ; preds = %103, %14
%106 = load i32, i32* %3
ret i32 %106
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [27 x i8] c"E: Can not open input file\00"
@.str.2 = [27 x i8] c"E: Can not stat input file\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [26 x i8] c"E: Can not map input file\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@.str.4 = [28 x i8] c"E: Can not open output file\00"
@.str.5 = [17 x i8] c"E: write() error\00"
@.str.6 = [28 x i8] c"E: Can not unmap input file\00"
@.str.7 = [28 x i8] c"E: Can not close input file\00"
@.str.8 = [29 x i8] c"E: Can not close output file\00"
@EXIT_SUCCESS = global i32 0
@str = [34 x i8] c"E: Need 2 filenames as arguments.\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 %8, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
br label %79
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 (i8*, i32, ...) @open(i8* %10, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %8
%19 = load i8*, i8** %9
%20 = call i32 @stat(i8* %19, %struct.stat* %3)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %18
%27 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%28 = load i64, i64* %27
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @MAP_SHARED
%31 = tail call i8* @mmap(i32* null, i64 %28, i32 %29, i32 %30, i32 %12, i32 0)
%32 = icmp eq i8* %31, inttoptr (i64 -1 to i8*)
br i1 %32, label %33, label %37
33: ; preds = %26
%34 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = tail call i32 @exit(i32 %35)
unreachable
37: ; preds = %26
%38 = getelementptr inbounds i8*, i8** %1, i64 2
%39 = load i8*, i8** %38
%40 = load i32, i32* @O_CREAT
%41 = load i32, i32* @O_RDWR
%42 = or i32 %41, %40
%43 = load i32, i32* @O_TRUNC
%44 = or i32 %42, %43
%45 = tail call i32 (i8*, i32, ...) @open(i8* %39, i32 %44, i32 438)
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %37
%48 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%49 = load i32, i32* @EXIT_FAILURE
%50 = tail call i32 @exit(i32 %49)
unreachable
51: ; preds = %37
%52 = tail call i64 @write(i32 %45, i8* %31, i64 %28)
%53 = icmp eq i64 %52, -1
br i1 %53, label %54, label %58
54: ; preds = %51
%55 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%56 = load i32, i32* @EXIT_FAILURE
%57 = tail call i32 @exit(i32 %56)
unreachable
58: ; preds = %51
%59 = tail call i64 @munmap(i8* %31, i64 %28)
%60 = icmp eq i64 %59, 0
br i1 %60, label %65, label %61
61: ; preds = %58
%62 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%63 = load i32, i32* @EXIT_FAILURE
%64 = tail call i32 @exit(i32 %63)
unreachable
65: ; preds = %58
%66 = tail call i64 @close(i32 %12)
%67 = icmp eq i64 %66, 0
br i1 %67, label %72, label %68
68: ; preds = %65
%69 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0))
%70 = load i32, i32* @EXIT_FAILURE
%71 = tail call i32 @exit(i32 %70)
unreachable
72: ; preds = %65
%73 = tail call i64 @close(i32 %45)
%74 = icmp eq i64 %73, 0
br i1 %74, label %79, label %75
75: ; preds = %72
%76 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0))
%77 = load i32, i32* @EXIT_FAILURE
%78 = tail call i32 @exit(i32 %77)
unreachable
79: ; preds = %72, %6
%80 = phi i32* [ @EXIT_FAILURE, %6 ], [ @EXIT_SUCCESS, %72 ]
%81 = load i32, i32* %80
ret i32 %81
}