// File: /workspace/github_c_makefile_star2_O0/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
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 0x23a50
cmpl $0x0, %eax
je 0x257e0
movq -0x10(%rbp), %rax
cmpl $0x0, 0xb8(%rax)
jle 0x256b2
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
addq $0xbc, %rsi
movq -0x10(%rbp), %rax
addq 0x68(%rax), %rsi
movq -0x10(%rbp), %rax
movslq 0xb8(%rax), %rdx
movq -0x10(%rbp), %rax
subq 0x68(%rax), %rdx
callq 0x121c0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jg 0x25663
jmp 0x257e0
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
addq 0x68(%rax), %rcx
movq %rcx, 0x68(%rax)
movq -0x20(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movq -0x10(%rbp), %rcx
cmpl 0xb8(%rcx), %eax
jne 0x256ad
movq -0x10(%rbp), %rax
movl $0x0, 0xb8(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x68(%rax)
jmp 0x25789
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x23ca0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x256f6
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq (%rax), %rsi
callq 0x12ec0
jmp 0x25602
movl -0x8(%rbp), %edi
movq -0x38(%rbp), %rsi
movq -0x10(%rbp), %rax
addq 0x68(%rax), %rsi
movq -0x30(%rbp), %rdx
movq -0x10(%rbp), %rax
subq 0x68(%rax), %rdx
callq 0x121c0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jg 0x25726
jmp 0x257e0
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
addq 0x68(%rax), %rcx
movq %rcx, 0x68(%rax)
movq -0x20(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
cmpq -0x30(%rbp), %rax
jne 0x25787
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq (%rax), %rsi
callq 0x12ec0
movq -0x10(%rbp), %rax
movq $0x0, 0x68(%rax)
movq -0x30(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x60(%rax)
jmp 0x25789
movq -0x28(%rbp), %rcx
leaq 0x99954(%rip), %rax     # 0xbf0e8
addq 0x3b8(%rax), %rcx
leaq 0x99946(%rip), %rax     # 0xbf0e8
movq %rcx, 0x3b8(%rax)
cmpq $0x10000, -0x28(%rbp)   # imm = 0x10000
jle 0x257db
leaq 0x9992e(%rip), %rax     # 0xbf0e8
cmpq $0x0, 0x610(%rax)
je 0x257d9
callq 0x20de0
leaq 0x99918(%rip), %rcx     # 0xbf0e8
cmpq 0x610(%rcx), %rax
jae 0x257db
jmp 0x257e0
jmp 0x25602
cmpq $-0x1, -0x20(%rbp)
jne 0x25834
callq 0x120b0
cmpl $0xb, (%rax)
jne 0x257fb
movq $0x0, -0x20(%rbp)
jmp 0x25832
callq 0x120b0
movl (%rax), %edi
callq 0x12900
movq %rax, %rdx
movl $0x1, %edi
leaq 0x7ff97(%rip), %rsi     # 0xa57ad
movb $0x0, %al
callq 0x18b10
movq -0x10(%rbp), %rdi
callq 0x252e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x258ba
jmp 0x25834
cmpq $0x0, -0x28(%rbp)
jle 0x25851
leaq 0x998a6(%rip), %rax     # 0xbf0e8
movq 0x648(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x78(%rax)
movq -0x10(%rbp), %rdi
callq 0x23a50
cmpl $0x0, %eax
jne 0x258b3
movq -0x10(%rbp), %rax
movq $0x0, 0x68(%rax)
cmpl $0x0, -0x14(%rbp)
je 0x2588d
leaq 0x99870(%rip), %rax     # 0xbf0e8
movq 0x38(%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x8(%rax), %esi
movl $0x2, %edx
callq 0x13990
movq -0x10(%rbp), %rax
movl 0x88(%rax), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x258b1
movq -0x10(%rbp), %rdi
callq 0x252e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x258ba
jmp 0x258b3
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)