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

/* Variables and functions */
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int copy(int in, int out) {
  int towrite;
  char buf[8000];
  towrite = read(in, buf, sizeof(buf));
  while (towrite > 0) {
    write(out, buf, towrite);
    towrite = read(in, buf, sizeof(buf));
  }
  return out;
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1f50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1f50(%rbp),%rsi
mov    $0x1f40,%edx
call   1040 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    1180 <copy+0x60>
mov    -0x8(%rbp),%edi
lea    -0x1f50(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x1f50(%rbp),%rsi
mov    $0x1f40,%edx
call   1040 <read@plt>
mov    %eax,-0xc(%rbp)
jmp    1148 <copy+0x28>
mov    -0x8(%rbp),%eax
add    $0x1f50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1f40,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x1f40,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    1170 <copy+0x50>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1f40,%edx
call   1040 <read@plt>
test   %eax,%eax
jg     1150 <copy+0x30>
mov    %r14d,%eax
add    $0x1f40,%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 [8000 x i8]
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %3
%8 = getelementptr inbounds [8000 x i8], [8000 x i8]* %6, i64 0, i64 0
%9 = call i32 @read(i32 %7, i8* %8, i32 8000)
store i32 %9, i32* %5
br label %10
10: ; preds = %13, %2
%11 = load i32, i32* %5
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %10
%14 = load i32, i32* %4
%15 = getelementptr inbounds [8000 x i8], [8000 x i8]* %6, i64 0, i64 0
%16 = load i32, i32* %5
%17 = call i32 @write(i32 %14, i8* %15, i32 %16)
%18 = load i32, i32* %3
%19 = getelementptr inbounds [8000 x i8], [8000 x i8]* %6, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 8000)
store i32 %20, i32* %5
br label %10
21: ; preds = %10
%22 = load i32, i32* %4
ret i32 %22
}

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