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

/* Variables and functions */
int /*<<< orphan*/ clearerr(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
char *malloc(int);
uint8_t copyfile(char *src, char *dst) {
  size_t nread;
  char *buf;
  FILE *inf;
  FILE *outf;
  buf = NULL;
  inf = NULL;
  outf = NULL;
  inf = fopen(src, "rb");
  if (inf == NULL)
    goto fail;
  outf = fopen(dst, "wb");
  if (outf == NULL)
    goto fail;
  buf = malloc(1048576);
  if (buf == NULL)
    goto fail;
  while (feof(inf) == 0) {
    clearerr(inf);
    clearerr(outf);
    nread = fread(buf, 1, 1048576, inf);
    if (ferror(inf))
      goto fail;
    if (nread == 0)
      goto fail;
    if (fwrite(buf, 1, nread, outf) != nread)
      goto fail;
    if (ferror(outf))
      goto fail;
  }
  fclose(inf);
  fclose(outf);
  free(buf);
  return 0;
fail:
  if (inf != NULL)
    fclose(inf);
  if (outf != NULL)
    fclose(outf);
  if (buf != NULL)
    free(buf);
  return 1;
}


x86 assembly (clang, -O0):
<copyfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe3d(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11dc <copyfile+0x4c>
jmp    12f4 <copyfile+0x164>
mov    -0x18(%rbp),%rdi
lea    0xe1c(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1200 <copyfile+0x70>
jmp    12f4 <copyfile+0x164>
mov    $0x100000,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    121e <copyfile+0x8e>
jmp    12f4 <copyfile+0x164>
jmp    1223 <copyfile+0x93>
mov    -0x30(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
jne    12cd <copyfile+0x13d>
mov    -0x30(%rbp),%rdi
call   1080 <clearerr@plt>
mov    -0x38(%rbp),%rdi
call   1080 <clearerr@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
mov    $0x100000,%edx
call   1050 <fread@plt>
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     127b <copyfile+0xeb>
jmp    12f4 <copyfile+0x164>
cmpq   $0x0,-0x20(%rbp)
jne    128b <copyfile+0xfb>
jmp    12f4 <copyfile+0x164>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
cmp    -0x20(%rbp),%rax
je     12b0 <copyfile+0x120>
jmp    12f4 <copyfile+0x164>
mov    -0x38(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     12c8 <copyfile+0x138>
jmp    12f4 <copyfile+0x164>
jmp    1223 <copyfile+0x93>
mov    -0x30(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1337 <copyfile+0x1a7>
cmpq   $0x0,-0x30(%rbp)
je     1308 <copyfile+0x178>
mov    -0x30(%rbp),%rdi
call   1060 <fclose@plt>
cmpq   $0x0,-0x38(%rbp)
je     131c <copyfile+0x18c>
mov    -0x38(%rbp),%rdi
call   1060 <fclose@plt>
cmpq   $0x0,-0x28(%rbp)
je     1330 <copyfile+0x1a0>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbp
lea    0xe5e(%rip),%rsi
call   10a0 <fopen@plt>
mov    $0x1,%r14d
test   %rax,%rax
je     12a9 <copyfile+0x119>
mov    %rax,%rbx
lea    0xe43(%rip),%rsi
mov    %rbp,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1262 <copyfile+0xd2>
mov    %rax,%r12
mov    $0x100000,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     126c <copyfile+0xdc>
mov    %rax,%r15
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %rax,%rax
jne    128e <copyfile+0xfe>
mov    %rbx,%rdi
call   1080 <clearerr@plt>
mov    %r12,%rdi
call   1080 <clearerr@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x100000,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %rbp,%rbp
je     126f <copyfile+0xdf>
test   %rax,%rax
jne    126f <copyfile+0xdf>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %rbp,%rdx
mov    %r12,%rcx
call   10b0 <fwrite@plt>
cmp    %rbp,%rax
jne    126f <copyfile+0xdf>
mov    %r12,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     11f0 <copyfile+0x60>
jmp    126f <copyfile+0xdf>
mov    %rbx,%rdi
call   1060 <fclose@plt>
jmp    12a9 <copyfile+0x119>
xor    %r15d,%r15d
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
test   %r15,%r15
je     12a9 <copyfile+0x119>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    12a9 <copyfile+0x119>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
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 @copyfile(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %7
store i32* null, i32** %8
store i32* null, i32** %9
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
br label %68
15: ; preds = %2
%16 = load i8*, i8** %5
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %9
%18 = load i32*, i32** %9
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %21
20: ; preds = %15
br label %68
21: ; preds = %15
%22 = call i8* @malloc(i32 1048576)
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %26
25: ; preds = %21
br label %68
26: ; preds = %21
br label %27
27: ; preds = %60, %26
%28 = load i32*, i32** %8
%29 = call i64 @feof(i32* %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %61
31: ; preds = %27
%32 = load i32*, i32** %8
%33 = call i32 @clearerr(i32* %32)
%34 = load i32*, i32** %9
%35 = call i32 @clearerr(i32* %34)
%36 = load i8*, i8** %7
%37 = load i32*, i32** %8
%38 = call i64 @fread(i8* %36, i32 1, i32 1048576, i32* %37)
store i64 %38, i64* %6
%39 = load i32*, i32** %8
%40 = call i64 @ferror(i32* %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %31
br label %68
43: ; preds = %31
%44 = load i64, i64* %6
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %43
br label %68
47: ; preds = %43
%48 = load i8*, i8** %7
%49 = load i64, i64* %6
%50 = load i32*, i32** %9
%51 = call i64 @fwrite(i8* %48, i32 1, i64 %49, i32* %50)
%52 = load i64, i64* %6
%53 = icmp ne i64 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %47
br label %68
55: ; preds = %47
%56 = load i32*, i32** %9
%57 = call i64 @ferror(i32* %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %55
br label %68
60: ; preds = %55
br label %27
61: ; preds = %27
%62 = load i32*, i32** %8
%63 = call i32 @fclose(i32* %62)
%64 = load i32*, i32** %9
%65 = call i32 @fclose(i32* %64)
%66 = load i8*, i8** %7
%67 = call i32 @free(i8* %66)
store i32 0, i32* %3
br label %87
68: ; preds = %59, %54, %46, %42, %25, %20, %14
%69 = load i32*, i32** %8
%70 = icmp ne i32* %69, null
br i1 %70, label %71, label %74
71: ; preds = %68
%72 = load i32*, i32** %8
%73 = call i32 @fclose(i32* %72)
br label %74
74: ; preds = %71, %68
%75 = load i32*, i32** %9
%76 = icmp ne i32* %75, null
br i1 %76, label %77, label %80
77: ; preds = %74
%78 = load i32*, i32** %9
%79 = call i32 @fclose(i32* %78)
br label %80
80: ; preds = %77, %74
%81 = load i8*, i8** %7
%82 = icmp ne i8* %81, null
br i1 %82, label %83, label %86
83: ; preds = %80
%84 = load i8*, i8** %7
%85 = call i32 @free(i8* %84)
br label %86
86: ; preds = %83, %80
store i32 1, i32* %3
br label %87
87: ; preds = %86, %61
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"wb\00"
define i32 @copyfile(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 %41, 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 %8, label %10
8: ; preds = %5
%9 = tail call i32 @fclose(i32* %3)
br label %41
10: ; preds = %5
%11 = tail call dereferenceable_or_null(1048576) i8* @malloc(i32 1048576)
%12 = icmp eq i8* %11, null
br i1 %12, label %34, label %13
13: ; preds = %10, %27
%14 = tail call i64 @feof(i32* %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %30
16: ; preds = %13
%17 = tail call i32 @clearerr(i32* %3)
%18 = tail call i32 @clearerr(i32* %6)
%19 = tail call i64 @fread(i8* %11, i32 1, i32 1048576, i32* %3)
%20 = tail call i64 @ferror(i32* %3)
%21 = icmp ne i64 %20, 0
%22 = icmp eq i64 %19, 0
%23 = or i1 %22, %21
br i1 %23, label %34, label %24
24: ; preds = %16
%25 = tail call i64 @fwrite(i8* %11, i32 1, i64 %19, i32* %6)
%26 = icmp eq i64 %25, %19
br i1 %26, label %27, label %34
27: ; preds = %24
%28 = tail call i64 @ferror(i32* %6)
%29 = icmp eq i64 %28, 0
br i1 %29, label %13, label %34
30: ; preds = %13
%31 = tail call i32 @fclose(i32* %3)
%32 = tail call i32 @fclose(i32* %6)
%33 = tail call i32 @free(i8* %11)
br label %41
34: ; preds = %16, %24, %27, %10
%35 = phi i8* [ null, %10 ], [ %11, %27 ], [ %11, %24 ], [ %11, %16 ]
%36 = tail call i32 @fclose(i32* %3)
%37 = tail call i32 @fclose(i32* %6)
%38 = icmp eq i8* %35, null
br i1 %38, label %41, label %39
39: ; preds = %34
%40 = tail call i32 @free(i8* %35)
br label %41
41: ; preds = %8, %2, %34, %39, %30
%42 = phi i32 [ 0, %30 ], [ 1, %39 ], [ 1, %34 ], [ 1, %2 ], [ 1, %8 ]
ret i32 %42
}