// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/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 && this->numslots == 0))
        {
            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 fiend 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 $0x38, %rsp
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x9bf46(%rip), %r12     # 0xf98a8
movq 0x9b0(%r12), %rax
movq 0x18(%rax), %rcx
movq 0x48(%rcx), %rax
addq 0x28(%rcx), %rax
leal -0x2(%rax), %r15d
pushq $0xa
popq %rcx
xorl %edx, %edx
divq %rcx
cmpl $0x4, %eax
pushq $0x3
popq %r13
cmovgel %eax, %r13d
cmpl %r15d, %r13d
cmovgel %r15d, %r13d
imull $0x68, %r13d, %eax
addl $0x8a0, %eax            # imm = 0x8A0
cmpl $0x10d1, %eax           # imm = 0x10D1
movl $0x10d0, %edi           # imm = 0x10D0
cmovgel %eax, %edi
callq 0x3280b
movq %rax, %r14
movq %rax, 0x8(%rsp)
testl %ebp, %ebp
jne 0x5d9d2
cmpq $0x0, 0x20(%rbx)
je 0x5d9d2
callq 0x2b8ae
movq 0x20(%rbx), %rcx
movq %rax, 0x858(%rcx)
movq %rbx, 0x30(%rsp)
movq %r14, 0x28(%rsp)
movq %r14, %rdi
movl %ebp, %esi
callq 0x5e068
leal (%r13,%r13,2), %ebx
leal (,%r13,2), %eax
movl %eax, 0x14(%rsp)
addq $0x8a0, 0x8(%rsp)       # imm = 0x8A0
xorl %r14d, %r14d
xorl %ebp, %ebp
movq %r13, 0x18(%rsp)
movq %rbp, 0x20(%rsp)
cmpl %r13d, %ebp
setl %al
movl %r15d, 0x4(%rsp)
testl %r15d, %r15d
setg %cl
andb %al, %cl
movb %cl, 0x3(%rsp)
cmpb $0x0, 0x3(%rsp)
je 0x5db8c
subl $0x1, %ebx
jb 0x5db8c
movq 0x9b0(%r12), %rax
movq 0x18(%rax), %rdi
callq 0x2aaf6
movq 0x8(%rax), %r15
cmpq 0xb07da(%rip), %r15     # 0x10e230
je 0x5da26
movl 0x30(%r15), %eax
cmpl 0x14(%rsp), %ebx
jle 0x5da69
movl %eax, %ecx
andl $0xc, %ecx
je 0x5da26
testb $0x60, %al
jne 0x5dad1
cmpq $0x0, 0x8c8(%r15)
jne 0x5da81
cmpl $0x0, 0x840(%r15)
je 0x5dad1
leaq 0x8(%r15), %r13
movq 0x8(%rsp), %rbp
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x5daad
movq %rbp, %rdi
movq %r13, %rsi
pushq $0x28
popq %rdx
callq 0x266f0
testl %eax, %eax
je 0x5dab7
decq %r12
addq $0x68, %rbp
jmp 0x5da8d
movq 0x20(%rsp), %rbp
movl %ebp, %r12d
jmp 0x5dabf
negl %r12d
movq 0x20(%rsp), %rbp
cmpl %ebp, %r12d
leaq 0x9bddf(%rip), %r12     # 0xf98a8
jne 0x5da26
jmp 0x5dae3
movl 0x4(%rsp), %r15d
decl %r15d
movq 0x18(%rsp), %r13
jmp 0x5da0f
movl %ebp, %eax
imulq $0x68, %rax, %rax
movq 0x20(%r13), %rcx
movq 0x8(%rsp), %rdx
movq %rcx, 0x20(%rdx,%rax)
movups (%r13), %xmm0
movups 0x10(%r13), %xmm1
movups %xmm1, 0x10(%rdx,%rax)
movups %xmm0, (%rdx,%rax)
movl 0x858(%r15), %ecx
bswapl %ecx
movl %ecx, 0x28(%rdx,%rax)
movl 0x860(%r15), %ecx
bswapl %ecx
movl %ecx, 0x2c(%rdx,%rax)
movups 0x890(%r15), %xmm0
movups 0x8a0(%r15), %xmm1
movups 0x8ae(%r15), %xmm2
movups %xmm0, 0x30(%rdx,%rax)
movups %xmm1, 0x40(%rdx,%rax)
movups %xmm2, 0x4e(%rdx,%rax)
movzwl 0x8c0(%r15), %ecx
rolw $0x8, %cx
movw %cx, 0x5e(%rdx,%rax)
movzwl 0x30(%r15), %ecx
rolw $0x8, %cx
movw %cx, 0x60(%rdx,%rax)
andw $0x0, 0x62(%rdx,%rax)
andl $0x0, 0x64(%rdx,%rax)
movl 0x4(%rsp), %r15d
decl %r15d
incl %ebp
decq %r14
movq 0x18(%rsp), %r13
jmp 0x5da0a
imull $0x68, %ebp, %eax
addl $0x8a0, %eax            # imm = 0x8A0
rolw $0x8, %bp
movq 0x28(%rsp), %rbx
movw %bp, 0xe(%rbx)
movslq %eax, %rdx
bswapl %eax
movl %eax, 0x4(%rbx)
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x5df88
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x328f9