#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 int /*<<< orphan*/ time_t;
struct link {
  int dummy;
};
typedef scalar_t__ ssize_t;
typedef scalar_t__ int64_t;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
scalar_t__ full_write(int, char *, scalar_t__);
scalar_t__ link_read(struct link *, char *, size_t, int /*<<< orphan*/);
int64_t link_stream_to_fd(struct link *link, int fd, int64_t length,
                          time_t stoptime) {
  int64_t total = 0;
  while (length > 0) {
    char buffer[1 << 16];
    size_t chunk = (((sizeof(buffer)) < ((size_t)length)) ? (sizeof(buffer))
                                                          : ((size_t)length));
    ssize_t ractual = link_read(link, buffer, chunk, stoptime);
    if (ractual <= 0)
      break;
    ssize_t wactual = full_write(fd, buffer, ractual);
    if (wactual != ractual) {
      total = -1;
      break;
    }
    total += ractual;
    length -= ractual;
  }
  return total;
}


x86 assembly (clang, -O0):
<link_stream_to_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10050,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    121d <link_stream_to_fd+0xfd>
mov    $0x10000,%eax
cmp    -0x18(%rbp),%rax
jae    116c <link_stream_to_fd+0x4c>
mov    $0x10000,%eax
mov    %rax,-0x10050(%rbp)
jmp    1177 <link_stream_to_fd+0x57>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10050(%rbp)
mov    -0x10050(%rbp),%rax
mov    %rax,-0x10038(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10030(%rbp),%rsi
mov    -0x10038(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1040 <link_read@plt>
mov    %rax,-0x10040(%rbp)
cmpq   $0x0,-0x10040(%rbp)
jg     11b9 <link_stream_to_fd+0x99>
jmp    121d <link_stream_to_fd+0xfd>
mov    -0xc(%rbp),%edi
lea    -0x10030(%rbp),%rsi
mov    -0x10040(%rbp),%rdx
call   1030 <full_write@plt>
mov    %rax,-0x10048(%rbp)
mov    -0x10048(%rbp),%rax
cmp    -0x10040(%rbp),%rax
je     11f7 <link_stream_to_fd+0xd7>
movq   $0xffffffffffffffff,-0x28(%rbp)
ff
jmp    121d <link_stream_to_fd+0xfd>
mov    -0x10040(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10040(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1141 <link_stream_to_fd+0x21>
mov    -0x28(%rbp),%rax
add    $0x10050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<link_stream_to_fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10018,%rsp
mov    %esi,0xc(%rsp)
test   %rdx,%rdx
jle    1194 <link_stream_to_fd+0x74>
mov    %ecx,%r14d
mov    %rdx,%rbp
mov    %rdi,%r12
xor    %ebx,%ebx
lea    0x10(%rsp),%r13
nopw   0x0(%rax,%rax,1)
cmp    $0x10000,%rbp
mov    $0x10000,%edx
cmovb  %rbp,%rdx
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r14d,%ecx
call   1040 <link_read@plt>
test   %rax,%rax
jle    119f <link_stream_to_fd+0x7f>
mov    %rax,%r15
mov    0xc(%rsp),%edi
mov    %r13,%rsi
mov    %rax,%rdx
call   1030 <full_write@plt>
cmp    %r15,%rax
jne    1198 <link_stream_to_fd+0x78>
add    %r15,%rbx
sub    %r15,%rbp
jg     1150 <link_stream_to_fd+0x30>
jmp    119f <link_stream_to_fd+0x7f>
xor    %ebx,%ebx
jmp    119f <link_stream_to_fd+0x7f>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x10018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.link = type { i32 }
define i64 @link_stream_to_fd(%struct.link* %0, i32 %1, i64 %2, i32 %3) {
%5 = alloca %struct.link*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca [65536 x i8]
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
store %struct.link* %0, %struct.link** %5
store i32 %1, i32* %6
store i64 %2, i64* %7
store i32 %3, i32* %8
store i64 0, i64* %9
br label %14
14: ; preds = %42, %4
%15 = load i64, i64* %7
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %49
17: ; preds = %14
%18 = load i64, i64* %7
%19 = icmp ult i64 65536, %18
br i1 %19, label %20, label %21
20: ; preds = %17
br label %23
21: ; preds = %17
%22 = load i64, i64* %7
br label %23
23: ; preds = %21, %20
%24 = phi i64 [ 65536, %20 ], [ %22, %21 ]
store i64 %24, i64* %11
%25 = load %struct.link*, %struct.link** %5
%26 = getelementptr inbounds [65536 x i8], [65536 x i8]* %10, i64 0, i64 0
%27 = load i64, i64* %11
%28 = load i32, i32* %8
%29 = call i64 @link_read(%struct.link* %25, i8* %26, i64 %27, i32 %28)
store i64 %29, i64* %12
%30 = load i64, i64* %12
%31 = icmp sle i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %23
br label %49
33: ; preds = %23
%34 = load i32, i32* %6
%35 = getelementptr inbounds [65536 x i8], [65536 x i8]* %10, i64 0, i64 0
%36 = load i64, i64* %12
%37 = call i64 @full_write(i32 %34, i8* %35, i64 %36)
store i64 %37, i64* %13
%38 = load i64, i64* %13
%39 = load i64, i64* %12
%40 = icmp ne i64 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %33
store i64 -1, i64* %9
br label %49
42: ; preds = %33
%43 = load i64, i64* %12
%44 = load i64, i64* %9
%45 = add nsw i64 %44, %43
store i64 %45, i64* %9
%46 = load i64, i64* %12
%47 = load i64, i64* %7
%48 = sub nsw i64 %47, %46
store i64 %48, i64* %7
br label %14
49: ; preds = %41, %32, %14
%50 = load i64, i64* %9
ret i64 %50
}

LLVM IR (-O2):
%struct.link = type { i32 }
define i64 @link_stream_to_fd(%struct.link* %0, i32 %1, i64 %2, i32 %3) {
%5 = alloca [65536 x i8]
%6 = icmp sgt i64 %2, 0
br i1 %6, label %7, label %25
7: ; preds = %4
%8 = getelementptr inbounds [65536 x i8], [65536 x i8]* %5, i64 0, i64 0
br label %9
9: ; preds = %7, %21
%10 = phi i64 [ %2, %7 ], [ %22, %21 ]
%11 = phi i64 [ 0, %7 ], [ %23, %21 ]
%12 = icmp ult i64 %10, 65536
%13 = select i1 %12, i64 %10, i64 65536
%14 = call i64 @link_read(%struct.link* %0, i8* %8, i64 %13, i32 %3)
%15 = icmp slt i64 %14, 1
br i1 %15, label %19, label %16
16: ; preds = %9
%17 = call i64 @full_write(i32 %1, i8* %8, i64 %14)
%18 = icmp eq i64 %17, %14
br i1 %18, label %21, label %19
19: ; preds = %16, %9
%20 = phi i64 [ %11, %9 ], [ -1, %16 ]
br label %25
21: ; preds = %16
%22 = sub nsw i64 %10, %14
%23 = add nsw i64 %14, %11
%24 = icmp sgt i64 %22, 0
br i1 %24, label %9, label %25
25: ; preds = %21, %4, %19
%26 = phi i64 [ %20, %19 ], [ 0, %4 ], [ %23, %21 ]
ret i64 %26
}