// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movq 0x18(%rdi), %r14
movl 0x4(%r14), %edx
movzwl 0xc(%r14), %ebp
leaq 0x41c47(%rip), %r12     # 0x6f088
movq 0x668(%r12), %rax
incq 0x48(%rax)
movl %edx, %ecx
bswapl %ecx
movl $0x1, %ebx
cmpl $0x10, %ecx
jb 0x2d928
cmpw $0x0, 0x8(%r14)
jne 0x2d928
movq %rdi, %r15
movl %ecx, %esi
movzbl -0x1(%r14), %edi
movl %edi, %r8d
andl $0x7, %r8d
cmpl $0x4, %r8d
ja 0x2d4b2
leaq 0x2947f(%rip), %r9      # 0x56908
movslq (%r9,%r8,4), %r8
addq %r9, %r8
jmpq *%r8
shrl $0x3, %edi
jmp 0x2d4b4
movq -0x11(%r14), %rdi
jmp 0x2d4b4
movzwl -0x5(%r14), %edi
jmp 0x2d4b4
movl -0x9(%r14), %edi
jmp 0x2d4b4
movzbl -0x3(%r14), %edi
jmp 0x2d4b4
xorl %edi, %edi
cmpq %rsi, %rdi
jb 0x2d928
movl %ebp, %r13d
rolw $0x8, %r13w
movzwl %r13w, %r8d
cmpw $0x2, %r8w
ja 0x2d4eb
movzwl 0xe(%r14), %edx
rolw $0x8, %dx
movzwl %dx, %edx
imull $0x68, %edx, %edx
addl $0x60, %edx
cmpl %edx, %ecx
je 0x2d527
jmp 0x2d928
leal -0x3(%r8), %esi
cmpl $0x3, %esi
ja 0x2d527
leaq 0x29421(%rip), %rdi     # 0x5691c
movslq (%rdi,%rsi,4), %rsi
addq %rdi, %rsi
jmpq *%rsi
cmpl $0x88000000, %edx       # imm = 0x88000000
je 0x2d527
jmp 0x2d928
movzwl 0x64(%r14), %edx
addl $0x66, %edx
jmp 0x2d4e2
cmpl $0x64000000, %edx       # imm = 0x64000000
jne 0x2d928
movq %r8, 0x28(%rsp)
leaq 0x10(%r14), %rsi
movq 0x10(%rax), %rdi
movq %rsi, 0x10(%rsp)
callq 0x190de
testq %rax, %rax
je 0x2d549
movq 0x8(%rax), %r8
jmp 0x2d54c
xorl %r8d, %r8d
testl $0xfffd, %r13d          # imm = 0xFFFD
jne 0x2d68c
movzwl %bp, %ecx
cmpl $0x200, %ecx            # imm = 0x200
movq %r8, 0x18(%rsp)
je 0x2d57a
movq 0x56030(%rip), %rax     # 0x835a0
cmpb $0x0, 0x50(%rax)
jne 0x2d5fd
movl %ecx, 0x8(%rsp)
movl 0x8(%r15), %edi
leaq 0x30(%rsp), %rsi
movl $0x2e, %edx
xorl %ecx, %ecx
callq 0x1863b
cmpl $-0x1, %eax
je 0x2d5f4
movq 0x56001(%rip), %rsi     # 0x835a0
addq $0x50, %rsi
leaq 0x30(%rsp), %rdi
movq %rsi, 0x20(%rsp)
callq 0x124a0
testl %eax, %eax
je 0x2d5f4
movups 0x4e(%rsp), %xmm0
movq 0x20(%rsp), %rdx
movups %xmm0, 0x1e(%rdx)
movaps 0x30(%rsp), %xmm0
movaps 0x40(%rsp), %xmm1
movups %xmm1, 0x10(%rdx)
movups %xmm0, (%rdx)
leaq 0x29935(%rip), %rsi     # 0x56f11
movl $0x3, %edi
xorl %eax, %eax
callq 0x19fb1
movq 0x668(%r12), %rax
orl $0x2, 0x28(%rax)
movq 0x18(%rsp), %r8
movl 0x8(%rsp), %ecx
cmpl $0x200, %ecx            # imm = 0x200
sete %al
testq %r8, %r8
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x2d67a
xorl %edi, %edi
movl $0x8, %esi
callq 0x2bf69
movq %rax, 0x8(%rsp)
leaq 0x50(%rax), %rsi
movl 0x8(%r15), %edi
movl $0x2e, %edx
xorl %ecx, %ecx
callq 0x1848c
movzwl 0x58(%r14), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movq 0x8(%rsp), %rdx
movl %eax, 0x80(%rdx)
movq 0x668(%r12), %rax
movq 0x10(%rax), %rdi
movq %rdx, %rsi
callq 0x18b97
movq 0x668(%r12), %rax
orl $0x2, 0x28(%rax)
movq %r14, %rdi
movq %r15, %rsi
callq 0x2d1ce
movq %r15, %rdi
movl $0x1, %esi
callq 0x2d989
movq 0x18(%rsp), %r8
cmpw $0x2, %r13w
ja 0x2d70c
movq 0x20(%r15), %rdi
testq %rdi, %rdi
je 0x2d875
testb $0x8, 0x30(%rdi)
jne 0x2d76d
movl $0x28, %edx
movq 0x10(%rsp), %rsi
movq %r8, %r13
callq 0x12620
movq %r13, %r8
testl %eax, %eax
je 0x2d875
leaq 0x298d6(%rip), %rsi     # 0x56fa4
xorl %ebx, %ebx
xorl %edi, %edi
xorl %eax, %eax
callq 0x19fb1
movq 0x20(%r15), %rax
orl $0x10, 0x30(%rax)
movb $0x0, 0x50(%rax)
movq 0x20(%r15), %rax
movl $0x0, 0x80(%rax)
movq %r15, %rdi
callq 0x2c8d9
movq 0x668(%r12), %rax
orl $0x2, 0x28(%rax)
jmp 0x2d928
movq 0x28(%rsp), %rdx
leal -0x3(%rdx), %eax
cmpl $0x3, %eax
ja 0x2d820
leaq 0x29208(%rip), %rcx     # 0x5692c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
testq %r8, %r8
je 0x2d928
addq $0x60, %r14
movq 0x668(%r12), %rax
movq 0x10(%rax), %rdi
movq %r14, %rsi
callq 0x190de
testq %rax, %rax
je 0x2d939
movq 0x8(%rax), %r15
jmp 0x2d93c
movzwl 0x7c(%r14), %edx
addl $0x7e, %edx
jmp 0x2d4e2
testq %r8, %r8
je 0x2d838
leaq 0x297bb(%rip), %rsi     # 0x56f38
xorl %ebx, %ebx
movl $0x1, %edi
movq %r8, %rdx
xorl %eax, %eax
movq %r8, %r13
callq 0x19fb1
movzwl 0x58(%r14), %eax
rolw $0x8, %ax
movzwl %ax, %edx
movq %r13, %rdi
movq %r15, %rsi
callq 0x2d357
testl %eax, %eax
je 0x2d7b8
movq 0x668(%r12), %rax
orl $0x3, 0x28(%rax)
movq 0x20(%r15), %rdi
callq 0x2c827
jmp 0x2d928
testq %r8, %r8
je 0x2d928
addq $0x60, %r14
movq %r14, %rdi
movq %r8, %rsi
callq 0x2dd48
jmp 0x2d928
leaq 0x2980b(%rip), %rsi     # 0x56ff5
movl $0x3, %edi
movq %r8, %rdx
xorl %eax, %eax
callq 0x19fb1
callq 0x1a646
jmp 0x2d928
testq %r8, %r8
je 0x2d928
addq $0x60, %r14
movq %r14, %rdi
movq %r8, %rsi
callq 0x2dbfb
jmp 0x2d928
leaq 0x297e8(%rip), %rsi     # 0x5700f
movl $0x3, %edi
xorl %eax, %eax
callq 0x19fb1
jmp 0x2d928
movq 0x10(%rsp), %rsi
movq %r8, %r13
callq 0x2ccd6
movq 0x20(%r15), %rdx
leaq 0x2972f(%rip), %rsi     # 0x56f7f
xorl %edi, %edi
xorl %eax, %eax
callq 0x19fb1
movq 0x20(%r15), %rdi
andl $-0x9, 0x30(%rdi)
movq 0x668(%r12), %rax
orl $0x3, 0x28(%rax)
callq 0x3209f
movq %r13, %r8
testw %bp, %bp
sete %al
testq %r8, %r8
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x2d8bc
testb $0x8, 0x30(%r8)
jne 0x2d8bc
movzwl 0x58(%r14), %eax
rolw $0x8, %ax
movzwl %ax, %edx
movq %r8, %rdi
movq %r15, %rsi
movq %r8, %r13
callq 0x2d357
movq %r13, %r8
testl %eax, %eax
je 0x2d8bc
movq 0x668(%r12), %rax
orl $0x3, 0x28(%rax)
movzwl %bp, %eax
cmpl $0x100, %eax            # imm = 0x100
sete %al
cmpq $0x0, 0x20(%r15)
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x2d918
movq %r8, %r13
callq 0x1a1d3
movq 0x20(%r15), %rdi
movq %rax, 0x40(%rdi)
movq $0x0, 0x38(%rdi)
movl 0x30(%rdi), %eax
testb $0x1, %al
jne 0x2d903
testb $0x2, %al
movq %r13, %r8
je 0x2d918
callq 0x2cfdd
jmp 0x2d915
andl $-0x2, %eax
movl %eax, 0x30(%rdi)
movq 0x668(%r12), %rax
orl $0x3, 0x28(%rax)
movq %r13, %r8
testq %r8, %r8
je 0x2d928
movq %r14, %rdi
movq %r15, %rsi
callq 0x2d1ce
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
testq %r15, %r15
je 0x2d928
testb $0x6, 0x30(%r15)
jne 0x2d928
leaq 0x29679(%rip), %rsi     # 0x56fc8
movl $0x1, %ebx
movl $0x1, %edi
movq 0x10(%rsp), %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x19fb1
orb $0x2, 0x30(%r15)
callq 0x1a1d3
movq %rax, 0x48(%r15)
andb $-0x2, 0x30(%r15)
movq 0x668(%r12), %rax
orl $0x3, 0x28(%rax)
jmp 0x2d928