#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 */
struct stat {
  int st_size;
};
typedef int int64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ copy_attributes(char *, int, struct stat);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
int file_copy(char *source, char *destination, struct stat stats) {
  FILE *Fp1;
  FILE *Fp2;
  char *buffer;
  int block_size;
  int64_t file_size;
  file_size = stats.st_size;
  Fp1 = fopen(source, "rb");
  Fp2 = fopen(destination, "wb");
  if (Fp1 == NULL)
    return -1;
  if (Fp2 == NULL)
    return -1;
  if (file_size >= 1024 * 1024) {
    block_size = 1024 * 1024;
  } else {
    block_size = file_size;
  }
  buffer = (char *)malloc(block_size * sizeof(char));
  if (buffer == NULL) {
    printf("[Error  ]\tInsufficient physical memory!");
    exit(-1);
  }
  while (file_size > 0) {
    if (file_size >= 1024 * 1024) {
      block_size = 1024 * 1024;
    } else {
      block_size = file_size;
    }
    fread(buffer, block_size, 1, Fp1);
    fwrite(buffer, block_size, 1, Fp2);
    file_size -= 1024 * 1024;
  }
  fclose(Fp1);
  fclose(Fp2);
  copy_attributes(destination, 8, stats);
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<file_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edx,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe4c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe3b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e8 <file_copy+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <file_copy+0x15d>
cmpq   $0x0,-0x28(%rbp)
jne    11ff <file_copy+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <file_copy+0x15d>
cmpl   $0x100000,-0x38(%rbp)
jl     1218 <file_copy+0x88>
movl   $0x100000,-0x34(%rbp)
jmp    121e <file_copy+0x8e>
mov    -0x38(%rbp),%eax
mov    %eax,-0x34(%rbp)
movslq -0x34(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1252 <file_copy+0xc2>
lea    0xdc3(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
jmp    1257 <file_copy+0xc7>
cmpl   $0x0,-0x38(%rbp)
jle    12ba <file_copy+0x12a>
cmpl   $0x100000,-0x38(%rbp)
jl     127a <file_copy+0xea>
movl   $0x100000,-0x34(%rbp)
jmp    1280 <file_copy+0xf0>
mov    -0x38(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    -0x38(%rbp),%eax
sub    $0x100000,%eax
mov    %eax,-0x38(%rbp)
jmp    1257 <file_copy+0xc7>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    $0x8,%esi
call   1070 <copy_attributes@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
mov    %rsi,%rbx
lea    0xe56(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r12
lea    0xe4a(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
mov    %rax,%r13
mov    $0xffffffff,%eax
test   %r12,%r12
je     128d <file_copy+0xfd>
test   %r13,%r13
je     128d <file_copy+0xfd>
mov    %rbx,0x10(%rsp)
cmp    $0x100000,%ebp
mov    $0x100000,%edi
cmovl  %ebp,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     129c <file_copy+0x10c>
mov    %rax,%rbx
mov    %ebp,0xc(%rsp)
test   %ebp,%ebp
jle    1260 <file_copy+0xd0>
mov    $0x100000,%r14d
mov    0xc(%rsp),%ebp
mov    %ebp,%r15d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
sub    $0x100000,%r15d
cmovae %r14d,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   10b0 <fwrite@plt>
lea    0x100000(%r15),%eax
mov    %r15d,%ebp
cmp    $0x100000,%eax
ja     1220 <file_copy+0x90>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    0x10(%rsp),%rdi
mov    $0x8,%esi
mov    0xc(%rsp),%edx
call   1070 <copy_attributes@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd63(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [40 x i8] c"[Error ]\09Insufficient physical memory!\00"
define i32 @file_copy(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.stat
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
store i32 %2, i32* %13
store i8* %0, i8** %6
store i8* %1, i8** %7
%14 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%15 = load i32, i32* %14
store i32 %15, i32* %12
%16 = load i8*, i8** %6
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %8
%18 = load i8*, i8** %7
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %9
%20 = load i32*, i32** %8
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %23
22: ; preds = %3
store i32 -1, i32* %4
br label %77
23: ; preds = %3
%24 = load i32*, i32** %9
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %27
26: ; preds = %23
store i32 -1, i32* %4
br label %77
27: ; preds = %23
%28 = load i32, i32* %12
%29 = icmp sge i32 %28, 1048576
br i1 %29, label %30, label %31
30: ; preds = %27
store i32 1048576, i32* %11
br label %33
31: ; preds = %27
%32 = load i32, i32* %12
store i32 %32, i32* %11
br label %33
33: ; preds = %31, %30
%34 = load i32, i32* %11
%35 = sext i32 %34 to i64
%36 = mul i64 %35, 1
%37 = trunc i64 %36 to i32
%38 = call i64 @malloc(i32 %37)
%39 = inttoptr i64 %38 to i8*
store i8* %39, i8** %10
%40 = load i8*, i8** %10
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %45
42: ; preds = %33
%43 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @exit(i32 -1)
unreachable
45: ; preds = %33
br label %46
46: ; preds = %55, %45
%47 = load i32, i32* %12
%48 = icmp sgt i32 %47, 0
br i1 %48, label %49, label %66
49: ; preds = %46
%50 = load i32, i32* %12
%51 = icmp sge i32 %50, 1048576
br i1 %51, label %52, label %53
52: ; preds = %49
store i32 1048576, i32* %11
br label %55
53: ; preds = %49
%54 = load i32, i32* %12
store i32 %54, i32* %11
br label %55
55: ; preds = %53, %52
%56 = load i8*, i8** %10
%57 = load i32, i32* %11
%58 = load i32*, i32** %8
%59 = call i32 @fread(i8* %56, i32 %57, i32 1, i32* %58)
%60 = load i8*, i8** %10
%61 = load i32, i32* %11
%62 = load i32*, i32** %9
%63 = call i32 @fwrite(i8* %60, i32 %61, i32 1, i32* %62)
%64 = load i32, i32* %12
%65 = sub nsw i32 %64, 1048576
store i32 %65, i32* %12
br label %46
66: ; preds = %46
%67 = load i32*, i32** %8
%68 = call i32 @fclose(i32* %67)
%69 = load i32*, i32** %9
%70 = call i32 @fclose(i32* %69)
%71 = load i8*, i8** %7
%72 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%73 = load i32, i32* %72
%74 = call i32 @copy_attributes(i8* %71, i32 8, i32 %73)
%75 = load i8*, i8** %10
%76 = call i32 @free(i8* %75)
store i32 0, i32* %4
br label %77
77: ; preds = %66, %26, %22
%78 = load i32, i32* %4
ret i32 %78
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [40 x i8] c"[Error ]\09Insufficient physical memory!\00"
define i32 @file_copy(i8* nocapture %0, i8* %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %4, null
%7 = icmp eq i32* %5, null
%8 = or i1 %6, %7
br i1 %8, label %33, label %9
9: ; preds = %3
%10 = icmp slt i32 %2, 1048576
%11 = select i1 %10, i32 %2, i32 1048576
%12 = tail call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = icmp eq i64 %12, 0
br i1 %14, label %17, label %15
15: ; preds = %9
%16 = icmp sgt i32 %2, 0
br i1 %16, label %20, label %28
17: ; preds = %9
%18 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %15, %20
%21 = phi i32 [ %26, %20 ], [ %2, %15 ]
%22 = icmp ult i32 %21, 1048576
%23 = select i1 %22, i32 %21, i32 1048576
%24 = tail call i32 @fread(i8* %13, i32 %23, i32 1, i32* %4)
%25 = tail call i32 @fwrite(i8* %13, i32 %23, i32 1, i32* %5)
%26 = add nsw i32 %21, -1048576
%27 = icmp ugt i32 %21, 1048576
br i1 %27, label %20, label %28
28: ; preds = %20, %15
%29 = tail call i32 @fclose(i32* %4)
%30 = tail call i32 @fclose(i32* %5)
%31 = tail call i32 @copy_attributes(i8* %1, i32 8, i32 %2)
%32 = tail call i32 @free(i8* %13)
br label %33
33: ; preds = %3, %28
%34 = phi i32 [ 0, %28 ], [ -1, %3 ]
ret i32 %34
}