#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__ link_read(struct link *, char *, size_t, int /*<<< orphan*/);
int64_t link_soak(struct link *link, 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;
    total += ractual;
    length -= ractual;
  }
  return total;
}


x86 assembly (clang, -O0):
<link_soak>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10040,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    11cc <link_soak+0xbc>
mov    $0x10000,%eax
cmp    -0x10(%rbp),%rax
jae    1159 <link_soak+0x49>
mov    $0x10000,%eax
mov    %rax,-0x10038(%rbp)
jmp    1164 <link_soak+0x54>
mov    -0x10(%rbp),%rax
mov    %rax,-0x10038(%rbp)
mov    -0x10038(%rbp),%rax
mov    %rax,-0x10028(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10020(%rbp),%rsi
mov    -0x10028(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1030 <link_read@plt>
mov    %rax,-0x10030(%rbp)
cmpq   $0x0,-0x10030(%rbp)
jg     11a6 <link_soak+0x96>
jmp    11cc <link_soak+0xbc>
mov    -0x10030(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10030(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    112e <link_soak+0x1e>
mov    -0x20(%rbp),%rax
add    $0x10040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<link_soak>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10000,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%r12
xor    %ecx,%ecx
mov    %rsp,%r15
nopl   (%rax)
mov    %rcx,%rbp
test   %rbx,%rbx
jle    1162 <link_soak+0x52>
cmp    $0x10000,%rbx
mov    $0x10000,%edx
cmovb  %rbx,%rdx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14d,%ecx
call   1030 <link_read@plt>
lea    (%rax,%rbp,1),%rcx
sub    %rax,%rbx
test   %rax,%rax
jg     1130 <link_soak+0x20>
mov    %rbp,%rax
add    $0x10000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.link = type { i32 }
define i64 @link_soak(%struct.link* %0, i64 %1, i32 %2) {
%4 = alloca %struct.link*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
%8 = alloca [65536 x i8]
%9 = alloca i64
%10 = alloca i64
store %struct.link* %0, %struct.link** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
store i64 0, i64* %7
br label %11
11: ; preds = %30, %3
%12 = load i64, i64* %5
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %37
14: ; preds = %11
%15 = load i64, i64* %5
%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* %5
br label %20
20: ; preds = %18, %17
%21 = phi i64 [ 65536, %17 ], [ %19, %18 ]
store i64 %21, i64* %9
%22 = load %struct.link*, %struct.link** %4
%23 = getelementptr inbounds [65536 x i8], [65536 x i8]* %8, i64 0, i64 0
%24 = load i64, i64* %9
%25 = load i32, i32* %6
%26 = call i64 @link_read(%struct.link* %22, i8* %23, i64 %24, i32 %25)
store i64 %26, i64* %10
%27 = load i64, i64* %10
%28 = icmp sle i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %20
br label %37
30: ; preds = %20
%31 = load i64, i64* %10
%32 = load i64, i64* %7
%33 = add nsw i64 %32, %31
store i64 %33, i64* %7
%34 = load i64, i64* %10
%35 = load i64, i64* %5
%36 = sub nsw i64 %35, %34
store i64 %36, i64* %5
br label %11
37: ; preds = %29, %11
%38 = load i64, i64* %7
ret i64 %38
}

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