#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 __off_t;

/* Variables and functions */
int O_CREAT;
int O_RDONLY;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int fSize(char *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int, char *, size_t);
int /*<<< orphan*/ write(int, char *, size_t);
void copy(char *source, char *destination) {
  int sourceDesc, targetDesc;
  char *buffer;
  sourceDesc = open(source, O_RDONLY, S_IRUSR);
  targetDesc = open(destination, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
  if (sourceDesc == -1) {
    printf("ERROR: Invalid source file\n");
    return;
  }
  if (targetDesc == -1) {
    printf("ERROR: Invalid target file\n");
    return;
  }
  __off_t sourceSize;
  if ((sourceSize = fSize(source)) != -1) {
    buffer = malloc((size_t)sourceSize);
    if (buffer == NULL) {
      printf("ERROR: Could not allocate enough memory to buffer\n");
      return;
    }
    while (read(sourceDesc, buffer, (size_t)sourceSize) > 0) {
      write(targetDesc, buffer, (size_t)sourceSize);
    }
    if (read(sourceDesc, buffer, (size_t)sourceSize) < 0) {
      printf("ERROR: Something went wrong while reading the file.\n");
    } else {
      close(sourceDesc);
      close(targetDesc);
      free(buffer);
    }
  } else {
    printf("ERROR: Could not determine file size.\n");
  }
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    0x2df6(%rip),%rax
or     (%rax),%esi
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    0x2e04(%rip),%rax
or     (%rax),%edx
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f9 <copy+0x79>
lea    0xe11(%rip),%rdi
call   1050 <printf@plt>
jmp    12db <copy+0x15b>
cmpl   $0xffffffff,-0x18(%rbp)
jne    1214 <copy+0x94>
lea    0xe12(%rip),%rdi
call   1050 <printf@plt>
jmp    12db <copy+0x15b>
mov    -0x8(%rbp),%rdi
call   1090 <fSize@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     12cf <copy+0x14f>
movslq -0x24(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1252 <copy+0xd2>
lea    0xdf0(%rip),%rdi
call   1050 <printf@plt>
jmp    12db <copy+0x15b>
jmp    1257 <copy+0xd7>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x24(%rbp),%rdx
call   1070 <read@plt>
cmp    $0x0,%rax
jle    1286 <copy+0x106>
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x24(%rbp),%rdx
call   1040 <write@plt>
jmp    1257 <copy+0xd7>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x24(%rbp),%rdx
call   1070 <read@plt>
cmp    $0x0,%rax
jge    12b1 <copy+0x131>
lea    0xdc4(%rip),%rdi
call   1050 <printf@plt>
jmp    12ca <copy+0x14a>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    12db <copy+0x15b>
lea    0xdca(%rip),%rdi
call   1050 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
mov    0x2e2b(%rip),%r14
mov    (%r14),%edx
call   10a0 <open@plt>
mov    %eax,%r15d
mov    0x2e49(%rip),%rax
mov    0x2e02(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
or     (%r14),%edx
mov    %rbp,%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%r15d
je     1223 <copy+0xa3>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     122c <copy+0xac>
mov    %rbx,%rdi
call   1090 <fSize@plt>
cmp    $0xffffffff,%eax
je     126a <copy+0xea>
movslq %eax,%rbx
mov    %rbx,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1273 <copy+0xf3>
mov    %rax,%rbp
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1070 <read@plt>
test   %rax,%rax
jle    1235 <copy+0xb5>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1050 <write@plt>
jmp    1200 <copy+0x80>
lea    0xe7d(%rip),%rdi
jmp    1283 <copy+0x103>
lea    0xe59(%rip),%rdi
jmp    1283 <copy+0x103>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1070 <read@plt>
test   %rax,%rax
js     127c <copy+0xfc>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
lea    0xd8f(%rip),%rdi
jmp    1283 <copy+0x103>
lea    0xde0(%rip),%rdi
jmp    1283 <copy+0x103>
lea    0xda3(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <puts@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IWUSR = global i32 0
@.str = [28 x i8] c"ERROR: Invalid source file\0A\00"
@.str.1 = [28 x i8] c"ERROR: Invalid target file\0A\00"
@.str.2 = [51 x i8] c"ERROR: Could not allocate enough memory to buffer\0A\00"
@.str.3 = [53 x i8] c"ERROR: Something went wrong while reading the file.\0A\00"
@.str.4 = [39 x i8] c"ERROR: Could not determine file size.\0A\00"
define void @copy(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDONLY
%11 = load i32, i32* @S_IRUSR
%12 = call i32 @open(i8* %9, i32 %10, i32 %11)
store i32 %12, i32* %5
%13 = load i8*, i8** %4
%14 = load i32, i32* @O_CREAT
%15 = load i32, i32* @O_WRONLY
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IRUSR
%18 = load i32, i32* @S_IWUSR
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %13, i32 %16, i32 %19)
store i32 %20, i32* %6
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %75
25: ; preds = %2
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %75
30: ; preds = %25
%31 = load i8*, i8** %3
%32 = call i32 @fSize(i8* %31)
store i32 %32, i32* %8
%33 = icmp ne i32 %32, -1
br i1 %33, label %34, label %73
34: ; preds = %30
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = call i8* @malloc(i64 %36)
store i8* %37, i8** %7
%38 = load i8*, i8** %7
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
br label %75
42: ; preds = %34
br label %43
43: ; preds = %50, %42
%44 = load i32, i32* %5
%45 = load i8*, i8** %7
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = call i64 @read(i32 %44, i8* %45, i64 %47)
%49 = icmp sgt i64 %48, 0
br i1 %49, label %50, label %56
50: ; preds = %43
%51 = load i32, i32* %6
%52 = load i8*, i8** %7
%53 = load i32, i32* %8
%54 = sext i32 %53 to i64
%55 = call i32 @write(i32 %51, i8* %52, i64 %54)
br label %43
56: ; preds = %43
%57 = load i32, i32* %5
%58 = load i8*, i8** %7
%59 = load i32, i32* %8
%60 = sext i32 %59 to i64
%61 = call i64 @read(i32 %57, i8* %58, i64 %60)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %56
%64 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0))
br label %72
65: ; preds = %56
%66 = load i32, i32* %5
%67 = call i32 @close(i32 %66)
%68 = load i32, i32* %6
%69 = call i32 @close(i32 %68)
%70 = load i8*, i8** %7
%71 = call i32 @free(i8* %70)
br label %72
72: ; preds = %65, %63
br label %75
73: ; preds = %30
%74 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
br label %75
75: ; preds = %23, %28, %40, %73, %72
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IWUSR = global i32 0
@str = [38 x i8] c"ERROR: Could not determine file size.\00"
@str.5 = [52 x i8] c"ERROR: Something went wrong while reading the file.\00"
@str.6 = [50 x i8] c"ERROR: Could not allocate enough memory to buffer\00"
@str.7 = [27 x i8] c"ERROR: Invalid target file\00"
@str.8 = [27 x i8] c"ERROR: Invalid source file\00"
define void @copy(i8* %0, i8* nocapture %1) {
%3 = load i32, i32* @O_RDONLY
%4 = load i32, i32* @S_IRUSR
%5 = tail call i32 @open(i8* %0, i32 %3, i32 %4)
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_WRONLY
%8 = or i32 %7, %6
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %1, i32 %8, i32 %11)
%13 = icmp eq i32 %5, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.8, i64 0, i64 0))
br label %47
16: ; preds = %2
%17 = icmp eq i32 %12, -1
br i1 %17, label %18, label %20
18: ; preds = %16
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.7, i64 0, i64 0))
br label %47
20: ; preds = %16
%21 = tail call i32 @fSize(i8* %0)
%22 = icmp eq i32 %21, -1
br i1 %22, label %45, label %23
23: ; preds = %20
%24 = sext i32 %21 to i64
%25 = tail call i8* @malloc(i64 %24)
%26 = icmp eq i8* %25, null
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = tail call i64 @read(i32 %5, i8* %25, i64 %24)
%29 = icmp sgt i64 %28, 0
br i1 %29, label %32, label %36
30: ; preds = %23
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.6, i64 0, i64 0))
br label %47
32: ; preds = %27, %32
%33 = tail call i32 @write(i32 %12, i8* %25, i64 %24)
%34 = tail call i64 @read(i32 %5, i8* %25, i64 %24)
%35 = icmp sgt i64 %34, 0
br i1 %35, label %32, label %36
36: ; preds = %32, %27
%37 = tail call i64 @read(i32 %5, i8* %25, i64 %24)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str.5, i64 0, i64 0))
br label %47
41: ; preds = %36
%42 = tail call i32 @close(i32 %5)
%43 = tail call i32 @close(i32 %12)
%44 = tail call i32 @free(i8* %25)
br label %47
45: ; preds = %20
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %47
47: ; preds = %30, %39, %41, %45, %18, %14
ret void
}