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

/* Variables and functions */
int O_CREAT;
int O_NONBLOCK;
int O_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int copy(char *src, char *dest) {
  int inF, ouF;
  char line[512];
  int bytes;
  if ((inF = open(src, O_RDONLY)) == -1) {
    return 1;
  }
  if ((ouF = open(dest, O_WRONLY | O_CREAT | O_NONBLOCK)) == -1) {
    return 1;
  }
  while ((bytes = read(inF, line, sizeof(line))) > 0)
    write(ouF, line, bytes);
  close(inF);
  close(ouF);
  return 0;
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    117d <copy+0x3d>
movl   $0x1,-0x4(%rbp)
jmp    1212 <copy+0xd2>
mov    -0x18(%rbp),%rdi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    0x2e67(%rip),%rax
or     (%rax),%esi
mov    0x2e46(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    11b9 <copy+0x79>
movl   $0x1,-0x4(%rbp)
jmp    1212 <copy+0xd2>
jmp    11be <copy+0x7e>
mov    -0x1c(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %eax,-0x224(%rbp)
cmp    $0x0,%eax
jle    11fb <copy+0xbb>
mov    -0x20(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x224(%rbp),%edx
call   1030 <write@plt>
jmp    11be <copy+0x7e>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%rbp
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    $0x1,%r14d
cmp    $0xffffffff,%eax
je     11e2 <copy+0xa2>
mov    %eax,%r15d
mov    0x2e4d(%rip),%rax
mov    0x2e7e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e5b(%rip),%rax
or     (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11e2 <copy+0xa2>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x200,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11d0 <copy+0x90>
mov    %rsp,%rbx
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1050 <read@plt>
test   %eax,%eax
jg     11b0 <copy+0x70>
mov    %r15d,%edi
call   1040 <close@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_NONBLOCK = global i32 0
define i32 @copy(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [512 x i8]
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 1, i32* %3
br label %41
15: ; preds = %2
%16 = load i8*, i8** %5
%17 = load i32, i32* @O_WRONLY
%18 = load i32, i32* @O_CREAT
%19 = or i32 %17, %18
%20 = load i32, i32* @O_NONBLOCK
%21 = or i32 %19, %20
%22 = call i32 @open(i8* %16, i32 %21)
store i32 %22, i32* %7
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %15
store i32 1, i32* %3
br label %41
25: ; preds = %15
br label %26
26: ; preds = %31, %25
%27 = load i32, i32* %6
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%29 = call i32 @read(i32 %27, i8* %28, i32 512)
store i32 %29, i32* %9
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %26
%32 = load i32, i32* %7
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%34 = load i32, i32* %9
%35 = call i32 @write(i32 %32, i8* %33, i32 %34)
br label %26
36: ; preds = %26
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %7
%40 = call i32 @close(i32 %39)
store i32 0, i32* %3
br label %41
41: ; preds = %36, %24, %14
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_NONBLOCK = global i32 0
define i32 @copy(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %27, label %8
8: ; preds = %2
%9 = load i32, i32* @O_WRONLY
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, %9
%12 = load i32, i32* @O_NONBLOCK
%13 = or i32 %11, %12
%14 = tail call i32 @open(i8* %1, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %27, label %16
16: ; preds = %8
%17 = call i32 @read(i32 %6, i8* %4, i32 512)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %16, %19
%20 = phi i32 [ %22, %19 ], [ %17, %16 ]
%21 = call i32 @write(i32 %14, i8* %4, i32 %20)
%22 = call i32 @read(i32 %6, i8* %4, i32 512)
%23 = icmp sgt i32 %22, 0
br i1 %23, label %19, label %24
24: ; preds = %19, %16
%25 = tail call i32 @close(i32 %6)
%26 = tail call i32 @close(i32 %14)
br label %27
27: ; preds = %8, %2, %24
%28 = phi i32 [ 0, %24 ], [ 1, %2 ], [ 1, %8 ]
ret i32 %28
}