#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*/ close(int);
int creat(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int write(int, char *, int);
void copyFile(char *sourceFile, char *destFile) {
  int numChars;
  char buf[4096];
  int inFile = open(sourceFile, O_RDONLY);
  int outFile = creat(destFile, 0644);
  while ((numChars = read(inFile, buf, 4096)) > 0) {
    if (write(outFile, buf, numChars) != numChars) {
      printf("Write error to %s\n", destFile);
    }
  }
  close(inFile);
  close(outFile);
}


x86 assembly (clang, -O0):
<copyFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1024(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1a4,%esi
call   1080 <creat@plt>
mov    %eax,-0x1028(%rbp)
mov    -0x1024(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    11f5 <copyFile+0x95>
mov    -0x1028(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
cmp    -0x14(%rbp),%eax
je     11f0 <copyFile+0x90>
mov    -0x10(%rbp),%rsi
lea    0xe15(%rip),%rdi
call   1040 <printf@plt>
jmp    119f <copyFile+0x3f>
mov    -0x1024(%rbp),%edi
call   1050 <close@plt>
mov    -0x1028(%rbp),%edi
call   1050 <close@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%r14
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%r15d
mov    %r14,%rdi
mov    $0x1a4,%esi
call   1080 <creat@plt>
mov    %eax,%r13d
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x1000,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    11f4 <copyFile+0x94>
mov    %eax,%ebx
mov    %rsp,%rbp
lea    0xe4b(%rip),%r12
jmp    11d6 <copyFile+0x76>
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    11f4 <copyFile+0x94>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     11c0 <copyFile+0x60>
mov    %r12,%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
jmp    11c0 <copyFile+0x60>
mov    %r15d,%edi
call   1050 <close@plt>
mov    %r13d,%edi
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <close@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [19 x i8] c"Write error to %s\0A\00"
define void @copyFile(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [4096 x i8]
%7 = alloca i32
%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 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %7
%12 = load i8*, i8** %4
%13 = call i32 @creat(i8* %12, i32 420)
store i32 %13, i32* %8
br label %14
14: ; preds = %29, %2
%15 = load i32, i32* %7
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%17 = call i32 @read(i32 %15, i8* %16, i32 4096)
store i32 %17, i32* %5
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %30
19: ; preds = %14
%20 = load i32, i32* %8
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%22 = load i32, i32* %5
%23 = call i32 @write(i32 %20, i8* %21, i32 %22)
%24 = load i32, i32* %5
%25 = icmp ne i32 %23, %24
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = load i8*, i8** %4
%28 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %26, %19
br label %14
30: ; preds = %14
%31 = load i32, i32* %7
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %8
%34 = call i32 @close(i32 %33)
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [19 x i8] c"Write error to %s\0A\00"
define void @copyFile(i8* nocapture %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = tail call i32 @creat(i8* %1, i32 420)
%8 = call i32 @read(i32 %6, i8* %4, i32 4096)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %19
10: ; preds = %2, %16
%11 = phi i32 [ %17, %16 ], [ %8, %2 ]
%12 = call i32 @write(i32 %7, i8* %4, i32 %11)
%13 = icmp eq i32 %12, %11
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %16
16: ; preds = %14, %10
%17 = call i32 @read(i32 %6, i8* %4, i32 4096)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %10, label %19
19: ; preds = %16, %2
%20 = tail call i32 @close(i32 %6)
%21 = tail call i32 @close(i32 %7)
ret void
}