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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int file_copy(const char *from, const char *to) {
  FILE *ffr = NULL;
  FILE *fto = NULL;
  char buf[8192];
  size_t size;
  ffr = fopen(from, "rb");
  if (ffr == NULL) {
    fprintf(stderr, "could not open source file '%s' for reading\n", from);
    return -1;
  }
  fto = fopen(to, "wb");
  if (fto == NULL) {
    fprintf(stderr, "could not open target file '%s' for writing\n", to);
    fclose(ffr);
    return -1;
  }
  while (!feof(ffr)) {
    size = fread(buf, 1, sizeof(buf), ffr);
    if (size > 0) {
      size_t bytes = fwrite(buf, 1, size, fto);
      if (bytes != size) {
        fclose(fto);
        fclose(ffr);
        return -1;
      }
    }
  }
  fclose(fto);
  fclose(ffr);
  return 0;
}


x86 assembly (clang, -O0):
<file_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c7 <file_copy+0x67>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe4d(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12de <file_copy+0x17e>
mov    -0x18(%rbp),%rdi
lea    0xe5e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1214 <file_copy+0xb4>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe39(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12de <file_copy+0x17e>
jmp    1219 <file_copy+0xb9>
mov    -0x20(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1237 <file_copy+0xd7>
jmp    12c5 <file_copy+0x165>
lea    -0x2030(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2000,%edx
call   1030 <fread@plt>
mov    %rax,-0x2038(%rbp)
cmpq   $0x0,-0x2038(%rbp)
jbe    12c0 <file_copy+0x160>
lea    -0x2030(%rbp),%rdi
mov    -0x2038(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    %rax,-0x2040(%rbp)
mov    -0x2040(%rbp),%rax
cmp    -0x2038(%rbp),%rax
je     12bb <file_copy+0x15b>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12de <file_copy+0x17e>
jmp    12c0 <file_copy+0x160>
jmp    1219 <file_copy+0xb9>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_copy>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2008,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe85(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c2 <file_copy+0x62>
mov    %rax,%r12
lea    0xea1(%rip),%rsi
mov    %r15,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11dc <file_copy+0x7c>
mov    %rax,%r14
mov    %r12,%rdi
call   1060 <feof@plt>
test   %eax,%eax
je     11f6 <file_copy+0x96>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    1256 <file_copy+0xf6>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
jmp    1251 <file_copy+0xf1>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
mov    %r15,%rdx
call   1050 <fprintf@plt>
jmp    1249 <file_copy+0xe9>
mov    %rsp,%r15
jmp    120c <file_copy+0xac>
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11ab <file_copy+0x4b>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x2000,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     1200 <file_copy+0xa0>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r14,%rcx
call   1080 <fwrite@plt>
cmp    %rbx,%rax
je     1200 <file_copy+0xa0>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [45 x i8] c"could not open source file '%s' for reading\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [45 x i8] c"could not open target file '%s' for writing\0A\00"
define i32 @file_copy(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [8192 x i8]
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32* null, i32** %6
store i32* null, i32** %7
%11 = load i8*, i8** %4
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %4
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %17)
store i32 -1, i32* %3
br label %62
19: ; preds = %2
%20 = load i8*, i8** %5
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %21, i32** %7
%22 = load i32*, i32** %7
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %30
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %5
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%28 = load i32*, i32** %6
%29 = call i32 @fclose(i32* %28)
store i32 -1, i32* %3
br label %62
30: ; preds = %19
br label %31
31: ; preds = %56, %30
%32 = load i32*, i32** %6
%33 = call i32 @feof(i32* %32)
%34 = icmp ne i32 %33, 0
%35 = xor i1 %34, true
br i1 %35, label %36, label %57
36: ; preds = %31
%37 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%38 = load i32*, i32** %6
%39 = call i64 @fread(i8* %37, i32 1, i32 8192, i32* %38)
store i64 %39, i64* %9
%40 = load i64, i64* %9
%41 = icmp ugt i64 %40, 0
br i1 %41, label %42, label %56
42: ; preds = %36
%43 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%44 = load i64, i64* %9
%45 = load i32*, i32** %7
%46 = call i64 @fwrite(i8* %43, i32 1, i64 %44, i32* %45)
store i64 %46, i64* %10
%47 = load i64, i64* %10
%48 = load i64, i64* %9
%49 = icmp ne i64 %47, %48
br i1 %49, label %50, label %55
50: ; preds = %42
%51 = load i32*, i32** %7
%52 = call i32 @fclose(i32* %51)
%53 = load i32*, i32** %6
%54 = call i32 @fclose(i32* %53)
store i32 -1, i32* %3
br label %62
55: ; preds = %42
br label %56
56: ; preds = %55, %36
br label %31
57: ; preds = %31
%58 = load i32*, i32** %7
%59 = call i32 @fclose(i32* %58)
%60 = load i32*, i32** %6
%61 = call i32 @fclose(i32* %60)
store i32 0, i32* %3
br label %62
62: ; preds = %57, %50, %24, %15
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [45 x i8] c"could not open source file '%s' for reading\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [45 x i8] c"could not open target file '%s' for writing\0A\00"
define i32 @file_copy(i8* %0, i8* %1) {
%3 = alloca [8192 x i8]
%4 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %35
10: ; preds = %2
%11 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = tail call i32 @feof(i32* %5)
%15 = icmp eq i32 %14, 0
br i1 %15, label %20, label %32
16: ; preds = %10
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i8* %1)
%19 = tail call i32 @fclose(i32* %5)
br label %35
20: ; preds = %13, %29
%21 = call i64 @fread(i8* %4, i32 1, i32 8192, i32* %5)
%22 = icmp eq i64 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %20
%24 = call i64 @fwrite(i8* %4, i32 1, i64 %21, i32* %11)
%25 = icmp eq i64 %24, %21
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = tail call i32 @fclose(i32* %11)
%28 = tail call i32 @fclose(i32* %5)
br label %35
29: ; preds = %23, %20
%30 = tail call i32 @feof(i32* %5)
%31 = icmp eq i32 %30, 0
br i1 %31, label %20, label %32
32: ; preds = %29, %13
%33 = tail call i32 @fclose(i32* %11)
%34 = tail call i32 @fclose(i32* %5)
br label %35
35: ; preds = %26, %32, %16, %7
%36 = phi i32 [ -1, %7 ], [ -1, %16 ], [ -1, %26 ], [ 0, %32 ]
ret i32 %36
}