#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 errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int copy_file(char *from, char *to) {
  FILE *fromf, *tof;
  int cc;
  char buf[16384];
  fromf = fopen(from, "r");
  if (fromf == NULL)
    return -1;
  tof = fopen(to, "w");
  if (tof == NULL) {
    int esave = errno;
    fclose(fromf);
    errno = esave;
    return -1;
  }
  while ((cc = fread(buf, 1, sizeof(buf), fromf)) > 0) {
    (void)fwrite(buf, cc, 1, tof);
  }
  fclose(fromf);
  fclose(tof);
  return 0;
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4040,%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_file+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <copy_file+0xf1>
mov    -0x18(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d0 <copy_file+0x90>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4034(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x4034(%rbp),%ecx
mov    0x2e1e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <copy_file+0xf1>
jmp    11d5 <copy_file+0x95>
lea    -0x4030(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4000,%edx
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jle    1218 <copy_file+0xd8>
lea    -0x4030(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1060 <fwrite@plt>
jmp    11d5 <copy_file+0x95>
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    $0x4040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x4008,%rsp
mov    %rsi,%rbx
lea    0xea9(%rip),%rsi
call   1050 <fopen@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     11f4 <copy_file+0xb4>
mov    %rax,%r14
lea    0xe8e(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11df <copy_file+0x9f>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x4000,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    11cb <copy_file+0x8b>
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %eax,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x4000,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jg     11a0 <copy_file+0x60>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
jmp    11f4 <copy_file+0xb4>
mov    0x2dfa(%rip),%r15
mov    (%r15),%ebx
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %ebx,(%r15)
mov    %ebp,%eax
add    $0x4008,%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"
@errno = global i32 0
define i32 @copy_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca [16384 x i8]
%10 = alloca i32
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 eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %42
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 eq i32* %19, null
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = load i32, i32* @errno
store i32 %22, i32* %10
%23 = load i32*, i32** %6
%24 = call i32 @fclose(i32* %23)
%25 = load i32, i32* %10
store i32 %25, i32* @errno
store i32 -1, i32* %3
br label %42
26: ; preds = %16
br label %27
27: ; preds = %32, %26
%28 = getelementptr inbounds [16384 x i8], [16384 x i8]* %9, i64 0, i64 0
%29 = load i32*, i32** %6
%30 = call i32 @fread(i8* %28, i32 1, i32 16384, i32* %29)
store i32 %30, i32* %8
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %27
%33 = getelementptr inbounds [16384 x i8], [16384 x i8]* %9, i64 0, i64 0
%34 = load i32, i32* %8
%35 = load i32*, i32** %7
%36 = call i32 @fwrite(i8* %33, i32 %34, i32 1, i32* %35)
br label %27
37: ; preds = %27
%38 = load i32*, i32** %6
%39 = call i32 @fclose(i32* %38)
%40 = load i32*, i32** %7
%41 = call i32 @fclose(i32* %40)
store i32 0, i32* %3
br label %42
42: ; preds = %37, %21, %15
%43 = load i32, i32* %3
ret i32 %43
}

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