#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 */
scalar_t__ ferror(int /*<<< orphan*/ *);
scalar_t__ fflush(int /*<<< orphan*/ *);
size_t fread(char *, int, size_t const, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
void copy_file(FILE *source, FILE *dest) {
  const size_t size = 1024;
  char buf[size];
  size_t n;
  while ((n = fread(buf, sizeof(*buf), size, source)) > 0)
    fwrite(buf, sizeof(*buf), n, dest);
  if (ferror(source) != 0)
    perror("source file; fread");
  if (ferror(dest) != 0)
    perror("destination file; fread");
  if (fflush(dest) != 0)
    perror("destination file; fflush");
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x400,-0x18(%rbp)
mov    %rsp,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rcx
lea    -0x430(%rbp),%rdi
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jbe    11b8 <copy_file+0x68>
mov    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    -0x430(%rbp),%rdi
mov    $0x1,%esi
call   1070 <fwrite@plt>
jmp    1172 <copy_file+0x22>
mov    -0x8(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11d7 <copy_file+0x87>
lea    0xe2e(%rip),%rdi
call   1060 <perror@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11f6 <copy_file+0xa6>
lea    0xe22(%rip),%rdi
call   1060 <perror@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fflush@plt>
cmp    $0x0,%rax
je     1215 <copy_file+0xc5>
lea    0xe1b(%rip),%rdi
call   1060 <perror@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsp,%rdi
mov    $0x400,%edx
mov    $0x1,%esi
mov    %r15,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     11ad <copy_file+0x5d>
mov    %rsp,%rbx
nop
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r14,%rcx
call   1070 <fwrite@plt>
mov    $0x400,%edx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r15,%rcx
call   1040 <fread@plt>
test   %rax,%rax
jne    1180 <copy_file+0x30>
mov    %r15,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    11e1 <copy_file+0x91>
mov    %r14,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    11ef <copy_file+0x9f>
mov    %r14,%rdi
call   1050 <fflush@plt>
test   %rax,%rax
jne    11fd <copy_file+0xad>
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe18(%rip),%rdi
call   1060 <perror@plt>
jmp    11ba <copy_file+0x6a>
lea    0xe1d(%rip),%rdi
call   1060 <perror@plt>
jmp    11c7 <copy_file+0x77>
lea    0xe27(%rip),%rdi
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <perror@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"source file; fread\00"
@.str.1 = [24 x i8] c"destination file; fread\00"
@.str.2 = [25 x i8] c"destination file; fflush\00"
define void @copy_file(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
store i32* %0, i32** %3
store i32* %1, i32** %4
store i64 1024, i64* %5
%8 = call i8* @llvm.stacksave()
store i8* %8, i8** %6
%9 = alloca i8, i64 1024
br label %10
10: ; preds = %14, %2
%11 = load i32*, i32** %3
%12 = call i64 @fread(i8* %9, i32 1, i64 1024, i32* %11)
store i64 %12, i64* %7
%13 = icmp ugt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i64, i64* %7
%16 = load i32*, i32** %4
%17 = call i32 @fwrite(i8* %9, i32 1, i64 %15, i32* %16)
br label %10
18: ; preds = %10
%19 = load i32*, i32** %3
%20 = call i64 @ferror(i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = load i32*, i32** %4
%26 = call i64 @ferror(i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %24
%31 = load i32*, i32** %4
%32 = call i64 @fflush(i32* %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %30
%37 = load i8*, i8** %6
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"source file; fread\00"
@.str.1 = [24 x i8] c"destination file; fread\00"
@.str.2 = [25 x i8] c"destination file; fflush\00"
define void @copy_file(i32* nocapture %0, i32* nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i64 @fread(i8* %4, i32 1, i64 1024, i32* %0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %12, label %7
7: ; preds = %2, %7
%8 = phi i64 [ %10, %7 ], [ %5, %2 ]
%9 = call i32 @fwrite(i8* %4, i32 1, i64 %8, i32* %1)
%10 = call i64 @fread(i8* %4, i32 1, i64 1024, i32* %0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %7
12: ; preds = %7, %2
%13 = tail call i64 @ferror(i32* %0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = tail call i64 @ferror(i32* %1)
%19 = icmp eq i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
%23 = tail call i64 @fflush(i32* %1)
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
ret void
}