// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/src/cluster.c
// Function: clusterUpdateState
void clusterUpdateState(void) {
    int j, new_state;
    int reachable_masters = 0;
    static mstime_t among_minority_time;
    static mstime_t first_call_time = 0;

    server.cluster->todo_before_sleep &= ~CLUSTER_TODO_UPDATE_STATE;

    /* If this is a master node, wait some time before turning the state
     * into OK, since it is not a good idea to rejoin the cluster as a writable
     * master, after a reboot, without giving the cluster a chance to
     * reconfigure this node. Note that the delay is calculated starting from
     * the first call to this function and not since the server start, in order
     * to don't count the DB loading time. */
    if (first_call_time == 0) first_call_time = mstime();
    if (nodeIsMaster(myself) &&
        server.cluster->state == CLUSTER_FAIL &&
        mstime() - first_call_time < CLUSTER_WRITABLE_DELAY) return;

    /* Start assuming the state is OK. We'll turn it into FAIL if there
     * are the right conditions. */
    new_state = CLUSTER_OK;

    /* Check if all the slots are covered. */
    if (server.cluster_require_full_coverage) {
        for (j = 0; j < CLUSTER_SLOTS; j++) {
            if (server.cluster->slots[j] == NULL ||
                server.cluster->slots[j]->flags & (CLUSTER_NODE_FAIL))
            {
                new_state = CLUSTER_FAIL;
                break;
            }
        }
    }

    /* Compute the cluster size, that is the number of master nodes
     * serving at least a single slot.
     *
     * At the same time count the number of reachable masters having
     * at least one slot. */
    {
        dictIterator *di;
        dictEntry *de;

        server.cluster->size = 0;
        di = dictGetSafeIterator(server.cluster->nodes);
        while((de = dictNext(di)) != NULL) {
            clusterNode *node = dictGetVal(de);

            if (nodeIsMaster(node) && node->numslots) {
                server.cluster->size++;
                if ((node->flags & (CLUSTER_NODE_FAIL|CLUSTER_NODE_PFAIL)) == 0)
                    reachable_masters++;
            }
        }
        dictReleaseIterator(di);
    }

    /* If we are in a minority partition, change the cluster state
     * to FAIL. */
    {
        int needed_quorum = (server.cluster->size / 2) + 1;

        if (reachable_masters < needed_quorum) {
            new_state = CLUSTER_FAIL;
            among_minority_time = mstime();
        }
    }

    /* Log a state change */
    if (new_state != server.cluster->state) {
        mstime_t rejoin_delay = server.cluster_node_timeout;

        /* If the instance is a master and was partitioned away with the
         * minority, don't let it accept queries for some time after the
         * partition heals, to make sure there is enough time to receive
         * a configuration update. */
        if (rejoin_delay > CLUSTER_MAX_REJOIN_DELAY)
            rejoin_delay = CLUSTER_MAX_REJOIN_DELAY;
        if (rejoin_delay < CLUSTER_MIN_REJOIN_DELAY)
            rejoin_delay = CLUSTER_MIN_REJOIN_DELAY;

        if (new_state == CLUSTER_OK &&
            nodeIsMaster(myself) &&
            mstime() - among_minority_time < rejoin_delay)
        {
            return;
        }

        /* Change the state and log the event. */
        serverLog(LL_WARNING,"Cluster state changed: %s",
            new_state == CLUSTER_OK ? "ok" : "fail");
        server.cluster->state = new_state;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
leaq 0x99a19(%rip), %r14     # 0x106858
movq 0x9a0(%r14), %rax
andl $-0x3, 0x60080(%rax)
cmpq $0x0, 0xae39b(%rip)     # 0x11b1f0
jne 0x6ce63
callq 0x330c8
movq %rax, 0xae38d(%rip)     # 0x11b1f0
movq 0xae366(%rip), %rax     # 0x11b1d0
testb $0x1, 0x30(%rax)
je 0x6ce95
movq 0x9a0(%r14), %rax
cmpl $0x1, 0x10(%rax)
jne 0x6ce95
callq 0x330c8
subq 0xae367(%rip), %rax     # 0x11b1f0
cmpq $0x7d0, %rax            # imm = 0x7D0
jl 0x6cfe8
xorl %r15d, %r15d
cmpl $0x0, 0x9b0(%r14)
movl $0x0, %ebp
je 0x6ced6
movq 0x9a0(%r14), %rax
xorl %ecx, %ecx
movl $0x1, %ebp
movq 0x40028(%rax,%rcx,8), %rdx
testq %rdx, %rdx
je 0x6ced6
testb $0x8, 0x30(%rdx)
jne 0x6ced6
incq %rcx
cmpq $0x4000, %rcx           # imm = 0x4000
jne 0x6ceb5
xorl %ebp, %ebp
movq 0x9a0(%r14), %rax
movl $0x0, 0x14(%rax)
movq 0x18(%rax), %rdi
callq 0x320e5
movq %rax, %rbx
movq %rax, %rdi
callq 0x32116
testq %rax, %rax
je 0x6cf35
xorl %r15d, %r15d
movq 0x8(%rax), %rcx
movl 0x30(%rcx), %eax
testb $0x1, %al
je 0x6cf28
cmpl $0x0, 0x840(%rcx)
je 0x6cf28
movq 0x9a0(%r14), %rcx
incl 0x14(%rcx)
andl $0xc, %eax
cmpl $0x1, %eax
adcl $0x0, %r15d
movq %rbx, %rdi
callq 0x32116
testq %rax, %rax
jne 0x6cf00
movq %rbx, %rdi
callq 0x321d3
movq 0x9a0(%r14), %rax
movl 0x14(%rax), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
cmpl %ecx, %r15d
jg 0x6cf66
callq 0x330c8
movq %rax, 0xae287(%rip)     # 0x11b1e8
movl $0x1, %ebp
movq 0x9a0(%r14), %rax
cmpl 0x10(%rax), %ebp
je 0x6cfe8
movq 0x990(%r14), %rax
movl $0x1388, %ecx           # imm = 0x1388
cmpq %rcx, %rax
cmovlq %rax, %rcx
cmpq $0x1f5, %rcx            # imm = 0x1F5
movl $0x1f4, %r15d           # imm = 0x1F4
cmovgeq %rcx, %r15
testl %ebp, %ebp
je 0x6cfa3
leaq 0x5d6fc(%rip), %rbx     # 0xca69d
jmp 0x6cfc8
movq 0xae226(%rip), %rax     # 0x11b1d0
leaq 0x670f6(%rip), %rbx     # 0xd40a7
testb $0x1, 0x30(%rax)
je 0x6cfc8
callq 0x330c8
subq 0xae225(%rip), %rax     # 0x11b1e8
cmpq %r15, %rax
jl 0x6cfe8
leaq 0x5e57d(%rip), %rsi     # 0xcb54c
movl $0x3, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x32ea6
movq 0x9a0(%r14), %rax
movl %ebp, 0x10(%rax)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq