#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__ int64_t;

/* Variables and functions */
scalar_t__ full_read(int, char *, int);
scalar_t__ full_write(int, char *, scalar_t__);
int64_t copy_fd_to_fd(int in, int out) {
  int64_t total = 0;
  while (1) {
    char buffer[(1 << 16)];
    int64_t actual_read = full_read(in, buffer, (1 << 16));
    if (actual_read <= 0) {
      if (total == 0)
        return -1;
      else
        break;
    }
    int64_t actual_write = full_write(out, buffer, actual_read);
    if (actual_write == -1) {
      if (total == 0)
        return -1;
      else
        break;
    }
    total += actual_write;
  }
  return total;
}


x86 assembly (clang, -O0):
<copy_fd_to_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10030,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x10020(%rbp),%rsi
mov    $0x10000,%edx
call   1030 <full_read@plt>
mov    %rax,-0x10028(%rbp)
cmpq   $0x0,-0x10028(%rbp)
jg     117f <copy_fd_to_fd+0x5f>
cmpq   $0x0,-0x18(%rbp)
jne    117a <copy_fd_to_fd+0x5a>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11e3 <copy_fd_to_fd+0xc3>
jmp    11db <copy_fd_to_fd+0xbb>
mov    -0x10(%rbp),%edi
lea    -0x10020(%rbp),%rsi
mov    -0x10028(%rbp),%rdx
call   1040 <full_write@plt>
mov    %rax,-0x10030(%rbp)
cmpq   $0xffffffffffffffff,-0x10030(%rbp)
ff
jne    11c7 <copy_fd_to_fd+0xa7>
cmpq   $0x0,-0x18(%rbp)
jne    11c2 <copy_fd_to_fd+0xa2>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11e3 <copy_fd_to_fd+0xc3>
jmp    11db <copy_fd_to_fd+0xbb>
mov    -0x10030(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1139 <copy_fd_to_fd+0x19>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_fd_to_fd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10000,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
xor    %r12d,%r12d
mov    %rsp,%r15
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x10000,%edx
call   1030 <full_read@plt>
test   %rax,%rax
jle    1194 <copy_fd_to_fd+0x74>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rax,%rdx
call   1040 <full_write@plt>
xor    %ecx,%ecx
test   %rbx,%rbx
setne  %cl
cmp    $0xffffffffffffffff,%rax
lea    0x1(%rcx,%rcx,1),%ecx
mov    $0x0,%edx
cmovne %rax,%rdx
cmovne %r12d,%ecx
add    %rdx,%rbx
test   %ecx,%ecx
je     1140 <copy_fd_to_fd+0x20>
cmp    $0x3,%ecx
je     119e <copy_fd_to_fd+0x7e>
mov    $0xffffffffffffffff,%rbx
jmp    119e <copy_fd_to_fd+0x7e>
cmp    $0x1,%rbx
sbb    %rax,%rax
or     %rax,%rbx
mov    %rbx,%rax
add    $0x10000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @copy_fd_to_fd(i32 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca [65536 x i8]
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
store i64 0, i64* %6
br label %10
10: ; preds = %2, %33
%11 = load i32, i32* %4
%12 = getelementptr inbounds [65536 x i8], [65536 x i8]* %7, i64 0, i64 0
%13 = call i64 @full_read(i32 %11, i8* %12, i32 65536)
store i64 %13, i64* %8
%14 = load i64, i64* %8
%15 = icmp sle i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %10
%17 = load i64, i64* %6
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16
store i64 -1, i64* %3
br label %39
20: ; preds = %16
br label %37
21: ; preds = %10
%22 = load i32, i32* %5
%23 = getelementptr inbounds [65536 x i8], [65536 x i8]* %7, i64 0, i64 0
%24 = load i64, i64* %8
%25 = call i64 @full_write(i32 %22, i8* %23, i64 %24)
store i64 %25, i64* %9
%26 = load i64, i64* %9
%27 = icmp eq i64 %26, -1
br i1 %27, label %28, label %33
28: ; preds = %21
%29 = load i64, i64* %6
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28
store i64 -1, i64* %3
br label %39
32: ; preds = %28
br label %37
33: ; preds = %21
%34 = load i64, i64* %9
%35 = load i64, i64* %6
%36 = add nsw i64 %35, %34
store i64 %36, i64* %6
br label %10
37: ; preds = %32, %20
%38 = load i64, i64* %6
store i64 %38, i64* %3
br label %39
39: ; preds = %37, %31, %19
%40 = load i64, i64* %3
ret i64 %40
}

LLVM IR (-O2):
define i64 @copy_fd_to_fd(i32 %0, i32 %1) {
%3 = alloca [65536 x i8]
%4 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %12, %2
%6 = phi i64 [ 0, %2 ], [ %18, %12 ]
%7 = call i64 @full_read(i32 %0, i8* %4, i32 65536)
%8 = icmp slt i64 %7, 1
br i1 %8, label %9, label %12
9: ; preds = %5
%10 = icmp eq i64 %6, 0
%11 = select i1 %10, i64 -1, i64 %6
br label %21
12: ; preds = %5
%13 = call i64 @full_write(i32 %1, i8* %4, i64 %7)
%14 = icmp eq i64 %13, -1
%15 = icmp eq i64 %6, 0
%16 = select i1 %15, i32 1, i32 3
%17 = select i1 %14, i64 0, i64 %13
%18 = add nsw i64 %17, %6
%19 = select i1 %14, i32 %16, i32 0
switch i32 %19, label %20 [
i32 0, label %5
i32 3, label %21
]
20: ; preds = %12
br label %21
21: ; preds = %12, %20, %9
%22 = phi i64 [ %11, %9 ], [ -1, %20 ], [ %18, %12 ]
ret i64 %22
}