#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
unsigned long fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
unsigned long fwrite(char *, int, unsigned long, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ stderr;
int copy_file(const char *src_file, const char *dest_file) {
  enum { BUF_SIZE = 4096 };
  char *buffer;
  FILE *fhr, *fhw;
  unsigned long int n;
  if (!(fhr = fopen(src_file, "rb"))) {
    fprintf(stderr, "Cannot open `%s' for reading.", src_file);
    return false;
  }
  if (!(fhw = fopen(dest_file, "wb"))) {
    fprintf(stderr, "Cannot open `%s' for writing.", dest_file);
    fclose(fhr);
    return false;
  }
  buffer = (char *)malloc(BUF_SIZE);
  do {
    n = fread(buffer, 1, BUF_SIZE, fhr);
    if (n && fwrite(buffer, 1, n, fhw) != n) {
      fprintf(stderr, "Cannot write to `%s'.", dest_file);
      break;
    }
  } while (n == BUF_SIZE);
  free(buffer);
  fclose(fhr);
  fclose(fhw);
  return true;
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11c3 <copy_file+0x53>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe51(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b6 <copy_file+0x146>
mov    -0x18(%rbp),%rdi
lea    0xe53(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    120f <copy_file+0x9f>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe2f(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b6 <copy_file+0x146>
mov    $0x1000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1281 <copy_file+0x111>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
cmp    -0x38(%rbp),%rax
je     1281 <copy_file+0x111>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdcb(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1294 <copy_file+0x124>
jmp    1286 <copy_file+0x116>
cmpq   $0x1000,-0x38(%rbp)
je     121d <copy_file+0xad>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe7a(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1200 <copy_file+0x90>
mov    %rax,%r15
lea    0xe87(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     121c <copy_file+0xac>
mov    %rax,%r12
mov    $0x1000,%edi
call   1070 <malloc@plt>
mov    %rax,%r13
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     1258 <copy_file+0xe8>
mov    %rax,%rbx
mov    %r13,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r12,%rcx
call   1090 <fwrite@plt>
cmp    %rbx,%rax
jne    1240 <copy_file+0xd0>
cmp    $0x1000,%rbx
je     11c0 <copy_file+0x50>
jmp    1258 <copy_file+0xe8>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xdf3(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
xor    %eax,%eax
jmp    1275 <copy_file+0x105>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    1275 <copy_file+0x105>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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