// File: /workspace/github_c_makefile_star2_O2/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 0xaf676(%rip), %rbx     # 0x10e230
movq 0x850(%rbx), %rdx
leaq 0x9ace0(%rip), %rcx     # 0xf98a8
movq 0x9b0(%rcx), %rax
cmpl $0x0, 0x10(%rax)
setne %sil
testq %rdx, %rdx
sete %dil
orb %sil, %dil
jne 0x5ed6f
movl 0x844(%rdx), %esi
xorl %edi, %edi
testl %esi, %esi
cmovlel %edi, %esi
xorl %r8d, %r8d
cmpq %rdi, %rsi
je 0x5ec1c
movq 0x848(%rdx), %r9
movq (%r9,%rdi,8), %r9
movb 0x30(%r9), %r9b
andb $0xc, %r9b
cmpb $0x1, %r9b
adcl $0x0, %r8d
incq %rdi
jmp 0x5ebf7
cmpl 0x9b8(%rcx), %r8d
jle 0x5ed6f
movq 0x18(%rax), %rdi
callq 0x2a9c4
movq %rax, %r15
movq $0x0, 0x8(%rsp)
movq %r15, %rdi
callq 0x2a9d3
testq %rax, %rax
je 0x5ed0d
movq 0x8(%rax), %r13
movl 0x30(%r13), %r14d
xorl %ebp, %ebp
testb $0x1, %r14b
je 0x5ec6d
movq %r13, %rdi
callq 0x5c1f4
movl %eax, %ebp
testl %eax, %eax
jg 0x5ecb3
andl $0x10a, %r14d           # imm = 0x10A
cmpl $0x100, %r14d           # imm = 0x100
jne 0x5ecb3
cmpq $0x0, 0x8(%rsp)
jne 0x5ec9b
cmpl $0x0, 0x840(%r13)
movl $0x0, %eax
cmovgq %r13, %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x880(%r13)
jne 0x5ecbb
callq 0x2b8ae
movq %rax, 0x880(%r13)
jmp 0x5ecbb
andq $0x0, 0x880(%r13)
cmpl 0x14(%rsp), %ebp
jne 0x5ec3e
movl 0x844(%r13), %ebp
testl %ebp, %ebp
movl $0x0, %eax
cmovlel %eax, %ebp
xorl %r14d, %r14d
cmpq %r14, %rbp
je 0x5ec3e
movq 0x848(%r13), %rax
movq (%rax,%r14,8), %r12
leaq 0x8(%r12), %rdi
leaq 0x8(%rbx), %rsi
pushq $0x28
popq %rdx
callq 0x269b0
testl %eax, %eax
js 0x5ed05
movq %rbx, %r12
incq %r14
movq %r12, %rbx
jmp 0x5ecd9
movq %r15, %rdi
callq 0x2aa94
cmpq $0x0, 0x8(%rsp)
je 0x5ed6f
cmpq 0xaf50c(%rip), %rbx     # 0x10e230
jne 0x5ed6f
callq 0x2b8ae
movq 0x8(%rsp), %rcx
subq 0x880(%rcx), %rax
cmpq $0x1389, %rax           # imm = 0x1389
jl 0x5ed6f
movq 0x8(%rsp), %rbx
leaq 0x8(%rbx), %rdx
leaq 0x5a73f(%rip), %rsi     # 0xb948e
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5ce6d
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq