#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_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int open(char *, int, ...);
int read(int, char *, int);
int write(int, char *, int);
int copy(char *name, char *target) {
  int from = open(name, O_RDONLY),
      to = open(target, (O_WRONLY | O_CREAT),
                (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
  char buffer[256];
  if (from < 0 || to < 0)
    return -1;
  int len = 0, ret = 0;
  do {
    len = read(from, buffer, 256);
    ret = write(to, buffer, len);
  } while (len > 0 && ret > 0);
  if (len < 0 || ret < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    0x2e8a(%rip),%rax
or     (%rax),%esi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
mov    0x2e58(%rip),%rax
or     (%rax),%edx
mov    0x2e3f(%rip),%rax
or     (%rax),%edx
mov    0x2e2e(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11b2 <copy+0x82>
cmpl   $0x0,-0x20(%rbp)
jge    11be <copy+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <copy+0x137>
movl   $0x0,-0x124(%rbp)
movl   $0x0,-0x128(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1040 <read@plt>
mov    %eax,-0x124(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    -0x124(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x128(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x124(%rbp)
mov    %al,-0x129(%rbp)
jle    122c <copy+0xfc>
cmpl   $0x0,-0x128(%rbp)
setg   %al
mov    %al,-0x129(%rbp)
mov    -0x129(%rbp),%al
test   $0x1,%al
jne    11d2 <copy+0xa2>
cmpl   $0x0,-0x124(%rbp)
jl     1254 <copy+0x124>
cmpl   $0x0,-0x128(%rbp)
jge    1260 <copy+0x130>
movl   $0xffffffff,-0x4(%rbp)
jmp    1267 <copy+0x137>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rsi,%rbx
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1050 <open@plt>
mov    0x2e51(%rip),%rcx
mov    0x2e9a(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2e4f(%rip),%rcx
mov    0x2e68(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e4d(%rip),%rcx
or     (%rcx),%edx
mov    %eax,%r14d
mov    0x2e39(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <open@plt>
mov    %eax,%r15d
or     %r14d,%eax
js     11ce <copy+0x9e>
mov    %rsp,%rbx
xchg   %ax,%ax
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   1040 <read@plt>
mov    %eax,%ebp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %ebp,%ebp
jle    11c7 <copy+0x97>
test   %eax,%eax
jg     11a0 <copy+0x70>
or     %ebp,%eax
sar    $0x1f,%eax
jmp    11d3 <copy+0xa3>
mov    $0xffffffff,%eax
add    $0x108,%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
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = 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 [256 x i8]
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %4
%12 = load i32, i32* @O_RDONLY
%13 = call i32 (i8*, i32, ...) @open(i8* %11, i32 %12)
store i32 %13, i32* %6
%14 = load i8*, i8** %5
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IRUSR
%19 = load i32, i32* @S_IWUSR
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IRGRP
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IROTH
%24 = or i32 %22, %23
%25 = call i32 (i8*, i32, ...) @open(i8* %14, i32 %17, i32 %24)
store i32 %25, i32* %7
%26 = load i32, i32* %6
%27 = icmp slt i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %2
%29 = load i32, i32* %7
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28, %2
store i32 -1, i32* %3
br label %57
32: ; preds = %28
store i32 0, i32* %9
store i32 0, i32* %10
br label %33
33: ; preds = %47, %32
%34 = load i32, i32* %6
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 256)
store i32 %36, i32* %9
%37 = load i32, i32* %7
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%39 = load i32, i32* %9
%40 = call i32 @write(i32 %37, i8* %38, i32 %39)
store i32 %40, i32* %10
br label %41
41: ; preds = %33
%42 = load i32, i32* %9
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = load i32, i32* %10
%46 = icmp sgt i32 %45, 0
br label %47
47: ; preds = %44, %41
%48 = phi i1 [ false, %41 ], [ %46, %44 ]
br i1 %48, label %33, label %49
49: ; preds = %47
%50 = load i32, i32* %9
%51 = icmp slt i32 %50, 0
br i1 %51, label %55, label %52
52: ; preds = %49
%53 = load i32, i32* %10
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %52, %49
store i32 -1, i32* %3
br label %57
56: ; preds = %52
store i32 0, i32* %3
br label %57
57: ; preds = %56, %55, %31
%58 = load i32, i32* %3
ret i32 %58
}

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