#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*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int copy_file(FILE *in, const char *dst) {
  FILE *out;
  int res = -1;
  size_t read;
  char *buff = NULL;
  out = fopen(dst, "w");
  if (!out) {
    fprintf(stderr, "Failed to open dst file (%s)!\n", strerror(errno));
    return -1;
  }
  buff = malloc((512 * 1024));
  while ((read = fread(buff, 1, (512 * 1024), in)) > 0) {
    if (fwrite(buff, 1, read, out) != read) {
      fprintf(stderr, "Failed to write data to dst file (%s)!\n",
              strerror(errno));
      goto exit;
    }
  }
  res = 0;
exit:
  free(buff);
  fclose(out);
  return res;
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f6 <copy_file+0x76>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xe1d(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <copy_file+0x11e>
mov    $0x80000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x80000,%edx
call   1040 <fread@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jbe    127f <copy_file+0xff>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
cmp    -0x30(%rbp),%rax
je     127a <copy_file+0xfa>
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xdb1(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1286 <copy_file+0x106>
jmp    1204 <copy_file+0x84>
movl   $0x0,-0x24(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rax
mov    %rdi,%r15
lea    0xe6c(%rip),%rsi
mov    %rax,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1224 <copy_file+0xa4>
mov    %rax,%r14
mov    $0x80000,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x80000,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     1253 <copy_file+0xd3>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r14,%rcx
call   1090 <fwrite@plt>
cmp    %rbp,%rax
je     11c0 <copy_file+0x40>
mov    0x2de4(%rip),%rax
mov    (%rax),%ebp
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe0e(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1255 <copy_file+0xd5>
mov    0x2db5(%rip),%rax
mov    (%rax),%ebx
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdc0(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1265 <copy_file+0xe5>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"Failed to open dst file (%s)!\0A\00"
@errno = global i32 0
@.str.2 = [40 x i8] c"Failed to write data to dst file (%s)!\0A\00"
define i32 @copy_file(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 -1, i32* %7
store i8* null, i8** %9
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %19, label %14
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %17)
store i32 -1, i32* %3
br label %46
19: ; preds = %2
%20 = call i8* @malloc(i32 524288)
store i8* %20, i8** %9
br label %21
21: ; preds = %38, %19
%22 = load i8*, i8** %9
%23 = load i32*, i32** %4
%24 = call i64 @fread(i8* %22, i32 1, i32 524288, i32* %23)
store i64 %24, i64* %8
%25 = icmp ugt i64 %24, 0
br i1 %25, label %26, label %39
26: ; preds = %21
%27 = load i8*, i8** %9
%28 = load i64, i64* %8
%29 = load i32*, i32** %6
%30 = call i64 @fwrite(i8* %27, i32 1, i64 %28, i32* %29)
%31 = load i64, i64* %8
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %38
33: ; preds = %26
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %36)
br label %40
38: ; preds = %26
br label %21
39: ; preds = %21
store i32 0, i32* %7
br label %40
40: ; preds = %39, %33
%41 = load i8*, i8** %9
%42 = call i32 @free(i8* %41)
%43 = load i32*, i32** %6
%44 = call i32 @fclose(i32* %43)
%45 = load i32, i32* %7
store i32 %45, i32* %3
br label %46
46: ; preds = %40, %14
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"Failed to open dst file (%s)!\0A\00"
@errno = global i32 0
@.str.2 = [40 x i8] c"Failed to write data to dst file (%s)!\0A\00"
define i32 @copy_file(i32* nocapture %0, i8* nocapture %1) {
%3 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %8)
br label %27
10: ; preds = %2
%11 = tail call dereferenceable_or_null(524288) i8* @malloc(i32 524288)
br label %12
12: ; preds = %15, %10
%13 = tail call i64 @fread(i8* %11, i32 1, i32 524288, i32* %0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %12
%16 = tail call i64 @fwrite(i8* %11, i32 1, i64 %13, i32* %3)
%17 = icmp eq i64 %16, %13
br i1 %17, label %12, label %18
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = tail call i8* @strerror(i32 %20)
%22 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %12, %18
%24 = phi i32 [ -1, %18 ], [ 0, %12 ]
%25 = tail call i32 @free(i8* %11)
%26 = tail call i32 @fclose(i32* %3)
br label %27
27: ; preds = %23, %5
%28 = phi i32 [ %24, %23 ], [ -1, %5 ]
ret i32 %28
}