// File: /workspace/github_c_makefile_star2_O2/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 %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rax
orq %rdx, %rax
sete %bpl
testq %rdx, %rdx
je 0x66efa
movq 0x60(%rbx), %rdi
movq %r14, %rsi
callq 0x31758
movq %rax, %r15
movq %rax, 0x60(%rbx)
movq %rax, %rdi
callq 0x66ddb
cmpq $0x4001, %rax           # imm = 0x4001
setae %al
orb %al, %bpl
jne 0x66f03
jmp 0x66f1d
testb %bpl, %bpl
je 0x66f1d
movq 0x60(%rbx), %r15
movq %r15, %rdi
callq 0x66ddb
movq %rax, %rcx
movb $0x1, %al
movq %rax, 0x8(%rsp)
movq %r15, %r14
jmp 0x66fe0
movq $0x0, 0x8(%rsp)
xorl %ecx, %ecx
jmp 0x66fe0
movl $0x400, %eax            # imm = 0x400
cmpq %rax, %rcx
movl $0x400, %r15d           # imm = 0x400
movq %rcx, 0x10(%rsp)
cmovbq %rcx, %r15
xorl %ebp, %ebp
xorl %ecx, %ecx
movslq 0x50(%rbx), %rax
cmpq %rax, %rbp
jge 0x66fcd
movq 0x48(%rbx), %rax
cmpl $0x0, (%rax,%rbp,4)
je 0x66f5f
incl %ecx
jmp 0x66fc5
movl %ecx, 0x4(%rsp)
xorl %r12d, %r12d
movq %r15, %rdx
subq %r12, %rdx
je 0x66f8b
movq 0x40(%rbx), %rax
movl (%rax,%rbp,4), %edi
leaq (%r14,%r12), %rsi
callq 0x26bb0
movq %rax, %r13
testq %rax, %rax
jle 0x66f91
addq %r13, %r12
jmp 0x66f66
movl 0x4(%rsp), %ecx
jmp 0x66fc5
callq 0x26880
cmpq $-0x1, %r13
jne 0x66fa7
cmpl $0xb, (%rax)
jne 0x66fa7
movl $0x6e, (%rax)
movl (%rax), %eax
movq 0x48(%rbx), %rcx
movl %eax, (%rcx,%rbp,4)
movq 0x48(%rbx), %rax
cmpl $0x0, (%rax,%rbp,4)
movl 0x4(%rsp), %ecx
jne 0x66fc5
movl $0x5, (%rax,%rbp,4)
incq %rbp
jmp 0x66f48
cmpl %eax, %ecx
je 0x66fff
addq %r15, %r14
movq 0x10(%rsp), %rcx
subq %r15, %rcx
addq %r15, 0x58(%rbx)
testq %rcx, %rcx
jne 0x66f2d
pushq $0x1
popq %r14
cmpb $0x0, 0x8(%rsp)
je 0x67002
movq 0x60(%rbx), %rdi
callq 0x31233
jmp 0x67002
xorl %r14d, %r14d
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq