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

/* Variables and functions */
int /*<<< orphan*/ fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, long, int /*<<< orphan*/ *);
void direct_copy(FILE *in, FILE *out, long size) {
  char buf[4096];
  long blocks = size / 4096;
  long remaining = size % 4096;
  for (; blocks > 0; --blocks) {
    (void)fread(buf, 1, 4096, in);
    (void)fwrite(buf, 1, 4096, out);
  }
  if (remaining > 0) {
    (void)fread(buf, 1, remaining, in);
    (void)fwrite(buf, 1, remaining, out);
  }
}


x86 assembly (clang, -O0):
<direct_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x1000,%ecx
cqto
idiv   %rcx
mov    %rax,-0x1028(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x1000,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x1030(%rbp)
cmpq   $0x0,-0x1028(%rbp)
jle    11ba <direct_copy+0x9a>
lea    -0x1020(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fwrite@plt>
mov    -0x1028(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x1028(%rbp)
jmp    1161 <direct_copy+0x41>
cmpq   $0x0,-0x1030(%rbp)
jle    1200 <direct_copy+0xe0>
lea    -0x1020(%rbp),%rdi
mov    -0x1030(%rbp),%rdx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x1030(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fwrite@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<direct_copy>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1000,%rsp
lea    0xfff(%rdx),%rax
test   %rdx,%rdx
cmovns %rdx,%rax
mov    %rsi,%r15
mov    %rdi,%r12
and    $0xfffffffffffff000,%rax
mov    %rdx,%r14
sub    %rax,%r14
cmp    $0x1000,%rdx
jl     11a4 <direct_copy+0x84>
mov    %rdx,%rbx
shr    $0xc,%rbx
add    $0x1,%rbx
mov    %rsp,%r13
nopw   0x0(%rax,%rax,1)
mov    $0x1000,%edx
mov    %r13,%rdi
mov    $0x1,%esi
mov    %r12,%rcx
call   1030 <fread@plt>
mov    $0x1000,%edx
mov    %r13,%rdi
mov    $0x1,%esi
mov    %r15,%rcx
call   1040 <fwrite@plt>
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     1170 <direct_copy+0x50>
test   %r14,%r14
jle    11d2 <direct_copy+0xb2>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %r12,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %r15,%rcx
call   1040 <fwrite@plt>
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @direct_copy(i32* %0, i32* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca [4096 x i8]
%8 = alloca i64
%9 = alloca i64
store i32* %0, i32** %4
store i32* %1, i32** %5
store i64 %2, i64* %6
%10 = load i64, i64* %6
%11 = sdiv i64 %10, 4096
store i64 %11, i64* %8
%12 = load i64, i64* %6
%13 = srem i64 %12, 4096
store i64 %13, i64* %9
br label %14
14: ; preds = %24, %3
%15 = load i64, i64* %8
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %27
17: ; preds = %14
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%19 = load i32*, i32** %4
%20 = call i32 @fread(i8* %18, i32 1, i64 4096, i32* %19)
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%22 = load i32*, i32** %5
%23 = call i32 @fwrite(i8* %21, i32 1, i64 4096, i32* %22)
br label %24
24: ; preds = %17
%25 = load i64, i64* %8
%26 = add nsw i64 %25, -1
store i64 %26, i64* %8
br label %14
27: ; preds = %14
%28 = load i64, i64* %9
%29 = icmp sgt i64 %28, 0
br i1 %29, label %30, label %39
30: ; preds = %27
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%32 = load i64, i64* %9
%33 = load i32*, i32** %4
%34 = call i32 @fread(i8* %31, i32 1, i64 %32, i32* %33)
%35 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%36 = load i64, i64* %9
%37 = load i32*, i32** %5
%38 = call i32 @fwrite(i8* %35, i32 1, i64 %36, i32* %37)
br label %39
39: ; preds = %30, %27
ret void
}

LLVM IR (-O2):
define void @direct_copy(i32* nocapture %0, i32* nocapture %1, i64 %2) {
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%6 = srem i64 %2, 4096
%7 = icmp sgt i64 %2, 4095
br i1 %7, label %8, label %16
8: ; preds = %3
%9 = lshr i64 %2, 12
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ %14, %10 ], [ %9, %8 ]
%12 = call i32 @fread(i8* %5, i32 1, i64 4096, i32* %0)
%13 = call i32 @fwrite(i8* %5, i32 1, i64 4096, i32* %1)
%14 = add nsw i64 %11, -1
%15 = icmp ugt i64 %11, 1
br i1 %15, label %10, label %16
16: ; preds = %10, %3
%17 = icmp sgt i64 %6, 0
br i1 %17, label %18, label %21
18: ; preds = %16
%19 = call i32 @fread(i8* %5, i32 1, i64 %6, i32* %0)
%20 = call i32 @fwrite(i8* %5, i32 1, i64 %6, i32* %1)
br label %21
21: ; preds = %18, %16
ret void
}