#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_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char **argv) {
  int fd1;
  int fd2;
  fd1 = open(argv[1], O_RDONLY);
  if (fd1 < 0) {
    perror("open");
    return -1;
  }
  fd2 = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, 0640);
  if (fd2 < 0) {
    perror("open");
    return -1;
  }
  char buff[1024] = {0};
  int ret;
  while (ret = read(fd1, buff, 1024)) {
    ret = write(fd2, buff, ret);
  }
  close(fd1);
  close(fd2);
  return 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)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b6 <main+0x56>
lea    0xe5b(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <main+0x117>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    0x2e22(%rip),%rax
or     (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
mov    $0x1a0,%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    120a <main+0xaa>
lea    0xe07(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <main+0x117>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x424(%rbp)
cmp    $0x0,%eax
je     1260 <main+0x100>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x424(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x424(%rbp)
jmp    121d <main+0xbd>
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    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
js     1230 <main+0xd0>
mov    %eax,%r15d
mov    0x2e2a(%rip),%rax
mov    0x2e53(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%rbx),%rdi
mov    0x2e4c(%rip),%rax
or     (%rax),%esi
mov    $0x1a0,%edx
xor    %eax,%eax
call   1070 <open@plt>
test   %eax,%eax
js     1230 <main+0xd0>
mov    %eax,%ebp
mov    %rsp,%rbx
xor    %r14d,%r14d
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
je     1210 <main+0xb0>
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
jne    11f0 <main+0x90>
mov    %r15d,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14d,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc9(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%r14d
jmp    121f <main+0xbf>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [1024 x i8]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = call i32 (i8*, i32, ...) @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_WRONLY
%24 = load i32, i32* @O_TRUNC
%25 = or i32 %23, %24
%26 = load i32, i32* @O_CREAT
%27 = or i32 %25, %26
%28 = call i32 (i8*, i32, ...) @open(i8* %22, i32 %27, i32 416)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %19
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
33: ; preds = %19
%34 = bitcast [1024 x i8]* %8 to i8*
br label %35
35: ; preds = %40, %33
%36 = load i32, i32* %6
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%38 = call i32 @read(i32 %36, i8* %37, i32 1024)
store i32 %38, i32* %9
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %35
%41 = load i32, i32* %7
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%43 = load i32, i32* %9
%44 = call i32 @write(i32 %41, i8* %42, i32 %43)
store i32 %44, i32* %9
br label %35
45: ; preds = %35
%46 = load i32, i32* %6
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
store i32 0, i32* %3
br label %50
50: ; preds = %45, %31, %17
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 (i8*, i32, ...) @open(i8* %5, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %35
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %15, %14
%17 = load i32, i32* @O_CREAT
%18 = or i32 %16, %17
%19 = tail call i32 (i8*, i32, ...) @open(i8* %13, i32 %18, i32 416)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %11
%22 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %35
23: ; preds = %11
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%25 = call i32 @read(i32 %7, i8* %24, i32 1024)
%26 = icmp eq i32 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %23, %27
%28 = phi i32 [ %30, %27 ], [ %25, %23 ]
%29 = call i32 @write(i32 %19, i8* %24, i32 %28)
%30 = call i32 @read(i32 %7, i8* %24, i32 1024)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %27
32: ; preds = %27, %23
%33 = tail call i32 @close(i32 %7)
%34 = tail call i32 @close(i32 %19)
br label %35
35: ; preds = %32, %21, %9
%36 = phi i32 [ -1, %9 ], [ -1, %21 ], [ 0, %32 ]
ret i32 %36
}