#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 /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int write(int, char *, int);
int copy(int in, int out) {
  int size = 1024;
  char buf[size];
  int er;
  while ((er = read(in, buf, size)) > 0) {
    if (write(out, buf, er) != er) {
      perror("write");
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x400,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    11c2 <copy+0x92>
mov    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
cmp    -0x24(%rbp),%eax
je     11bd <copy+0x8d>
lea    0xe5b(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    11d0 <copy+0xa0>
jmp    116b <copy+0x3b>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    1189 <copy+0x59>
mov    %eax,%ebx
mov    %r14d,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     1150 <copy+0x20>
lea    0xe83(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    118b <copy+0x5b>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
define i32 @copy(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 1024, i32* %6
%11 = load i32, i32* %6
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
br label %15
15: ; preds = %28, %2
%16 = load i32, i32* %4
%17 = load i32, i32* %6
%18 = call i32 @read(i32 %16, i8* %14, i32 %17)
store i32 %18, i32* %9
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %29
20: ; preds = %15
%21 = load i32, i32* %5
%22 = load i32, i32* %9
%23 = call i32 @write(i32 %21, i8* %14, i32 %22)
%24 = load i32, i32* %9
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %10
br label %30
28: ; preds = %20
br label %15
29: ; preds = %15
store i32 0, i32* %3
store i32 1, i32* %10
br label %30
30: ; preds = %29, %26
%31 = load i8*, i8** %7
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
define i32 @copy(i32 %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %8, %2
%6 = call i32 @read(i32 %0, i8* %4, i32 1024)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %5
%9 = call i32 @write(i32 %1, i8* %4, i32 %6)
%10 = icmp eq i32 %9, %6
br i1 %10, label %5, label %11
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %5, %11
%14 = phi i32 [ -1, %11 ], [ 0, %5 ]
ret i32 %14
}