#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*/ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
size_t fread(char *, int, size_t const, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
unsigned file_copy(FILE *dest, FILE *src) {
  unsigned error = 0;
  rewind(dest);
  rewind(src);
  const size_t block_size = 65536;
  char buffer[block_size];
  while (!feof(src)) {
    size_t bytes_buffered = fread(buffer, 1, block_size, src);
    if (ferror(src)) {
      error = 1;
      break;
    }
    fwrite(buffer, 1, bytes_buffered, dest);
    if (ferror(dest)) {
      error = 1;
      break;
    }
  }
  return error;
}


x86 assembly (clang, -O0):
<file_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x10(%rbp),%rdi
call   1050 <rewind@plt>
movq   $0x10000,-0x20(%rbp)
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11a9 <file_copy+0x59>
jmp    1223 <file_copy+0xd3>
mov    -0x10(%rbp),%rcx
lea    -0x10030(%rbp),%rdi
mov    $0x1,%esi
mov    $0x10000,%edx
call   1040 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11e6 <file_copy+0x96>
movl   $0x1,-0x14(%rbp)
jmp    1223 <file_copy+0xd3>
mov    -0x30(%rbp),%rdx
mov    -0x8(%rbp),%rcx
lea    -0x10030(%rbp),%rdi
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    -0x8(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     121e <file_copy+0xce>
movl   $0x1,-0x14(%rbp)
jmp    1223 <file_copy+0xd3>
jmp    118b <file_copy+0x3b>
mov    -0x14(%rbp),%eax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsp
add    $0x10030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_copy>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10000,%rsp
mov    %rsi,%r12
mov    %rdi,%r14
call   1050 <rewind@plt>
mov    %r12,%rdi
call   1050 <rewind@plt>
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11dd <file_copy+0x8d>
mov    $0x1,%ebp
mov    $0x10000,%edx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    11df <file_copy+0x8f>
mov    $0x1,%ebp
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1070 <fwrite@plt>
mov    %r14,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1180 <file_copy+0x30>
jmp    11df <file_copy+0x8f>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @file_copy(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store i32* %0, i32** %3
store i32* %1, i32** %4
store i32 0, i32* %5
%9 = load i32*, i32** %3
%10 = call i32 @rewind(i32* %9)
%11 = load i32*, i32** %4
%12 = call i32 @rewind(i32* %11)
store i64 65536, i64* %6
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 65536
br label %15
15: ; preds = %35, %2
%16 = load i32*, i32** %4
%17 = call i32 @feof(i32* %16)
%18 = icmp ne i32 %17, 0
%19 = xor i1 %18, true
br i1 %19, label %20, label %36
20: ; preds = %15
%21 = load i32*, i32** %4
%22 = call i64 @fread(i8* %14, i32 1, i64 65536, i32* %21)
store i64 %22, i64* %8
%23 = load i32*, i32** %4
%24 = call i64 @ferror(i32* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 1, i32* %5
br label %36
27: ; preds = %20
%28 = load i64, i64* %8
%29 = load i32*, i32** %3
%30 = call i32 @fwrite(i8* %14, i32 1, i64 %28, i32* %29)
%31 = load i32*, i32** %3
%32 = call i64 @ferror(i32* %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %27
store i32 1, i32* %5
br label %36
35: ; preds = %27
br label %15
36: ; preds = %34, %26, %15
%37 = load i32, i32* %5
%38 = load i8*, i8** %7
ret i32 %37
}

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