#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 /*<<< orphan*/ 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, 1024);
    if (wret == -1) {
      perror("write error");
      exit(4);
    }
    printf("loop iteration %d\n", iterations);
    iterations++;
  }
  close(sourcefile);
  close(destfile);
  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     12fc <main+0x18c>
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    $0x400,%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    12d4 <main+0x164>
lea    0xd8f(%rip),%rdi
call   1080 <perror@plt>
mov    $0x4,%edi
call   1090 <exit@plt>
mov    -0x424(%rbp),%esi
lea    0xd7f(%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    -0x18(%rbp),%edi
call   1050 <close@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    0xe8b(%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     128f <main+0x10f>
cmp    $0xffffffff,%eax
je     12a6 <main+0x126>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     12bc <main+0x13c>
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>
mov    %r12d,%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xd81(%rip),%rdi
call   1090 <perror@plt>
mov    $0x3,%edi
call   10a0 <exit@plt>
lea    0xd76(%rip),%rdi
call   1090 <perror@plt>
mov    $0x4,%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"
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
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 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 5)
unreachable
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 = load i32, i32* %6
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %17
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = load i32, i32* @O_WRONLY
%33 = load i32, i32* @O_CREAT
%34 = or i32 %32, %33
%35 = load i32, i32* @O_TRUNC
%36 = or i32 %34, %35
%37 = load i32, i32* @S_IRUSR
%38 = load i32, i32* @S_IWUSR
%39 = or i32 %37, %38
%40 = call i32 (i8*, i32, ...) @open(i8* %31, i32 %36, i32 %39)
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %28
%44 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @exit(i32 2)
unreachable
46: ; preds = %28
store i32 0, i32* %8
store i32 0, i32* %9
store i32 0, i32* %11
br label %47
47: ; preds = %67, %46
%48 = load i32, i32* %6
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%50 = call i32 @read(i32 %48, i8* %49, i32 1024)
store i32 %50, i32* %8
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %72
52: ; preds = %47
%53 = load i32, i32* %8
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%57 = call i32 @exit(i32 3)
unreachable
58: ; preds = %52
%59 = load i32, i32* %7
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%61 = call i32 @write(i32 %59, i8* %60, i32 1024)
store i32 %61, i32* %9
%62 = load i32, i32* %9
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %67
64: ; preds = %58
%65 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%66 = call i32 @exit(i32 4)
unreachable
67: ; preds = %58
%68 = load i32, i32* %11
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %68)
%70 = load i32, i32* %11
%71 = add nsw i32 %70, 1
store i32 %71, i32* %11
br label %47
72: ; preds = %47
%73 = load i32, i32* %6
%74 = call i32 @close(i32 %73)
%75 = load i32, i32* %7
%76 = call i32 @close(i32 %75)
%77 = 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 = [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 1024)
%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 = tail call i32 @close(i32 %28)
%53 = tail call i32 @exit(i32 0)
unreachable
}