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

/* Variables and functions */
scalar_t__ full_fwrite(int /*<<< orphan*/ *, char *, scalar_t__);
scalar_t__ full_read(int, char *, int);
int64_t copy_fd_to_stream(int fd, FILE *output) {
  int64_t total = 0;
  while (1) {
    char buffer[(1 << 16)];
    int64_t actual_read = full_read(fd, buffer, sizeof(buffer));
    if (actual_read <= 0) {
      if (total == 0)
        return -1;
      else
        break;
    }
    int64_t actual_write = full_fwrite(output, 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_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10030,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%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     1180 <copy_fd_to_stream+0x60>
cmpq   $0x0,-0x20(%rbp)
jne    117b <copy_fd_to_stream+0x5b>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11e5 <copy_fd_to_stream+0xc5>
jmp    11dd <copy_fd_to_stream+0xbd>
mov    -0x18(%rbp),%rdi
lea    -0x10020(%rbp),%rsi
mov    -0x10028(%rbp),%rdx
call   1040 <full_fwrite@plt>
mov    %rax,-0x10030(%rbp)
cmpq   $0xffffffffffffffff,-0x10030(%rbp)
ff
jne    11c9 <copy_fd_to_stream+0xa9>
cmpq   $0x0,-0x20(%rbp)
jne    11c4 <copy_fd_to_stream+0xa4>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11e5 <copy_fd_to_stream+0xc5>
jmp    11dd <copy_fd_to_stream+0xbd>
mov    -0x10030(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    113a <copy_fd_to_stream+0x1a>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_fd_to_stream>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10000,%rsp
mov    %rsi,%r14
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_stream+0x74>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   1040 <full_fwrite@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_stream+0x20>
cmp    $0x3,%ecx
je     119e <copy_fd_to_stream+0x7e>
mov    $0xffffffffffffffff,%rbx
jmp    119e <copy_fd_to_stream+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_stream(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_fwrite(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_stream(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_fwrite(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
}