#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 EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
void lerror(char *, char *);
void file_copy(char *src, char *dest) {
  FILE *fp_src = fopen(src, "r");
  if (fp_src == NULL)
    return lerror("could not open file '%s'", src);
  FILE *fp_dest = fopen(dest, "w+");
  if (fp_dest == NULL) {
    fclose(fp_src);
    return lerror("could not open file '%s' for writing", dest);
  }
  int c = fgetc(fp_src);
  while (c != EOF) {
    fputc(c, fp_dest);
    c = fgetc(fp_src);
  }
  fclose(fp_src);
  fclose(fp_dest);
}


x86 assembly (clang, -O0):
<file_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1194 <file_copy+0x44>
mov    -0x8(%rbp),%rsi
lea    0xe78(%rip),%rdi
call   1030 <lerror@plt>
jmp    121e <file_copy+0xce>
mov    -0x10(%rbp),%rdi
lea    0xe7c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d1 <file_copy+0x81>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rsi
lea    0xe57(%rip),%rdi
call   1030 <lerror@plt>
jmp    121e <file_copy+0xce>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2e01(%rip),%rcx
cmp    (%rcx),%eax
je     120c <file_copy+0xbc>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1060 <fputc@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x24(%rbp)
jmp    11dd <file_copy+0x8d>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_copy>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe9e(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11cc <file_copy+0x7c>
mov    %rax,%rbx
lea    0xea5(%rip),%rsi
mov    %r15,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d8 <file_copy+0x88>
mov    %rax,%r14
mov    %rbx,%rdi
call   1050 <fgetc@plt>
mov    0x2e53(%rip),%r15
cmp    (%r15),%eax
je     11b7 <file_copy+0x67>
nopw   0x0(%rax,%rax,1)
mov    %eax,%edi
mov    %r14,%rsi
call   1060 <fputc@plt>
mov    %rbx,%rdi
call   1050 <fgetc@plt>
cmp    (%r15),%eax
jne    11a0 <file_copy+0x50>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <fclose@plt>
lea    0xe2f(%rip),%rdi
mov    %r14,%rsi
jmp    11ea <file_copy+0x9a>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe37(%rip),%rdi
mov    %r15,%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <lerror@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [25 x i8] c"could not open file '%s'\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [37 x i8] c"could not open file '%s' for writing\00"
@EOF = global i32 0
define void @file_copy(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i8*, i8** %3
br label %41
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %16, i32** %6
%17 = load i32*, i32** %6
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i32*, i32** %5
%21 = call i32 @fclose(i32* %20)
%22 = load i8*, i8** %4
br label %41
23: ; preds = %14
%24 = load i32*, i32** %5
%25 = call i32 @fgetc(i32* %24)
store i32 %25, i32* %7
br label %26
26: ; preds = %30, %23
%27 = load i32, i32* %7
%28 = load i32, i32* @EOF
%29 = icmp ne i32 %27, %28
br i1 %29, label %30, label %36
30: ; preds = %26
%31 = load i32, i32* %7
%32 = load i32*, i32** %6
%33 = call i32 @fputc(i32 %31, i32* %32)
%34 = load i32*, i32** %5
%35 = call i32 @fgetc(i32* %34)
store i32 %35, i32* %7
br label %26
36: ; preds = %26
%37 = load i32*, i32** %5
%38 = call i32 @fclose(i32* %37)
%39 = load i32*, i32** %6
%40 = call i32 @fclose(i32* %39)
br label %41
41: ; preds = %36, %19, %12
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [25 x i8] c"could not open file '%s'\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [37 x i8] c"could not open file '%s' for writing\00"
@EOF = global i32 0
define void @file_copy(i8* %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %6
5: ; preds = %2
tail call void @lerror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %24
6: ; preds = %2
%7 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @fclose(i32* %3)
tail call void @lerror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %24
11: ; preds = %6
%12 = tail call i32 @fgetc(i32* %3)
%13 = load i32, i32* @EOF
%14 = icmp eq i32 %12, %13
br i1 %14, label %21, label %15
15: ; preds = %11, %15
%16 = phi i32 [ %18, %15 ], [ %12, %11 ]
%17 = tail call i32 @fputc(i32 %16, i32* %7)
%18 = tail call i32 @fgetc(i32* %3)
%19 = load i32, i32* @EOF
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %15
21: ; preds = %15, %11
%22 = tail call i32 @fclose(i32* %3)
%23 = tail call i32 @fclose(i32* %7)
br label %24
24: ; preds = %9, %21, %5
ret void
}