#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*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int copyfile(const char *src, const char *dst) {
  char buf[1024];
  FILE *fd_src, *fd_dst;
  size_t count;
  fd_src = fopen(src, "r");
  if (!fd_src)
    return 0;
  fd_dst = fopen(dst, "w");
  if (!fd_dst) {
    fclose(fd_src);
    return 0;
  }
  while (count = fread(buf, 1, 1024, fd_src)) {
    if (ferror(fd_src)) {
      fclose(fd_src);
      fclose(fd_src);
      return 0;
    }
    if (fwrite(buf, 1, count, fd_dst) != count) {
      fclose(fd_src);
      fclose(fd_dst);
      return 0;
    }
    if (feof(fd_src))
      break;
  }
  fclose(fd_src);
  fclose(fd_dst);
  return 1;
}


x86 assembly (clang, -O0):
<copyfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    11a4 <copyfile+0x44>
movl   $0x0,-0x4(%rbp)
jmp    12dd <copyfile+0x17d>
mov    -0x18(%rbp),%rdi
lea    0xe53(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
jne    11e1 <copyfile+0x81>
mov    -0x428(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12dd <copyfile+0x17d>
jmp    11e6 <copyfile+0x86>
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
mov    %rax,-0x438(%rbp)
cmp    $0x0,%rax
je     12be <copyfile+0x15e>
mov    -0x428(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     124e <copyfile+0xee>
mov    -0x428(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x428(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12dd <copyfile+0x17d>
lea    -0x420(%rbp),%rdi
mov    -0x438(%rbp),%rdx
mov    -0x430(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
cmp    -0x438(%rbp),%rax
je     129e <copyfile+0x13e>
mov    -0x428(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x430(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12dd <copyfile+0x17d>
mov    -0x428(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     12b9 <copyfile+0x159>
jmp    12be <copyfile+0x15e>
jmp    11e6 <copyfile+0x86>
mov    -0x428(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x430(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%rbp
lea    0xe87(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1201 <copyfile+0xa1>
mov    %rax,%rbx
lea    0xe75(%rip),%rsi
mov    %rbp,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1206 <copyfile+0xa6>
mov    %rax,%r12
mov    %rsp,%r15
mov    $0x1,%r14d
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     1211 <copyfile+0xb1>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    120b <copyfile+0xab>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rbp,%rdx
mov    %r12,%rcx
call   1080 <fwrite@plt>
cmp    %rbp,%rax
jne    120e <copyfile+0xae>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %rax,%rax
je     11b0 <copyfile+0x50>
jmp    1211 <copyfile+0xb1>
xor    %r14d,%r14d
jmp    1224 <copyfile+0xc4>
xor    %r14d,%r14d
jmp    121c <copyfile+0xbc>
mov    %rbx,%r12
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r12,%rbx
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14d,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
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 @copyfile(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [1024 x i8]
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %2
store i32 0, i32* %3
br label %61
15: ; preds = %2
%16 = load i8*, i8** %5
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %8
%18 = load i32*, i32** %8
%19 = icmp ne i32* %18, null
br i1 %19, label %23, label %20
20: ; preds = %15
%21 = load i32*, i32** %7
%22 = call i32 @fclose(i32* %21)
store i32 0, i32* %3
br label %61
23: ; preds = %15
br label %24
24: ; preds = %55, %23
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%26 = load i32*, i32** %7
%27 = call i64 @fread(i8* %25, i32 1, i32 1024, i32* %26)
store i64 %27, i64* %9
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %56
29: ; preds = %24
%30 = load i32*, i32** %7
%31 = call i64 @ferror(i32* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %29
%34 = load i32*, i32** %7
%35 = call i32 @fclose(i32* %34)
%36 = load i32*, i32** %7
%37 = call i32 @fclose(i32* %36)
store i32 0, i32* %3
br label %61
38: ; preds = %29
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%40 = load i64, i64* %9
%41 = load i32*, i32** %8
%42 = call i64 @fwrite(i8* %39, i32 1, i64 %40, i32* %41)
%43 = load i64, i64* %9
%44 = icmp ne i64 %42, %43
br i1 %44, label %45, label %50
45: ; preds = %38
%46 = load i32*, i32** %7
%47 = call i32 @fclose(i32* %46)
%48 = load i32*, i32** %8
%49 = call i32 @fclose(i32* %48)
store i32 0, i32* %3
br label %61
50: ; preds = %38
%51 = load i32*, i32** %7
%52 = call i64 @feof(i32* %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %50
br label %56
55: ; preds = %50
br label %24
56: ; preds = %54, %24
%57 = load i32*, i32** %7
%58 = call i32 @fclose(i32* %57)
%59 = load i32*, i32** %8
%60 = call i32 @fclose(i32* %59)
store i32 1, i32* %3
br label %61
61: ; preds = %56, %45, %33, %20, %14
%62 = load i32, i32* %3
ret i32 %62
}

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