#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*/ doread(int, unsigned char *, int);
int /*<<< orphan*/ dowrite(int, unsigned char *, int);
void docopy(int infd, int outfd, unsigned long long len) {
  unsigned char buf[65536];
  while (len > 0) {
    int chunk = len > 65536 ? 65536 : len;
    doread(infd, buf, chunk);
    dowrite(outfd, buf, chunk);
    len -= chunk;
  }
}


x86 assembly (clang, -O0):
<docopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10020,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jbe    11b8 <docopy+0x98>
cmpq   $0x10000,-0x10(%rbp)
jbe    115f <docopy+0x3f>
mov    $0x10000,%eax
mov    %rax,-0x10020(%rbp)
jmp    116a <docopy+0x4a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x10020(%rbp)
mov    -0x10020(%rbp),%rax
mov    %eax,-0x10014(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10010(%rbp),%rsi
mov    -0x10014(%rbp),%edx
call   1040 <doread@plt>
mov    -0x8(%rbp),%edi
lea    -0x10010(%rbp),%rsi
mov    -0x10014(%rbp),%edx
call   1030 <dowrite@plt>
movslq -0x10014(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1135 <docopy+0x15>
add    $0x10020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<docopy>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10000,%rsp
test   %rdx,%rdx
je     116f <docopy+0x4f>
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %edi,%r15d
mov    %rsp,%r12
cmp    $0x10000,%rbx
mov    $0x10000,%ebp
cmovb  %rbx,%rbp
mov    %r15d,%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1040 <doread@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <dowrite@plt>
sub    %rbp,%rbx
jne    1140 <docopy+0x20>
add    $0x10000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @docopy(i32 %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca [65536 x i8]
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i64 %2, i64* %6
br label %9
9: ; preds = %18, %3
%10 = load i64, i64* %6
%11 = icmp ugt i64 %10, 0
br i1 %11, label %12, label %33
12: ; preds = %9
%13 = load i64, i64* %6
%14 = icmp ugt i64 %13, 65536
br i1 %14, label %15, label %16
15: ; preds = %12
br label %18
16: ; preds = %12
%17 = load i64, i64* %6
br label %18
18: ; preds = %16, %15
%19 = phi i64 [ 65536, %15 ], [ %17, %16 ]
%20 = trunc i64 %19 to i32
store i32 %20, i32* %8
%21 = load i32, i32* %4
%22 = getelementptr inbounds [65536 x i8], [65536 x i8]* %7, i64 0, i64 0
%23 = load i32, i32* %8
%24 = call i32 @doread(i32 %21, i8* %22, i32 %23)
%25 = load i32, i32* %5
%26 = getelementptr inbounds [65536 x i8], [65536 x i8]* %7, i64 0, i64 0
%27 = load i32, i32* %8
%28 = call i32 @dowrite(i32 %25, i8* %26, i32 %27)
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = load i64, i64* %6
%32 = sub i64 %31, %30
store i64 %32, i64* %6
br label %9
33: ; preds = %9
ret void
}

LLVM IR (-O2):
define void @docopy(i32 %0, i32 %1, i64 %2) {
%4 = alloca [65536 x i8]
%5 = getelementptr inbounds [65536 x i8], [65536 x i8]* %4, i64 0, i64 0
%6 = icmp eq i64 %2, 0
br i1 %6, label %16, label %7
7: ; preds = %3, %7
%8 = phi i64 [ %14, %7 ], [ %2, %3 ]
%9 = icmp ult i64 %8, 65536
%10 = select i1 %9, i64 %8, i64 65536
%11 = trunc i64 %10 to i32
%12 = call i32 @doread(i32 %0, i8* %5, i32 %11)
%13 = call i32 @dowrite(i32 %1, i8* %5, i32 %11)
%14 = sub i64 %8, %10
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %7
16: ; preds = %7, %3
ret void
}