// File: /workspace/github_c_makefile_star2_O3/gplessis[P]dotdeb-redis/src/cluster.c
// Function: clusterSendFailoverAuthIfNeeded
void clusterSendFailoverAuthIfNeeded(clusterNode *node, clusterMsg *request) {
    clusterNode *master = node->slaveof;
    uint64_t requestCurrentEpoch = ntohu64(request->currentEpoch);
    uint64_t requestConfigEpoch = ntohu64(request->configEpoch);
    unsigned char *claimed_slots = request->myslots;
    int force_ack = request->mflags[0] & CLUSTERMSG_FLAG0_FORCEACK;
    int j;

    /* IF we are not a master serving at least 1 slot, we don't have the
     * right to vote, as the cluster size in Redis Cluster is the number
     * of masters serving at least one slot, and quorum is the cluster
     * size + 1 */
    if (nodeIsSlave(myself) || myself->numslots == 0) return;

    /* Request epoch must be >= our currentEpoch.
     * Note that it is impossible for it to actually be greater since
     * our currentEpoch was updated as a side effect of receiving this
     * request, if the request epoch was greater. */
    if (requestCurrentEpoch < server.cluster->currentEpoch) {
        serverLog(LL_WARNING,
            "Failover auth denied to %.40s: reqEpoch (%llu) < curEpoch(%llu)",
            node->name,
            (unsigned long long) requestCurrentEpoch,
            (unsigned long long) server.cluster->currentEpoch);
        return;
    }

    /* I already voted for this epoch? Return ASAP. */
    if (server.cluster->lastVoteEpoch == server.cluster->currentEpoch) {
        serverLog(LL_WARNING,
                "Failover auth denied to %.40s: already voted for epoch %llu",
                node->name,
                (unsigned long long) server.cluster->currentEpoch);
        return;
    }

    /* Node must be a slave and its master down.
     * The master can be non failing if the request is flagged
     * with CLUSTERMSG_FLAG0_FORCEACK (manual failover). */
    if (nodeIsMaster(node) || master == NULL ||
        (!nodeFailed(master) && !force_ack))
    {
        if (nodeIsMaster(node)) {
            serverLog(LL_WARNING,
                    "Failover auth denied to %.40s: it is a master node",
                    node->name);
        } else if (master == NULL) {
            serverLog(LL_WARNING,
                    "Failover auth denied to %.40s: I don't know its master",
                    node->name);
        } else if (!nodeFailed(master)) {
            serverLog(LL_WARNING,
                    "Failover auth denied to %.40s: its master is up",
                    node->name);
        }
        return;
    }

    /* We did not voted for a slave about this master for two
     * times the node timeout. This is not strictly needed for correctness
     * of the algorithm but makes the base case more linear. */
    if (mstime() - node->slaveof->voted_time < server.cluster_node_timeout * 2)
    {
        serverLog(LL_WARNING,
                "Failover auth denied to %.40s: "
                "can't vote about this master before %lld milliseconds",
                node->name,
                (long long) ((server.cluster_node_timeout*2)-
                             (mstime() - node->slaveof->voted_time)));
        return;
    }

    /* The slave requesting the vote must have a configEpoch for the claimed
     * slots that is >= the one of the masters currently serving the same
     * slots in the current configuration. */
    for (j = 0; j < CLUSTER_SLOTS; j++) {
        if (bitmapTestBit(claimed_slots, j) == 0) continue;
        if (server.cluster->slots[j] == NULL ||
            server.cluster->slots[j]->configEpoch <= requestConfigEpoch)
        {
            continue;
        }
        /* If we reached this point we found a slot that in our current slots
         * is served by a master with a greater configEpoch than the one claimed
         * by the slave requesting our vote. Refuse to vote for this slave. */
        serverLog(LL_WARNING,
                "Failover auth denied to %.40s: "
                "slot %d epoch (%llu) > reqEpoch (%llu)",
                node->name, j,
                (unsigned long long) server.cluster->slots[j]->configEpoch,
                (unsigned long long) requestConfigEpoch);
        return;
    }

    /* We can vote for this slave. */
    clusterSendFailoverAuth(node);
    server.cluster->lastVoteEpoch = server.cluster->currentEpoch;
    node->slaveof->voted_time = mstime();
    serverLog(LL_WARNING, "Failover auth granted to %.40s for epoch %llu",
        node->name, (unsigned long long) server.cluster->currentEpoch);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
movq 0x850(%rdi), %rbp
movq 0x10(%rsi), %rdi
callq 0x6d177
movq %rax, %r12
movq 0x18(%r14), %rdi
callq 0x6d177
movq %rax, %r15
movq 0xadb32(%rip), %rax     # 0x116230
testb $0x2, 0x30(%rax)
jne 0x687d8
cmpl $0x0, 0x840(%rax)
je 0x687d8
leaq 0x9918c(%rip), %r13     # 0x1018a8
movq 0x9b0(%r13), %rax
movq 0x8(%rax), %r8
cmpq %r8, %r12
jae 0x68757
addq $0x8, %rbx
leaq 0x5f93b(%rip), %rsi     # 0xc8072
movl $0x3, %edi
movq %rbx, %rdx
movq %r12, %rcx
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2fdad
cmpq %r8, 0x60078(%rax)
jne 0x6878b
addq $0x8, %rbx
leaq 0x5f947(%rip), %rsi     # 0xc80b2
movl $0x3, %edi
movq %rbx, %rdx
movq %r8, %rcx
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2fdad
movl 0x30(%rbx), %eax
testq %rbp, %rbp
sete %cl
orb %al, %cl
testb $0x1, %cl
je 0x687b1
testb $0x1, %al
jne 0x6883a
testq %rbp, %rbp
je 0x688de
movl 0x30(%rbp), %eax
jmp 0x687c4
movl 0x30(%rbp), %eax
testb $0x8, %al
jne 0x687e7
movb 0x89d(%r14), %cl
andb $0x2, %cl
jne 0x687e7
testb $0x8, %al
jne 0x687d8
addq $0x8, %rbx
leaq 0x5f985(%rip), %rsi     # 0xc8158
jmp 0x688e9
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2ffce
movq 0x850(%rbx), %rcx
subq 0x870(%rcx), %rax
movq 0x9a0(%r13), %r12
addq %r12, %r12
cmpq %r12, %rax
jge 0x6884a
leaq 0x8(%rbx), %r14
callq 0x2ffce
movq 0x850(%rbx), %rcx
subq %rax, %r12
addq 0x870(%rcx), %r12
leaq 0x5f95e(%rip), %rsi     # 0xc8188
movl $0x3, %edi
movq %r14, %rdx
movq %r12, %rcx
jmp 0x68776
addq $0x8, %rbx
leaq 0x5f8a9(%rip), %rsi     # 0xc80ee
jmp 0x688e9
movq 0x9b0(%r13), %rax
xorl %ecx, %ecx
movl %ecx, %edx
shrl $0x3, %edx
movzbl 0x50(%r14,%rdx), %edx
movl %ecx, %esi
andl $0x7, %esi
btl %esi, %edx
jae 0x68882
movq 0x40028(%rax,%rcx,8), %rdx
testq %rdx, %rdx
je 0x68882
movq 0x38(%rdx), %r8
cmpq %r15, %r8
ja 0x68906
incq %rcx
cmpq $0x4000, %rcx           # imm = 0x4000
jne 0x68853
movq %rbx, %rdi
callq 0x69008
movq 0x9b0(%r13), %rax
movq 0x8(%rax), %rcx
movq %rcx, 0x60078(%rax)
callq 0x2ffce
movq 0x850(%rbx), %rcx
movq %rax, 0x870(%rcx)
addq $0x8, %rbx
movq 0x9b0(%r13), %rax
movq 0x8(%rax), %rcx
leaq 0x5f952(%rip), %rsi     # 0xc8223
movl $0x3, %edi
movq %rbx, %rdx
jmp 0x68776
addq $0x8, %rbx
leaq 0x5f838(%rip), %rsi     # 0xc8121
movl $0x3, %edi
movq %rbx, %rdx
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2fdad
addq $0x8, %rbx
leaq 0x5f8cc(%rip), %rsi     # 0xc81dd
movl $0x3, %edi
movq %rbx, %rdx
movq %r15, %r9
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2fdad