#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
scalar_t__ close(int);
int creat(char *, int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int open(char *, int /*<<< orphan*/, ...);
int read(int, char *, int);
int /*<<< orphan*/ stdout;
int write(int, char *, int);
int copyFile(char *src, char *dst) {
  int fin, fout;
  int nr, nw;
  char buf[1024];
  fin = open(src, O_RDONLY);
  if (fin == -1) {
    fprintf(stdout, "Error opening src#%d\n", errno);
    fin = open(src, O_RDWR, 0777);
  }
  fout = creat(dst, 0666);
  if (fout == -1) {
    fprintf(stdout, "Error opening dst#%d\n", errno);
    fout = creat(dst, 0777);
  }
  while ((nr = read(fin, buf, 1024)) > 0) {
    nw = write(fout, buf, nr);
    if (nr != nw)
      fprintf(stdout, "Error write1#%d\n", errno);
  }
  if (nw < 0)
    fprintf(stdout, "Error write2#%d\n", errno);
  if (close(fin))
    fprintf(stdout, "Errno close src#%d\n", errno);
  if (close(fout))
    fprintf(stdout, "Errno close dst#%d\n", errno);
  return 0;
}


x86 assembly (clang, -O0):
<copyFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11ce <copyFile+0x6e>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
lea    0xe53(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    $0x1ff,%edx
mov    $0x0,%al
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1b6,%esi
call   1080 <creat@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1218 <copyFile+0xb8>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
lea    0xe14(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x1ff,%esi
call   1080 <creat@plt>
mov    %eax,-0x18(%rbp)
jmp    121d <copyFile+0xbd>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    1281 <copyFile+0x121>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
je     127c <copyFile+0x11c>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
mov    0x2d62(%rip),%rax
mov    (%rax),%edx
lea    0xdb5(%rip),%rsi
call   1060 <fprintf@plt>
jmp    121d <copyFile+0xbd>
cmpl   $0x0,-0x20(%rbp)
jge    12a9 <copyFile+0x149>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
mov    0x2d35(%rip),%rax
mov    (%rax),%edx
lea    0xd99(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     12d9 <copyFile+0x179>
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
mov    0x2d05(%rip),%rax
mov    (%rax),%edx
lea    0xd7a(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     1309 <copyFile+0x1a9>
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
mov    0x2cd5(%rip),%rax
mov    (%rax),%edx
lea    0xd5e(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    11c8 <copyFile+0x68>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
lea    0xe58(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    $0x1ff,%edx
xor    %eax,%eax
call   1070 <open@plt>
mov    %eax,%r15d
mov    %r14,%rdi
mov    $0x1b6,%esi
call   1080 <creat@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    120b <copyFile+0xab>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
lea    0xe20(%rip),%rsi
call   1060 <fprintf@plt>
mov    %r14,%rdi
mov    $0x1ff,%esi
call   1080 <creat@plt>
mov    %eax,%r12d
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1297 <copyFile+0x137>
mov    %eax,%ebx
mov    %rsp,%r13
lea    0xe01(%rip),%r14
jmp    1246 <copyFile+0xe6>
nopl   (%rax)
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1275 <copyFile+0x115>
mov    %r12d,%edi
mov    %r13,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
mov    %eax,%ebp
cmp    %eax,%ebx
je     1230 <copyFile+0xd0>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
mov    0x2d67(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1060 <fprintf@plt>
jmp    1230 <copyFile+0xd0>
test   %ebp,%ebp
jns    1297 <copyFile+0x137>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
mov    0x2d47(%rip),%rax
mov    (%rax),%edx
lea    0xdab(%rip),%rsi
call   1060 <fprintf@plt>
mov    %r15d,%edi
call   1040 <close@plt>
test   %rax,%rax
je     12c2 <copyFile+0x162>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
mov    0x2d1c(%rip),%rax
mov    (%rax),%edx
lea    0xd91(%rip),%rsi
call   1060 <fprintf@plt>
mov    %r12d,%edi
call   1040 <close@plt>
test   %rax,%rax
je     12ed <copyFile+0x18d>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
mov    0x2cf1(%rip),%rax
mov    (%rax),%edx
lea    0xd7a(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stdout = global i32 0
@.str = [22 x i8] c"Error opening src#%d\0A\00"
@errno = global i32 0
@O_RDWR = global i32 0
@.str.1 = [22 x i8] c"Error opening dst#%d\0A\00"
@.str.2 = [17 x i8] c"Error write1#%d\0A\00"
@.str.3 = [17 x i8] c"Error write2#%d\0A\00"
@.str.4 = [20 x i8] c"Errno close src#%d\0A\00"
@.str.5 = [20 x i8] c"Errno close dst#%d\0A\00"
define i32 @copyFile(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1024 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %3
%11 = load i32, i32* @O_RDONLY
%12 = call i32 (i8*, i32, ...) @open(i8* %10, i32 %11)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %22
15: ; preds = %2
%16 = load i32, i32* @stdout
%17 = load i32, i32* @errno
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = load i8*, i8** %3
%20 = load i32, i32* @O_RDWR
%21 = call i32 (i8*, i32, ...) @open(i8* %19, i32 %20, i32 511)
store i32 %21, i32* %5
br label %22
22: ; preds = %15, %2
%23 = load i8*, i8** %4
%24 = call i32 @creat(i8* %23, i32 438)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %33
27: ; preds = %22
%28 = load i32, i32* @stdout
%29 = load i32, i32* @errno
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = load i8*, i8** %4
%32 = call i32 @creat(i8* %31, i32 511)
store i32 %32, i32* %6
br label %33
33: ; preds = %27, %22
br label %34
34: ; preds = %51, %33
%35 = load i32, i32* %5
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%37 = call i32 @read(i32 %35, i8* %36, i32 1024)
store i32 %37, i32* %7
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %52
39: ; preds = %34
%40 = load i32, i32* %6
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%42 = load i32, i32* %7
%43 = call i32 @write(i32 %40, i8* %41, i32 %42)
store i32 %43, i32* %8
%44 = load i32, i32* %7
%45 = load i32, i32* %8
%46 = icmp ne i32 %44, %45
br i1 %46, label %47, label %51
47: ; preds = %39
%48 = load i32, i32* @stdout
%49 = load i32, i32* @errno
%50 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %47, %39
br label %34
52: ; preds = %34
%53 = load i32, i32* %8
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %52
%56 = load i32, i32* @stdout
%57 = load i32, i32* @errno
%58 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %57)
br label %59
59: ; preds = %55, %52
%60 = load i32, i32* %5
%61 = call i64 @close(i32 %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %59
%64 = load i32, i32* @stdout
%65 = load i32, i32* @errno
%66 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %65)
br label %67
67: ; preds = %63, %59
%68 = load i32, i32* %6
%69 = call i64 @close(i32 %68)
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %75
71: ; preds = %67
%72 = load i32, i32* @stdout
%73 = load i32, i32* @errno
%74 = call i32 @fprintf(i32 %72, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %73)
br label %75
75: ; preds = %71, %67
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stdout = global i32 0
@.str = [22 x i8] c"Error opening src#%d\0A\00"
@errno = global i32 0
@O_RDWR = global i32 0
@.str.1 = [22 x i8] c"Error opening dst#%d\0A\00"
@.str.2 = [17 x i8] c"Error write1#%d\0A\00"
@.str.3 = [17 x i8] c"Error write2#%d\0A\00"
@.str.4 = [20 x i8] c"Errno close src#%d\0A\00"
@.str.5 = [20 x i8] c"Errno close dst#%d\0A\00"
define i32 @copyFile(i8* nocapture %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%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 %14
8: ; preds = %2
%9 = load i32, i32* @stdout
%10 = load i32, i32* @errno
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = load i32, i32* @O_RDWR
%13 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %12, i32 511)
br label %14
14: ; preds = %8, %2
%15 = phi i32 [ %13, %8 ], [ %6, %2 ]
%16 = tail call i32 @creat(i8* %1, i32 438)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i32, i32* @stdout
%20 = load i32, i32* @errno
%21 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = tail call i32 @creat(i8* %1, i32 511)
br label %23
23: ; preds = %18, %14
%24 = phi i32 [ %22, %18 ], [ %16, %14 ]
%25 = call i32 @read(i32 %15, i8* %4, i32 1024)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %44
27: ; preds = %23, %35
%28 = phi i32 [ %36, %35 ], [ %25, %23 ]
%29 = call i32 @write(i32 %24, i8* %4, i32 %28)
%30 = icmp eq i32 %28, %29
br i1 %30, label %35, label %31
31: ; preds = %27
%32 = load i32, i32* @stdout
%33 = load i32, i32* @errno
%34 = tail call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %31, %27
%36 = call i32 @read(i32 %15, i8* %4, i32 1024)
%37 = icmp sgt i32 %36, 0
br i1 %37, label %27, label %38
38: ; preds = %35
%39 = icmp slt i32 %29, 0
br i1 %39, label %40, label %44
40: ; preds = %38
%41 = load i32, i32* @stdout
%42 = load i32, i32* @errno
%43 = tail call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %23, %40, %38
%45 = tail call i64 @close(i32 %15)
%46 = icmp eq i64 %45, 0
br i1 %46, label %51, label %47
47: ; preds = %44
%48 = load i32, i32* @stdout
%49 = load i32, i32* @errno
%50 = tail call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %47, %44
%52 = tail call i64 @close(i32 %24)
%53 = icmp eq i64 %52, 0
br i1 %53, label %58, label %54
54: ; preds = %51
%55 = load i32, i32* @stdout
%56 = load i32, i32* @errno
%57 = tail call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %56)
br label %58
58: ; preds = %54, %51
ret i32 0
}