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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int stat(char const *, struct stat *);
int cpfile(const char *src, const char *dst, const int overWrite) {
  char buf[4096];
  struct stat info;
  size_t size;
  if (!overWrite) {
    if (-1 != stat(dst, &info)) {
      return -1;
    }
  }
  FILE *source = fopen(src, "rb");
  FILE *dest = fopen(dst, "wb");
  if ((source) && (dest)) {
    while ((size = fread(buf, 1, 4096, source))) {
      fwrite(buf, 1, size, dest);
    }
    fclose(source);
    fclose(dest);
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<cpfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11a0 <cpfile+0x50>
mov    -0x18(%rbp),%rdi
lea    -0x1028(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     119b <cpfile+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <cpfile+0x11c>
jmp    11a0 <cpfile+0x50>
mov    -0x10(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x1038(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe41(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x1040(%rbp)
cmpq   $0x0,-0x1038(%rbp)
je     1265 <cpfile+0x115>
cmpq   $0x0,-0x1040(%rbp)
je     1265 <cpfile+0x115>
jmp    11ef <cpfile+0x9f>
lea    -0x1020(%rbp),%rdi
mov    -0x1038(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
mov    %rax,-0x1030(%rbp)
cmp    $0x0,%rax
je     1241 <cpfile+0xf1>
lea    -0x1020(%rbp),%rdi
mov    -0x1030(%rbp),%rdx
mov    -0x1040(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
jmp    11ef <cpfile+0x9f>
mov    -0x1038(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1040(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    126c <cpfile+0x11c>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpfile>:
push   %r15
push   %r14
push   %rbx
sub    $0x1010,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
test   %edx,%edx
je     1211 <cpfile+0xc1>
lea    0xe8f(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
mov    %rax,%r15
lea    0xe80(%rip),%rsi
mov    %r14,%rdi
call   1060 <fopen@plt>
mov    %rax,%r14
mov    $0xffffffff,%eax
test   %r15,%r15
je     122e <cpfile+0xde>
test   %r14,%r14
je     122e <cpfile+0xde>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     11fd <cpfile+0xad>
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r14,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %rax,%rax
jne    11d0 <cpfile+0x80>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    122e <cpfile+0xde>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1050 <stat@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ecx
je     116a <cpfile+0x1a>
add    $0x1010,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
define i32 @cpfile(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [4096 x i8]
%9 = alloca %struct.stat
%10 = alloca i64
%11 = alloca i32*
%12 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%13 = load i32, i32* %7
%14 = icmp ne i32 %13, 0
br i1 %14, label %21, label %15
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = call i32 @stat(i8* %16, %struct.stat* %9)
%18 = icmp ne i32 -1, %17
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 -1, i32* %4
br label %48
20: ; preds = %15
br label %21
21: ; preds = %20, %3
%22 = load i8*, i8** %5
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %23, i32** %11
%24 = load i8*, i8** %6
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %12
%26 = load i32*, i32** %11
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %47
28: ; preds = %21
%29 = load i32*, i32** %12
%30 = icmp ne i32* %29, null
br i1 %30, label %31, label %47
31: ; preds = %28
br label %32
32: ; preds = %37, %31
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%34 = load i32*, i32** %11
%35 = call i64 @fread(i8* %33, i32 1, i32 4096, i32* %34)
store i64 %35, i64* %10
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %32
%38 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%39 = load i64, i64* %10
%40 = load i32*, i32** %12
%41 = call i32 @fwrite(i8* %38, i32 1, i64 %39, i32* %40)
br label %32
42: ; preds = %32
%43 = load i32*, i32** %11
%44 = call i32 @fclose(i32* %43)
%45 = load i32*, i32** %12
%46 = call i32 @fclose(i32* %45)
store i32 0, i32* %4
br label %48
47: ; preds = %28, %21
store i32 -1, i32* %4
br label %48
48: ; preds = %47, %42, %19
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
define i32 @cpfile(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = alloca [4096 x i8]
%5 = alloca %struct.stat
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = bitcast %struct.stat* %5 to i8*
%8 = icmp eq i32 %2, 0
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = call i32 @stat(i8* %1, %struct.stat* %5)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %29
12: ; preds = %9, %3
%13 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp ne i32* %13, null
%16 = icmp ne i32* %14, null
%17 = and i1 %15, %16
br i1 %17, label %18, label %29
18: ; preds = %12
%19 = call i64 @fread(i8* %6, i32 1, i32 4096, i32* %13)
%20 = icmp eq i64 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %18, %21
%22 = phi i64 [ %24, %21 ], [ %19, %18 ]
%23 = call i32 @fwrite(i8* %6, i32 1, i64 %22, i32* %14)
%24 = call i64 @fread(i8* %6, i32 1, i32 4096, i32* %13)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %21
26: ; preds = %21, %18
%27 = tail call i32 @fclose(i32* %13)
%28 = tail call i32 @fclose(i32* %14)
br label %29
29: ; preds = %26, %12, %9
%30 = phi i32 [ -1, %9 ], [ 0, %26 ], [ -1, %12 ]
ret i32 %30
}