// File: /workspace/github_c_makefile_star2_O1/erikdubbelboer[P]discnt/src/cluster.c
// Function: clusterReadHandler
void clusterReadHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
    char buf[sizeof(clusterMsg)];
    ssize_t nread;
    clusterMsg *hdr;
    clusterLink *link = (clusterLink*) privdata;
    unsigned int readlen, rcvbuflen;
    UNUSED(el);
    UNUSED(mask);

    while(1) { /* Read as long as there is data to read. */
        rcvbuflen = sdslen(link->rcvbuf);
        if (rcvbuflen < 8) {
            /* First, obtain the first 8 bytes to get the full message
             * length. */
            readlen = 8 - rcvbuflen;
        } else {
            /* Finally read the full message. */
            hdr = (clusterMsg*) link->rcvbuf;
            if (rcvbuflen == 8) {
                /* Perform some sanity check on the message signature
                 * and length. */
                if (memcmp(hdr->sig,"DbuZ",4) != 0 ||
                    ntohl(hdr->totlen) < CLUSTERMSG_MIN_LEN)
                {
                    serverLog(LL_WARNING,
                        "Bad message length or signature received "
                        "from Cluster bus.");
                    handleLinkIOError(link);
                    return;
                }
            }
            readlen = ntohl(hdr->totlen) - rcvbuflen;
            if (readlen > sizeof(buf)) readlen = sizeof(buf);
        }

        nread = read(fd,buf,readlen);
        if (nread == -1 && errno == EAGAIN) return; /* No more data ready. */

        if (nread <= 0) {
            /* I/O error... */
            serverLog(LL_DEBUG,"I/O error reading from node link: %s",
                (nread == 0) ? "connection closed" : strerror(errno));
            handleLinkIOError(link);
            return;
        } else {
            /* Read data and recast the pointer to the new buffer. */
            link->rcvbuf = sdscatlen(link->rcvbuf,buf,nread);
            hdr = (clusterMsg*) link->rcvbuf;
            rcvbuflen += nread;
        }

        /* Total length obtained? Process this packet. */
        if (rcvbuflen >= 8 && rcvbuflen == ntohl(hdr->totlen)) {
            if (clusterProcessPacket(link)) {
                sdsfree(link->rcvbuf);
                link->rcvbuf = sdsempty();
            } else {
                return; /* Link no longer valid. */
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rdx, %rbx
movl %esi, %ebp
movq %rsp, %r14
leaq 0x29f89(%rip), %r13     # 0x568f4
movq 0x18(%rbx), %rax
movzbl -0x1(%rax), %r12d
movl %r12d, %ecx
andl $0x7, %ecx
cmpl $0x4, %ecx
ja 0x2c9a9
movslq (%r13,%rcx,4), %rcx
addq %r13, %rcx
jmpq *%rcx
shrl $0x3, %r12d
jmp 0x2c9ac
movq -0x11(%rax), %r12
jmp 0x2c9ac
movzwl -0x5(%rax), %r12d
jmp 0x2c9ac
movl -0x9(%rax), %r12d
jmp 0x2c9ac
movzbl -0x3(%rax), %r12d
jmp 0x2c9ac
xorl %r12d, %r12d
cmpl $0x7, %r12d
ja 0x2c9bc
movl $0x8, %eax
subl %r12d, %eax
jmp 0x2c9ee
cmpl $0x8, %r12d
jne 0x2c9dc
cmpl $0x5a756244, (%rax)     # imm = 0x5A756244
jne 0x2ca7a
movl 0x4(%rax), %ecx
bswapl %ecx
cmpl $0x5f, %ecx
jbe 0x2ca7a
movl 0x4(%rax), %eax
bswapl %eax
subl %r12d, %eax
movl $0xc8, %ecx
cmpl %ecx, %eax
cmovael %ecx, %eax
movl %eax, %edx
movl %ebp, %edi
movq %r14, %rsi
callq 0x12420
movq %rax, %r15
cmpq $-0x1, %rax
jne 0x2ca11
callq 0x120d0
cmpl $0xb, (%rax)
je 0x2caae
testq %r15, %r15
jle 0x2ca67
movq 0x18(%rbx), %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1e66d
movq %rax, 0x18(%rbx)
addl %r12d, %r15d
cmpl $0x8, %r15d
jb 0x2c96b
movl 0x4(%rax), %eax
bswapl %eax
cmpl %r15d, %eax
jne 0x2c96b
movq %rbx, %rdi
callq 0x2d41f
testl %eax, %eax
je 0x2caae
movq 0x18(%rbx), %rdi
callq 0x1ded3
callq 0x1de3f
movq %rax, 0x18(%rbx)
jmp 0x2c96b
je 0x2ca8f
callq 0x120d0
movl (%rax), %edi
callq 0x128f0
movq %rax, %rdx
jmp 0x2ca96
leaq 0x2a5d7(%rip), %rsi     # 0x57058
movl $0x3, %edi
xorl %eax, %eax
callq 0x19fb1
jmp 0x2caa6
leaq 0x2a622(%rip), %rdx     # 0x570b8
leaq 0x2a5f6(%rip), %rsi     # 0x57093
xorl %edi, %edi
xorl %eax, %eax
callq 0x19fb1
movq %rbx, %rdi
callq 0x2c8d9
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq