// File: /workspace/github_c_makefile_star2_O1/erikdubbelboer[P]discnt/src/cluster.c
// Function: clusterCron
void clusterCron(void) {
    dictIterator *di;
    dictEntry *de;
    int update_state = 0;
    mstime_t min_pong = 0, now = mstime();
    clusterNode *min_pong_node = NULL;
    static unsigned long long iteration = 0;
    mstime_t handshake_timeout;

    iteration++; /* Number of times this function was called so far. */

    /* The handshake timeout is the time after which a handshake node that was
     * not turned into a normal node is removed from the nodes. Usually it is
     * just the NODE_TIMEOUT value, but when NODE_TIMEOUT is too small we use
     * the value of 1 second. */
    handshake_timeout = server.cluster_node_timeout;
    if (handshake_timeout < 1000) handshake_timeout = 1000;

    /* Check if we have disconnected nodes and re-establish the connection. */
    di = dictGetSafeIterator(server.cluster->nodes);
    while((de = dictNext(di)) != NULL) {
        clusterNode *node = dictGetVal(de);

        if (node->flags & (CLUSTER_NODE_MYSELF|CLUSTER_NODE_NOADDR)) continue;

        /* A Node in HANDSHAKE state has a limited lifespan equal to the
         * configured node timeout. */
        if (nodeInHandshake(node) && now - node->ctime > handshake_timeout) {
            clusterDelNode(node);
            continue;
        }

        if (node->link == NULL) {
            int fd;
            mstime_t old_ping_sent;
            clusterLink *link;

            fd = anetTcpNonBlockBindConnect(server.neterr, node->ip,
                node->port+CLUSTER_PORT_INCR, NET_FIRST_BIND_ADDR);
            if (fd == -1) {
                /* We got a synchronous error from connect before
                 * clusterSendPing() had a chance to be called.
                 * If node->ping_sent is zero, failure detection can't work,
                 * so we claim we actually sent a ping now (that will
                 * be really sent as soon as the link is obtained). */
                if (node->ping_sent == 0) node->ping_sent = mstime();
                serverLog(LL_DEBUG, "Unable to connect to "
                    "Cluster Node [%s]:%d -> %s", node->ip,
                    node->port+CLUSTER_PORT_INCR,
                    server.neterr);
                continue;
            }
            anetEnableTcpNoDelay(NULL,fd);
            link = createClusterLink(node);
            link->fd = fd;
            node->link = link;
            aeCreateFileEvent(server.el,link->fd,AE_READABLE,
                    clusterReadHandler,link);
            /* Queue a PING in the new connection ASAP: this is crucial
             * to avoid false positives in failure detection.
             *
             * If the node is flagged as MEET, we send a MEET message instead
             * of a PING one, to force the receiver to add us in its node
             * table. */
            old_ping_sent = node->ping_sent;
            clusterSendPing(link, node->flags & CLUSTER_NODE_MEET ?
                    CLUSTERMSG_TYPE_MEET : CLUSTERMSG_TYPE_PING);
            if (old_ping_sent) {
                /* If there was an active ping before the link was
                 * disconnected, we want to restore the ping time, otherwise
                 * replaced by the clusterSendPing() call. */
                node->ping_sent = old_ping_sent;
            }
            /* We can clear the flag after the first packet is sent.
             * If we'll never receive a PONG, we'll never send new packets
             * to this node. Instead after the PONG is received and we
             * are no longer in meet/handshake status, we want to send
             * normal PING packets. */
            node->flags &= ~CLUSTER_NODE_MEET;

            serverLog(LL_DEBUG,"Connecting with Node %.40s at %s:%d",
                    node->name, node->ip, node->port+CLUSTER_PORT_INCR);
        }
    }
    dictReleaseIterator(di);

    /* Ping some random node 1 time every 10 iterations, so that we usually ping
     * one random node every second. */
    if (!(iteration % 10)) {
        int j;

        /* Check a few random nodes and ping the one with the oldest
         * pong_received time. */
        for (j = 0; j < 5; j++) {
            de = dictGetRandomKey(server.cluster->nodes);
            clusterNode *this = dictGetVal(de);

            /* Don't ping nodes disconnected or with a ping currently active. */
            if (this->link == NULL || this->ping_sent != 0) continue;
            if (this->flags & (CLUSTER_NODE_MYSELF|CLUSTER_NODE_HANDSHAKE))
                continue;
            if (min_pong_node == NULL || min_pong > this->pong_received) {
                min_pong_node = this;
                min_pong = this->pong_received;
            }
        }
        if (min_pong_node) {
            /*serverLog(LL_DEBUG,"Pinging node %.40s", min_pong_node->name);*/
            clusterSendPing(min_pong_node->link, CLUSTERMSG_TYPE_PING);
        }
    }

    /* Iterate nodes to check if we need to flag something as failing.
     * This loop is also responsible to:
     * 1) Check if there are orphaned masters (masters without non failing
     *    slaves).
     * 2) Count the max number of non failing slaves for a single master.
     * 3) Count the number of slaves for our master, if we are a slave. */
    di = dictGetSafeIterator(server.cluster->nodes);
    while((de = dictNext(di)) != NULL) {
        clusterNode *node = dictGetVal(de);
        now = mstime(); /* Use an updated time at every iteration. */
        mstime_t delay;

        if (node->flags &
            (CLUSTER_NODE_MYSELF|CLUSTER_NODE_NOADDR|CLUSTER_NODE_HANDSHAKE))
                continue;

        /* If we are waiting for the PONG more than half the cluster
         * timeout, reconnect the link: maybe there is a connection
         * issue even if the node is alive. */
        if (node->link && /* is connected */
            now - node->link->ctime >
            server.cluster_node_timeout && /* was not already reconnected */
            node->ping_sent && /* we already sent a ping */
            node->pong_received < node->ping_sent && /* still waiting pong */
            /* and we are waiting for the pong more than timeout/2 */
            now - node->ping_sent > server.cluster_node_timeout/2)
        {
            /* Disconnect the link, it will be reconnected automatically. */
            freeClusterLink(node->link);
        }

        /* If we have currently no active ping in this instance, and the
         * received PONG is older than half the cluster timeout, send
         * a new ping now, to ensure all the nodes are pinged without
         * a too big delay. */
        if (node->link &&
            node->ping_sent == 0 &&
            (now - node->pong_received) > server.cluster_node_timeout/2)
        {
            clusterSendPing(node->link, CLUSTERMSG_TYPE_PING);
            continue;
        }

        /* Check only if we have an active ping for this instance. */
        if (node->ping_sent == 0) continue;

        /* Compute the delay of the PONG. Note that if we already received
         * the PONG, then node->ping_sent is zero, so can't reach this
         * code at all. */
        delay = now - node->ping_sent;

        if (delay > server.cluster_node_timeout) {
            /* Timeout reached. Set the node as possibly failing if it is
             * not already in this state. */
            if (!(node->flags & (CLUSTER_NODE_PFAIL|CLUSTER_NODE_FAIL))) {
                serverLog(LL_DEBUG,"*** NODE %.40s possibly failing",
                    node->name);
                node->flags |= CLUSTER_NODE_PFAIL;
                update_state = 1;
            }
        }
    }
    dictReleaseIterator(di);

    if (update_state || server.cluster->state == CLUSTER_FAIL)
        clusterUpdateState();

    clusterUpdateReachableNodes();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
callq 0x1a1d3
movq %rax, %r12
incq 0x55540(%rip)           # 0x835a8
leaq 0x41019(%rip), %r13     # 0x6f088
movq 0x658(%r13), %rax
movq 0x668(%r13), %rcx
cmpq $0x3e9, %rax            # imm = 0x3E9
movl $0x3e8, %ebx            # imm = 0x3E8
cmovgeq %rax, %rbx
movq 0x10(%rcx), %rdi
callq 0x1926c
movq %rax, %r14
movq %rax, %rdi
callq 0x1929d
testq %rax, %rax
je 0x2e230
movl $0x2710, %r15d          # imm = 0x2710
movl $0x218, %ecx            # imm = 0x218
addq 0x3fefd(%rip), %rcx     # 0x6dfb8
movq %rcx, (%rsp)
leaq 0x218(%r13), %rcx
movq %rcx, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
movq 0x8(%rax), %rbp
movl 0x30(%rbp), %eax
testb $0x14, %al
jne 0x2e21f
testb $0x8, %al
je 0x2e0fc
movq %r12, %rax
subq 0x28(%rbp), %rax
cmpq %rbx, %rax
jle 0x2e0fc
movq %rbp, %rdi
callq 0x2c827
jmp 0x2e21f
cmpq $0x0, 0x88(%rbp)
jne 0x2e21f
cmpl $0x0, 0x140(%r13)
je 0x2e11d
movq 0xc0(%r13), %rcx
jmp 0x2e11f
xorl %ecx, %ecx
leaq 0x50(%rbp), %r13
movl 0x80(%rbp), %edx
addl %r15d, %edx
movq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x17eef
cmpl $-0x1, %eax
je 0x2e1e2
movl %eax, %r15d
movq %r12, %rbx
xorl %edi, %edi
movl %eax, %esi
callq 0x17a1f
movq %rbp, %rdi
callq 0x2c894
movq %rax, %r12
movl %r15d, 0x8(%rax)
movq %rax, 0x88(%rbp)
leaq 0x40f1a(%rip), %rax     # 0x6f088
movq 0x38(%rax), %rdi
movl %r15d, %esi
movl $0x1, %edx
leaq -0x1836(%rip), %rcx     # 0x2c94b
movq %r12, %r8
callq 0x17077
movq 0x38(%rbp), %r15
movl 0x30(%rbp), %esi
shrl $0x4, %esi
andl $0x2, %esi
movq %r12, %rdi
callq 0x2d989
testq %r15, %r15
je 0x2e1a7
movq %r15, 0x38(%rbp)
andb $-0x21, 0x30(%rbp)
movl 0x80(%rbp), %r8d
movl $0x2710, %r15d          # imm = 0x2710
addl %r15d, %r8d
xorl %edi, %edi
leaq 0x28f36(%rip), %rsi     # 0x570fa
movq %rbp, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x19fb1
leaq 0x40eb0(%rip), %r13     # 0x6f088
movq %rbx, %r12
movq 0x8(%rsp), %rbx
jmp 0x2e21f
cmpq $0x0, 0x38(%rbp)
jne 0x2e1f2
callq 0x1a1d3
movq %rax, 0x38(%rbp)
movl 0x80(%rbp), %ecx
movl $0x2710, %r15d          # imm = 0x2710
addl %r15d, %ecx
xorl %edi, %edi
leaq 0x28ec0(%rip), %rsi     # 0x570ca
movq %r13, %rdx
movq (%rsp), %r8
xorl %eax, %eax
callq 0x19fb1
leaq 0x40e69(%rip), %r13     # 0x6f088
movq %r14, %rdi
callq 0x1929d
testq %rax, %rax
jne 0x2e0d0
movq %r14, %rdi
callq 0x1935a
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
imulq 0x5535e(%rip), %rax     # 0x835a8
rorq %rax
movabsq $0x1999999999999999, %rcx # imm = 0x1999999999999999
cmpq %rcx, %rax
ja 0x2e2bb
movl $0x5, %ebx
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x668(%r13), %rax
movq 0x10(%rax), %rdi
callq 0x193bf
movq 0x8(%rax), %rax
cmpq $0x0, 0x88(%rax)
je 0x2e2a4
cmpq $0x0, 0x38(%rax)
jne 0x2e2a4
testb $0xc, 0x30(%rax)
jne 0x2e2a4
testq %r15, %r15
je 0x2e29d
cmpq 0x40(%rax), %r14
jle 0x2e2a4
movq 0x40(%rax), %r14
movq %rax, %r15
decl %ebx
jne 0x2e267
testq %r15, %r15
je 0x2e2bb
movq 0x88(%r15), %rdi
xorl %esi, %esi
callq 0x2d989
movq 0x668(%r13), %rax
movq 0x10(%rax), %rdi
callq 0x1926c
movq %rax, %rbx
movq %rax, %rdi
callq 0x1929d
testq %rax, %rax
je 0x2e3ed
leaq 0x28e38(%rip), %r14     # 0x5711e
xorl %ebp, %ebp
movq 0x8(%rax), %r15
callq 0x1a1d3
testb $0x1c, 0x30(%r15)
je 0x2e30a
movq %rbx, %rdi
callq 0x1929d
testq %rax, %rax
jne 0x2e2e8
jmp 0x2e3df
movq %rax, %r12
movq 0x88(%r15), %rdi
testq %rdi, %rdi
je 0x2e357
movq %r12, %rcx
subq (%rdi), %rcx
movq 0x658(%r13), %rax
cmpq %rax, %rcx
jle 0x2e357
movq 0x38(%r15), %rcx
testq %rcx, %rcx
je 0x2e357
cmpq %rcx, 0x40(%r15)
jge 0x2e357
movq %r12, %rdx
subq %rcx, %rdx
movq %rax, %rcx
shrq $0x3f, %rcx
addq %rax, %rcx
sarq %rcx
cmpq %rcx, %rdx
jle 0x2e357
callq 0x2c8d9
movq 0x88(%r15), %rdi
testq %rdi, %rdi
je 0x2e399
cmpq $0x0, 0x38(%r15)
jne 0x2e399
movq 0x40(%r15), %rax
movq %r12, %rcx
subq %rax, %rcx
movq 0x658(%r13), %rax
movq %rax, %rdx
shrq $0x3f, %rdx
addq %rax, %rdx
sarq %rdx
cmpq %rdx, %rcx
jle 0x2e399
xorl %esi, %esi
callq 0x2d989
jmp 0x2e2f8
movq 0x38(%r15), %rax
testq %rax, %rax
je 0x2e2f8
subq %rax, %r12
cmpq 0x658(%r13), %r12
jle 0x2e2f8
testb $0x3, 0x30(%r15)
jne 0x2e2f8
xorl %edi, %edi
movq %r14, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x19fb1
orb $0x1, 0x30(%r15)
movl $0x1, %ebp
jmp 0x2e2f8
movq %rbx, %rdi
callq 0x1935a
testl %ebp, %ebp
jne 0x2e402
jmp 0x2e3f5
movq %rbx, %rdi
callq 0x1935a
movq 0x668(%r13), %rax
cmpl $0x1, 0x8(%rax)
jne 0x2e407
callq 0x2c664
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c6c2