#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 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) {
  int fol1, fol2;
  int cpy, wri;
  char buffer[256];
  char *ptr;
  if (argc != 3) {
    printf("args too less\n");
    exit(1);
  }
  fol1 = open(argv[1], O_RDONLY);
  if (fol1 == -1) {
    perror("open file1 fail\n");
    exit(1);
  }
  fol2 = open(argv[2], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
  if (fol2 == -1) {
    perror("open file2 fail\n");
    exit(1);
  }
  while ((cpy = read(fol1, buffer, 256)) != 0) {
    if (cpy == -1) {
      perror("read fail\n");
      break;
    } else if (cpy > 0) {
      ptr = buffer;
      while ((wri = write(fol2, ptr, wri)) != 0) {
        if (wri == -1) {
          perror("write fail\n");
          exit(1);
        } else if (wri == cpy)
          break;
        else if (wri > 0) {
          ptr += wri;
          cpy -= wri;
        }
      }
      if (wri == -1)
        break;
    }
  }
  close(fol1);
  close(fol2);
  printf("done!\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%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    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e28(%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    0xe3a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
mov    0x2dfc(%rip),%rax
or     (%rax),%esi
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
mov    0x2dca(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    123a <main+0xca>
lea    0xdf5(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    123f <main+0xcf>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     133a <main+0x1ca>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    127a <main+0x10a>
lea    0xdc1(%rip),%rdi
call   1080 <perror@plt>
jmp    133a <main+0x1ca>
cmpl   $0x0,-0x1c(%rbp)
jle    1330 <main+0x1c0>
lea    -0x120(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x128(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     131c <main+0x1ac>
cmpl   $0xffffffff,-0x20(%rbp)
jne    12d0 <main+0x160>
lea    0xd7b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    12e1 <main+0x171>
jmp    131c <main+0x1ac>
cmpl   $0x0,-0x20(%rbp)
jle    130d <main+0x19d>
mov    -0x20(%rbp),%ecx
mov    -0x128(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x128(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    1312 <main+0x1a2>
jmp    1317 <main+0x1a7>
jmp    1292 <main+0x122>
cmpl   $0xffffffff,-0x20(%rbp)
jne    132b <main+0x1bb>
jmp    133a <main+0x1ca>
jmp    1330 <main+0x1c0>
jmp    1335 <main+0x1c5>
jmp    123f <main+0xcf>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
lea    0xcf7(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
cmp    $0x3,%edi
jne    12a5 <main+0x135>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     12bb <main+0x14b>
mov    %eax,%r12d
mov    0x10(%rbx),%rdi
mov    0x2e00(%rip),%rax
mov    0x2e39(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dfe(%rip),%rax
mov    0x2e07(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     12c4 <main+0x154>
mov    %eax,%r14d
mov    %rsp,%r15
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x100,%edx
call   1060 <read@plt>
test   %eax,%eax
je     125f <main+0xef>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1253 <main+0xe3>
test   %ebp,%ebp
jle    11f0 <main+0x80>
mov    %r15,%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %r13,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11f0 <main+0x80>
cmp    $0xffffffff,%ebx
je     128f <main+0x11f>
cmp    %ebx,%ebp
je     124f <main+0xdf>
test   %ebx,%ebx
mov    %ebx,%eax
mov    $0x0,%ecx
cmovs  %ecx,%eax
sub    %eax,%ebp
add    %rax,%r13
jmp    1220 <main+0xb0>
mov    %ebp,%ebx
jmp    11f0 <main+0x80>
lea    0xdc8(%rip),%rdi
call   1080 <perror@plt>
mov    %r12d,%edi
call   1050 <close@plt>
mov    %r14d,%edi
call   1050 <close@plt>
lea    0xdc3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd97(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xd93(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xd3e(%rip),%rdi
jmp    1296 <main+0x126>
lea    0xd46(%rip),%rdi
jmp    1296 <main+0x126>

LLVM IR (-O0):
@.str = [15 x i8] c"args too less\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [17 x i8] c"open file1 fail\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [17 x i8] c"open file2 fail\0A\00"
@.str.3 = [11 x i8] c"read fail\0A\00"
@.str.4 = [12 x i8] c"write fail\0A\00"
@.str.5 = [7 x i8] c"done!\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 [256 x i8]
%11 = alloca i8*
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 ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
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 ([17 x i8], [17 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* @S_IRUSR
%36 = load i32, i32* @S_IWUSR
%37 = or i32 %35, %36
%38 = call i32 (i8*, i32, ...) @open(i8* %31, i32 %34, i32 %37)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %28
%42 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %28
br label %45
45: ; preds = %97, %44
%46 = load i32, i32* %6
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%48 = call i32 @read(i32 %46, i8* %47, i32 256)
store i32 %48, i32* %8
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %98
50: ; preds = %45
%51 = load i32, i32* %8
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %98
55: ; preds = %50
%56 = load i32, i32* %8
%57 = icmp sgt i32 %56, 0
br i1 %57, label %58, label %96
58: ; preds = %55
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
store i8* %59, i8** %11
br label %60
60: ; preds = %90, %58
%61 = load i32, i32* %7
%62 = load i8*, i8** %11
%63 = load i32, i32* %9
%64 = call i32 @write(i32 %61, i8* %62, i32 %63)
store i32 %64, i32* %9
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %91
66: ; preds = %60
%67 = load i32, i32* %9
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %72
69: ; preds = %66
%70 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%71 = call i32 @exit(i32 1)
unreachable
72: ; preds = %66
%73 = load i32, i32* %9
%74 = load i32, i32* %8
%75 = icmp eq i32 %73, %74
br i1 %75, label %76, label %77
76: ; preds = %72
br label %91
77: ; preds = %72
%78 = load i32, i32* %9
%79 = icmp sgt i32 %78, 0
br i1 %79, label %80, label %88
80: ; preds = %77
%81 = load i32, i32* %9
%82 = load i8*, i8** %11
%83 = sext i32 %81 to i64
%84 = getelementptr inbounds i8, i8* %82, i64 %83
store i8* %84, i8** %11
%85 = load i32, i32* %9
%86 = load i32, i32* %8
%87 = sub nsw i32 %86, %85
store i32 %87, i32* %8
br label %88
88: ; preds = %80, %77
br label %89
89: ; preds = %88
br label %90
90: ; preds = %89
br label %60
91: ; preds = %76, %60
%92 = load i32, i32* %9
%93 = icmp eq i32 %92, -1
br i1 %93, label %94, label %95
94: ; preds = %91
br label %98
95: ; preds = %91
br label %96
96: ; preds = %95, %55
br label %97
97: ; preds = %96
br label %45
98: ; preds = %94, %53, %45
%99 = load i32, i32* %6
%100 = call i32 @close(i32 %99)
%101 = load i32, i32* %7
%102 = call i32 @close(i32 %101)
%103 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str.1 = [17 x i8] c"open file1 fail\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [17 x i8] c"open file2 fail\0A\00"
@.str.3 = [11 x i8] c"read fail\0A\00"
@.str.4 = [12 x i8] c"write fail\0A\00"
@str = [6 x i8] c"done!\00"
@str.6 = [14 x i8] c"args too less\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 3
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.6, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 (i8*, i32, ...) @open(i8* %11, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %9
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_WRONLY
%22 = load i32, i32* @O_CREAT
%23 = or i32 %22, %21
%24 = load i32, i32* @S_IRUSR
%25 = load i32, i32* @S_IWUSR
%26 = or i32 %25, %24
%27 = tail call i32 (i8*, i32, ...) @open(i8* %20, i32 %23, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %34
29: ; preds = %18
%30 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %50, %42
%33 = phi i32 [ %43, %50 ], [ %46, %42 ]
br label %34
34: ; preds = %18, %32
%35 = phi i32 [ %33, %32 ], [ undef, %18 ]
br label %36
36: ; preds = %34, %40
%37 = call i32 @read(i32 %13, i8* %4, i32 256)
switch i32 %37, label %40 [
i32 0, label %58
i32 -1, label %38
]
38: ; preds = %36
%39 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %58
40: ; preds = %36
%41 = icmp sgt i32 %37, 0
br i1 %41, label %42, label %36
42: ; preds = %40, %52
%43 = phi i32 [ %55, %52 ], [ %37, %40 ]
%44 = phi i32 [ %46, %52 ], [ %35, %40 ]
%45 = phi i8* [ %57, %52 ], [ %4, %40 ]
%46 = call i32 @write(i32 %27, i8* %45, i32 %44)
switch i32 %46, label %50 [
i32 -1, label %47
i32 0, label %32
]
47: ; preds = %42
%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 1)
unreachable
50: ; preds = %42
%51 = icmp eq i32 %43, %46
br i1 %51, label %32, label %52
52: ; preds = %50
%53 = icmp sgt i32 %46, 0
%54 = select i1 %53, i32 %46, i32 0
%55 = sub nsw i32 %43, %54
%56 = zext i32 %54 to i64
%57 = getelementptr i8, i8* %45, i64 %56
br label %42
58: ; preds = %36, %38
%59 = tail call i32 @close(i32 %13)
%60 = tail call i32 @close(i32 %27)
%61 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
ret i32 0
}