// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x10(%rbp), %rdi
callq 0x424d0
cmpl $0x0, %eax
je 0x44758
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1c0(%rax)
jle 0x44622
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
addq $0x1c4, %rsi            # imm = 0x1C4
movq -0x10(%rbp), %rax
addq 0x78(%rax), %rsi
movq -0x10(%rbp), %rax
movslq 0x1c0(%rax), %rdx
movq -0x10(%rbp), %rax
subq 0x78(%rax), %rdx
callq 0x26bc0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jg 0x445d3
jmp 0x44758
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
addq 0x78(%rax), %rcx
movq %rcx, 0x78(%rax)
movq -0x20(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
movq -0x10(%rbp), %rcx
cmpl 0x1c0(%rcx), %eax
jne 0x4461d
movq -0x10(%rbp), %rax
movl $0x0, 0x1c0(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x78(%rax)
jmp 0x44721
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x42810
movq %rax, -0x30(%rbp)
movq -0x40(%rbp), %rdi
callq 0x41ca0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x4468a
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movq (%rax), %rsi
callq 0x271e0
movq -0x38(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x70(%rax)
jmp 0x44572
movl -0x8(%rbp), %edi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x10(%rbp), %rax
addq 0x78(%rax), %rsi
movq -0x30(%rbp), %rdx
movq -0x10(%rbp), %rax
subq 0x78(%rax), %rdx
callq 0x26bc0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jg 0x446be
jmp 0x44758
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
addq 0x78(%rax), %rcx
movq %rcx, 0x78(%rax)
movq -0x20(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rax
cmpq -0x30(%rbp), %rax
jne 0x4471f
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movq (%rax), %rsi
callq 0x271e0
movq -0x10(%rbp), %rax
movq $0x0, 0x78(%rax)
movq -0x38(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x70(%rax)
jmp 0x44721
cmpq $0x10000, -0x28(%rbp)   # imm = 0x10000
jle 0x44753
leaq 0x133aa6(%rip), %rax    # 0x1781d8
cmpq $0x0, 0x908(%rax)
je 0x44751
callq 0x3b090
leaq 0x133a90(%rip), %rcx    # 0x1781d8
cmpq 0x908(%rcx), %rax
jae 0x44753
jmp 0x44758
jmp 0x44572
movq -0x28(%rbp), %rcx
leaq 0x133a75(%rip), %rax    # 0x1781d8
addq 0x428(%rax), %rcx
leaq 0x133a67(%rip), %rax    # 0x1781d8
movq %rcx, 0x428(%rax)
cmpq $-0x1, -0x20(%rbp)
jne 0x447cc
callq 0x26880
cmpl $0xb, (%rax)
jne 0x44793
movq $0x0, -0x20(%rbp)
jmp 0x447ca
callq 0x26880
movl (%rax), %edi
callq 0x26690
movq %rax, %rdx
movl $0x1, %edi
leaq 0xf637d(%rip), %rsi     # 0x13ab2b
movb $0x0, %al
callq 0x2fc60
movq -0x10(%rbp), %rdi
callq 0x43ea0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x44869
jmp 0x447cc
cmpq $0x0, -0x28(%rbp)
jle 0x44800
movq -0x10(%rbp), %rax
movl 0x98(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x447fe
leaq 0x1339ec(%rip), %rax    # 0x1781d8
movq 0x978(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x88(%rax)
jmp 0x44800
movq -0x10(%rbp), %rdi
callq 0x424d0
cmpl $0x0, %eax
jne 0x44862
movq -0x10(%rbp), %rax
movq $0x0, 0x78(%rax)
cmpl $0x0, -0x14(%rbp)
je 0x4483c
leaq 0x1339b1(%rip), %rax    # 0x1781d8
movq 0x40(%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x2, %edx
callq 0x2aab0
movq -0x10(%rbp), %rax
movl 0x98(%rax), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x44860
movq -0x10(%rbp), %rdi
callq 0x43ea0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x44869
jmp 0x44862
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)