#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ warn(char *, char const *);
scalar_t__ write(int, char *, scalar_t__);
bool copyFile(int sourceFd, const char *sourcePath, int destFd,
              const char *destPath) {
  while (true) {
    char buffer[1024];
    ssize_t bytesAvailable = read(sourceFd, buffer, sizeof(buffer));
    if (bytesAvailable < 0) {
      warn("read: '%s'", sourcePath);
      return false;
    } else if (bytesAvailable == 0) {
      return true;
    }
    while (bytesAvailable) {
      ssize_t bytesWritten = write(destFd, buffer, bytesAvailable);
      if (bytesWritten < 0) {
        warn("write: '%s'", destPath);
        return false;
      }
      bytesAvailable -= bytesWritten;
    }
  }
}


x86 assembly (clang, -O0):
<copyFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jge    118e <copyFile+0x5e>
mov    -0x10(%rbp),%rsi
lea    0xe83(%rip),%rdi
call   1030 <warn@plt>
movl   $0x0,-0x4(%rbp)
jmp    1229 <copyFile+0xf9>
cmpq   $0x0,-0x428(%rbp)
jne    11a8 <copyFile+0x78>
movl   $0x1,-0x4(%rbp)
jmp    1229 <copyFile+0xf9>
jmp    11ad <copyFile+0x7d>
jmp    11b2 <copyFile+0x82>
cmpq   $0x0,-0x428(%rbp)
je     1224 <copyFile+0xf4>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x428(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
jge    1207 <copyFile+0xd7>
mov    -0x20(%rbp),%rsi
lea    0xe15(%rip),%rdi
call   1030 <warn@plt>
movl   $0x0,-0x4(%rbp)
jmp    1229 <copyFile+0xf9>
mov    -0x430(%rbp),%rcx
mov    -0x428(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x428(%rbp)
jmp    11b2 <copyFile+0x82>
jmp    1149 <copyFile+0x19>
mov    -0x4(%rbp),%eax
add    $0x430,%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    %rcx,%r15
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r12d
mov    %rsp,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %rax,%rax
js     119f <copyFile+0x6f>
mov    %rax,%rbx
mov    %rsp,%r13
test   %rbx,%rbx
je     11ce <copyFile+0x9e>
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1040 <write@plt>
test   %rax,%rax
js     11ab <copyFile+0x7b>
sub    %rax,%rbx
jne    1170 <copyFile+0x40>
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    1164 <copyFile+0x34>
lea    0xe5a(%rip),%rdi
mov    %r14,%rsi
jmp    11b5 <copyFile+0x85>
lea    0xe59(%rip),%rdi
mov    %r15,%rsi
call   1030 <warn@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%eax
jmp    11bc <copyFile+0x8c>

LLVM IR (-O0):
@.str = [11 x i8] c"read: '%s'\00"
@.str.1 = [12 x i8] c"write: '%s'\00"
define i32 @copyFile(i32 %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca [1024 x i8]
%11 = alloca i64
%12 = alloca i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
br label %13
13: ; preds = %4, %45
%14 = load i32, i32* %6
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%16 = call i64 @read(i32 %14, i8* %15, i32 1024)
store i64 %16, i64* %11
%17 = load i64, i64* %11
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i8*, i8** %7
%21 = call i32 @warn(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 0, i32* %5
br label %46
22: ; preds = %13
%23 = load i64, i64* %11
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %22
store i32 1, i32* %5
br label %46
26: ; preds = %22
br label %27
27: ; preds = %26
br label %28
28: ; preds = %41, %27
%29 = load i64, i64* %11
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %45
31: ; preds = %28
%32 = load i32, i32* %8
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%34 = load i64, i64* %11
%35 = call i64 @write(i32 %32, i8* %33, i64 %34)
store i64 %35, i64* %12
%36 = load i64, i64* %12
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %31
%39 = load i8*, i8** %9
%40 = call i32 @warn(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %39)
store i32 0, i32* %5
br label %46
41: ; preds = %31
%42 = load i64, i64* %12
%43 = load i64, i64* %11
%44 = sub nsw i64 %43, %42
store i64 %44, i64* %11
br label %28
45: ; preds = %28
br label %13
46: ; preds = %38, %25, %19
%47 = load i32, i32* %5
ret i32 %47
}

LLVM IR (-O2):
@.str = [11 x i8] c"read: '%s'\00"
@.str.1 = [12 x i8] c"write: '%s'\00"
define i32 @copyFile(i32 %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = call i64 @read(i32 %0, i8* %6, i32 1024)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %23, %4
%10 = tail call i32 @warn(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %26
11: ; preds = %4, %23
%12 = phi i64 [ %24, %23 ], [ %7, %4 ]
%13 = icmp eq i64 %12, 0
br i1 %13, label %26, label %16
14: ; preds = %16
%15 = icmp eq i64 %20, 0
br i1 %15, label %23, label %16
16: ; preds = %11, %14
%17 = phi i64 [ %20, %14 ], [ %12, %11 ]
%18 = call i64 @write(i32 %2, i8* %6, i64 %17)
%19 = icmp slt i64 %18, 0
%20 = sub nsw i64 %17, %18
br i1 %19, label %21, label %14
21: ; preds = %16
%22 = tail call i32 @warn(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %26
23: ; preds = %14
%24 = call i64 @read(i32 %0, i8* %6, i32 1024)
%25 = icmp slt i64 %24, 0
br i1 %25, label %9, label %11
26: ; preds = %11, %9, %21
%27 = phi i32 [ 0, %21 ], [ 0, %9 ], [ 1, %11 ]
ret i32 %27
}