#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 /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
void copy_file(const char *input, const char *output) {
  char buf[8192];
  int nread;
  FILE *in, *out;
  if (!(in = fopen(input, "r"))) {
    fprintf(stderr, "failed to open %s for reading\n", input);
    perror(" ");
    exit(1);
  }
  if (!(out = fopen(output, "w"))) {
    fprintf(stderr, "failed to open %s for writing\n", output);
    perror(" ");
    exit(1);
  }
  while ((nread = fread(buf, 1, sizeof(buf), in)))
    if (nread != fwrite(buf, 1, nread, out)) {
      fprintf(stderr, "failed writing %s\n", output);
      perror(" ");
      exit(1);
    }
  fclose(in);
  fclose(out);
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x2020(%rbp)
cmp    $0x0,%rax
jne    11d3 <copy_file+0x63>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe4a(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe5d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x2028(%rbp)
cmp    $0x0,%rax
jne    1223 <copy_file+0xb3>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe1d(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe0d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1228 <copy_file+0xb8>
lea    -0x2010(%rbp),%rdi
mov    -0x2020(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2000,%edx
call   1030 <fread@plt>
mov    %eax,-0x2014(%rbp)
cmp    $0x0,%eax
je     12c2 <copy_file+0x152>
mov    -0x2014(%rbp),%eax
mov    %eax,-0x202c(%rbp)
lea    -0x2010(%rbp),%rdi
mov    -0x2014(%rbp),%edx
mov    -0x2028(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    %eax,%ecx
mov    -0x202c(%rbp),%eax
cmp    %ecx,%eax
je     12bd <copy_file+0x14d>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xda2(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xd73(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1228 <copy_file+0xb8>
mov    -0x2020(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x2028(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2000,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe74(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     123e <copy_file+0xce>
mov    %rax,%r15
lea    0xe7f(%rip),%rsi
mov    %r14,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1253 <copy_file+0xe3>
mov    %rax,%r12
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x2000,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %eax,%eax
je     121f <copy_file+0xaf>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r12,%rcx
call   1090 <fwrite@plt>
cmp    %eax,%ebp
je     11c0 <copy_file+0x50>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
lea    0xe11(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r12,%rdi
add    $0x2000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
lea    0xdb4(%rip),%rsi
mov    %rbx,%rdx
jmp    1204 <copy_file+0x94>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
lea    0xdc2(%rip),%rsi
jmp    1201 <copy_file+0x91>

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"failed to open %s for reading\0A\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [31 x i8] c"failed to open %s for writing\0A\00"
@.str.5 = [19 x i8] c"failed writing %s\0A\00"
define void @copy_file(i8* %0, i8* %1) {
%3 = alloca [8192 x i8]
%4 = getelementptr inbounds [8192 x i8], [8192 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 %7, label %12
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%10 = tail call i32 @perror(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i8* %1)
%18 = tail call i32 @perror(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %12, %23
%21 = call i32 @fread(i8* %4, i32 1, i32 8192, i32* %5)
%22 = icmp eq i32 %21, 0
br i1 %22, label %31, label %23
23: ; preds = %20
%24 = call i32 @fwrite(i8* %4, i32 1, i32 %21, i32* %13)
%25 = icmp eq i32 %21, %24
br i1 %25, label %20, label %26
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %1)
%29 = tail call i32 @perror(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %20
%32 = tail call i32 @fclose(i32* %5)
%33 = tail call i32 @fclose(i32* %13)
ret void
}