// File: /workspace/github_c_makefile_star2_O3/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 %rbx
pushq %rax
leaq 0x985b0(%rip), %r14     # 0x1018a8
movq 0x9b0(%r14), %rax
andl $-0x3, 0x60080(%rax)
cmpq $0x0, 0xacf42(%rip)     # 0x116250
jne 0x69323
callq 0x2ffce
movq %rax, 0xacf34(%rip)     # 0x116250
movq 0x9b0(%r14), %rax
movq 0xacf06(%rip), %rcx     # 0x116230
testb $0x1, 0x30(%rcx)
je 0x69355
cmpl $0x1, 0x10(%rax)
jne 0x69355
callq 0x2ffce
subq 0xacf0e(%rip), %rax     # 0x116250
cmpq $0x7d0, %rax            # imm = 0x7D0
jl 0x6949a
movq 0x9b0(%r14), %rax
xorl %r15d, %r15d
cmpl $0x0, 0x9c0(%r14)
movl $0x0, %ebp
je 0x6938f
xorl %ecx, %ecx
movl $0x1, %ebp
movq 0x40028(%rax,%rcx,8), %rdx
testq %rdx, %rdx
je 0x6938f
testb $0x8, 0x30(%rdx)
jne 0x6938f
incq %rcx
cmpq $0x4000, %rcx           # imm = 0x4000
jne 0x6936e
xorl %ebp, %ebp
movl $0x0, 0x14(%rax)
movq 0x18(%rax), %rdi
callq 0x2efe5
movq %rax, %rbx
movq %rax, %rdi
callq 0x2f015
testq %rax, %rax
je 0x693e7
xorl %r15d, %r15d
movq 0x8(%rax), %rcx
movl 0x30(%rcx), %eax
testb $0x1, %al
je 0x693da
cmpl $0x0, 0x840(%rcx)
je 0x693da
movq 0x9b0(%r14), %rcx
incl 0x14(%rcx)
andl $0xc, %eax
cmpl $0x1, %eax
adcl $0x0, %r15d
movq %rbx, %rdi
callq 0x2f015
testq %rax, %rax
jne 0x693b2
movq %rbx, %rdi
callq 0x2f0dd
movq 0x9b0(%r14), %rax
movl 0x14(%rax), %ecx
movl %ecx, %edx
shrl $0x1f, %edx
addl %ecx, %edx
sarl %edx
cmpl %edx, %r15d
jg 0x6941f
callq 0x2ffce
movq %rax, 0xace35(%rip)     # 0x116248
movq 0x9b0(%r14), %rax
movl $0x1, %ebp
cmpl 0x10(%rax), %ebp
je 0x6949a
movq 0x9a0(%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 0x69455
leaq 0x5e2d2(%rip), %rbx     # 0xc7725
jmp 0x6947a
movq 0xacdd4(%rip), %rax     # 0x116230
leaq 0x67d74(%rip), %rbx     # 0xd11d7
testb $0x1, 0x30(%rax)
je 0x6947a
callq 0x2ffce
subq 0xacdd3(%rip), %rax     # 0x116248
cmpq %r15, %rax
jl 0x6949a
leaq 0x5f153(%rip), %rsi     # 0xc85d4
movl $0x3, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2fdad
movq 0x9b0(%r14), %rax
movl %ebp, 0x10(%rax)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq