#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 */

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(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("useage: copy srcfile dstfile\n");
    return 1;
  }
  printf("argv[0]=%s\n", argv[0]);
  printf("argv[1]=%s\n", argv[1]);
  printf("argv[2]=%s\n", argv[2]);
  printf("cp start.\n");
  int srcfd = open(argv[1], O_RDONLY);
  if (srcfd == -1) {
    printf("open %s fail.\n", argv[1]);
  }
  int dstfd = open(argv[2], O_CREAT | O_WRONLY, 0666);
  if (dstfd == -1) {
    printf("open %s fail.\n", argv[2]);
    return 1;
  }
  int len = 0;
  char buffer[4096] = {0};
  while ((len = read(srcfd, buffer, 4096)) > 0) {
    if (write(dstfd, buffer, len) != len) {
      printf("write error.\n");
      return 1;
    }
  }
  if (len < 0) {
    perror("read error.\n");
    return 1;
  }
  close(srcfd);
  close(dstfd);
  printf("cp finish.\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11ad <main+0x3d>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1347 <main+0x1d7>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1237 <main+0xc7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
mov    0x2d79(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    128c <main+0x11c>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1347 <main+0x1d7>
movl   $0x0,-0x1c(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    1300 <main+0x190>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
cmp    -0x1c(%rbp),%eax
je     12fb <main+0x18b>
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1347 <main+0x1d7>
jmp    12a6 <main+0x136>
cmpl   $0x0,-0x1c(%rbp)
jge    1322 <main+0x1b2>
lea    0xd59(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1347 <main+0x1d7>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
lea    0xd3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
cmp    $0x3,%edi
jne    1280 <main+0x100>
mov    %rsi,%r14
mov    (%rsi),%rsi
lea    0xe5b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%r14),%rsi
lea    0xe55(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%r14),%rsi
lea    0xe4f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe69(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%r14),%rdi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    120a <main+0x8a>
mov    0x8(%r14),%rsi
lea    0xe21(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%r14),%rdi
mov    0x2de3(%rip),%rax
mov    0x2dac(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1293 <main+0x113>
mov    %eax,%r12d
mov    %rsp,%rbp
mov    $0x1000,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    12ac <main+0x12c>
mov    %eax,%ebx
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    %ebx,%eax
je     1250 <main+0xd0>
lea    0xdd7(%rip),%rdi
jmp    1287 <main+0x107>
lea    0xddb(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    12cc <main+0x14c>
mov    0x10(%r14),%rsi
lea    0xd86(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
jmp    12cc <main+0x14c>
js     12dc <main+0x15c>
mov    %r15d,%edi
call   1070 <close@plt>
mov    %r12d,%edi
call   1070 <close@plt>
lea    0xd85(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd50(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%eax
jmp    12cc <main+0x14c>

LLVM IR (-O0):
@.str = [30 x i8] c"useage: copy srcfile dstfile\0A\00"
@.str.1 = [12 x i8] c"argv[0]=%s\0A\00"
@.str.2 = [12 x i8] c"argv[1]=%s\0A\00"
@.str.3 = [12 x i8] c"argv[2]=%s\0A\00"
@.str.4 = [11 x i8] c"cp start.\0A\00"
@O_RDONLY = global i32 0
@.str.5 = [15 x i8] c"open %s fail.\0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.6 = [14 x i8] c"write error.\0A\00"
@.str.7 = [13 x i8] c"read error.\0A\00"
@.str.8 = [12 x i8] c"cp finish.\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 [4096 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %83
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %25)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = load i32, i32* @O_RDONLY
%32 = call i32 (i8*, i32, ...) @open(i8* %30, i32 %31)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %40
35: ; preds = %14
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %35, %14
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 2
%43 = load i8*, i8** %42
%44 = load i32, i32* @O_CREAT
%45 = load i32, i32* @O_WRONLY
%46 = or i32 %44, %45
%47 = call i32 (i8*, i32, ...) @open(i8* %43, i32 %46, i32 438)
store i32 %47, i32* %7
%48 = load i32, i32* %7
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %55
50: ; preds = %40
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 2
%53 = load i8*, i8** %52
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %53)
store i32 1, i32* %3
br label %83
55: ; preds = %40
store i32 0, i32* %8
%56 = bitcast [4096 x i8]* %9 to i8*
br label %57
57: ; preds = %71, %55
%58 = load i32, i32* %6
%59 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%60 = call i32 @read(i32 %58, i8* %59, i32 4096)
store i32 %60, i32* %8
%61 = icmp sgt i32 %60, 0
br i1 %61, label %62, label %72
62: ; preds = %57
%63 = load i32, i32* %7
%64 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%65 = load i32, i32* %8
%66 = call i32 @write(i32 %63, i8* %64, i32 %65)
%67 = load i32, i32* %8
%68 = icmp ne i32 %66, %67
br i1 %68, label %69, label %71
69: ; preds = %62
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %83
71: ; preds = %62
br label %57
72: ; preds = %57
%73 = load i32, i32* %8
%74 = icmp slt i32 %73, 0
br i1 %74, label %75, label %77
75: ; preds = %72
%76 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %83
77: ; preds = %72
%78 = load i32, i32* %6
%79 = call i32 @close(i32 %78)
%80 = load i32, i32* %7
%81 = call i32 @close(i32 %80)
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %3
br label %83
83: ; preds = %77, %75, %69, %50, %12
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
@.str.1 = [12 x i8] c"argv[0]=%s\0A\00"
@.str.2 = [12 x i8] c"argv[1]=%s\0A\00"
@.str.3 = [12 x i8] c"argv[2]=%s\0A\00"
@O_RDONLY = global i32 0
@.str.5 = [15 x i8] c"open %s fail.\0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.7 = [13 x i8] c"read error.\0A\00"
@str = [10 x i8] c"cp start.\00"
@str.9 = [11 x i8] c"cp finish.\00"
@str.10 = [13 x i8] c"write error.\00"
@str.11 = [29 x i8] c"useage: copy srcfile dstfile\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = icmp eq i32 %0, 3
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.11, i64 0, i64 0))
br label %54
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %14)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%17 = load i8*, i8** %10
%18 = load i32, i32* @O_RDONLY
%19 = tail call i32 (i8*, i32, ...) @open(i8* %17, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %7
%22 = load i8*, i8** %10
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %21, %7
%25 = load i8*, i8** %13
%26 = load i32, i32* @O_CREAT
%27 = load i32, i32* @O_WRONLY
%28 = or i32 %27, %26
%29 = tail call i32 (i8*, i32, ...) @open(i8* %25, i32 %28, i32 438)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = load i8*, i8** %13
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %32)
br label %54
34: ; preds = %24
%35 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
br label %36
36: ; preds = %39, %34
%37 = call i32 @read(i32 %19, i8* %35, i32 4096)
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %36
%40 = call i32 @write(i32 %29, i8* %35, i32 %37)
%41 = icmp eq i32 %40, %37
br i1 %41, label %36, label %42
42: ; preds = %39
%43 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.10, i64 0, i64 0))
br label %52
44: ; preds = %36
%45 = icmp slt i32 %37, 0
br i1 %45, label %46, label %48
46: ; preds = %44
%47 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
br label %52
48: ; preds = %44
%49 = tail call i32 @close(i32 %19)
%50 = tail call i32 @close(i32 %29)
%51 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.9, i64 0, i64 0))
br label %52
52: ; preds = %48, %46, %42
%53 = phi i32 [ 1, %42 ], [ 1, %46 ], [ 0, %48 ]
br label %54
54: ; preds = %31, %52, %5
%55 = phi i32 [ 1, %5 ], [ 1, %31 ], [ %53, %52 ]
ret i32 %55
}