#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int file_copy(char *srcpath, char *destpath) {
  FILE *fsrc, *fdest;
  char c;
  fsrc = fopen(srcpath, "rb");
  if (!fsrc) {
    return -1;
  }
  fdest = fopen(destpath, "wb");
  if (!fdest) {
    fclose(fsrc);
    return -1;
  }
  while (!feof(fsrc)) {
    c = fgetc(fsrc);
    if (ferror(fsrc)) {
      fclose(fsrc);
      fclose(fdest);
      return -1;
    }
    if (!feof(fsrc)) {
      fputc(c, fdest);
    }
    if (ferror(fdest)) {
      fclose(fsrc);
      fclose(fdest);
      return -1;
    }
  }
  fclose(fsrc);
  fclose(fdest);
  return 0;
}


x86 assembly (clang, -O0):
<file_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119b <file_copy+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <file_copy+0x13f>
mov    -0x18(%rbp),%rdi
lea    0xe5d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11cf <file_copy+0x6f>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <file_copy+0x13f>
jmp    11d4 <file_copy+0x74>
mov    -0x20(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11f2 <file_copy+0x92>
jmp    1286 <file_copy+0x126>
mov    -0x20(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x29(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     122f <file_copy+0xcf>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <file_copy+0x13f>
mov    -0x20(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
jne    1250 <file_copy+0xf0>
mov    -0x29(%rbp),%al
mov    -0x28(%rbp),%rsi
movsbl %al,%edi
call   1060 <fputc@plt>
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1281 <file_copy+0x121>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129f <file_copy+0x13f>
jmp    11d4 <file_copy+0x74>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_copy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
lea    0xe8f(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11e9 <file_copy+0x89>
mov    %rax,%rbp
lea    0xe7e(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f1 <file_copy+0x91>
mov    %rax,%r14
mov    $0xffffffff,%r15d
jmp    11ad <file_copy+0x4d>
nopl   (%rax)
mov    %r14,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    11ff <file_copy+0x9f>
mov    %rbp,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    11fc <file_copy+0x9c>
mov    %rbp,%rdi
call   1050 <fgetc@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    11ff <file_copy+0x9f>
mov    %rbp,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    11a0 <file_copy+0x40>
movsbl %bl,%edi
mov    %r14,%rsi
call   1060 <fputc@plt>
jmp    11a0 <file_copy+0x40>
mov    $0xffffffff,%r15d
jmp    120f <file_copy+0xaf>
mov    $0xffffffff,%r15d
mov    %rbp,%r14
jmp    1207 <file_copy+0xa7>
xor    %r15d,%r15d
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r15d,%eax
add    $0x8,%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 @file_copy(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %2
store i32 -1, i32* %3
br label %62
14: ; preds = %2
%15 = load i8*, i8** %5
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %7
%17 = load i32*, i32** %7
%18 = icmp ne i32* %17, null
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = load i32*, i32** %6
%21 = call i32 @fclose(i32* %20)
store i32 -1, i32* %3
br label %62
22: ; preds = %14
br label %23
23: ; preds = %56, %22
%24 = load i32*, i32** %6
%25 = call i32 @feof(i32* %24)
%26 = icmp ne i32 %25, 0
%27 = xor i1 %26, true
br i1 %27, label %28, label %57
28: ; preds = %23
%29 = load i32*, i32** %6
%30 = call signext i8 @fgetc(i32* %29)
store i8 %30, i8* %8
%31 = load i32*, i32** %6
%32 = call i64 @ferror(i32* %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %28
%35 = load i32*, i32** %6
%36 = call i32 @fclose(i32* %35)
%37 = load i32*, i32** %7
%38 = call i32 @fclose(i32* %37)
store i32 -1, i32* %3
br label %62
39: ; preds = %28
%40 = load i32*, i32** %6
%41 = call i32 @feof(i32* %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %47, label %43
43: ; preds = %39
%44 = load i8, i8* %8
%45 = load i32*, i32** %7
%46 = call i32 @fputc(i8 signext %44, i32* %45)
br label %47
47: ; preds = %43, %39
%48 = load i32*, i32** %7
%49 = call i64 @ferror(i32* %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %47
%52 = load i32*, i32** %6
%53 = call i32 @fclose(i32* %52)
%54 = load i32*, i32** %7
%55 = call i32 @fclose(i32* %54)
store i32 -1, i32* %3
br label %62
56: ; preds = %47
br label %23
57: ; preds = %23
%58 = load i32*, i32** %6
%59 = call i32 @fclose(i32* %58)
%60 = load i32*, i32** %7
%61 = call i32 @fclose(i32* %60)
store i32 0, i32* %3
br label %62
62: ; preds = %57, %51, %34, %19, %13
%63 = load i32, i32* %3
ret i32 %63
}

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