#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 */
typedef int ssize_t;

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char **argv) {
  if (argc != 3) {
    printf("wrong number of arguments\n");
    exit(5);
  }
  int sourcefile = open(argv[1], O_RDONLY);
  if (sourcefile == -1) {
    perror("open source file failed");
    exit(1);
  }
  int destfile = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  if (destfile == -1) {
    perror("open dest file failed");
    exit(2);
  }
  ssize_t rret = 0;
  char buf[1024];
  while ((rret = read(sourcefile, buf, 1024))) {
    if (rret == -1) {
      perror("read error");
      exit(3);
    }
    ssize_t wret = 0;
    ssize_t wcount = 0;
    do {
      wret = write(destfile, &buf[wcount], rret - wcount);
      if (wret == -1) {
        perror("write error");
        exit(4);
      }
      wcount += wret;
    } while (wcount < rret);
  }
  int scret = close(sourcefile);
  if (scret == -1) {
    perror("closing source");
    exit(6);
  }
  int dcret = close(destfile);
  if (dcret == -1) {
    perror("closing copy");
    exit(7);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11a9 <main+0x39>
lea    0xe66(%rip),%rdi
call   1040 <printf@plt>
mov    $0x5,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e4 <main+0x74>
lea    0xe46(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    0x2dfc(%rip),%rax
or     (%rax),%esi
mov    0x2deb(%rip),%rax
or     (%rax),%esi
mov    0x2db2(%rip),%rax
mov    (%rax),%edx
mov    0x2db9(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1243 <main+0xd3>
lea    0xdff(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     130f <main+0x19f>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    128a <main+0x11a>
lea    0xdce(%rip),%rdi
call   1080 <perror@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
movl   $0x0,-0x424(%rbp)
movl   $0x0,-0x428(%rbp)
mov    -0x18(%rbp),%edi
movslq -0x428(%rbp),%rax
lea    -0x420(%rbp),%rsi
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
sub    -0x428(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0xffffffff,-0x424(%rbp)
jne    12e9 <main+0x179>
lea    0xd7a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x4,%edi
call   1090 <exit@plt>
mov    -0x424(%rbp),%eax
add    -0x428(%rbp),%eax
mov    %eax,-0x428(%rbp)
mov    -0x428(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jl     129e <main+0x12e>
jmp    124a <main+0xda>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x42c(%rbp)
cmpl   $0xffffffff,-0x42c(%rbp)
jne    1340 <main+0x1d0>
lea    0xd2f(%rip),%rdi
call   1080 <perror@plt>
mov    $0x6,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x430(%rbp)
cmpl   $0xffffffff,-0x430(%rbp)
jne    1371 <main+0x201>
lea    0xd0d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x7,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
cmp    $0x3,%edi
jne    11b6 <main+0x46>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
jne    11cc <main+0x5c>
lea    0xe59(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%edi
call   1090 <exit@plt>
mov    %eax,%r15d
mov    0x10(%rbx),%rdi
mov    0x2dd6(%rip),%rax
mov    0x2e17(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e04(%rip),%rax
or     (%rax),%esi
mov    0x2dcb(%rip),%rax
mov    0x2dd4(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
jne    1222 <main+0xb2>
lea    0xe05(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    %eax,%r12d
mov    %rsp,%r14
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     1286 <main+0x116>
mov    %eax,%ebx
test   %eax,%eax
je     129c <main+0x12c>
xor    %ebp,%ebp
nopl   (%rax)
movslq %ebp,%rax
lea    (%rsp,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %r12d,%edi
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1270 <main+0x100>
add    %eax,%ebp
cmp    %ebx,%ebp
jl     1250 <main+0xe0>
jmp    1230 <main+0xc0>
lea    0xdc2(%rip),%rdi
call   1080 <perror@plt>
mov    $0x4,%edi
call   1090 <exit@plt>
lea    0xda1(%rip),%rdi
call   1080 <perror@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
mov    %r15d,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    12bf <main+0x14f>
lea    0xd95(%rip),%rdi
call   1080 <perror@plt>
mov    $0x6,%edi
call   1090 <exit@plt>
mov    %r12d,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    12e2 <main+0x172>
lea    0xd81(%rip),%rdi
call   1080 <perror@plt>
mov    $0x7,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"wrong number of arguments\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [24 x i8] c"open source file failed\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [22 x i8] c"open dest file failed\00"
@.str.3 = [11 x i8] c"read error\00"
@.str.4 = [12 x i8] c"write error\00"
@.str.5 = [15 x i8] c"closing source\00"
@.str.6 = [13 x i8] c"closing copy\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 i32
%9 = alloca [1024 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 5)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDONLY
%24 = call i32 (i8*, i32, ...) @open(i8* %22, i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %19
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = load i32, i32* @O_WRONLY
%35 = load i32, i32* @O_CREAT
%36 = or i32 %34, %35
%37 = load i32, i32* @O_TRUNC
%38 = or i32 %36, %37
%39 = load i32, i32* @S_IRUSR
%40 = load i32, i32* @S_IWUSR
%41 = or i32 %39, %40
%42 = call i32 (i8*, i32, ...) @open(i8* %33, i32 %38, i32 %41)
store i32 %42, i32* %7
%43 = load i32, i32* %7
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %30
%46 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%47 = call i32 @exit(i32 2)
unreachable
48: ; preds = %30
store i32 0, i32* %8
br label %49
49: ; preds = %83, %48
%50 = load i32, i32* %6
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%52 = call i32 @read(i32 %50, i8* %51, i32 1024)
store i32 %52, i32* %8
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %84
54: ; preds = %49
%55 = load i32, i32* %8
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%59 = call i32 @exit(i32 3)
unreachable
60: ; preds = %54
store i32 0, i32* %10
store i32 0, i32* %11
br label %61
61: ; preds = %79, %60
%62 = load i32, i32* %7
%63 = load i32, i32* %11
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 %64
%66 = load i32, i32* %8
%67 = load i32, i32* %11
%68 = sub nsw i32 %66, %67
%69 = call i32 @write(i32 %62, i8* %65, i32 %68)
store i32 %69, i32* %10
%70 = load i32, i32* %10
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %75
72: ; preds = %61
%73 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%74 = call i32 @exit(i32 4)
unreachable
75: ; preds = %61
%76 = load i32, i32* %10
%77 = load i32, i32* %11
%78 = add nsw i32 %77, %76
store i32 %78, i32* %11
br label %79
79: ; preds = %75
%80 = load i32, i32* %11
%81 = load i32, i32* %8
%82 = icmp slt i32 %80, %81
br i1 %82, label %61, label %83
83: ; preds = %79
br label %49
84: ; preds = %49
%85 = load i32, i32* %6
%86 = call i32 @close(i32 %85)
store i32 %86, i32* %12
%87 = load i32, i32* %12
%88 = icmp eq i32 %87, -1
br i1 %88, label %89, label %92
89: ; preds = %84
%90 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%91 = call i32 @exit(i32 6)
unreachable
92: ; preds = %84
%93 = load i32, i32* %7
%94 = call i32 @close(i32 %93)
store i32 %94, i32* %13
%95 = load i32, i32* %13
%96 = icmp eq i32 %95, -1
br i1 %96, label %97, label %100
97: ; preds = %92
%98 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%99 = call i32 @exit(i32 7)
unreachable
100: ; preds = %92
%101 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str.1 = [24 x i8] c"open source file failed\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [22 x i8] c"open dest file failed\00"
@.str.3 = [11 x i8] c"read error\00"
@.str.4 = [12 x i8] c"write error\00"
@.str.5 = [15 x i8] c"closing source\00"
@.str.6 = [13 x i8] c"closing copy\00"
@str = [26 x i8] c"wrong number of arguments\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = icmp eq i32 %0, 3
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 5)
unreachable
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 eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %8
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_WRONLY
%21 = load i32, i32* @O_CREAT
%22 = or i32 %21, %20
%23 = load i32, i32* @O_TRUNC
%24 = or i32 %22, %23
%25 = load i32, i32* @S_IRUSR
%26 = load i32, i32* @S_IWUSR
%27 = or i32 %26, %25
%28 = tail call i32 (i8*, i32, ...) @open(i8* %19, i32 %24, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %17
%31 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%32 = tail call i32 @exit(i32 2)
unreachable
33: ; preds = %17
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
br label %35
35: ; preds = %50, %33
%36 = call i32 @read(i32 %12, i8* %34, i32 1024)
switch i32 %36, label %40 [
i32 0, label %53
i32 -1, label %37
]
37: ; preds = %35
%38 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%39 = tail call i32 @exit(i32 3)
unreachable
40: ; preds = %35, %50
%41 = phi i32 [ %51, %50 ], [ 0, %35 ]
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %42
%44 = sub nsw i32 %36, %41
%45 = call i32 @write(i32 %28, i8* %43, i32 %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %50
47: ; preds = %40
%48 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%49 = tail call i32 @exit(i32 4)
unreachable
50: ; preds = %40
%51 = add nsw i32 %45, %41
%52 = icmp slt i32 %51, %36
br i1 %52, label %40, label %35
53: ; preds = %35
%54 = tail call i32 @close(i32 %12)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%58 = tail call i32 @exit(i32 6)
unreachable
59: ; preds = %53
%60 = tail call i32 @close(i32 %28)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %65
62: ; preds = %59
%63 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%64 = tail call i32 @exit(i32 7)
unreachable
65: ; preds = %59
%66 = tail call i32 @exit(i32 0)
unreachable
}