// File: /workspace/github_c_makefile_star2_O3/erikdubbelboer[P]discnt/src/networking.c
// Function: writeToClient
int writeToClient(int fd, client *c, int handler_installed) {
    ssize_t nwritten = 0, totwritten = 0;
    size_t objlen;
    sds o;

    while(clientHasPendingReplies(c)) {
        if (c->bufpos > 0) {
            nwritten = write(fd,c->buf+c->sentlen,c->bufpos-c->sentlen);
            if (nwritten <= 0) break;
            c->sentlen += nwritten;
            totwritten += nwritten;

            /* If the buffer was sent, set bufpos to zero to continue with
             * the remainder of the reply. */
            if ((int)c->sentlen == c->bufpos) {
                c->bufpos = 0;
                c->sentlen = 0;
            }
        } else {
            o = listNodeValue(listFirst(c->reply));
            objlen = sdslen(o);

            if (objlen == 0) {
                listDelNode(c->reply,listFirst(c->reply));
                continue;
            }

            nwritten = write(fd, o + c->sentlen, objlen - c->sentlen);
            if (nwritten <= 0) break;
            c->sentlen += nwritten;
            totwritten += nwritten;

            /* If we fully sent the object on head go to the next one */
            if (c->sentlen == objlen) {
                listDelNode(c->reply,listFirst(c->reply));
                c->sentlen = 0;
                c->reply_bytes -= objlen;
            }
        }
        /* Note that we avoid to send more than NET_MAX_WRITES_PER_EVENT
         * bytes, in a single threaded server it's a good idea to serve
         * other clients as well, even if a very large request comes from
         * super fast link that is always able to accept data (in real world
         * scenario think about 'KEYS *' against the loopback interface).
         *
         * However if we are over the maxmemory limit we ignore that and
         * just deliver as much data as it is possible to deliver. */
        server.stat_net_output_bytes += totwritten;
        if (totwritten > NET_MAX_WRITES_PER_EVENT &&
            (server.maxmemory == 0 ||
             zmalloc_used_memory() < server.maxmemory)) break;
    }
    if (nwritten == -1) {
        if (errno == EAGAIN) {
            nwritten = 0;
        } else {
            serverLog(LL_VERBOSE,
                "Error writing to client: %s", strerror(errno));
            freeClient(c);
            return C_ERR;
        }
    }
    if (totwritten > 0) {
        c->lastinteraction = server.unixtime;
    }
    if (!clientHasPendingReplies(c)) {
        c->sentlen = 0;
        if (handler_installed) aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);

        /* Close connection after entire reply has been sent. */
        if (c->flags & CLIENT_CLOSE_AFTER_REPLY) {
            freeClient(c);
            return C_ERR;
        }
    }
    return C_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, 0xc(%rsp)
movq %rsi, %rbx
movl %edi, 0x8(%rsp)
leaq 0xbc(%rsi), %rax
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
leaq 0x31cd1(%rip), %rbp     # 0x54afc
leaq 0x4c256(%rip), %rcx     # 0x6f088
xorl %r15d, %r15d
movq %rcx, %r14
movl 0xb8(%rbx), %edx
testq %rdx, %rdx
je 0x22e51
testl %edx, %edx
jg 0x22efd
movq 0x58(%rbx), %rdi
jmp 0x22e60
movq 0x58(%rbx), %rdi
cmpq $0x0, 0x28(%rdi)
je 0x22f7e
movq (%rdi), %rax
movq 0x10(%rax), %rsi
movzbl -0x1(%rsi), %r12d
movl %r12d, %ecx
andl $0x7, %ecx
cmpl $0x4, %ecx
ja 0x22ea4
movslq (%rbp,%rcx,4), %rcx
addq %rbp, %rcx
jmpq *%rcx
shrl $0x3, %r12d
jmp 0x22e9f
movq -0x11(%rsi), %r12
jmp 0x22e9f
movzwl -0x5(%rsi), %r12d
jmp 0x22e9f
movl -0x9(%rsi), %r12d
jmp 0x22e9f
movzbl -0x3(%rsi), %r12d
testq %r12, %r12
jne 0x22eae
movq %rax, %rsi
callq 0x16691
jmp 0x22e38
movq 0x68(%rbx), %rax
addq %rax, %rsi
movq %r12, %rdx
subq %rax, %rdx
movl 0x8(%rsp), %edi
callq 0x121e0
movq %rax, %r15
testq %rax, %rax
jle 0x22f7e
movq 0x68(%rbx), %rax
addq %r15, %rax
movq %rax, 0x68(%rbx)
cmpq %r12, %rax
jne 0x22ef8
movq 0x58(%rbx), %rdi
movq (%rdi), %rsi
callq 0x16691
movq $0x0, 0x68(%rbx)
subq %r12, 0x60(%rbx)
movq %r14, %rcx
jmp 0x22f46
movq 0x68(%rbx), %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax), %rsi
subq %rax, %rdx
movl 0x8(%rsp), %edi
callq 0x121e0
movq %rax, %r15
testq %rax, %rax
jle 0x22f7e
movq 0x68(%rbx), %rax
addq %r15, %rax
movq %rax, 0x68(%rbx)
cmpl %eax, 0xb8(%rbx)
movq %r14, %rcx
jne 0x22f46
movl $0x0, 0xb8(%rbx)
movq $0x0, 0x68(%rbx)
addq %r15, %r13
addq %r13, 0x3b8(%rcx)
cmpq $0x10001, %r13          # imm = 0x10001
jl 0x22e35
cmpq $0x0, 0x610(%rcx)
je 0x22f9b
callq 0x1fd86
movq %r14, %rcx
cmpq 0x610(%r14), %rax
jae 0x22e35
jmp 0x22f9b
movq %r14, %rcx
cmpq $-0x1, %r15
jne 0x22f96
callq 0x120d0
movq %r14, %rcx
movl (%rax), %edi
cmpl $0xb, %edi
jne 0x22fe8
testq %r13, %r13
jle 0x22fa6
movq 0x648(%rcx), %rax
movq %rax, 0x78(%rbx)
xorl %r14d, %r14d
cmpl $0x0, 0xb8(%rbx)
jne 0x23011
movq 0x58(%rbx), %rax
cmpq $0x0, 0x28(%rax)
jne 0x23011
movq $0x0, 0x68(%rbx)
cmpl $0x0, 0xc(%rsp)
je 0x22fdd
movq 0x38(%rcx), %rdi
movl 0x8(%rbx), %esi
movl $0x2, %edx
callq 0x16dc8
testb $0x40, 0x88(%rbx)
jne 0x23003
jmp 0x23011
callq 0x128e0
leaq 0x31ce5(%rip), %rsi     # 0x54cd9
movl $0x1, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x19c06
movq %rbx, %rdi
callq 0x22c06
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq