#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 /*<<< orphan*/ buff;

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_EXCL;
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, int *, int);
int /*<<< orphan*/ write(int, int *, int);
int main(int argc, char *argv[]) {
  int fd1, fd2, len, buff[20];
  if (argc != 3) {
    printf("error!");
    return -1;
  }
  if ((fd1 = open(argv[1], O_RDONLY)) < 0) {
    perror("open");
    return -1;
  }
  if ((fd2 = open(argv[2], O_WRONLY | O_CREAT | O_EXCL | O_APPEND, 0644)) < 0) {
    perror("create ");
    return -1;
  }
  while ((len = read(fd1, buff, sizeof(buff))) > 0) {
    write(fd2, buff, len);
  }
  close(fd1);
  close(fd2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1198 <main+0x38>
lea    0xe79(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127d <main+0x11d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11d4 <main+0x74>
lea    0xe44(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127d <main+0x11d>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    0x2e0c(%rip),%rax
or     (%rax),%esi
mov    0x2ddb(%rip),%rax
or     (%rax),%esi
mov    0x2dba(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1230 <main+0xd0>
lea    0xded(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127d <main+0x11d>
jmp    1235 <main+0xd5>
mov    -0x14(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x50,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    1266 <main+0x106>
mov    -0x18(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
jmp    1235 <main+0xd5>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
cmp    $0x3,%edi
jne    1223 <main+0xc3>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
js     123d <main+0xdd>
mov    %eax,%r14d
mov    0x2e26(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e24(%rip),%rax
or     (%rax),%esi
mov    0x10(%rbp),%rdi
mov    0x2dff(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
js     1246 <main+0xe6>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x50,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1210 <main+0xb0>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x50,%edx
call   1060 <read@plt>
test   %eax,%eax
jg     11f0 <main+0x90>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    1234 <main+0xd4>
lea    0xdd6(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc3(%rip),%rdi
jmp    124d <main+0xed>
lea    0xdbf(%rip),%rdi
call   1080 <perror@plt>
jmp    122f <main+0xcf>

LLVM IR (-O0):
@.str = [7 x i8] c"error!\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_APPEND = global i32 0
@.str.2 = [8 x i8] c"create \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 [20 x i32]
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 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %54
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDONLY
%19 = call i32 (i8*, i32, ...) @open(i8* %17, i32 %18)
store i32 %19, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %54
23: ; preds = %14
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_WRONLY
%28 = load i32, i32* @O_CREAT
%29 = or i32 %27, %28
%30 = load i32, i32* @O_EXCL
%31 = or i32 %29, %30
%32 = load i32, i32* @O_APPEND
%33 = or i32 %31, %32
%34 = call i32 (i8*, i32, ...) @open(i8* %26, i32 %33, i32 420)
store i32 %34, i32* %7
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %23
%37 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %54
38: ; preds = %23
br label %39
39: ; preds = %44, %38
%40 = load i32, i32* %6
%41 = getelementptr inbounds [20 x i32], [20 x i32]* %9, i64 0, i64 0
%42 = call i32 @read(i32 %40, i32* %41, i32 80)
store i32 %42, i32* %8
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %39
%45 = load i32, i32* %7
%46 = getelementptr inbounds [20 x i32], [20 x i32]* %9, i64 0, i64 0
%47 = load i32, i32* %8
%48 = call i32 @write(i32 %45, i32* %46, i32 %47)
br label %39
49: ; preds = %39
%50 = load i32, i32* %6
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %7
%53 = call i32 @close(i32 %52)
store i32 0, i32* %3
br label %54
54: ; preds = %49, %36, %21, %12
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@.str = [7 x i8] c"error!\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_APPEND = global i32 0
@.str.2 = [8 x i8] c"create \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [20 x i32]
%4 = bitcast [20 x i32]* %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %42
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 slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %42
16: ; preds = %8
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_WRONLY
%20 = load i32, i32* @O_CREAT
%21 = or i32 %20, %19
%22 = load i32, i32* @O_EXCL
%23 = or i32 %21, %22
%24 = load i32, i32* @O_APPEND
%25 = or i32 %23, %24
%26 = tail call i32 (i8*, i32, ...) @open(i8* %18, i32 %25, i32 420)
%27 = icmp slt i32 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %16
%29 = getelementptr inbounds [20 x i32], [20 x i32]* %3, i64 0, i64 0
%30 = call i32 @read(i32 %12, i32* %29, i32 80)
%31 = icmp sgt i32 %30, 0
br i1 %31, label %34, label %39
32: ; preds = %16
%33 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %42
34: ; preds = %28, %34
%35 = phi i32 [ %37, %34 ], [ %30, %28 ]
%36 = call i32 @write(i32 %26, i32* %29, i32 %35)
%37 = call i32 @read(i32 %12, i32* %29, i32 80)
%38 = icmp sgt i32 %37, 0
br i1 %38, label %34, label %39
39: ; preds = %34, %28
%40 = tail call i32 @close(i32 %12)
%41 = tail call i32 @close(i32 %26)
br label %42
42: ; preds = %39, %32, %14, %6
%43 = phi i32 [ -1, %6 ], [ -1, %14 ], [ -1, %32 ], [ 0, %39 ]
ret i32 %43
}