#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
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(char *source_file, char *target_file) {
  char ch;
  FILE *source, *target;
  printf("Enter name of file to copy\n");
  source = fopen(source_file, "r");
  if (source == NULL) {
    printf("Press any key to exit...\n");
    exit(EXIT_FAILURE);
  }
  printf("Enter name of target file\n");
  target = fopen(target_file, "w");
  if (target == NULL) {
    fclose(source);
    printf("Press any key to exit...\n");
    exit(EXIT_FAILURE);
  }
  while ((ch = fgetc(source)) != EOF)
    fputc(ch, target);
  printf("File copied successfully.\n");
  fclose(source);
  fclose(target);
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe89(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b5 <copy+0x55>
lea    0xe7c(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xe7c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe87(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1203 <copy+0xa3>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe2e(%rip),%rdi
call   1040 <printf@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1208 <copy+0xa8>
mov    -0x20(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%eax
mov    0x2dca(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     123d <copy+0xdd>
mov    -0x11(%rbp),%al
mov    -0x28(%rbp),%rsi
movsbl %al,%edi
call   1060 <fputc@plt>
jmp    1208 <copy+0xa8>
lea    0xe11(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xe92(%rip),%rdi
call   1030 <puts@plt>
lea    0xe82(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1211 <copy+0xb1>
mov    %rax,%r14
lea    0xe86(%rip),%rdi
call   1030 <puts@plt>
lea    0xe5d(%rip),%rsi
mov    %r15,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1209 <copy+0xa9>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <fgetc@plt>
mov    0x2e24(%rip),%r15
cmp    (%r15),%al
je     11e8 <copy+0x88>
nopl   0x0(%rax)
movsbl %al,%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
mov    %r14,%rdi
call   1050 <fgetc@plt>
cmp    (%r15),%al
jne    11d0 <copy+0x70>
lea    0xe4a(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xe3b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"Enter name of file to copy\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [26 x i8] c"Press any key to exit...\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [27 x i8] c"Enter name of target file\0A\00"
@.str.4 = [2 x i8] c"w\00"
@EOF = global i8 0
@.str.5 = [27 x i8] c"File copied successfully.\0A\00"
define void @copy(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32*
%7 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, 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 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%19 = load i8*, i8** %4
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %20, i32** %7
%21 = load i32*, i32** %7
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %29
23: ; preds = %17
%24 = load i32*, i32** %6
%25 = call i32 @fclose(i32* %24)
%26 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %17
br label %30
30: ; preds = %37, %29
%31 = load i32*, i32** %6
%32 = call signext i8 @fgetc(i32* %31)
store i8 %32, i8* %5
%33 = sext i8 %32 to i32
%34 = load i8, i8* @EOF
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %33, %35
br i1 %36, label %37, label %41
37: ; preds = %30
%38 = load i8, i8* %5
%39 = load i32*, i32** %7
%40 = call i32 @fputc(i8 signext %38, i32* %39)
br label %30
41: ; preds = %30
%42 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%43 = load i32*, i32** %6
%44 = call i32 @fclose(i32* %43)
%45 = load i32*, i32** %7
%46 = call i32 @fclose(i32* %45)
ret void
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"r\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [2 x i8] c"w\00"
@EOF = global i8 0
@str = [27 x i8] c"Enter name of file to copy\00"
@str.6 = [26 x i8] c"Enter name of target file\00"
@str.7 = [26 x i8] c"File copied successfully.\00"
@str.9 = [25 x i8] c"Press any key to exit...\00"
define void @copy(i8* nocapture %0, i8* nocapture %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.9, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.6, i64 0, i64 0))
%12 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = tail call signext i8 @fgetc(i32* %4)
%16 = load i8, i8* @EOF
%17 = icmp eq i8 %15, %16
br i1 %17, label %29, label %23
18: ; preds = %10
%19 = tail call i32 @fclose(i32* %4)
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.9, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %14, %23
%24 = phi i8 [ %26, %23 ], [ %15, %14 ]
%25 = tail call i32 @fputc(i8 signext %24, i32* %12)
%26 = tail call signext i8 @fgetc(i32* %4)
%27 = load i8, i8* @EOF
%28 = icmp eq i8 %26, %27
br i1 %28, label %29, label %23
29: ; preds = %23, %14
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0))
%31 = tail call i32 @fclose(i32* %4)
%32 = tail call i32 @fclose(i32* %12)
ret void
}