// File: /workspace/github_c_makefile_star2_O1/erikdubbelboer[P]discnt/src/rio.c
// Function: rioFdsetWrite
static size_t rioFdsetWrite(rio *r, const void *buf, size_t len) {
    ssize_t retval;
    int j;
    unsigned char *p = (unsigned char*) buf;
    int doflush = (buf == NULL && len == 0);

    /* To start we always append to our buffer. If it gets larger than
     * a given size, we actually write to the sockets. */
    if (len) {
        r->io.fdset.buf = sdscatlen(r->io.fdset.buf,buf,len);
        len = 0; /* Prevent entering the while belove if we don't flush. */
        if (sdslen(r->io.fdset.buf) > PROTO_IOBUF_LEN) doflush = 1;
    }

    if (doflush) {
        p = (unsigned char*) r->io.fdset.buf;
        len = sdslen(r->io.fdset.buf);
    }

    /* Write in little chunchs so that when there are big writes we
     * parallelize while the kernel is sending data in background to
     * the TCP socket. */
    while(len) {
        size_t count = len < 1024 ? len : 1024;
        int broken = 0;
        for (j = 0; j < r->io.fdset.numfds; j++) {
            if (r->io.fdset.state[j] != 0) {
                /* Skip FDs alraedy in error. */
                broken++;
                continue;
            }

            /* Make sure to write 'count' bytes to the socket regardless
             * of short writes. */
            size_t nwritten = 0;
            while(nwritten != count) {
                retval = write(r->io.fdset.fds[j],p+nwritten,count-nwritten);
                if (retval <= 0) {
                    /* With blocking sockets, which is the sole user of this
                     * rio target, EWOULDBLOCK is returned only because of
                     * the SO_SNDTIMEO socket option, so we translate the error
                     * into one more recognizable by the user. */
                    if (retval == -1 && errno == EWOULDBLOCK) errno = ETIMEDOUT;
                    break;
                }
                nwritten += retval;
            }

            if (nwritten != count) {
                /* Mark this FD as broken. */
                r->io.fdset.state[j] = errno;
                if (r->io.fdset.state[j] == 0) r->io.fdset.state[j] = EIO;
            }
        }
        if (broken == r->io.fdset.numfds) return 0; /* All the FDs in error. */
        p += count;
        len -= count;
        r->io.fdset.pos += count;
    }

    if (doflush) sdsclear(r->io.fdset.buf);
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rax
orq %rdx, %rax
sete %bpl
testq %rdx, %rdx
je 0x340a6
movq 0x60(%rbx), %rdi
movq %r14, %rsi
callq 0x1e66d
movq %rax, 0x60(%rbx)
movzbl -0x1(%rax), %ecx
movl %ecx, %edx
andl $0x7, %edx
cmpl $0x4, %edx
ja 0x34097
leaq 0x24d3e(%rip), %rsi     # 0x58db0
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
shrl $0x3, %ecx
jmp 0x34099
movq -0x11(%rax), %rcx
jmp 0x34099
movzwl -0x5(%rax), %ecx
jmp 0x34099
movl -0x9(%rax), %ecx
jmp 0x34099
movzbl -0x3(%rax), %ecx
jmp 0x34099
xorl %ecx, %ecx
cmpq $0x4001, %rcx           # imm = 0x4001
setae %al
orb %al, %bpl
testb %bpl, %bpl
je 0x340d3
movq 0x60(%rbx), %r14
movzbl -0x1(%r14), %edx
movl %edx, %eax
andl $0x7, %eax
cmpl $0x4, %eax
ja 0x340d3
leaq 0x24cff(%rip), %rcx     # 0x58dc4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
shrl $0x3, %edx
jmp 0x340d5
xorl %edx, %edx
movb %bpl, 0xf(%rsp)
testq %rdx, %rdx
je 0x341b3
movl $0x400, %eax            # imm = 0x400
cmpq %rax, %rdx
movl $0x400, %ebp            # imm = 0x400
cmovbq %rdx, %rbp
movl 0x50(%rbx), %eax
testl %eax, %eax
jle 0x3418e
movq %rdx, 0x10(%rsp)
xorl %r13d, %r13d
xorl %r15d, %r15d
movq 0x48(%rbx), %rax
cmpl $0x0, (%rax,%r13,4)
je 0x3411a
incl %r15d
jmp 0x3417b
xorl %r12d, %r12d
movq 0x40(%rbx), %rax
movl (%rax,%r13,4), %edi
leaq (%r14,%r12), %rsi
movq %rbp, %rdx
subq %r12, %rdx
callq 0x121e0
testq %rax, %rax
jle 0x34143
addq %rax, %r12
cmpq %rbp, %r12
jne 0x3411d
jmp 0x3417b
cmpq $-0x1, %rax
jne 0x34159
callq 0x120d0
cmpl $0xb, (%rax)
jne 0x34159
movl $0x6e, (%rax)
callq 0x120d0
movl (%rax), %eax
movq 0x48(%rbx), %rcx
movl %eax, (%rcx,%r13,4)
movq 0x48(%rbx), %rax
cmpl $0x0, (%rax,%r13,4)
jne 0x3417b
movl $0x5, (%rax,%r13,4)
incq %r13
movslq 0x50(%rbx), %rax
cmpq %rax, %r13
jl 0x3410a
movq 0x10(%rsp), %rdx
jmp 0x34191
xorl %r15d, %r15d
cmpl %eax, %r15d
je 0x341a0
addq %rbp, %r14
subq %rbp, %rdx
addq %rbp, 0x58(%rbx)
movb 0xf(%rsp), %bpl
cmpl %eax, %r15d
jne 0x340da
xorl %r14d, %r14d
jmp 0x341c7
movl $0x1, %r14d
testb %bpl, %bpl
je 0x341c7
movq 0x60(%rbx), %rdi
callq 0x1df40
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x11(%r14), %rdx
jmp 0x340d5
movzwl -0x5(%r14), %edx
jmp 0x340d5
movl -0x9(%r14), %edx
jmp 0x340d5
movzbl -0x3(%r14), %edx
jmp 0x340d5