// File: /workspace/github_c_makefile_star2_O3/gplessis[P]dotdeb-redis/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 below 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 %rdi, %rbx
movq %rsi, %rax
orq %rdx, %rax
sete %bpl
testq %rdx, %rdx
je 0x72b7f
movq 0x60(%rbx), %rdi
callq 0x36ad1
movq %rax, %r14
movq %rax, 0x60(%rbx)
movzbl -0x1(%rax), %edx
movl %edx, %eax
andl $0x7, %eax
cmpl $0x4, %eax
ja 0x72bb2
leaq 0x586a5(%rip), %rcx     # 0xcb214
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl %edx, %eax
shrl $0x3, %eax
jmp 0x72bb4
movl $0x1, %eax
testb %bpl, %bpl
je 0x72ceb
movq 0x60(%rbx), %r14
movzbl -0x1(%r14), %edx
jmp 0x72bcb
movq -0x11(%r14), %rax
jmp 0x72bb4
movzwl -0x5(%r14), %eax
jmp 0x72bb4
movl -0x9(%r14), %eax
jmp 0x72bb4
movzbl -0x3(%r14), %eax
jmp 0x72bb4
xorl %eax, %eax
cmpq $0x4001, %rax           # imm = 0x4001
setae %al
orb %al, %bpl
movl $0x1, %eax
je 0x72ceb
movl %edx, %eax
andl $0x7, %eax
cmpl $0x4, %eax
ja 0x72cd9
leaq 0x58648(%rip), %rcx     # 0xcb228
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
shrl $0x3, %edx
jmp 0x72c06
movq -0x11(%r14), %rdx
jmp 0x72c06
movzwl -0x5(%r14), %edx
jmp 0x72c06
movl -0x9(%r14), %edx
jmp 0x72c06
movzbl -0x3(%r14), %edx
testq %rdx, %rdx
je 0x72cd9
movl 0x50(%rbx), %eax
movl $0x400, %ecx            # imm = 0x400
cmpq %rcx, %rdx
movl $0x400, %ebp            # imm = 0x400
cmovbq %rdx, %rbp
movl $0x0, %ecx
testl %eax, %eax
jle 0x72cc5
movq %rdx, 0x10(%rsp)
xorl %r13d, %r13d
xorl %ecx, %ecx
movq 0x48(%rbx), %rax
cmpl $0x0, (%rax,%r13,4)
je 0x72c49
incl %ecx
jmp 0x72cb0
movl %ecx, 0xc(%rsp)
xorl %r15d, %r15d
movq 0x40(%rbx), %rax
movl (%rax,%r13,4), %edi
leaq (%r14,%r15), %rsi
movq %rbp, %rdx
subq %r15, %rdx
callq 0x26bb0
movq %rax, %r12
testq %rax, %rax
jle 0x72c79
addq %r12, %r15
cmpq %rbp, %r15
jne 0x72c50
jmp 0x72cac
callq 0x26880
cmpq $-0x1, %r12
jne 0x72c8f
cmpl $0xb, (%rax)
jne 0x72c8f
movl $0x6e, (%rax)
movl (%rax), %eax
movq 0x48(%rbx), %rcx
movl %eax, (%rcx,%r13,4)
movq 0x48(%rbx), %rax
cmpl $0x0, (%rax,%r13,4)
jne 0x72cac
movl $0x5, (%rax,%r13,4)
movl 0xc(%rsp), %ecx
incq %r13
movslq 0x50(%rbx), %rax
cmpq %rax, %r13
jl 0x72c3a
movq 0x10(%rsp), %rdx
cmpl %eax, %ecx
je 0x72ce9
addq %rbp, %r14
addq %rbp, 0x58(%rbx)
subq %rbp, %rdx
jne 0x72c12
movq 0x60(%rbx), %rdi
callq 0x3639d
movl $0x1, %eax
jmp 0x72ceb
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq