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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(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 fcopy(const char *src, const char *dst) {
  char buf[0x1000];
  FILE *fr, *fw;
  int res = -1;
  size_t rd;
  if (!(fr = fopen(src, "rb")))
    return (res);
  if (!(fw = fopen(dst, "wb")))
    goto err_w;
  while (!feof(fr)) {
    if (!(rd = fread(buf, 1, sizeof(buf), fr))) {
      if (ferror(fr))
        goto err;
      continue;
    }
    if (!fwrite(buf, 1, rd, fw))
      goto err;
  }
  res = 0;
err:
  fclose(fw);
err_w:
  fclose(fr);
  return (res);
}


x86 assembly (clang, -O0):
<fcopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x1034(%rbp)
ff ff ff
mov    -0x10(%rbp),%rdi
lea    0xe78(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
jne    11ac <fcopy+0x4c>
mov    -0x1034(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12a3 <fcopy+0x143>
mov    -0x18(%rbp),%rdi
lea    0xe4c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x1030(%rbp)
cmp    $0x0,%rax
jne    11d2 <fcopy+0x72>
jmp    128e <fcopy+0x12e>
jmp    11d7 <fcopy+0x77>
mov    -0x1028(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11f8 <fcopy+0x98>
jmp    1278 <fcopy+0x118>
lea    -0x1020(%rbp),%rdi
mov    -0x1028(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1040 <fread@plt>
mov    %rax,-0x1040(%rbp)
cmp    $0x0,%rax
jne    1246 <fcopy+0xe6>
mov    -0x1028(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1241 <fcopy+0xe1>
jmp    1282 <fcopy+0x122>
jmp    11d7 <fcopy+0x77>
lea    -0x1020(%rbp),%rdi
mov    -0x1040(%rbp),%rdx
mov    -0x1030(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
cmp    $0x0,%eax
jne    1273 <fcopy+0x113>
jmp    1282 <fcopy+0x122>
jmp    11d7 <fcopy+0x77>
movl   $0x0,-0x1034(%rbp)
mov    -0x1030(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x1028(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x1034(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fcopy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%rbp
lea    0xe89(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     121a <fcopy+0xba>
mov    %rax,%rbx
lea    0xe74(%rip),%rsi
mov    %rbp,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1222 <fcopy+0xc2>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1060 <feof@plt>
xor    %r14d,%r14d
test   %eax,%eax
jne    1210 <fcopy+0xb0>
mov    %rsp,%r15
jmp    11d9 <fcopy+0x79>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    120a <fcopy+0xaa>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    1210 <fcopy+0xb0>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     11c0 <fcopy+0x60>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %rbp,%rcx
call   1080 <fwrite@plt>
test   %eax,%eax
jne    11cd <fcopy+0x6d>
mov    $0xffffffff,%r14d
mov    %rbp,%rdi
call   1050 <fclose@plt>
jmp    1228 <fcopy+0xc8>
mov    $0xffffffff,%r14d
jmp    1230 <fcopy+0xd0>
mov    $0xffffffff,%r14d
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14d,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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