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

/* Variables and functions */
int fread(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int fwrite(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int);
int copy_data(FILE *dest, FILE *src, uint64_t size) {
  uint8_t *buffer;
  size_t chunk;
  int rc;
  buffer = malloc(4096);
  if (!buffer)
    return -1;
  rc = 0;
  while (size) {
    chunk = 4096 < size ? 4096 : size;
    if (fread(buffer, chunk, 1, src) != 1) {
      rc = -1;
      break;
    }
    if (fwrite(buffer, chunk, 1, dest) != 1) {
      rc = -1;
      break;
    }
    size -= chunk;
  }
  free(buffer);
  return rc;
}


x86 assembly (clang, -O0):
<copy_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    $0x1000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1178 <copy_data+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <copy_data+0xeb>
movl   $0x0,-0x34(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     121c <copy_data+0xdc>
mov    $0x1000,%eax
cmp    -0x1c(%rbp),%eax
jge    11a4 <copy_data+0x64>
mov    $0x1000,%eax
mov    %eax,-0x38(%rbp)
jmp    11aa <copy_data+0x6a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     11de <copy_data+0x9e>
movl   $0xffffffff,-0x34(%rbp)
jmp    121c <copy_data+0xdc>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1060 <fwrite@plt>
cmp    $0x1,%eax
je     1209 <copy_data+0xc9>
movl   $0xffffffff,-0x34(%rbp)
jmp    121c <copy_data+0xdc>
mov    -0x30(%rbp),%rcx
movslq -0x1c(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x1c(%rbp)
jmp    117f <copy_data+0x3f>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,(%rsp)
mov    $0x1000,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11c6 <copy_data+0x86>
mov    %rax,%r12
test   %ebp,%ebp
je     11b9 <copy_data+0x79>
mov    $0xffffffff,%r13d
cmp    $0x1000,%ebp
mov    $0x1000,%r15d
cmovl  %ebp,%r15d
movslq %r15d,%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    11bc <copy_data+0x7c>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
mov    (%rsp),%rcx
call   1060 <fwrite@plt>
cmp    $0x1,%eax
jne    11bc <copy_data+0x7c>
sub    %r15d,%ebp
jne    1170 <copy_data+0x30>
xor    %r13d,%r13d
mov    %r12,%rdi
call   1030 <free@plt>
jmp    11cc <copy_data+0x8c>
mov    $0xffffffff,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @copy_data(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%11 = call i32* @malloc(i32 4096)
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %3
store i32 -1, i32* %4
br label %51
15: ; preds = %3
store i32 0, i32* %10
br label %16
16: ; preds = %41, %15
%17 = load i32, i32* %7
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %47
19: ; preds = %16
%20 = load i32, i32* %7
%21 = icmp slt i32 4096, %20
br i1 %21, label %22, label %23
22: ; preds = %19
br label %25
23: ; preds = %19
%24 = load i32, i32* %7
br label %25
25: ; preds = %23, %22
%26 = phi i32 [ 4096, %22 ], [ %24, %23 ]
%27 = sext i32 %26 to i64
store i64 %27, i64* %9
%28 = load i32*, i32** %8
%29 = load i64, i64* %9
%30 = load i32*, i32** %6
%31 = call i32 @fread(i32* %28, i64 %29, i32 1, i32* %30)
%32 = icmp ne i32 %31, 1
br i1 %32, label %33, label %34
33: ; preds = %25
store i32 -1, i32* %10
br label %47
34: ; preds = %25
%35 = load i32*, i32** %8
%36 = load i64, i64* %9
%37 = load i32*, i32** %5
%38 = call i32 @fwrite(i32* %35, i64 %36, i32 1, i32* %37)
%39 = icmp ne i32 %38, 1
br i1 %39, label %40, label %41
40: ; preds = %34
store i32 -1, i32* %10
br label %47
41: ; preds = %34
%42 = load i64, i64* %9
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = sub i64 %44, %42
%46 = trunc i64 %45 to i32
store i32 %46, i32* %7
br label %16
47: ; preds = %40, %33, %16
%48 = load i32*, i32** %8
%49 = call i32 @free(i32* %48)
%50 = load i32, i32* %10
store i32 %50, i32* %4
br label %51
51: ; preds = %47, %14
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
define i32 @copy_data(i32* nocapture %0, i32* nocapture %1, i32 %2) {
%4 = tail call i32* @malloc(i32 4096)
%5 = icmp eq i32* %4, null
br i1 %5, label %24, label %6
6: ; preds = %3
%7 = icmp eq i32 %2, 0
br i1 %7, label %21, label %10
8: ; preds = %17
%9 = icmp eq i32 %20, 0
br i1 %9, label %21, label %10
10: ; preds = %6, %8
%11 = phi i32 [ %20, %8 ], [ %2, %6 ]
%12 = icmp slt i32 %11, 4096
%13 = select i1 %12, i32 %11, i32 4096
%14 = sext i32 %13 to i64
%15 = tail call i32 @fread(i32* %4, i64 %14, i32 1, i32* %1)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %21
17: ; preds = %10
%18 = tail call i32 @fwrite(i32* %4, i64 %14, i32 1, i32* %0)
%19 = icmp eq i32 %18, 1
%20 = sub i32 %11, %13
br i1 %19, label %8, label %21
21: ; preds = %8, %10, %17, %6
%22 = phi i32 [ 0, %6 ], [ -1, %17 ], [ -1, %10 ], [ 0, %8 ]
%23 = tail call i32 @free(i32* %4)
br label %24
24: ; preds = %3, %21
%25 = phi i32 [ %22, %21 ], [ -1, %3 ]
ret i32 %25
}