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

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


x86 assembly (clang, -O0):
<link_stream_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10050,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x10000,%eax
cmp    -0x18(%rbp),%rax
jae    1162 <link_stream_from_file+0x42>
mov    $0x10000,%eax
mov    %rax,-0x10050(%rbp)
jmp    116d <link_stream_from_file+0x4d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10050(%rbp)
mov    -0x10050(%rbp),%rax
mov    %rax,-0x10038(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x10030(%rbp),%rsi
mov    -0x10038(%rbp),%rdx
call   1030 <full_fread@plt>
mov    %rax,-0x10040(%rbp)
cmpq   $0x0,-0x10040(%rbp)
jg     11ac <link_stream_from_file+0x8c>
jmp    1214 <link_stream_from_file+0xf4>
mov    -0x8(%rbp),%rdi
lea    -0x10030(%rbp),%rsi
mov    -0x10040(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1040 <link_write@plt>
mov    %rax,-0x10048(%rbp)
mov    -0x10048(%rbp),%rax
cmp    -0x10040(%rbp),%rax
je     11ee <link_stream_from_file+0xce>
movq   $0xffffffffffffffff,-0x28(%rbp)
ff
jmp    1214 <link_stream_from_file+0xf4>
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    1142 <link_stream_from_file+0x22>
mov    -0x28(%rbp),%rax
add    $0x10050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<link_stream_from_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10018,%rsp
mov    %ecx,0xc(%rsp)
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rdi,%r15
cmp    $0x10000,%rdx
mov    $0x10000,%edx
cmovb  %rbx,%rdx
lea    0x10(%rsp),%rsi
mov    %r12,%rdi
call   1030 <full_fread@plt>
test   %rax,%rax
jle    11b2 <link_stream_from_file+0x92>
mov    %rax,%r14
xor    %ebp,%ebp
lea    0x10(%rsp),%r13
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
mov    0xc(%rsp),%ecx
call   1040 <link_write@plt>
cmp    %r14,%rax
jne    11b6 <link_stream_from_file+0x96>
sub    %r14,%rbx
add    %r14,%rbp
cmp    $0x10000,%rbx
mov    $0x10000,%edx
cmovb  %rbx,%rdx
mov    %r12,%rdi
mov    %r13,%rsi
call   1030 <full_fread@plt>
mov    %rax,%r14
test   %rax,%rax
jg     1170 <link_stream_from_file+0x50>
jmp    11bd <link_stream_from_file+0x9d>
xor    %ebp,%ebp
jmp    11bd <link_stream_from_file+0x9d>
mov    $0xffffffffffffffff,%rbp
mov    %rbp,%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_from_file(%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 = %4, %39
%15 = load i64, i64* %7
%16 = icmp ult i64 65536, %15
br i1 %16, label %17, label %18
17: ; preds = %14
br label %20
18: ; preds = %14
%19 = load i64, i64* %7
br label %20
20: ; preds = %18, %17
%21 = phi i64 [ 65536, %17 ], [ %19, %18 ]
store i64 %21, i64* %11
%22 = load i32*, i32** %6
%23 = getelementptr inbounds [65536 x i8], [65536 x i8]* %10, i64 0, i64 0
%24 = load i64, i64* %11
%25 = call i64 @full_fread(i32* %22, i8* %23, i64 %24)
store i64 %25, i64* %12
%26 = load i64, i64* %12
%27 = icmp sle i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %20
br label %46
29: ; preds = %20
%30 = load %struct.link*, %struct.link** %5
%31 = getelementptr inbounds [65536 x i8], [65536 x i8]* %10, i64 0, i64 0
%32 = load i64, i64* %12
%33 = load i32, i32* %8
%34 = call i64 @link_write(%struct.link* %30, i8* %31, i64 %32, i32 %33)
store i64 %34, i64* %13
%35 = load i64, i64* %13
%36 = load i64, i64* %12
%37 = icmp ne i64 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %29
store i64 -1, i64* %9
br label %46
39: ; preds = %29
%40 = load i64, i64* %12
%41 = load i64, i64* %9
%42 = add nsw i64 %41, %40
store i64 %42, i64* %9
%43 = load i64, i64* %12
%44 = load i64, i64* %7
%45 = sub nsw i64 %44, %43
store i64 %45, i64* %7
br label %14
46: ; preds = %38, %28
%47 = load i64, i64* %9
ret i64 %47
}

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