// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/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 %r12
pushq %rbx
leaq 0x9b1df(%rip), %r14     # 0xf98a8
movq 0x9b0(%r14), %rax
andl $-0x3, 0x60080(%rax)
cmpq $0x0, 0xafb71(%rip)     # 0x10e250
jne 0x5e6f4
callq 0x2b8ae
movq %rax, 0xafb63(%rip)     # 0x10e250
movq 0x9b0(%r14), %rax
movq 0xafb35(%rip), %rcx     # 0x10e230
testb $0x1, 0x30(%rcx)
je 0x5e726
cmpl $0x1, 0x10(%rax)
jne 0x5e726
callq 0x2b8ae
subq 0xafb3d(%rip), %rax     # 0x10e250
cmpq $0x7d0, %rax            # imm = 0x7D0
jl 0x5e857
movq 0x9b0(%r14), %rax
xorl %ebp, %ebp
cmpl $0x0, 0x9c0(%r14)
movl $0x0, %r15d
je 0x5e762
pushq $0x1
popq %rcx
xorl %r15d, %r15d
xorl %edx, %edx
cmpq $0x4000, %rdx           # imm = 0x4000
je 0x5e762
movq 0x40028(%rax,%rdx,8), %rsi
testq %rsi, %rsi
je 0x5e75f
incq %rdx
testb $0x8, 0x30(%rsi)
je 0x5e740
movl %ecx, %r15d
andl $0x0, 0x14(%rax)
movq 0x18(%rax), %rdi
callq 0x2a9c4
movq %rax, %rbx
movq %rbx, %rdi
callq 0x2a9d3
testq %rax, %rax
je 0x5e7a8
movq 0x8(%rax), %rcx
movl 0x30(%rcx), %eax
testb $0x1, %al
je 0x5e772
cmpl $0x0, 0x840(%rcx)
je 0x5e772
movq 0x9b0(%r14), %rcx
incl 0x14(%rcx)
andl $0xc, %eax
cmpl $0x1, %eax
adcl $0x0, %ebp
jmp 0x5e772
movq %rbx, %rdi
callq 0x2aa94
movq 0x9b0(%r14), %rcx
movl 0x14(%rcx), %eax
pushq $0x2
popq %rsi
cltd
idivl %esi
cmpl %eax, %ebp
jg 0x5e7db
callq 0x2b8ae
movq %rax, 0xafa78(%rip)     # 0x10e248
movq 0x9b0(%r14), %rcx
pushq $0x1
popq %r15
cmpl 0x10(%rcx), %r15d
je 0x5e857
movq 0x9a0(%r14), %rax
movl $0x1388, %ecx           # imm = 0x1388
cmpq %rcx, %rax
cmovlq %rax, %rcx
cmpq $0x1f5, %rcx            # imm = 0x1F5
movl $0x1f4, %r12d           # imm = 0x1F4
cmovgeq %rcx, %r12
testl %r15d, %r15d
je 0x5e813
leaq 0x59efc(%rip), %rbx     # 0xb870d
jmp 0x5e838
movq 0xafa16(%rip), %rax     # 0x10e230
leaq 0x63476(%rip), %rbx     # 0xc1c97
testb $0x1, 0x30(%rax)
je 0x5e838
callq 0x2b8ae
subq 0xafa15(%rip), %rax     # 0x10e248
cmpq %r12, %rax
jl 0x5e857
leaq 0x5ad7d(%rip), %rsi     # 0xb95bc
pushq $0x3
popq %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2b699
movq 0x9b0(%r14), %rax
movl %r15d, 0x10(%rax)
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq