#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 read(int, char *, int const);
int write(int, char *, int);
int copy(int in, int out) {
  const int buf_len = 4096;
  char buf[buf_len];
  int read_len;
  while ((read_len = read(in, buf, buf_len)) > 0) {
    if (write(out, buf, read_len) == -1)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x1000,-0x10(%rbp)
mov    %rsp,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    1192 <copy+0x72>
mov    -0xc(%rbp),%edi
mov    -0x1c(%rbp),%edx
lea    -0x1020(%rbp),%rsi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    118d <copy+0x6d>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x20(%rbp)
jmp    11a0 <copy+0x80>
jmp    113f <copy+0x1f>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    116c <copy+0x4c>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1140 <copy+0x20>
mov    $0xffffffff,%eax
jmp    116e <copy+0x4e>
xor    %eax,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @copy(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 4096, i32* %6
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %7
%11 = alloca i8, i64 4096
br label %12
12: ; preds = %22, %2
%13 = load i32, i32* %4
%14 = call i32 @read(i32 %13, i8* %11, i32 4096)
store i32 %14, i32* %8
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %12
%17 = load i32, i32* %5
%18 = load i32, i32* %8
%19 = call i32 @write(i32 %17, i8* %11, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 -1, i32* %3
store i32 1, i32* %9
br label %24
22: ; preds = %16
br label %12
23: ; preds = %12
store i32 0, i32* %3
store i32 1, i32* %9
br label %24
24: ; preds = %23, %21
%25 = load i8*, i8** %7
%26 = load i32, i32* %3
ret i32 %26
}

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