// File: /workspace/github_c_makefile_star2_O1/erikdubbelboer[P]discnt/src/cluster.c
// Function: clusterSendPing
void clusterSendPing(clusterLink *link, int type) {
    unsigned char *buf;
    clusterMsg *hdr;
    int gossipcount = 0; /* Number of gossip sections added so far. */
    int wanted; /* Number of gossip sections we want to append if possible. */
    int totlen; /* Total packet length. */
    /* freshnodes is the max number of nodes we can hope to append at all:
     * nodes available minus two (ourself and the node we are sending the
     * message to). However practically there may be less valid nodes since
     * nodes in handshake state, disconnected, are not considered. */
    int freshnodes = dictSize(server.cluster->nodes)-2;

    /* How many gossip sections we want to add? 1/10 of the number of nodes
     * and anyway at least 3. Why 1/10?
     *
     * If we have N masters, with N/10 entries, and we consider that in
     * node_timeout we exchange with each other node at least 4 packets
     * (we ping in the worst case in node_timeout/2 time, and we also
     * receive two pings from the host), we have a total of 8 packets
     * in the node_timeout*2 falure reports validity time. So we have
     * that, for a single PFAIL node, we can expect to receive the following
     * number of failure reports (in the specified window of time):
     *
     * PROB * GOSSIP_ENTRIES_PER_PACKET * TOTAL_PACKETS:
     *
     * PROB = probability of being featured in a single gossip entry,
     *        which is 1 / NUM_OF_NODES.
     * ENTRIES = 10.
     * TOTAL_PACKETS = 2 * 4 * NUM_OF_MASTERS.
     *
     * If we assume we have just masters (so num of nodes and num of masters
     * is the same), with 1/10 we always get over the majority, and specifically
     * 80% of the number of nodes, to account for many masters failing at the
     * same time.
     *
     * Since we have non-voting slaves that lower the probability of an entry
     * to feature our node, we set the number of entires per packet as
     * 10% of the total nodes we have. */
    wanted = floor(dictSize(server.cluster->nodes)/10);
    if (wanted < 3) wanted = 3;
    if (wanted > freshnodes) wanted = freshnodes;

    /* Compute the maxium totlen to allocate our buffer. We'll fix the totlen
     * later according to the number of gossip sections we really were able
     * to put inside the packet. */
    totlen = sizeof(clusterMsg)-sizeof(union clusterMsgData);
    totlen += (sizeof(clusterMsgDataGossip)*wanted);
    /* Note: clusterBuildMessageHdr() expects the buffer to be always at least
     * sizeof(clusterMsg) or more. */
    if (totlen < (int)sizeof(clusterMsg)) totlen = sizeof(clusterMsg);
    buf = zcalloc(totlen);
    hdr = (clusterMsg*) buf;

    /* Populate the header. */
    if (link->node && type == CLUSTERMSG_TYPE_PING)
        link->node->ping_sent = mstime();
    clusterBuildMessageHdr(hdr,type);

    /* Populate the gossip fields */
    int maxiterations = wanted*3;
    while(freshnodes > 0 && gossipcount < wanted && maxiterations--) {
        dictEntry *de = dictGetRandomKey(server.cluster->nodes);
        clusterNode *this = dictGetVal(de);
        clusterMsgDataGossip *gossip;
        int j;

        /* Don't include this node: the whole packet header is about us
         * already, so we just gossip about other nodes. */
        if (this == myself) continue;

        /* Give a bias to FAIL/PFAIL nodes. */
        if (maxiterations > wanted*2 &&
            !(this->flags & (CLUSTER_NODE_PFAIL|CLUSTER_NODE_FAIL)))
            continue;

        /* In the gossip section don't include:
         * 1) Nodes in HANDSHAKE state.
         * 3) Nodes with the NOADDR flag set.
         * 4) Disconnected nodes if they don't have configured slots.
         */
        if (this->flags & (CLUSTER_NODE_HANDSHAKE|CLUSTER_NODE_NOADDR) ||
            this->link == NULL)
        {
            freshnodes--; /* Tecnically not correct, but saves CPU. */
            continue;
        }

        /* Check if we already added this node */
        for (j = 0; j < gossipcount; j++) {
            if (memcmp(hdr->data.ping.gossip[j].nodename,this->name,
                    CLUSTER_NAMELEN) == 0) break;
        }
        if (j != gossipcount) continue;

        /* Add it */
        freshnodes--;
        gossip = &(hdr->data.ping.gossip[gossipcount]);
        memcpy(gossip->nodename,this->name,CLUSTER_NAMELEN);
        gossip->ping_sent = htonl(this->ping_sent);
        gossip->pong_received = htonl(this->pong_received);
        memcpy(gossip->ip,this->ip,sizeof(this->ip));
        gossip->port = htons(this->port);
        gossip->flags = htons(this->flags);
        gossip->notused1 = 0;
        gossip->notused2 = 0;
        gossipcount++;
    }

    /* Ready to send... fix the totlen field and queue the message in the
     * output buffer. */
    totlen = sizeof(clusterMsg)-sizeof(union clusterMsgData);
    totlen += (sizeof(clusterMsgDataGossip)*gossipcount);
    hdr->count = htons(gossipcount);
    hdr->totlen = htonl(totlen);
    clusterSendMessage(link,buf,totlen);
    zfree(buf);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x416e5(%rip), %r12     # 0x6f088
movq 0x668(%r12), %rax
movq 0x10(%rax), %rax
movq 0x48(%rax), %rcx
addq 0x28(%rax), %rcx
movabsq $-0x3333333333333333, %rdx # imm = 0xCCCCCCCCCCCCCCCD
movq %rcx, %rax
mulq %rdx
leal -0x2(%rcx), %r15d
shrq $0x3, %rdx
cmpl $0x4, %edx
movl $0x3, %eax
cmovgel %edx, %eax
cmpl %r15d, %eax
cmovgel %r15d, %eax
movq %rax, 0x20(%rsp)
imull $0x68, %eax, %eax
addl $0x60, %eax
cmpl $0xc9, %eax
movl $0xc8, %edi
cmovgel %eax, %edi
callq 0x1feff
movq %rax, %r13
testl %ebp, %ebp
jne 0x2da19
cmpq $0x0, 0x20(%rbx)
je 0x2da19
callq 0x1a1d3
movq 0x20(%rbx), %rcx
movq %rax, 0x38(%rcx)
movq %r13, %rdi
movl %ebp, %esi
callq 0x2dfb7
testl %r15d, %r15d
jle 0x2dbc0
movq 0x20(%rsp), %rax
leal (%rax,%rax,2), %r14d
addl %eax, %eax
movl %eax, 0x4(%rsp)
movq %r13, %rax
addq $0x60, %rax
movq %rax, 0x18(%rsp)
xorl %edx, %edx
movq %rbx, 0x8(%rsp)
movq %r13, 0x10(%rsp)
subl $0x1, %r14d
jb 0x2dbc2
movq %rdx, %rbp
movl %r15d, (%rsp)
movq 0x668(%r12), %rax
movq 0x10(%rax), %rdi
callq 0x193bf
movq 0x8(%rax), %r15
cmpq 0x55b20(%rip), %r15     # 0x835a0
je 0x2daaa
cmpl 0x4(%rsp), %r14d
movq %rbp, %rdx
jle 0x2da97
testb $0x3, 0x30(%r15)
je 0x2dbab
testb $0x18, 0x30(%r15)
je 0x2dab6
movl (%rsp), %r15d
decl %r15d
jmp 0x2dbaf
movl (%rsp), %r15d
movq %rbp, %rdx
jmp 0x2dbaf
cmpq $0x0, 0x88(%r15)
je 0x2da9e
testl %edx, %edx
jle 0x2db02
movl %edx, %ebx
movq 0x18(%rsp), %r12
xorl %r13d, %r13d
movl $0x28, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x12620
testl %eax, %eax
je 0x2daf1
incq %r13
addq $0x68, %r12
cmpq %r13, %rbx
jne 0x2dace
movl %ebx, %r13d
movq 0x8(%rsp), %rbx
leaq 0x4158b(%rip), %r12     # 0x6f088
movq %rbp, %rdx
jmp 0x2db05
xorl %r13d, %r13d
cmpl %edx, %r13d
jne 0x2dba6
movl (%rsp), %ebp
decl %ebp
movslq %edx, %rax
imulq $0x68, %rax, %rax
movq 0x20(%r15), %rcx
movq 0x18(%rsp), %rsi
movq %rcx, 0x20(%rsi,%rax)
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups %xmm1, 0x10(%rsi,%rax)
movups %xmm0, (%rsi,%rax)
movl 0x38(%r15), %ecx
bswapl %ecx
movl %ecx, 0x28(%rsi,%rax)
movl 0x40(%r15), %ecx
bswapl %ecx
movl %ecx, 0x2c(%rsi,%rax)
movups 0x50(%r15), %xmm0
movups 0x60(%r15), %xmm1
movups 0x6e(%r15), %xmm2
movups %xmm0, 0x30(%rsi,%rax)
movups %xmm1, 0x40(%rsi,%rax)
movups %xmm2, 0x4e(%rsi,%rax)
movzwl 0x80(%r15), %ecx
rolw $0x8, %cx
movw %cx, 0x5e(%rsi,%rax)
movzwl 0x30(%r15), %ecx
movl %ebp, %r15d
rolw $0x8, %cx
movw %cx, 0x60(%rsi,%rax)
movw $0x0, 0x62(%rsi,%rax)
movl $0x0, 0x64(%rsi,%rax)
incl %edx
movq 0x10(%rsp), %r13
jmp 0x2dbaf
movq 0x10(%rsp), %r13
movl (%rsp), %r15d
testl %r15d, %r15d
jle 0x2dbc2
cmpl 0x20(%rsp), %edx
jl 0x2da53
jmp 0x2dbc2
xorl %edx, %edx
imull $0x68, %edx, %eax
addl $0x60, %eax
rolw $0x8, %dx
movw %dx, 0xe(%r13)
movslq %eax, %rdx
bswapl %eax
movl %eax, 0x4(%r13)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2dec0
movq %r13, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1ffff