#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 */
char EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
void copy_file(char *filename, char *backup_name) {
  FILE *source;
  FILE *backup;
  char c;
  source = fopen(filename, "r");
  if (source == NULL) {
    printf("Error trying to open file\n");
    return;
  }
  backup = fopen(backup_name, "w");
  if (backup == NULL) {
    fclose(source);
    printf("Error creating backup file\n");
    return;
  }
  while ((c = fgetc(source)) != EOF) {
    fputc(c, backup);
  }
  printf("Backup sucessfully created\n");
  fclose(source);
  fclose(backup);
}


x86 assembly (clang, -O0):
<copy_file>:
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    1190 <copy_file+0x40>
lea    0xe7c(%rip),%rdi
call   1040 <printf@plt>
jmp    1221 <copy_file+0xd1>
mov    -0x10(%rbp),%rdi
lea    0xe82(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c9 <copy_file+0x79>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe60(%rip),%rdi
call   1040 <printf@plt>
jmp    1221 <copy_file+0xd1>
jmp    11ce <copy_file+0x7e>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x21(%rbp)
movsbl %al,%eax
mov    0x2e04(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1203 <copy_file+0xb3>
mov    -0x21(%rbp),%al
mov    -0x20(%rbp),%rsi
movsbl %al,%edi
call   1060 <fputc@plt>
jmp    11ce <copy_file+0x7e>
lea    0xe31(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
lea    0xea1(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d9 <copy_file+0x89>
mov    %rax,%rbx
lea    0xe8f(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11e2 <copy_file+0x92>
mov    %rax,%r14
mov    %rbx,%rdi
call   1050 <fgetc@plt>
mov    0x2e56(%rip),%r15
cmp    (%r15),%al
je     11b8 <copy_file+0x68>
nopw   0x0(%rax,%rax,1)
movsbl %al,%edi
mov    %r14,%rsi
call   1060 <fputc@plt>
mov    %rbx,%rdi
call   1050 <fgetc@plt>
cmp    (%r15),%al
jne    11a0 <copy_file+0x50>
lea    0xe45(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <fclose@plt>
lea    0xe5a(%rip),%rdi
jmp    11f1 <copy_file+0xa1>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe2e(%rip),%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [27 x i8] c"Error trying to open file\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [28 x i8] c"Error creating backup file\0A\00"
@EOF = global i8 0
@.str.4 = [28 x i8] c"Backup sucessfully created\0A\00"
define void @copy_file(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8
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 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %41
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 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 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %41
23: ; preds = %14
br label %24
24: ; preds = %31, %23
%25 = load i32*, i32** %5
%26 = call signext i8 @fgetc(i32* %25)
store i8 %26, i8* %7
%27 = sext i8 %26 to i32
%28 = load i8, i8* @EOF
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %27, %29
br i1 %30, label %31, label %35
31: ; preds = %24
%32 = load i8, i8* %7
%33 = load i32*, i32** %6
%34 = call i32 @fputc(i8 signext %32, i32* %33)
br label %24
35: ; preds = %24
%36 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%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 = %35, %19, %12
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
@EOF = global i8 0
@str = [27 x i8] c"Backup sucessfully created\00"
@str.5 = [27 x i8] c"Error creating backup file\00"
@str.6 = [26 x i8] c"Error trying to open file\00"
define void @copy_file(i8* nocapture %0, i8* nocapture %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 %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.6, i64 0, i64 0))
br label %27
7: ; preds = %2
%8 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %14, label %10
10: ; preds = %7
%11 = tail call signext i8 @fgetc(i32* %3)
%12 = load i8, i8* @EOF
%13 = icmp eq i8 %11, %12
br i1 %13, label %23, label %17
14: ; preds = %7
%15 = tail call i32 @fclose(i32* %3)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.5, i64 0, i64 0))
br label %27
17: ; preds = %10, %17
%18 = phi i8 [ %20, %17 ], [ %11, %10 ]
%19 = tail call i32 @fputc(i8 signext %18, i32* %8)
%20 = tail call signext i8 @fgetc(i32* %3)
%21 = load i8, i8* @EOF
%22 = icmp eq i8 %20, %21
br i1 %22, label %23, label %17
23: ; preds = %17, %10
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%25 = tail call i32 @fclose(i32* %3)
%26 = tail call i32 @fclose(i32* %8)
br label %27
27: ; preds = %23, %14, %5
ret void
}