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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int get_file_size(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ stderr;
ssize_t copy_file(FILE *dest, const char *file) {
  FILE *read_file = fopen(file, "rb");
  uint8_t *buffer;
  if (!read_file) {
    fprintf(stderr, "ERROR: Cannot open %s for reading!\n", file);
    return -1;
  }
  ssize_t fsize = get_file_size(read_file);
  ssize_t rsize = fsize;
  buffer = malloc((1024 * 1024));
  if (!buffer) {
    fprintf(stderr, "ERROR: Out of memory!\n");
    fclose(read_file);
    return -1;
  }
  while (fsize > 0) {
    ssize_t write_size = (fsize > (1024 * 1024)) ? (1024 * 1024) : fsize;
    fsize -= write_size;
    fread(buffer, 1, write_size, read_file);
    fwrite(buffer, 1, write_size, dest);
  }
  free(buffer);
  fclose(read_file);
  return rsize;
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d6 <copy_file+0x56>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe40(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <copy_file+0x134>
mov    -0x20(%rbp),%rdi
call   1080 <get_file_size@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    $0x100000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122d <copy_file+0xad>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <copy_file+0x134>
jmp    1232 <copy_file+0xb2>
cmpl   $0x0,-0x2c(%rbp)
jle    129c <copy_file+0x11c>
cmpl   $0x100000,-0x2c(%rbp)
jle    1256 <copy_file+0xd6>
mov    $0x100000,%eax
mov    %eax,-0x38(%rbp)
jmp    125c <copy_file+0xdc>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%ecx
mov    -0x2c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x28(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
jmp    1232 <copy_file+0xb2>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%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   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %rdi,%r15
lea    0xe68(%rip),%rsi
mov    %rbp,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1218 <copy_file+0x98>
mov    %rax,%r12
mov    %rax,%rdi
call   1080 <get_file_size@plt>
mov    %eax,%r14d
mov    $0x100000,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1234 <copy_file+0xb4>
mov    %rax,%r13
test   %r14d,%r14d
jle    1206 <copy_file+0x86>
mov    %r14d,%ebp
nopl   (%rax)
cmp    $0x100000,%ebp
mov    $0x100000,%ebx
cmovb  %ebp,%ebx
mov    %r13,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
sub    %ebx,%ebp
jg     11d0 <copy_file+0x50>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
jmp    1259 <copy_file+0xd9>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1253 <copy_file+0xd3>
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
lea    0xde3(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [36 x i8] c"ERROR: Cannot open %s for reading!\0A\00"
@.str.2 = [23 x i8] c"ERROR: Out of memory!\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 i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %5
%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 ne i32* %13, null
br i1 %14, label %19, label %15
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %5
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %17)
store i32 -1, i32* %3
br label %60
19: ; preds = %2
%20 = load i32*, i32** %6
%21 = call i32 @get_file_size(i32* %20)
store i32 %21, i32* %8
%22 = load i32, i32* %8
store i32 %22, i32* %9
%23 = call i32* @malloc(i32 1048576)
store i32* %23, i32** %7
%24 = load i32*, i32** %7
%25 = icmp ne i32* %24, null
br i1 %25, label %31, label %26
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32*, i32** %6
%30 = call i32 @fclose(i32* %29)
store i32 -1, i32* %3
br label %60
31: ; preds = %19
br label %32
32: ; preds = %41, %31
%33 = load i32, i32* %8
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %54
35: ; preds = %32
%36 = load i32, i32* %8
%37 = icmp sgt i32 %36, 1048576
br i1 %37, label %38, label %39
38: ; preds = %35
br label %41
39: ; preds = %35
%40 = load i32, i32* %8
br label %41
41: ; preds = %39, %38
%42 = phi i32 [ 1048576, %38 ], [ %40, %39 ]
store i32 %42, i32* %10
%43 = load i32, i32* %10
%44 = load i32, i32* %8
%45 = sub nsw i32 %44, %43
store i32 %45, i32* %8
%46 = load i32*, i32** %7
%47 = load i32, i32* %10
%48 = load i32*, i32** %6
%49 = call i32 @fread(i32* %46, i32 1, i32 %47, i32* %48)
%50 = load i32*, i32** %7
%51 = load i32, i32* %10
%52 = load i32*, i32** %4
%53 = call i32 @fwrite(i32* %50, i32 1, i32 %51, i32* %52)
br label %32
54: ; preds = %32
%55 = load i32*, i32** %7
%56 = call i32 @free(i32* %55)
%57 = load i32*, i32** %6
%58 = call i32 @fclose(i32* %57)
%59 = load i32, i32* %9
store i32 %59, i32* %3
br label %60
60: ; preds = %54, %26, %15
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [36 x i8] c"ERROR: Cannot open %s for reading!\0A\00"
@.str.2 = [23 x i8] c"ERROR: Out of memory!\0A\00"
define i32 @copy_file(i32* nocapture %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %1, 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 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %29
8: ; preds = %2
%9 = tail call i32 @get_file_size(i32* %3)
%10 = tail call i32* @malloc(i32 1048576)
%11 = icmp eq i32* %10, null
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = icmp sgt i32 %9, 0
br i1 %13, label %18, label %26
14: ; preds = %8
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @fclose(i32* %3)
br label %29
18: ; preds = %12, %18
%19 = phi i32 [ %22, %18 ], [ %9, %12 ]
%20 = icmp ult i32 %19, 1048576
%21 = select i1 %20, i32 %19, i32 1048576
%22 = sub nsw i32 %19, %21
%23 = tail call i32 @fread(i32* %10, i32 1, i32 %21, i32* %3)
%24 = tail call i32 @fwrite(i32* %10, i32 1, i32 %21, i32* %0)
%25 = icmp sgt i32 %22, 0
br i1 %25, label %18, label %26
26: ; preds = %18, %12
%27 = tail call i32 @free(i32* %10)
%28 = tail call i32 @fclose(i32* %3)
br label %29
29: ; preds = %14, %26, %5
%30 = phi i32 [ -1, %5 ], [ %9, %26 ], [ -1, %14 ]
ret i32 %30
}