#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 */

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int read(int, char **, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char **, int);
int copyFile(char *src, char *dest) {
  int src_fd, dest_fd;
  char *buff[1024 * 64];
  int buff_size = 1024 * 64;
  int size;
  if ((src_fd = open(src, O_RDONLY)) == -1) {
    fprintf(stderr, "Error opening %s for reading\n", src);
    perror("");
    return 0;
  }
  if ((dest_fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC,
                      S_IWUSR | S_IRUSR | S_IRGRP)) == -1) {
    fprintf(stderr, "Error opening %s for writing\n", dest);
    perror("");
    return 0;
  }
  while (1) {
    size = read(src_fd, buff, buff_size);
    if (size > 0) {
      write(dest_fd, buff, size);
    } else if (size == -1) {
      fprintf(stderr, "Error reading source file\n");
      return 0;
    } else if (size == 0) {
      close(src_fd);
      close(dest_fd);
      return 1;
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<copyFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80030,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x10000,-0x80024(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11d0 <copyFile+0x70>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe4f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    0xe97(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ff <copyFile+0x19f>
mov    -0x18(%rbp),%rdi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
mov    0x2e14(%rip),%rax
or     (%rax),%esi
mov    0x2e03(%rip),%rax
or     (%rax),%esi
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
mov    0x2db1(%rip),%rax
or     (%rax),%edx
mov    0x2db0(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    1250 <copyFile+0xf0>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    0xe17(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ff <copyFile+0x19f>
jmp    1255 <copyFile+0xf5>
mov    -0x1c(%rbp),%edi
lea    -0x80020(%rbp),%rsi
mov    -0x80024(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x80028(%rbp)
cmpl   $0x0,-0x80028(%rbp)
jle    1297 <copyFile+0x137>
mov    -0x20(%rbp),%edi
lea    -0x80020(%rbp),%rsi
mov    -0x80028(%rbp),%edx
call   1030 <write@plt>
jmp    12fa <copyFile+0x19a>
cmpl   $0xffffffff,-0x80028(%rbp)
jne    12c7 <copyFile+0x167>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
lea    0xd88(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ff <copyFile+0x19f>
cmpl   $0x0,-0x80028(%rbp)
jne    12f0 <copyFile+0x190>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ff <copyFile+0x19f>
jmp    12f5 <copyFile+0x195>
jmp    12fa <copyFile+0x19a>
jmp    1255 <copyFile+0xf5>
mov    -0x4(%rbp),%eax
add    $0x80030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyFile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x80000,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %r15d,%r15d
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     124a <copyFile+0xea>
mov    %eax,%ebp
mov    0x2e06(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e44(%rip),%rax
or     (%rax),%esi
mov    0x2e1b(%rip),%rax
mov    0x2df4(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2df1(%rip),%rax
or     (%rax),%edx
xor    %r15d,%r15d
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     125f <copyFile+0xff>
mov    %eax,%r12d
mov    %rsp,%rbx
jmp    11fd <copyFile+0x9d>
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x10000,%edx
call   1050 <read@plt>
test   %eax,%eax
jg     11f0 <copyFile+0x90>
je     1233 <copyFile+0xd3>
cmp    $0xffffffff,%eax
jne    11fd <copyFile+0x9d>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
xor    %r15d,%r15d
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1285 <copyFile+0x125>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r12d,%edi
call   1040 <close@plt>
mov    $0x1,%r15d
jmp    1285 <copyFile+0x125>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
lea    0xda6(%rip),%rsi
mov    %rbx,%rdx
jmp    1272 <copyFile+0x112>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xdaf(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0xdd6(%rip),%rdi
call   1080 <perror@plt>
mov    %r15d,%eax
add    $0x80000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [30 x i8] c"Error opening %s for reading\0A\00"
@.str.1 = [1 x i8] zeroinitializer
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@.str.2 = [30 x i8] c"Error opening %s for writing\0A\00"
@.str.3 = [27 x i8] c"Error reading source file\0A\00"
define i32 @copyFile(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [65536 x i8*]
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 65536, i32* %9
%11 = load i8*, i8** %4
%12 = load i32, i32* @O_RDONLY
%13 = call i32 (i8*, i32, ...) @open(i8* %11, i32 %12)
store i32 %13, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %4
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %69
20: ; preds = %2
%21 = load i8*, i8** %5
%22 = load i32, i32* @O_WRONLY
%23 = load i32, i32* @O_CREAT
%24 = or i32 %22, %23
%25 = load i32, i32* @O_TRUNC
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IWUSR
%28 = load i32, i32* @S_IRUSR
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IRGRP
%31 = or i32 %29, %30
%32 = call i32 (i8*, i32, ...) @open(i8* %21, i32 %26, i32 %31)
store i32 %32, i32* %7
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %39
34: ; preds = %20
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %5
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %36)
%38 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %69
39: ; preds = %20
br label %40
40: ; preds = %39, %68
%41 = load i32, i32* %6
%42 = getelementptr inbounds [65536 x i8*], [65536 x i8*]* %8, i64 0, i64 0
%43 = load i32, i32* %9
%44 = call i32 @read(i32 %41, i8** %42, i32 %43)
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %40
%48 = load i32, i32* %7
%49 = getelementptr inbounds [65536 x i8*], [65536 x i8*]* %8, i64 0, i64 0
%50 = load i32, i32* %10
%51 = call i32 @write(i32 %48, i8** %49, i32 %50)
br label %68
52: ; preds = %40
%53 = load i32, i32* %10
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %69
58: ; preds = %52
%59 = load i32, i32* %10
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %58
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
%64 = load i32, i32* %7
%65 = call i32 @close(i32 %64)
store i32 1, i32* %3
br label %69
66: ; preds = %58
br label %67
67: ; preds = %66
br label %68
68: ; preds = %67, %47
br label %40
69: ; preds = %61, %55, %34, %15
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [30 x i8] c"Error opening %s for reading\0A\00"
@.str.1 = [1 x i8] zeroinitializer
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@.str.2 = [30 x i8] c"Error opening %s for writing\0A\00"
@.str.3 = [27 x i8] c"Error reading source file\0A\00"
define i32 @copyFile(i8* %0, i8* %1) {
%3 = alloca [65536 x i8*]
%4 = bitcast [65536 x i8*]* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0)
%11 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %44
12: ; preds = %2
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_CREAT
%15 = or i32 %14, %13
%16 = load i32, i32* @O_TRUNC
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IWUSR
%19 = load i32, i32* @S_IRUSR
%20 = or i32 %19, %18
%21 = load i32, i32* @S_IRGRP
%22 = or i32 %20, %21
%23 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %17, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %27, label %25
25: ; preds = %12
%26 = getelementptr inbounds [65536 x i8*], [65536 x i8*]* %3, i64 0, i64 0
br label %31
27: ; preds = %12
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%30 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %44
31: ; preds = %37, %25
%32 = call i32 @read(i32 %6, i8** %26, i32 65536)
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @write(i32 %23, i8** %26, i32 %32)
br label %37
36: ; preds = %31
switch i32 %32, label %37 [
i32 -1, label %38
i32 0, label %41
]
37: ; preds = %36, %34
br label %31
38: ; preds = %36
%39 = load i32, i32* @stderr
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %44
41: ; preds = %36
%42 = tail call i32 @close(i32 %6)
%43 = tail call i32 @close(i32 %23)
br label %44
44: ; preds = %41, %38, %27, %8
%45 = phi i32 [ 0, %8 ], [ 0, %27 ], [ 0, %38 ], [ 1, %41 ]
ret i32 %45
}