#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int PAGE_SIZE;
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, size_t);
ssize_t FileCopy(const char *src, const char *dst) {
  ssize_t totalSize = 0, size;
  int fdSrc = open(src, O_RDONLY);
  if (fdSrc < 0) {
    return -1;
  }
  int fdDst = open(dst, O_WRONLY);
  if (fdDst < 0) {
    close(fdSrc);
    return -1;
  }
  char buffer[PAGE_SIZE];
  while ((size = read(fdSrc, buffer, PAGE_SIZE)) > 0) {
    totalSize += size;
    write(fdDst, buffer, (size_t)size);
  }
  close(fdSrc);
  close(fdDst);
  return totalSize;
}


x86 assembly (clang, -O0):
<FileCopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1182 <FileCopy+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    123d <FileCopy+0xfd>
mov    -0x18(%rbp),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11b5 <FileCopy+0x75>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123d <FileCopy+0xfd>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%rsi
mov    -0x24(%rbp),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jle    1220 <FileCopy+0xe0>
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%edi
movslq -0x20(%rbp),%rdx
call   1030 <write@plt>
jmp    11e1 <FileCopy+0xa1>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FileCopy>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1200 <FileCopy+0xc0>
mov    %eax,%r15d
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11f8 <FileCopy+0xb8>
mov    %eax,%r14d
mov    %rsp,-0x30(%rbp)
mov    0x2e4e(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    0x0(%r13),%edx
mov    %r15d,%edi
mov    %r12,%rsi
call   1050 <read@plt>
xor    %ebx,%ebx
test   %eax,%eax
jle    11e2 <FileCopy+0xa2>
nopl   0x0(%rax)
add    %eax,%ebx
mov    %eax,%edx
mov    %r14d,%edi
mov    %r12,%rsi
call   1030 <write@plt>
mov    0x0(%r13),%edx
mov    %r15d,%edi
mov    %r12,%rsi
call   1050 <read@plt>
test   %eax,%eax
jg     11c0 <FileCopy+0x80>
mov    %r15d,%edi
call   1040 <close@plt>
mov    %r14d,%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%rsp
jmp    1200 <FileCopy+0xc0>
mov    %r15d,%edi
call   1040 <close@plt>
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@PAGE_SIZE = global i32 0
define i32 @FileCopy(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %6
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -1, i32* %3
br label %52
18: ; preds = %2
%19 = load i8*, i8** %5
%20 = load i32, i32* @O_WRONLY
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = load i32, i32* %8
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %3
br label %52
27: ; preds = %18
%28 = load i32, i32* @PAGE_SIZE
%29 = zext i32 %28 to i64
%30 = call i8* @llvm.stacksave()
store i8* %30, i8** %10
%31 = alloca i8, i64 %29
store i64 %29, i64* %11
br label %32
32: ; preds = %37, %27
%33 = load i32, i32* %8
%34 = load i32, i32* @PAGE_SIZE
%35 = call i32 @read(i32 %33, i8* %31, i32 %34)
store i32 %35, i32* %7
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %45
37: ; preds = %32
%38 = load i32, i32* %7
%39 = load i32, i32* %6
%40 = add nsw i32 %39, %38
store i32 %40, i32* %6
%41 = load i32, i32* %9
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = call i32 @write(i32 %41, i8* %31, i64 %43)
br label %32
45: ; preds = %32
%46 = load i32, i32* %8
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %9
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* %6
store i32 %50, i32* %3
%51 = load i8*, i8** %10
br label %52
52: ; preds = %45, %24, %17
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@PAGE_SIZE = global i32 0
define i32 @FileCopy(i8* nocapture %0, i8* nocapture %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %33, label %6
6: ; preds = %2
%7 = load i32, i32* @O_WRONLY
%8 = tail call i32 @open(i8* %1, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @close(i32 %4)
br label %33
12: ; preds = %6
%13 = load i32, i32* @PAGE_SIZE
%14 = zext i32 %13 to i64
%15 = tail call i8* @llvm.stacksave()
%16 = alloca i8, i64 %14
%17 = load i32, i32* @PAGE_SIZE
%18 = call i32 @read(i32 %4, i8* %16, i32 %17)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %29
20: ; preds = %12, %20
%21 = phi i32 [ %27, %20 ], [ %18, %12 ]
%22 = phi i32 [ %23, %20 ], [ 0, %12 ]
%23 = add nsw i32 %21, %22
%24 = zext i32 %21 to i64
%25 = call i32 @write(i32 %8, i8* %16, i64 %24)
%26 = load i32, i32* @PAGE_SIZE
%27 = call i32 @read(i32 %4, i8* %16, i32 %26)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %20, label %29
29: ; preds = %20, %12
%30 = phi i32 [ 0, %12 ], [ %23, %20 ]
%31 = tail call i32 @close(i32 %4)
%32 = tail call i32 @close(i32 %8)
tail call void @llvm.stackrestore(i8* %15)
br label %33
33: ; preds = %10, %29, %2
%34 = phi i32 [ -1, %2 ], [ -1, %10 ], [ %30, %29 ]
ret i32 %34
}