// File: /workspace/github_c_makefile_star2_O3/gplessis[P]dotdeb-redis/src/cluster.c
// Function: clusterHandleSlaveMigration
void clusterHandleSlaveMigration(int max_slaves) {
    int j, okslaves = 0;
    clusterNode *mymaster = myself->slaveof, *target = NULL, *candidate = NULL;
    dictIterator *di;
    dictEntry *de;

    /* Step 1: Don't migrate if the cluster state is not ok. */
    if (server.cluster->state != CLUSTER_OK) return;

    /* Step 2: Don't migrate if my master will not be left with at least
     *         'migration-barrier' slaves after my migration. */
    if (mymaster == NULL) return;
    for (j = 0; j < mymaster->numslaves; j++)
        if (!nodeFailed(mymaster->slaves[j]) &&
            !nodeTimedOut(mymaster->slaves[j])) okslaves++;
    if (okslaves <= server.cluster_migration_barrier) return;

    /* Step 3: Idenitfy a candidate for migration, and check if among the
     * masters with the greatest number of ok slaves, I'm the one with the
     * smallest node ID (the "candidate slave").
     *
     * Note: this means that eventually a replica migration will occurr
     * since slaves that are reachable again always have their FAIL flag
     * cleared, so eventually there must be a candidate. At the same time
     * this does not mean that there are no race conditions possible (two
     * slaves migrating at the same time), but this is unlikely to
     * happen, and harmless when happens. */
    candidate = myself;
    di = dictGetSafeIterator(server.cluster->nodes);
    while((de = dictNext(di)) != NULL) {
        clusterNode *node = dictGetVal(de);
        int okslaves = 0, is_orphaned = 1;

        /* We want to migrate only if this master is working, orphaned, and
         * used to have slaves or if failed over a master that had slaves
         * (MIGRATE_TO flag). This way we only migrate to instances that were
         * supposed to have replicas. */
        if (nodeIsSlave(node) || nodeFailed(node)) is_orphaned = 0;
        if (!(node->flags & CLUSTER_NODE_MIGRATE_TO)) is_orphaned = 0;

        /* Check number of working slaves. */
        if (nodeIsMaster(node)) okslaves = clusterCountNonFailingSlaves(node);
        if (okslaves > 0) is_orphaned = 0;

        if (is_orphaned) {
            if (!target && node->numslots > 0) target = node;

            /* Track the starting time of the orphaned condition for this
             * master. */
            if (!node->orphaned_time) node->orphaned_time = mstime();
        } else {
            node->orphaned_time = 0;
        }

        /* Check if I'm the slave candidate for the migration: attached
         * to a master with the maximum number of slaves and with the smallest
         * node ID. */
        if (okslaves == max_slaves) {
            for (j = 0; j < node->numslaves; j++) {
                if (memcmp(node->slaves[j]->name,
                           candidate->name,
                           CLUSTER_NAMELEN) < 0)
                {
                    candidate = node->slaves[j];
                }
            }
        }
    }
    dictReleaseIterator(di);

    /* Step 4: perform the migration if there is a target, and if I'm the
     * candidate, but only if the master is continuously orphaned for a
     * couple of seconds, so that during failovers, we give some time to
     * the natural slaves of this instance to advertise their switch from
     * the old master to the new one. */
    if (target && candidate == myself &&
        (mstime()-target->orphaned_time) > CLUSTER_SLAVE_MIGRATION_DELAY)
    {
        serverLog(LL_WARNING,"Migrating to orphaned master %.40s",
            target->name);
        clusterSetMaster(target);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edi, 0x14(%rsp)
movq 0xac9ee(%rip), %r13     # 0x116230
movq 0x850(%r13), %rsi
leaq 0x98058(%rip), %rcx     # 0x1018a8
movq 0x9b0(%rcx), %rax
cmpl $0x0, 0x10(%rax)
setne %dl
testq %rsi, %rsi
sete %dil
orb %dl, %dil
jne 0x69a37
movslq 0x844(%rsi), %rdx
testq %rdx, %rdx
jle 0x698a3
movq 0x848(%rsi), %rdi
xorl %r8d, %r8d
xorl %esi, %esi
movq (%rdi,%r8,8), %r9
movb 0x30(%r9), %r9b
andb $0xc, %r9b
cmpb $0x1, %r9b
adcl $0x0, %esi
incq %r8
cmpq %r8, %rdx
jne 0x69886
jmp 0x698a5
xorl %esi, %esi
cmpl 0x9b8(%rcx), %esi
jle 0x69a37
movq 0x18(%rax), %rdi
callq 0x2efe5
movq %rax, %r15
movq %rax, %rdi
callq 0x2f015
testq %rax, %rax
je 0x699ca
movq $0x0, 0x8(%rsp)
movq %r13, %r12
movq 0x8(%rax), %r13
movl 0x30(%r13), %eax
movl $0x0, %ebp
testb $0x1, %al
je 0x6991e
movslq 0x844(%r13), %rcx
testq %rcx, %rcx
jle 0x6991e
movq 0x848(%r13), %rdx
xorl %esi, %esi
xorl %ebp, %ebp
movq (%rdx,%rsi,8), %rdi
movl 0x30(%rdi), %edi
andl $0x8, %edi
cmpl $0x1, %edi
adcl $0x0, %ebp
incq %rsi
cmpq %rsi, %rcx
jne 0x69902
testl %ebp, %ebp
jg 0x69960
andl $0x10a, %eax            # imm = 0x10A
cmpl $0x100, %eax            # imm = 0x100
jne 0x69960
cmpq $0x0, 0x8(%rsp)
jne 0x69948
cmpl $0x0, 0x840(%r13)
movl $0x0, %eax
cmovgq %r13, %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x880(%r13)
jne 0x6996b
callq 0x2ffce
movq %rax, 0x880(%r13)
jmp 0x6996b
movq $0x0, 0x880(%r13)
cmpl 0x14(%rsp), %ebp
jne 0x699b1
movslq 0x844(%r13), %rbp
testq %rbp, %rbp
jle 0x699b1
movq 0x848(%r13), %r14
xorl %ebx, %ebx
movq (%r14,%rbx,8), %r13
leaq 0x8(%r13), %rdi
leaq 0x8(%r12), %rsi
movl $0x28, %edx
callq 0x269b0
testl %eax, %eax
js 0x699a4
movq %r12, %r13
incq %rbx
movq %r13, %r12
cmpq %rbx, %rbp
jne 0x69986
jmp 0x699b4
movq %r12, %r13
movq %r15, %rdi
callq 0x2f015
movq %r13, %r12
testq %rax, %rax
jne 0x698da
jmp 0x699d3
movq $0x0, 0x8(%rsp)
movq %r15, %rdi
callq 0x2f0dd
cmpq $0x0, 0x8(%rsp)
je 0x69a37
cmpq 0xac846(%rip), %r13     # 0x116230
jne 0x69a37
callq 0x2ffce
movq 0x8(%rsp), %rcx
subq 0x880(%rcx), %rax
cmpq $0x1389, %rax           # imm = 0x1389
jl 0x69a37
movq 0x8(%rsp), %rbx
leaq 0x8(%rbx), %rdx
leaq 0x5ea91(%rip), %rsi     # 0xc84a6
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6778b
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq