// File: /workspace/github_c_makefile_star2_O2/erikdubbelboer[P]discnt/src/cluster.c
// Function: clusterProcessPacket
int clusterProcessPacket(clusterLink *link) {
    clusterMsg *hdr = (clusterMsg*) link->rcvbuf;
    uint32_t totlen = ntohl(hdr->totlen);
    uint16_t type = ntohs(hdr->type);
    clusterNode *sender;

    server.cluster->stats_bus_messages_received++;
    /*serverLog(LL_DEBUG,"--- Processing packet of type %d, %lu bytes",
        type, (unsigned long) totlen);*/

    /* Perform sanity checks */
    if (totlen < 16) return 1; /* At least signature, version, totlen, count. */
    if (ntohs(hdr->ver) != CLUSTER_PROTO_VER)
        return 1; /* Can't handle versions other than the current one.*/
    if (totlen > sdslen(link->rcvbuf)) return 1;
    if (type == CLUSTERMSG_TYPE_PING || type == CLUSTERMSG_TYPE_PONG ||
        type == CLUSTERMSG_TYPE_MEET)
    {
        uint16_t count = ntohs(hdr->count);
        uint32_t explen; /* expected length of this packet */

        explen = sizeof(clusterMsg)-sizeof(union clusterMsgData);
        explen += (sizeof(clusterMsgDataGossip)*count);
        if (totlen != explen) return 1;
    } else if (type == CLUSTERMSG_TYPE_FAIL) {
        uint32_t explen = sizeof(clusterMsg)-sizeof(union clusterMsgData);

        explen += sizeof(clusterMsgDataFail);
        if (totlen != explen) return 1;
    } else if (type == CLUSTERMSG_TYPE_COUNTER) {
        uint32_t explen = sizeof(clusterMsg)-sizeof(union clusterMsgData);

        explen += sizeof(clusterMsgDataCounter) -
                  sizeof(hdr->data.counter.shard.name);
        explen += intrev16ifbe(hdr->data.counter.shard.name_length);
        if (totlen != explen) return 1;
    } else if (type == CLUSTERMSG_TYPE_ACK) {
        uint32_t explen = sizeof(clusterMsg)-sizeof(union clusterMsgData);

        explen += sizeof(clusterMsgDataAck) -
                  sizeof(hdr->data.ack.about.name);
        explen += intrev16ifbe(hdr->data.ack.about.name_length);
        if (totlen != explen) return 1;
    } else if (type == CLUSTERMSG_TYPE_RESET) {
        uint32_t explen = sizeof(clusterMsg)-sizeof(union clusterMsgData);
        explen += sizeof(clusterMsgDataReset);
        if (totlen != explen) return 1;
    }

    /* Check if the sender is a known node. */
    sender = clusterLookupNode(hdr->sender);

    /* Initial processing of PING and MEET requests replying with a PONG. */
    if (type == CLUSTERMSG_TYPE_PING || type == CLUSTERMSG_TYPE_MEET) {
        /*serverLog(LL_DEBUG,"Ping packet received: %p", (void*)link->node);*/

        /* We use incoming MEET messages in order to set the address
         * for 'myself', since only other cluster nodes will send us
         * MEET messages on handshakes, when the cluster joins, or
         * later if we changed address, and those nodes will use our
         * official address to connect to us. So by obtaining this address
         * from the socket is a simple way to discover / update our own
         * address in the cluster without it being hardcoded in the config.
         *
         * However if we don't have an address at all, we update the address
         * even with a normal PING packet. If it's wrong it will be fixed
         * by MEET later. */
        if (type == CLUSTERMSG_TYPE_MEET || myself->ip[0] == '\0') {
            char ip[NET_IP_STR_LEN];

            if (anetSockName(link->fd,ip,sizeof(ip),NULL) != -1 &&
                strcmp(ip,myself->ip))
            {
                memcpy(myself->ip,ip,NET_IP_STR_LEN);
                serverLog(LL_WARNING,"IP address for this node updated to %s",
                    myself->ip);
                clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);
            }
        }

        /* Add this node if it is new for us and the msg type is MEET.
         * In this stage we don't try to add the node with the right
         * flags, slaveof pointer, and so forth, as this details will be
         * resolved when we'll receive PONGs from the node. */
        if (!sender && type == CLUSTERMSG_TYPE_MEET) {
            clusterNode *node;

            node = createClusterNode(NULL,CLUSTER_NODE_HANDSHAKE);
            nodeIp2String(node->ip,link);
            node->port = ntohs(hdr->port);
            clusterAddNode(node);
            clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);
        }

        /* If this is a MEET packet from an unknown node, we still process
         * the gossip section here since we have to trust the sender because
         * of the message type. */
        if (!sender && type == CLUSTERMSG_TYPE_MEET)
            clusterProcessGossipSection(hdr,link);

        /* Anyway reply with a PONG */
        clusterSendPing(link,CLUSTERMSG_TYPE_PONG);
    }

    /* PING, PONG, MEET: process config information. */
    if (type == CLUSTERMSG_TYPE_PING || type == CLUSTERMSG_TYPE_PONG ||
        type == CLUSTERMSG_TYPE_MEET)
    {
        /*serverLog(LL_DEBUG,"%s packet received: %p",
            type == CLUSTERMSG_TYPE_PING ? "ping" : "pong",
            (void*)link->node);*/
        if (link->node) {
            if (nodeInHandshake(link->node)) {
                /* If we already have this node, try to change the
                 * IP/port of the node with the new one. */
                if (sender) {
                    serverLog(LL_VERBOSE,
                        "Handshake: we already know node %.40s, "
                        "updating the address if needed.", sender->name);
                    if (nodeUpdateAddressIfNeeded(sender,link,ntohs(hdr->port)))
                    {
                        clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|
                                             CLUSTER_TODO_UPDATE_STATE);
                    }
                    /* Free this node as we already have it. This will
                     * cause the link to be freed as well. */
                    clusterDelNode(link->node);
                    return 0;
                }

                /* First thing to do is replacing the random name with the
                 * right node name if this was a handshake stage. */
                clusterRenameNode(link->node, hdr->sender);
                serverLog(LL_DEBUG,"Handshake with node %.40s completed.",
                    link->node->name);
                link->node->flags &= ~CLUSTER_NODE_HANDSHAKE;
                clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE|
                                     CLUSTER_TODO_SAVE_CONFIG);

                /* When the handshaking is done we can sync all counters. */
                countersSync(link->node);
            } else if (memcmp(link->node->name,hdr->sender,
                        CLUSTER_NAMELEN) != 0)
            {
                /* If the reply has a non matching node ID we
                 * disconnect this node and set it as not having an associated
                 * address. */
                serverLog(LL_DEBUG,"PONG contains mismatching sender ID");
                link->node->flags |= CLUSTER_NODE_NOADDR;
                link->node->ip[0] = '\0';
                link->node->port = 0;
                freeClusterLink(link);
                clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);
                return 0;
            }
        }

        /* Update the node address if it changed. */
        if (sender && type == CLUSTERMSG_TYPE_PING &&
            !nodeInHandshake(sender) &&
            nodeUpdateAddressIfNeeded(sender,link,ntohs(hdr->port)))
        {
            clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|
                                 CLUSTER_TODO_UPDATE_STATE);
        }

        /* Update our info about the node */
        if (link->node && type == CLUSTERMSG_TYPE_PONG) {
            link->node->pong_received = mstime();
            link->node->ping_sent = 0;

            /* The PFAIL condition can be reversed without external
             * help if it is momentary (that is, if it does not
             * turn into a FAIL state).
             *
             * The FAIL condition is also reversible under specific
             * conditions detected by clearNodeFailureIfNeeded(). */
            if (nodeTimedOut(link->node)) {
                link->node->flags &= ~CLUSTER_NODE_PFAIL;
                clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|
                                     CLUSTER_TODO_UPDATE_STATE);
            } else if (nodeFailed(link->node)) {
                clearNodeFailureIfNeeded(link->node);
            }
        }

        /* Get info from the gossip section */
        if (sender) clusterProcessGossipSection(hdr,link);
    } else if (type == CLUSTERMSG_TYPE_FAIL) {
        clusterNode *failing;

        if (!sender) return 1;
        failing = clusterLookupNode(hdr->data.fail.about.nodename);
        if (failing &&
            !(failing->flags & (CLUSTER_NODE_FAIL|CLUSTER_NODE_MYSELF)))
        {
            serverLog(LL_VERBOSE,
                "FAIL message received from %.40s about %.40s",
                hdr->sender, hdr->data.fail.about.nodename);

            failing->flags |= CLUSTER_NODE_FAIL;
            failing->fail_time = mstime();
            failing->flags &= ~CLUSTER_NODE_PFAIL;

            clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|
                                 CLUSTER_TODO_UPDATE_STATE);
        }
    } else if (type == CLUSTERMSG_TYPE_COUNTER) {
        if (!sender) return 1;

        cluserReadShard(&hdr->data.counter.shard, sender);
    } else if (type == CLUSTERMSG_TYPE_ACK) {
        if (!sender) return 1;

        cluserReadAck(&hdr->data.ack.about, sender);
    } else if (type == CLUSTERMSG_TYPE_RESET) {
        serverLog(LL_WARNING,"Received RESET from %.40s",sender->name);
        flushServerData();
    } else {
        serverLog(LL_WARNING,"Received unknown packet type: %d", type);
    }
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq 0x18(%rdi), %r14
movl 0x4(%r14), %ebp
movzwl 0xc(%r14), %r12d
leaq 0x43b1e(%rip), %rax     # 0x6a088
movq 0x668(%rax), %rax
incq 0x48(%rax)
movl %ebp, %r13d
bswapl %r13d
pushq $0x1
popq %rbx
cmpl $0x10, %r13d
jb 0x265a2
cmpw $0x0, 0x8(%r14)
jne 0x265a2
movq %rdi, %r15
movl %r13d, %ebx
movq %r14, %rdi
callq 0x2552a
cmpq %rbx, %rax
jae 0x265b3
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r12d, %ebx
rolw $0x8, %bx
movzwl %bx, %edx
cmpw $0x2, %dx
ja 0x265dc
movzwl 0xe(%r14), %eax
rolw $0x8, %ax
movzwl %ax, %eax
imull $0x68, %eax, %eax
addl $0x60, %eax
cmpl %eax, %r13d
jne 0x2659f
jmp 0x265ff
leal -0x3(%rdx), %eax
cmpl $0x3, %eax
ja 0x265ff
leaq 0x29acd(%rip), %rcx     # 0x500b8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x88000000, %ebp       # imm = 0x88000000
jmp 0x26818
movq %rdx, %rbp
leaq 0x10(%r14), %rdi
movq %rdi, 0x8(%rsp)
callq 0x25290
movq %rax, %r13
testw $0xfffd, %bx            # imm = 0xFFFD
jne 0x267e7
cmpw $0x200, %r12w           # imm = 0x200
pushq $0x1
popq %rbx
je 0x2663a
movq 0x57f70(%rip), %rax     # 0x7e5a0
cmpb $0x0, 0x50(%rax)
jne 0x26715
movl 0x8(%r15), %edi
leaq 0x10(%rsp), %rsi
pushq $0x2e
popq %rdx
xorl %ecx, %ecx
callq 0x14457
cmpl $-0x1, %eax
je 0x266af
movq 0x57f47(%rip), %rbp     # 0x7e5a0
addq $0x50, %rbp
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x12490
testl %eax, %eax
je 0x266af
movups 0x2e(%rsp), %xmm0
movups %xmm0, 0x1e(%rbp)
movaps 0x10(%rsp), %xmm0
movaps 0x20(%rsp), %xmm1
movups %xmm1, 0x10(%rbp)
movups %xmm0, (%rbp)
leaq 0x29f55(%rip), %rsi     # 0x505e5
pushq $0x3
popq %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x15b9e
leaq 0x439e4(%rip), %rax     # 0x6a088
movq 0x668(%rax), %rax
orl $0x2, 0x28(%rax)
cmpw $0x200, %r12w           # imm = 0x200
sete %al
testq %r13, %r13
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x26715
pushq $0x8
popq %rsi
xorl %edi, %edi
callq 0x252ba
movq %rax, %rbp
leaq 0x50(%rax), %rdi
movq %r15, %rsi
callq 0x2647e
movzwl 0x58(%r14), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movl %eax, 0x80(%rbp)
movq %rbp, %rdi
callq 0x2534c
leaq 0x43989(%rip), %rax     # 0x6a088
movq 0x668(%rax), %rax
orl $0x2, 0x28(%rax)
movq %r14, %rdi
movq %r15, %rsi
callq 0x2632c
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x26a49
movq 0x20(%r15), %rbp
testq %rbp, %rbp
je 0x2678f
testb $0x8, 0x30(%rbp)
jne 0x268bd
pushq $0x28
popq %rdx
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
callq 0x12610
testl %eax, %eax
je 0x2678f
leaq 0x29f2a(%rip), %rsi     # 0x50678
xorl %ebx, %ebx
xorl %edi, %edi
xorl %eax, %eax
callq 0x15b9e
movq 0x20(%r15), %rax
orl $0x10, 0x30(%rax)
movb $0x0, 0x50(%rax)
movq 0x20(%r15), %rax
andl $0x0, 0x80(%rax)
movq %r15, %rdi
callq 0x25b27
leaq 0x43909(%rip), %rax     # 0x6a088
movq 0x668(%rax), %rax
orl $0x2, 0x28(%rax)
jmp 0x265a2
testw %r12w, %r12w
sete %al
testq %r13, %r13
setne %dl
andb %dl, %al
cmpb $0x1, %al
jne 0x269ce
testb $0x8, 0x30(%r13)
jne 0x26a39
movzwl 0x58(%r14), %eax
rolw $0x8, %ax
movzwl %ax, %edx
movq %r13, %rdi
movq %r15, %rsi
callq 0x26491
testl %eax, %eax
je 0x26a39
leaq 0x438b1(%rip), %rax     # 0x6a088
movq 0x668(%rax), %rax
orl $0x3, 0x28(%rax)
jmp 0x26a39
cmpw $0x2, %bx
pushq $0x1
popq %rbx
jbe 0x26720
movq 0x8(%rsp), %r12
jmp 0x26833
movzwl 0x64(%r14), %eax
addl $0x66, %eax
jmp 0x26815
cmpl $0x64000000, %ebp       # imm = 0x64000000
jmp 0x26818
movzwl 0x7c(%r14), %eax
addl $0x7e, %eax
cmpl %eax, %r13d
pushq $0x1
popq %rbx
jne 0x265a2
movq %rdx, %rbp
leaq 0x10(%r14), %r12
movq %r12, %rdi
callq 0x25290
movq %rax, %r13
leal -0x3(%rbp), %eax
cmpl $0x3, %eax
ja 0x2696f
leaq 0x29882(%rip), %rcx     # 0x500c8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
testq %r13, %r13
je 0x265a2
addq $0x60, %r14
movq %r14, %rdi
callq 0x25290
testq %rax, %rax
je 0x265a2
movq %rax, %r15
testb $0x6, 0x30(%rax)
jne 0x265a2
leaq 0x29e1b(%rip), %rsi     # 0x5069c
pushq $0x1
popq %rbx
movl %ebx, %edi
movq %r12, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x15b9e
orb $0x2, 0x30(%r15)
callq 0x15db3
movq %rax, 0x48(%r15)
andb $-0x2, 0x30(%r15)
leaq 0x437db(%rip), %rax     # 0x6a088
movq 0x668(%rax), %rax
orl $0x3, 0x28(%rax)
jmp 0x265a2
testq %r13, %r13
je 0x26988
leaq 0x29d3f(%rip), %rsi     # 0x5060c
xorl %ebx, %ebx
pushq $0x1
popq %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x15b9e
movzwl 0x58(%r14), %eax
rolw $0x8, %ax
movzwl %ax, %edx
movq %r13, %rdi
movq %r15, %rsi
callq 0x26491
testl %eax, %eax
je 0x26909
leaq 0x4378a(%rip), %rax     # 0x6a088
movq 0x668(%rax), %rax
orl $0x3, 0x28(%rax)
movq 0x20(%r15), %rdi
callq 0x25a7d
jmp 0x265a2
testq %r13, %r13
je 0x265a2
addq $0x60, %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x26dd1
jmp 0x265a2
leaq 0x29d8e(%rip), %rsi     # 0x506c9
pushq $0x3
popq %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x15b9e
callq 0x1609c
jmp 0x265a2
testq %r13, %r13
je 0x265a2
addq $0x60, %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x26c99
jmp 0x265a2
movq %rbp, %rdx
leaq 0x29d6a(%rip), %rsi     # 0x506e3
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x15b9e
jmp 0x265a2
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
callq 0x25ed5
movq 0x20(%r15), %rdx
leaq 0x29cb3(%rip), %rsi     # 0x50653
xorl %edi, %edi
xorl %eax, %eax
callq 0x15b9e
movq 0x20(%r15), %rdi
andl $-0x9, 0x30(%rdi)
leaq 0x436d0(%rip), %rax     # 0x6a088
movq 0x668(%rax), %rax
orl $0x3, 0x28(%rax)
callq 0x2a981
xorl %edx, %edx
movq 0x20(%r15), %rbp
cmpw $0x100, %r12w           # imm = 0x100
sete %al
testq %rbp, %rbp
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x26a05
movl %edx, %ebp
callq 0x15db3
movq 0x20(%r15), %rdi
movq %rax, 0x40(%rdi)
andq $0x0, 0x38(%rdi)
movl 0x30(%rdi), %eax
testb $0x1, %al
jne 0x26a0f
testb $0x2, %al
movl %ebp, %edx
jne 0x26a29
testb %dl, %dl
je 0x265a2
jmp 0x26a39
andl $-0x2, %eax
movl %eax, 0x30(%rdi)
leaq 0x4366c(%rip), %rax     # 0x6a088
movq 0x668(%rax), %rax
orl $0x3, 0x28(%rax)
jmp 0x26a30
movl %edx, %ebp
callq 0x26162
testb %bpl, %bpl
je 0x265a2
movq %r14, %rdi
movq %r15, %rsi
callq 0x2632c
jmp 0x265a2