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

/* Variables and functions */
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ ferror(int /*<<< orphan*/ *);
size_t fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int /*<<< orphan*/ *, int, size_t,
                          int /*<<< orphan*/ *);
size_t copy_file(FILE *to, FILE *from) {
  size_t i, fsize = 0;
  uint8_t buf[8 * 1024];
  while (!feof(from) && !ferror(from)) {
    i = fread(buf, sizeof(uint8_t), 8 * 1024, from);
    if (i > 0) {
      fsize += i;
      fwrite(buf, sizeof(uint8_t), i, to);
    }
  }
  return fsize;
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x8021(%rbp)
jne    118e <copy_file+0x4e>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x8021(%rbp)
mov    -0x8021(%rbp),%al
test   $0x1,%al
jne    11a1 <copy_file+0x61>
jmp    11f4 <copy_file+0xb4>
lea    -0x8020(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x4,%esi
mov    $0x2000,%edx
call   1040 <fread@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11ef <copy_file+0xaf>
mov    -0x18(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x8020(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x8(%rbp),%rcx
mov    $0x4,%esi
call   1060 <fwrite@plt>
jmp    115b <copy_file+0x1b>
mov    -0x20(%rbp),%rax
add    $0x8030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x8008,%rsp
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rsi,%rdi
call   1050 <feof@plt>
xor    %r14d,%r14d
test   %eax,%eax
je     1175 <copy_file+0x35>
mov    %r14,%rax
add    $0x8008,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %rsp,%r12
jmp    118c <copy_file+0x4c>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <feof@plt>
test   %eax,%eax
jne    1163 <copy_file+0x23>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %eax,%eax
jne    1163 <copy_file+0x23>
mov    %r12,%rdi
mov    $0x4,%esi
mov    $0x2000,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     1180 <copy_file+0x40>
add    %rax,%r14
mov    %r12,%rdi
mov    $0x4,%esi
mov    %rax,%rdx
mov    %r15,%rcx
call   1060 <fwrite@plt>
jmp    1180 <copy_file+0x40>

LLVM IR (-O0):
define i64 @copy_file(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
%7 = alloca [8192 x i32]
store i32* %0, i32** %3
store i32* %1, i32** %4
store i64 0, i64* %6
br label %8
8: ; preds = %33, %2
%9 = load i32*, i32** %4
%10 = call i32 @feof(i32* %9)
%11 = icmp ne i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %8
%13 = load i32*, i32** %4
%14 = call i32 @ferror(i32* %13)
%15 = icmp ne i32 %14, 0
%16 = xor i1 %15, true
br label %17
17: ; preds = %12, %8
%18 = phi i1 [ false, %8 ], [ %16, %12 ]
br i1 %18, label %19, label %34
19: ; preds = %17
%20 = getelementptr inbounds [8192 x i32], [8192 x i32]* %7, i64 0, i64 0
%21 = load i32*, i32** %4
%22 = call i64 @fread(i32* %20, i32 4, i32 8192, i32* %21)
store i64 %22, i64* %5
%23 = load i64, i64* %5
%24 = icmp ugt i64 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %19
%26 = load i64, i64* %5
%27 = load i64, i64* %6
%28 = add i64 %27, %26
store i64 %28, i64* %6
%29 = getelementptr inbounds [8192 x i32], [8192 x i32]* %7, i64 0, i64 0
%30 = load i64, i64* %5
%31 = load i32*, i32** %3
%32 = call i32 @fwrite(i32* %29, i32 4, i64 %30, i32* %31)
br label %33
33: ; preds = %25, %19
br label %8
34: ; preds = %17
%35 = load i64, i64* %6
ret i64 %35
}

LLVM IR (-O2):
define i64 @copy_file(i32* nocapture %0, i32* nocapture %1) {
%3 = alloca [8192 x i32]
%4 = bitcast [8192 x i32]* %3 to i8*
%5 = tail call i32 @feof(i32* %1)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %23
7: ; preds = %2
%8 = getelementptr inbounds [8192 x i32], [8192 x i32]* %3, i64 0, i64 0
br label %9
9: ; preds = %7, %19
%10 = phi i64 [ 0, %7 ], [ %20, %19 ]
%11 = tail call i32 @ferror(i32* %1)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %23
13: ; preds = %9
%14 = call i64 @fread(i32* %8, i32 4, i32 8192, i32* %1)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = add i64 %14, %10
%18 = call i32 @fwrite(i32* %8, i32 4, i64 %14, i32* %0)
br label %19
19: ; preds = %16, %13
%20 = phi i64 [ %17, %16 ], [ %10, %13 ]
%21 = tail call i32 @feof(i32* %1)
%22 = icmp eq i32 %21, 0
br i1 %22, label %9, label %23
23: ; preds = %9, %19, %2
%24 = phi i64 [ 0, %2 ], [ %20, %19 ], [ %10, %9 ]
ret i64 %24
}