#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*/ *fopen(char const *, char *);
size_t fread(unsigned char *, int, int, int /*<<< orphan*/ *);
size_t fwrite(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int copy(const char *fn_in, const char *fn_out) {
  FILE *fd1 = fopen(fn_in, "r");
  if (!fd1)
    return -1;
  FILE *fd2 = fopen(fn_out, "w");
  if (!fd2) {
    fclose(fd1);
    return -2;
  }
  size_t l1;
  unsigned char buffer[8192];
  while ((l1 = fread(buffer, 1, sizeof buffer, fd1)) > 0) {
    size_t l2 = fwrite(buffer, 1, l1, fd2);
    if (l2 == 0 || l2 < l1) {
      fclose(fd1);
      fclose(fd2);
      if (l2 == 0)
        return -3;
      return -4;
    }
  }
  fclose(fd1);
  fclose(fd2);
  return 0;
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117e <copy+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1274 <copy+0x134>
mov    -0x18(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b2 <copy+0x72>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1274 <copy+0x134>
jmp    11b7 <copy+0x77>
lea    -0x2030(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2000,%edx
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jbe    125b <copy+0x11b>
lea    -0x2030(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    %rax,-0x2038(%rbp)
cmpq   $0x0,-0x2038(%rbp)
je     121e <copy+0xde>
mov    -0x2038(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    1256 <copy+0x116>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x2038(%rbp)
jne    124a <copy+0x10a>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1274 <copy+0x134>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1274 <copy+0x134>
jmp    11b7 <copy+0x77>
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    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x2008,%rsp
mov    %rsi,%rbp
lea    0xea9(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11d3 <copy+0x93>
mov    %rax,%r15
lea    0xe97(%rip),%rsi
mov    %rbp,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11da <copy+0x9a>
mov    %rax,%r14
mov    %rsp,%rbp
xchg   %ax,%ax
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x2000,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     11e9 <copy+0xa9>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r14,%rcx
call   1060 <fwrite@plt>
cmp    %rbx,%rax
jae    1180 <copy+0x40>
xor    %ebp,%ebp
test   %rax,%rax
sete   %bpl
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
or     $0xfffffffc,%ebp
jmp    11fb <copy+0xbb>
mov    $0xffffffff,%ebp
jmp    11fb <copy+0xbb>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    $0xfffffffe,%ebp
jmp    11fb <copy+0xbb>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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