#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;
  ssize_t wret = 0;
  char buf[1024];
  int iterations = 0;
  while ((rret = read(sourcefile, buf, 1024))) {
    if (rret == -1) {
      perror("read error");
      exit(3);
    }
    wret = write(destfile, buf, rret);
    if (wret == -1) {
      perror("write error");
      exit(4);
    }
    printf("loop iteration %d\n", iterations);
    iterations++;
  }
  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     11ab <main+0x3b>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x5,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e6 <main+0x76>
lea    0xe44(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    0x2dfa(%rip),%rax
or     (%rax),%esi
mov    0x2de9(%rip),%rax
or     (%rax),%esi
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
mov    0x2db7(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1245 <main+0xd5>
lea    0xdfd(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x424(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     12fa <main+0x18a>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    129d <main+0x12d>
lea    0xdbb(%rip),%rdi
call   1080 <perror@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    12d2 <main+0x162>
lea    0xd91(%rip),%rdi
call   1080 <perror@plt>
mov    $0x4,%edi
call   1090 <exit@plt>
mov    -0x424(%rbp),%esi
lea    0xd81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
jmp    125d <main+0xed>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x428(%rbp)
cmpl   $0xffffffff,-0x428(%rbp)
jne    132b <main+0x1bb>
lea    0xd57(%rip),%rdi
call   1080 <perror@plt>
mov    $0x6,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x42c(%rbp)
cmpl   $0xffffffff,-0x42c(%rbp)
jne    135c <main+0x1ec>
lea    0xd35(%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    11c6 <main+0x46>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
jne    11dc <main+0x5c>
lea    0xe49(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xea7(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%edi
call   10a0 <exit@plt>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
mov    0x2dc6(%rip),%rax
mov    0x2e07(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df4(%rip),%rax
or     (%rax),%esi
mov    0x2dbb(%rip),%rax
mov    0x2dc4(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
xor    %eax,%eax
call   1080 <open@plt>
cmp    $0xffffffff,%eax
jne    1232 <main+0xb2>
lea    0xdf5(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    %eax,%r12d
mov    %rsp,%rbx
lea    0xe06(%rip),%r15
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %eax,%eax
je     128c <main+0x10c>
cmp    $0xffffffff,%eax
je     12af <main+0x12f>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     12c5 <main+0x145>
mov    %r15,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
jmp    1250 <main+0xd0>
mov    %r14d,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    12db <main+0x15b>
lea    0xdb8(%rip),%rdi
call   1090 <perror@plt>
mov    $0x6,%edi
call   10a0 <exit@plt>
lea    0xd78(%rip),%rdi
call   1090 <perror@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>
lea    0xd6d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x4,%edi
call   10a0 <exit@plt>
mov    %r12d,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    12fe <main+0x17e>
lea    0xd78(%rip),%rdi
call   1090 <perror@plt>
mov    $0x7,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   10a0 <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 = [19 x i8] c"loop iteration %d\0A\00"
@.str.6 = [15 x i8] c"closing source\00"
@.str.7 = [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 i32
%10 = alloca [1024 x i8]
%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 (i8*, ...) @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
store i32 0, i32* %9
store i32 0, i32* %11
br label %49
49: ; preds = %70, %48
%50 = load i32, i32* %6
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, 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 %75
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
%61 = load i32, i32* %7
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%63 = load i32, i32* %8
%64 = call i32 @write(i32 %61, i8* %62, i32 %63)
store i32 %64, i32* %9
%65 = load i32, i32* %9
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %70
67: ; preds = %60
%68 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%69 = call i32 @exit(i32 4)
unreachable
70: ; preds = %60
%71 = load i32, i32* %11
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %71)
%73 = load i32, i32* %11
%74 = add nsw i32 %73, 1
store i32 %74, i32* %11
br label %49
75: ; preds = %49
%76 = load i32, i32* %6
%77 = call i32 @close(i32 %76)
store i32 %77, i32* %12
%78 = load i32, i32* %12
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %83
80: ; preds = %75
%81 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%82 = call i32 @exit(i32 6)
unreachable
83: ; preds = %75
%84 = load i32, i32* %7
%85 = call i32 @close(i32 %84)
store i32 %85, i32* %13
%86 = load i32, i32* %13
%87 = icmp eq i32 %86, -1
br i1 %87, label %88, label %91
88: ; preds = %83
%89 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%90 = call i32 @exit(i32 7)
unreachable
91: ; preds = %83
%92 = 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 = [19 x i8] c"loop iteration %d\0A\00"
@.str.6 = [15 x i8] c"closing source\00"
@.str.7 = [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 = %47, %33
%36 = phi i32 [ 0, %33 ], [ %49, %47 ]
%37 = call i32 @read(i32 %12, i8* %34, i32 1024)
switch i32 %37, label %41 [
i32 0, label %50
i32 -1, label %38
]
38: ; preds = %35
%39 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%40 = tail call i32 @exit(i32 3)
unreachable
41: ; preds = %35
%42 = call i32 @write(i32 %28, i8* %34, i32 %37)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%46 = tail call i32 @exit(i32 4)
unreachable
47: ; preds = %41
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %36)
%49 = add nuw nsw i32 %36, 1
br label %35
50: ; preds = %35
%51 = tail call i32 @close(i32 %12)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%55 = tail call i32 @exit(i32 6)
unreachable
56: ; preds = %50
%57 = tail call i32 @close(i32 %28)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%61 = tail call i32 @exit(i32 7)
unreachable
62: ; preds = %56
%63 = tail call i32 @exit(i32 0)
unreachable
}