// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x18(%rbp)
movb %al, -0x59(%rbp)
jne 0x8ee23
cmpq $0x0, -0x20(%rbp)
sete %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x3c(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x8ee7b
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x39040
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x60(%rax)
movq $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
callq 0x8ec10
cmpq $0x4000, %rax           # imm = 0x4000
jbe 0x8ee79
movl $0x1, -0x3c(%rbp)
jmp 0x8ee7b
cmpl $0x0, -0x3c(%rbp)
je 0x8ee9e
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
callq 0x8ec10
movq %rax, -0x20(%rbp)
jmp 0x8eea0
cmpq $0x0, -0x20(%rbp)
je 0x8f01a
cmpq $0x400, -0x20(%rbp)     # imm = 0x400
jae 0x8eebf
movq -0x20(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0x8eeca
movl $0x400, %eax            # imm = 0x400
movq %rax, -0x68(%rbp)
jmp 0x8eeca
movq -0x68(%rbp), %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x50(%rcx), %eax
jge 0x8efd4
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movslq -0x2c(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x8ef10
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x8efc6
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0x8ef7d
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movslq -0x2c(%rbp), %rcx
movl (%rax,%rcx,4), %edi
movq -0x38(%rbp), %rsi
addq -0x58(%rbp), %rsi
movq -0x48(%rbp), %rdx
subq -0x58(%rbp), %rdx
callq 0x26bc0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jg 0x8ef6f
cmpq $-0x1, -0x28(%rbp)
jne 0x8ef6d
callq 0x26880
cmpl $0xb, (%rax)
jne 0x8ef6d
callq 0x26880
movl $0x6e, (%rax)
jmp 0x8ef7d
movq -0x28(%rbp), %rax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0x8ef18
movq -0x58(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0x8efc4
callq 0x26880
movl (%rax), %edx
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movslq -0x2c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movslq -0x2c(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jne 0x8efc2
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movslq -0x2c(%rbp), %rcx
movl $0x5, (%rax,%rcx,4)
jmp 0x8efc4
jmp 0x8efc6
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x8eee0
movl -0x4c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x50(%rcx), %eax
jne 0x8efea
movq $0x0, -0x8(%rbp)
jmp 0x8f035
movq -0x48(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x48(%rbp), %rcx
movq -0x20(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x48(%rbp), %rcx
movq -0x10(%rbp), %rax
addq 0x58(%rax), %rcx
movq %rcx, 0x58(%rax)
jmp 0x8eea0
cmpl $0x0, -0x3c(%rbp)
je 0x8f02d
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rdi
callq 0x38740
movq $0x1, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nop