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

/* Variables and functions */
scalar_t__ net_write(int, char const *, scalar_t__, int);
ssize_t stream__write(void *cookie, const char *buf, size_t size) {
  ssize_t wsize = -1;
  ssize_t total = 0;
  while (total < (ssize_t)size) {
    wsize = net_write((int)cookie, buf + total, size - total, -1);
    if (wsize < 0)
      break;
    total += wsize;
  }
  return total > 0 || wsize >= 0 ? total : -1;
}


x86 assembly (clang, -O0):
<stream__write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0xffffffffffffffff,-0x20(%rbp)
ff
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    1187 <stream__write+0x77>
mov    -0x8(%rbp),%rax
mov    %eax,%edi
mov    -0x10(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
mov    $0xffffffff,%ecx
call   1030 <net_write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1176 <stream__write+0x66>
jmp    1187 <stream__write+0x77>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1134 <stream__write+0x24>
cmpq   $0x0,-0x28(%rbp)
jg     119d <stream__write+0x8d>
cmpq   $0x0,-0x20(%rbp)
jl     11aa <stream__write+0x9a>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11ba <stream__write+0xaa>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
jmp    11ba <stream__write+0xaa>
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stream__write>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
jle    115c <stream__write+0x4c>
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
lea    (%r14,%rbx,1),%rsi
mov    %r12,%rdx
sub    %rbx,%rdx
mov    %r15d,%edi
mov    $0xffffffff,%ecx
call   1030 <net_write@plt>
test   %rax,%rax
js     1154 <stream__write+0x44>
add    %rax,%rbx
cmp    %r12,%rbx
jl     1130 <stream__write+0x20>
test   %rax,%rax
setns  %al
jmp    1160 <stream__write+0x50>
xor    %ebx,%ebx
xor    %eax,%eax
test   %al,%al
mov    $0xffffffffffffffff,%rax
cmovne %rbx,%rax
test   %rbx,%rbx
cmovg  %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i64 @stream__write(i8* %0, i8* %1, i64 %2) {
%4 = ptrtoint i8* %0 to i64
%5 = trunc i64 %4 to i32
%6 = icmp sgt i64 %2, 0
br i1 %6, label %7, label %19
7: ; preds = %3, %13
%8 = phi i64 [ %14, %13 ], [ 0, %3 ]
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = sub i64 %2, %8
%11 = tail call i64 @net_write(i32 %5, i8* %9, i64 %10, i32 -1)
%12 = icmp slt i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %7
%14 = add nsw i64 %11, %8
%15 = icmp slt i64 %14, %2
br i1 %15, label %7, label %16
16: ; preds = %7, %13
%17 = phi i64 [ %14, %13 ], [ %8, %7 ]
%18 = icmp sgt i64 %11, -1
br label %19
19: ; preds = %16, %3
%20 = phi i64 [ 0, %3 ], [ %17, %16 ]
%21 = phi i1 [ false, %3 ], [ %18, %16 ]
%22 = icmp sgt i64 %20, 0
%23 = select i1 %22, i1 true, i1 %21
%24 = select i1 %23, i64 %20, i64 -1
ret i64 %24
}