// File: /workspace/github_c_makefile_star2_O3/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 0x19e27
movq %rax, %r12
incq 0x55b26(%rip)           # 0x835a8
leaq 0x415ff(%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 0x18ec7
movq %rax, %r14
movq %rax, %rdi
callq 0x18ef7
testq %rax, %rax
je 0x2dc4a
movl $0x2710, %r15d          # imm = 0x2710
movl $0x218, %ecx            # imm = 0x218
addq 0x404e3(%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 0x2dc39
testb $0x8, %al
je 0x2db16
movq %r12, %rax
subq 0x28(%rbp), %rax
cmpq %rbx, %rax
jle 0x2db16
movq %rbp, %rdi
callq 0x2c23c
jmp 0x2dc39
cmpq $0x0, 0x88(%rbp)
jne 0x2dc39
cmpl $0x0, 0x140(%r13)
je 0x2db37
movq 0xc0(%r13), %rcx
jmp 0x2db39
xorl %ecx, %ecx
leaq 0x50(%rbp), %r13
movl 0x80(%rbp), %edx
addl %r15d, %edx
movq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x17b71
cmpl $-0x1, %eax
je 0x2dbfc
movl %eax, %r15d
movq %r12, %rbx
xorl %edi, %edi
movl %eax, %esi
callq 0x176bb
movq %rbp, %rdi
callq 0x2c2a9
movq %rax, %r12
movl %r15d, 0x8(%rax)
movq %rax, 0x88(%rbp)
leaq 0x41500(%rip), %rax     # 0x6f088
movq 0x38(%rax), %rdi
movl %r15d, %esi
movl $0x1, %edx
leaq -0x183b(%rip), %rcx     # 0x2c360
movq %r12, %r8
callq 0x16d02
movq 0x38(%rbp), %r15
movl 0x30(%rbp), %esi
shrl $0x4, %esi
andl $0x2, %esi
movq %r12, %rdi
callq 0x2d3d2
testq %r15, %r15
je 0x2dbc1
movq %r15, 0x38(%rbp)
andb $-0x21, 0x30(%rbp)
movl 0x80(%rbp), %r8d
movl $0x2710, %r15d          # imm = 0x2710
addl %r15d, %r8d
xorl %edi, %edi
leaq 0x29508(%rip), %rsi     # 0x570e6
movq %rbp, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x19c06
leaq 0x41496(%rip), %r13     # 0x6f088
movq %rbx, %r12
movq 0x8(%rsp), %rbx
jmp 0x2dc39
cmpq $0x0, 0x38(%rbp)
jne 0x2dc0c
callq 0x19e27
movq %rax, 0x38(%rbp)
movl 0x80(%rbp), %ecx
movl $0x2710, %r15d          # imm = 0x2710
addl %r15d, %ecx
xorl %edi, %edi
leaq 0x29492(%rip), %rsi     # 0x570b6
movq %r13, %rdx
movq (%rsp), %r8
xorl %eax, %eax
callq 0x19c06
leaq 0x4144f(%rip), %r13     # 0x6f088
movq %r14, %rdi
callq 0x18ef7
testq %rax, %rax
jne 0x2daea
movq %r14, %rdi
callq 0x18fbf
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
imulq 0x55944(%rip), %rax     # 0x835a8
rorq %rax
movabsq $0x1999999999999999, %rcx # imm = 0x1999999999999999
cmpq %rcx, %rax
ja 0x2dcdf
movl $0x5, %ebx
xorl %r15d, %r15d
xorl %r14d, %r14d
movq 0x668(%r13), %rax
movq 0x10(%rax), %rdi
callq 0x19024
movq 0x8(%rax), %rax
cmpq $0x0, 0x88(%rax)
je 0x2dcc8
cmpq $0x0, 0x38(%rax)
jne 0x2dcc8
testb $0xc, 0x30(%rax)
jne 0x2dcc8
movq 0x40(%rax), %rcx
testq %r14, %r14
je 0x2dcc2
cmpq %rcx, %r15
cmovgq %rax, %r14
cmovgeq %rcx, %r15
jmp 0x2dcc8
movq %rax, %r14
movq %rcx, %r15
decl %ebx
jne 0x2dc81
testq %r14, %r14
je 0x2dcdf
movq 0x88(%r14), %rdi
xorl %esi, %esi
callq 0x2d3d2
movq 0x668(%r13), %rax
movq 0x10(%rax), %rdi
callq 0x18ec7
movq %rax, %rbx
movq %rax, %rdi
callq 0x18ef7
testq %rax, %rax
je 0x2de15
leaq 0x29400(%rip), %r14     # 0x5710a
xorl %ebp, %ebp
movq 0x8(%rax), %r15
callq 0x19e27
testb $0x1c, 0x30(%r15)
je 0x2dd2e
movq %rbx, %rdi
callq 0x18ef7
testq %rax, %rax
jne 0x2dd0c
jmp 0x2de07
movq %rax, %r12
movq 0x88(%r15), %rdi
testq %rdi, %rdi
je 0x2ddc1
movq %r12, %rcx
subq (%rdi), %rcx
movq 0x658(%r13), %rax
cmpq %rax, %rcx
jle 0x2dd8b
movq 0x38(%r15), %rcx
testq %rcx, %rcx
je 0x2dd8b
cmpq %rcx, 0x40(%r15)
jge 0x2dd8b
movq %r12, %rdx
subq %rcx, %rdx
movq %rax, %rcx
shrq $0x3f, %rcx
addq %rax, %rcx
sarq %rcx
cmpq %rcx, %rdx
jle 0x2dd8b
callq 0x2c2ee
movq 0x88(%r15), %rdi
testq %rdi, %rdi
je 0x2ddc1
cmpq $0x0, 0x38(%r15)
jne 0x2ddc1
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 0x2ddc1
xorl %esi, %esi
callq 0x2d3d2
jmp 0x2dd1c
movq 0x38(%r15), %rax
testq %rax, %rax
je 0x2dd1c
subq %rax, %r12
cmpq 0x658(%r13), %r12
jle 0x2dd1c
testb $0x3, 0x30(%r15)
jne 0x2dd1c
xorl %edi, %edi
movq %r14, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x19c06
orb $0x1, 0x30(%r15)
movl $0x1, %ebp
jmp 0x2dd1c
movq %rbx, %rdi
callq 0x18fbf
testl %ebp, %ebp
jne 0x2de2a
jmp 0x2de1d
movq %rbx, %rdi
callq 0x18fbf
movq 0x668(%r13), %rax
cmpl $0x1, 0x8(%rax)
jne 0x2de2f
callq 0x2c079
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2c0d7