#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 *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
bool copy_file(const char *source, const char *dest) {
  FILE *in = NULL;
  FILE *out = NULL;
  char block;
  in = fopen(source, "r");
  if (in == NULL) {
    fprintf(stderr, "Failed to open %s.\n", source);
    return false;
  }
  out = fopen(dest, "w");
  if (out == NULL) {
    fprintf(stderr, "Failed to open %s.\n", dest);
    fclose(in);
    return false;
  }
  while (fread(&block, 1, 1, in) == 1)
    fwrite(&block, 1, 1, out);
  fclose(in);
  fclose(out);
  return true;
}


x86 assembly (clang, -O0):
<copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b4 <copy_file+0x64>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe5f(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1255 <copy_file+0x105>
mov    -0x18(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1201 <copy_file+0xb1>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe1b(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1255 <copy_file+0x105>
jmp    1206 <copy_file+0xb6>
mov    -0x20(%rbp),%rcx
lea    -0x29(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    123c <copy_file+0xec>
mov    -0x28(%rbp),%rcx
lea    -0x29(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
jmp    1206 <copy_file+0xb6>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0xe9c(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11e9 <copy_file+0x99>
mov    %rax,%rbx
lea    0xe9e(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1203 <copy_file+0xb3>
mov    %rax,%rbp
lea    0x7(%rsp),%rdi
mov    $0x1,%r14d
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    11d7 <copy_file+0x87>
lea    0x7(%rsp),%r15
nop
movsbl 0x7(%rsp),%edi
mov    %rbp,%rsi
call   1050 <fputc@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     11b0 <copy_file+0x60>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
jmp    1226 <copy_file+0xd6>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
mov    %rbp,%rdx
call   1060 <fprintf@plt>
jmp    1223 <copy_file+0xd3>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"Failed to open %s.\0A\00"
@.str.2 = [2 x i8] c"w\00"
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 i8
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32* null, i32** %6
store i32* null, i32** %7
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %4
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i32 0, i32* %3
br label %41
17: ; preds = %2
%18 = load i8*, i8** %5
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %19, i32** %7
%20 = load i32*, i32** %7
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %28
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %5
%25 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i32*, i32** %6
%27 = call i32 @fclose(i32* %26)
store i32 0, i32* %3
br label %41
28: ; preds = %17
br label %29
29: ; preds = %33, %28
%30 = load i32*, i32** %6
%31 = call i32 @fread(i8* %8, i32 1, i32 1, i32* %30)
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i32*, i32** %7
%35 = call i32 @fwrite(i8* %8, i32 1, i32 1, i32* %34)
br label %29
36: ; preds = %29
%37 = load i32*, i32** %6
%38 = call i32 @fclose(i32* %37)
%39 = load i32*, i32** %7
%40 = call i32 @fclose(i32* %39)
store i32 1, i32* %3
br label %41
41: ; preds = %36, %22, %13
%42 = load i32, i32* %3
ret i32 %42
}

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