#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*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ my_fread(char *, int, int /*<<< orphan*/ *);
long copy(FILE *f, long bytes, FILE *out) {
  long length;
  int size;
  static char buffer[512];
  for (length = 0; length < bytes; length += size) {
    size = (int)(bytes - length > sizeof(buffer) ? sizeof(buffer)
                                                 : bytes - length);
    my_fread(buffer, size, f);
    fwrite(buffer, size, 1, out);
  }
  return length;
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    11bb <copy+0x9b>
mov    -0x10(%rbp),%rax
sub    -0x20(%rbp),%rax
cmp    $0x200,%rax
jbe    116c <copy+0x4c>
mov    $0x200,%eax
mov    %rax,-0x30(%rbp)
jmp    1178 <copy+0x58>
mov    -0x10(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
mov    -0x8(%rbp),%rdx
lea    0x2eb3(%rip),%rdi
call   1030 <my_fread@plt>
mov    -0x24(%rbp),%esi
mov    -0x18(%rbp),%rcx
lea    0x2ea0(%rip),%rdi
mov    $0x1,%edx
call   1040 <fwrite@plt>
movslq -0x24(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    113c <copy+0x1c>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
jle    118f <copy+0x6f>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
xor    %ebx,%ebx
lea    0x2efe(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rbp
sub    %rbx,%rbp
cmp    $0x200,%rbp
mov    $0x200,%eax
cmovae %rax,%rbp
mov    %r13,%rdi
mov    %ebp,%esi
mov    %r12,%rdx
call   1030 <my_fread@plt>
mov    %r13,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <fwrite@plt>
add    %rbp,%rbx
cmp    %r15,%rbx
jl     1150 <copy+0x30>
jmp    1191 <copy+0x71>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@copy.buffer = internal global [512 x i8] zeroinitializer
define i64 @copy(i32* %0, i64 %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32
store i32* %0, i32** %4
store i64 %1, i64* %5
store i32* %2, i32** %6
store i64 0, i64* %7
br label %9
9: ; preds = %32, %3
%10 = load i64, i64* %7
%11 = load i64, i64* %5
%12 = icmp slt i64 %10, %11
br i1 %12, label %13, label %37
13: ; preds = %9
%14 = load i64, i64* %5
%15 = load i64, i64* %7
%16 = sub nsw i64 %14, %15
%17 = icmp ugt i64 %16, 512
br i1 %17, label %18, label %19
18: ; preds = %13
br label %23
19: ; preds = %13
%20 = load i64, i64* %5
%21 = load i64, i64* %7
%22 = sub nsw i64 %20, %21
br label %23
23: ; preds = %19, %18
%24 = phi i64 [ 512, %18 ], [ %22, %19 ]
%25 = trunc i64 %24 to i32
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = load i32*, i32** %4
%28 = call i32 @my_fread(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @copy.buffer, i64 0, i64 0), i32 %26, i32* %27)
%29 = load i32, i32* %8
%30 = load i32*, i32** %6
%31 = call i32 @fwrite(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @copy.buffer, i64 0, i64 0), i32 %29, i32 1, i32* %30)
br label %32
32: ; preds = %23
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = load i64, i64* %7
%36 = add nsw i64 %35, %34
store i64 %36, i64* %7
br label %9
37: ; preds = %9
%38 = load i64, i64* %7
ret i64 %38
}

LLVM IR (-O2):
@copy.buffer = internal global [512 x i8] zeroinitializer
define i64 @copy(i32* %0, i64 %1, i32* nocapture %2) {
%4 = icmp sgt i64 %1, 0
br i1 %4, label %5, label %15
5: ; preds = %3, %5
%6 = phi i64 [ %13, %5 ], [ 0, %3 ]
%7 = sub nsw i64 %1, %6
%8 = icmp ult i64 %7, 512
%9 = select i1 %8, i64 %7, i64 512
%10 = trunc i64 %9 to i32
%11 = tail call i32 @my_fread(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @copy.buffer, i64 0, i64 0), i32 %10, i32* %0)
%12 = tail call i32 @fwrite(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @copy.buffer, i64 0, i64 0), i32 %10, i32 1, i32* %2)
%13 = add nuw nsw i64 %9, %6
%14 = icmp slt i64 %13, %1
br i1 %14, label %5, label %15
15: ; preds = %5, %3
%16 = phi i64 [ 0, %3 ], [ %13, %5 ]
ret i64 %16
}