// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/src/networking.c
// Function: writeToClient
int writeToClient(int fd, client *c, int handler_installed) {
    ssize_t nwritten = 0, totwritten = 0;
    size_t objlen;
    size_t objmem;
    robj *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->ptr);
            objmem = getStringObjectSdsUsedMemory(o);

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

            nwritten = write(fd, ((char*)o->ptr)+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 -= objmem;
            }
        }
        /* 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. */
        if (totwritten > NET_MAX_WRITES_PER_EVENT &&
            (server.maxmemory == 0 ||
             zmalloc_used_memory() < server.maxmemory)) break;
    }
    server.stat_net_output_bytes += totwritten;
    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) {
        /* For clients representing masters we don't count sending data
         * as an interaction, since we always send REPLCONF ACK commands
         * that take some time to just fill the socket output buffer.
         * We just rely on data / pings received for timeout detection. */
        if (!(c->flags & CLIENT_MASTER)) 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 0x1c4(%rsi), %rax
movq %rax, 0x10(%rsp)
xorl %r14d, %r14d
xorl %r15d, %r15d
movl 0x1c0(%rbx), %edx
testq %rdx, %rdx
je 0x37989
testl %edx, %edx
jg 0x37a21
movq 0x68(%rbx), %rax
jmp 0x37998
movq 0x68(%rbx), %rax
cmpq $0x0, 0x28(%rax)
je 0x37a98
movq (%rax), %rax
movq 0x10(%rax), %rbp
movq 0x8(%rbp), %rdi
callq 0x3670c
movq %rax, %r13
movq %rbp, %rdi
callq 0x360db
movq %rax, %r12
testq %r13, %r13
jne 0x379cd
movq 0x68(%rbx), %rdi
movq (%rdi), %rsi
callq 0x26fa8
subq %r12, 0x70(%rbx)
jmp 0x37970
movq 0x78(%rbx), %rax
movq 0x8(%rbp), %rsi
addq %rax, %rsi
movq %r13, %rdx
subq %rax, %rdx
movl 0x8(%rsp), %edi
callq 0x26bb0
movq %rax, %r15
testq %rax, %rax
jle 0x37a98
movq 0x78(%rbx), %rax
addq %r15, %rax
movq %rax, 0x78(%rbx)
cmpq %r13, %rax
jne 0x37a18
movq 0x68(%rbx), %rdi
movq (%rdi), %rsi
callq 0x26fa8
andq $0x0, 0x78(%rbx)
subq %r12, 0x70(%rbx)
leaq 0xc1e89(%rip), %r12     # 0xf98a8
jmp 0x37a68
movq 0x78(%rbx), %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rax), %rsi
subq %rax, %rdx
movl 0x8(%rsp), %edi
callq 0x26bb0
movq %rax, %r15
testq %rax, %rax
jle 0x37a98
movq 0x78(%rbx), %rax
addq %r15, %rax
movq %rax, 0x78(%rbx)
cmpl %eax, 0x1c0(%rbx)
leaq 0xc1e4e(%rip), %r12     # 0xf98a8
jne 0x37a68
andl $0x0, 0x1c0(%rbx)
andq $0x0, 0x78(%rbx)
addq %r15, %r14
cmpq $0x10001, %r14          # imm = 0x10001
jl 0x37970
cmpq $0x0, 0x908(%r12)
je 0x37a9f
callq 0x32960
cmpq 0x908(%r12), %rax
jae 0x37970
jmp 0x37a9f
leaq 0xc1e09(%rip), %r12     # 0xf98a8
addq %r14, 0x428(%r12)
cmpq $-0x1, %r15
jne 0x37ab9
callq 0x26880
movl (%rax), %edi
cmpl $0xb, %edi
jne 0x37b14
testq %r14, %r14
jle 0x37ad6
testb $0x2, 0x98(%rbx)
jne 0x37ad6
movq 0x978(%r12), %rax
movq %rax, 0x88(%rbx)
xorl %r14d, %r14d
cmpl $0x0, 0x1c0(%rbx)
jne 0x37b39
movq 0x68(%rbx), %rax
cmpq $0x0, 0x28(%rax)
jne 0x37b39
andq $0x0, 0x78(%rbx)
cmpl $0x0, 0xc(%rsp)
je 0x37b09
movq 0x40(%r12), %rdi
movl 0x8(%rbx), %esi
pushq $0x2
popq %rdx
callq 0x28ba8
testb $0x40, 0x98(%rbx)
jne 0x37b2d
jmp 0x37b39
callq 0x26680
leaq 0x79ecf(%rip), %rsi     # 0xb19ef
pushq $0x1
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2b699
movq %rbx, %rdi
callq 0x37527
pushq $-0x1
popq %r14
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq