// File: /workspace/github_c_makefile_star2_O3/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), %edx
movzwl 0xc(%r14), %r12d
leaq 0x42207(%rip), %rax     # 0x6f088
movq 0x668(%rax), %rax
incq 0x48(%rax)
movl %edx, %ecx
bswapl %ecx
movl $0x1, %ebx
cmpl $0x10, %ecx
jb 0x2d361
cmpw $0x0, 0x8(%r14)
jne 0x2d361
movq %rdi, %r15
movl %ecx, %esi
movzbl -0x1(%r14), %edi
movl %edi, %r8d
andl $0x7, %r8d
cmpl $0x4, %r8d
ja 0x2cef1
leaq 0x29a30(%rip), %r9      # 0x568f8
movslq (%r9,%r8,4), %r8
addq %r9, %r8
jmpq *%r8
shrl $0x3, %edi
jmp 0x2cef3
movq -0x11(%r14), %rdi
jmp 0x2cef3
movzwl -0x5(%r14), %edi
jmp 0x2cef3
movl -0x9(%r14), %edi
jmp 0x2cef3
movzbl -0x3(%r14), %edi
jmp 0x2cef3
xorl %edi, %edi
cmpq %rsi, %rdi
jb 0x2d361
movl %r12d, %r13d
rolw $0x8, %r13w
movzwl %r13w, %ebp
cmpw $0x2, %bp
ja 0x2cf29
movzwl 0xe(%r14), %edx
rolw $0x8, %dx
movzwl %dx, %edx
imull $0x68, %edx, %edx
addl $0x60, %edx
cmpl %edx, %ecx
je 0x2cf64
jmp 0x2d361
leal -0x3(%rbp), %esi
cmpl $0x3, %esi
ja 0x2cf64
leaq 0x299d4(%rip), %rdi     # 0x5690c
movslq (%rdi,%rsi,4), %rsi
addq %rdi, %rsi
jmpq *%rsi
cmpl $0x88000000, %edx       # imm = 0x88000000
je 0x2cf64
jmp 0x2d361
movzwl 0x64(%r14), %edx
addl $0x66, %edx
jmp 0x2cf20
cmpl $0x64000000, %edx       # imm = 0x64000000
jne 0x2d361
leaq 0x10(%r14), %rsi
movq 0x10(%rax), %rdi
movq %rsi, (%rsp)
callq 0x18d3e
testq %rax, %rax
je 0x2cf80
movq 0x8(%rax), %r8
jmp 0x2cf83
xorl %r8d, %r8d
testl $0xfffd, %r13d          # imm = 0xFFFD
jne 0x2d193
movq %r8, 0x8(%rsp)
movzwl %r12w, %r13d
cmpl $0x200, %r13d           # imm = 0x200
je 0x2cfb3
movq 0x565f7(%rip), %rax     # 0x835a0
cmpb $0x0, 0x50(%rax)
jne 0x2d0ae
movl 0x8(%r15), %edi
leaq 0x10(%rsp), %rsi
movl $0x2e, %edx
xorl %ecx, %ecx
callq 0x1829c
cmpl $-0x1, %eax
je 0x2d02c
movq 0x565cc(%rip), %rbp     # 0x835a0
addq $0x50, %rbp
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x12490
testl %eax, %eax
je 0x2d02c
movups 0x2e(%rsp), %xmm0
movups %xmm0, 0x1e(%rbp)
movaps 0x10(%rsp), %xmm0
movaps 0x20(%rsp), %xmm1
movups %xmm1, 0x10(%rbp)
movups %xmm0, (%rbp)
leaq 0x29ef2(%rip), %rsi     # 0x56efd
movl $0x3, %edi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x19c06
leaq 0x42067(%rip), %rax     # 0x6f088
movq 0x668(%rax), %rax
orl $0x2, 0x28(%rax)
cmpl $0x200, %r13d           # imm = 0x200
sete %al
cmpq $0x0, 0x8(%rsp)
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x2d0ae
xorl %edi, %edi
movl $0x8, %esi
callq 0x2b97c
movq %rax, %rbp
leaq 0x50(%rax), %rsi
movl 0x8(%r15), %edi
movl $0x2e, %edx
xorl %ecx, %ecx
callq 0x180ed
movzwl 0x58(%r14), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movl %eax, 0x80(%rbp)
leaq 0x42006(%rip), %r13     # 0x6f088
movq 0x668(%r13), %rax
movq 0x10(%rax), %rdi
movq %rbp, %rsi
movq %rbp, %rdx
callq 0x18802
movq 0x668(%r13), %rax
orl $0x2, 0x28(%rax)
movq %r14, %rdi
movq %r15, %rsi
callq 0x2cbf4
movq %r15, %rdi
movl $0x1, %esi
callq 0x2d3d2
movq 0x8(%rsp), %r8
movq 0x20(%r15), %rbp
testq %rbp, %rbp
je 0x2d139
testb $0x8, 0x30(%rbp)
jne 0x2d24b
movl $0x28, %edx
movq %rbp, %rdi
movq (%rsp), %rsi
movq %r8, %r13
callq 0x12610
movq %r13, %r8
testl %eax, %eax
je 0x2d139
leaq 0x29e9b(%rip), %rsi     # 0x56f90
xorl %ebx, %ebx
xorl %edi, %edi
xorl %eax, %eax
callq 0x19c06
movq 0x20(%r15), %rax
orl $0x10, 0x30(%rax)
movb $0x0, 0x50(%rax)
movq 0x20(%r15), %rax
movl $0x0, 0x80(%rax)
movq %r15, %rdi
callq 0x2c2ee
leaq 0x41f5f(%rip), %rax     # 0x6f088
movq 0x668(%rax), %rax
orl $0x2, 0x28(%rax)
jmp 0x2d361
testw %r12w, %r12w
sete %al
testq %r8, %r8
setne %r13b
andb %r13b, %al
cmpb $0x1, %al
jne 0x2d2f9
testb $0x8, 0x30(%r8)
jne 0x2d356
movzwl 0x58(%r14), %eax
rolw $0x8, %ax
movzwl %ax, %edx
movq %r8, %rdi
movq %r15, %rsi
callq 0x2cd95
testl %eax, %eax
je 0x2d356
leaq 0x41f05(%rip), %rax     # 0x6f088
movq 0x668(%rax), %rax
orl $0x3, 0x28(%rax)
jmp 0x2d356
cmpw $0x2, %r13w
jbe 0x2d0c0
leal -0x3(%rbp), %eax
cmpl $0x3, %eax
leaq 0x41edd(%rip), %rdx     # 0x6f088
ja 0x2d3bb
leaq 0x29764(%rip), %rcx     # 0x5691c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
testq %r8, %r8
je 0x2d361
addq $0x60, %r14
movq 0x668(%rdx), %rax
movq 0x10(%rax), %rdi
movq %r14, %rsi
callq 0x18d3e
testq %rax, %rax
je 0x2d361
movq 0x8(%rax), %r15
testq %r15, %r15
je 0x2d361
testb $0x6, 0x30(%r15)
jne 0x2d361
leaq 0x29dab(%rip), %rsi     # 0x56fb4
movl $0x1, %ebx
movl $0x1, %edi
movq (%rsp), %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x19c06
orb $0x2, 0x30(%r15)
callq 0x19e27
movq %rax, 0x48(%r15)
andb $-0x2, 0x30(%r15)
leaq 0x41e4d(%rip), %rax     # 0x6f088
movq 0x668(%rax), %rax
orl $0x3, 0x28(%rax)
jmp 0x2d361
testq %r8, %r8
je 0x2d2b3
leaq 0x29ccd(%rip), %rsi     # 0x56f24
xorl %ebx, %ebx
movl $0x1, %edi
movq %r8, %rdx
xorl %eax, %eax
movq %r8, %r12
callq 0x19c06
movzwl 0x58(%r14), %eax
rolw $0x8, %ax
movzwl %ax, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x2cd95
testl %eax, %eax
je 0x2d298
leaq 0x41dfb(%rip), %rax     # 0x6f088
movq 0x668(%rax), %rax
orl $0x3, 0x28(%rax)
movq 0x20(%r15), %rdi
callq 0x2c23c
jmp 0x2d361
movzwl 0x7c(%r14), %edx
addl $0x7e, %edx
jmp 0x2cf20
movq %rbp, %rdi
movq (%rsp), %rsi
callq 0x2c6e7
movq 0x20(%r15), %rdx
leaq 0x29ca1(%rip), %rsi     # 0x56f6b
xorl %r13d, %r13d
xorl %edi, %edi
xorl %eax, %eax
callq 0x19c06
movq 0x20(%r15), %rdi
andl $-0x9, 0x30(%rdi)
leaq 0x41da3(%rip), %rax     # 0x6f088
movq 0x668(%rax), %rax
orl $0x3, 0x28(%rax)
callq 0x31aec
movq 0x20(%r15), %rbp
movzwl %r12w, %eax
cmpl $0x100, %eax            # imm = 0x100
sete %al
testq %rbp, %rbp
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x2d351
callq 0x19e27
movq 0x20(%r15), %rdi
movq %rax, 0x40(%rdi)
movq $0x0, 0x38(%rdi)
movl 0x30(%rdi), %eax
testb $0x1, %al
jne 0x2d339
testb $0x2, %al
je 0x2d351
callq 0x2c9ee
jmp 0x2d351
andl $-0x2, %eax
movl %eax, 0x30(%rdi)
leaq 0x41d42(%rip), %rax     # 0x6f088
movq 0x668(%rax), %rax
orl $0x3, 0x28(%rax)
testb %r13b, %r13b
je 0x2d361
movq %r14, %rdi
movq %r15, %rsi
callq 0x2cbf4
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r8, %r8
je 0x2d361
addq $0x60, %r14
movq %r14, %rdi
movq %r8, %rsi
callq 0x2d766
jmp 0x2d361
leaq 0x29c52(%rip), %rsi     # 0x56fe1
movl $0x3, %edi
movq %r8, %rdx
xorl %eax, %eax
callq 0x19c06
callq 0x1a29a
jmp 0x2d361
testq %r8, %r8
je 0x2d361
addq $0x60, %r14
movq %r14, %rdi
movq %r8, %rsi
callq 0x2d61b
jmp 0x2d361
leaq 0x29c39(%rip), %rsi     # 0x56ffb
movl $0x3, %edi
movl %ebp, %edx
xorl %eax, %eax
callq 0x19c06
jmp 0x2d361