// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/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 != REDIS_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
     * smaller node ID.
     *
     * Note that this means that eventually a replica migration will occurr
     * since slaves that are reachable again always have their FAIL flag
     * cleared. 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 extremely unlikely to happen, and harmless. */
    candidate = myself;
    di = dictGetSafeIterator(server.cluster->nodes);
    while((de = dictNext(di)) != NULL) {
        clusterNode *node = dictGetVal(de);
        int okslaves;

        /* Only iterate over working masters. */
        if (nodeIsSlave(node) || nodeFailed(node)) continue;
        okslaves = clusterCountNonFailingSlaves(node);

        if (okslaves == 0 && target == NULL && node->numslots > 0)
            target = node;

        if (okslaves == max_slaves) {
            for (j = 0; j < node->numslaves; j++) {
                if (memcmp(node->slaves[j]->name,
                           candidate->name,
                           REDIS_CLUSTER_NAMELEN) < 0)
                {
                    candidate = node->slaves[j];
                }
            }
        }
    }

    /* Step 4: perform the migration if there is a target, and if I'm the
     * candidate. */
    if (target && candidate == myself) {
        redisLog(REDIS_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, %ebp
movq 0x8c3ac(%rip), %rbx     # 0xdb628
movq 0x850(%rbx), %rdx
leaq 0x77d1e(%rip), %rcx     # 0xc6fa8
movq 0x7d0(%rcx), %rax
cmpl $0x0, 0x10(%rax)
setne %sil
testq %rdx, %rdx
sete %dil
orb %sil, %dil
jne 0x4f3cf
movl 0x844(%rdx), %esi
xorl %edi, %edi
testl %esi, %esi
cmovlel %edi, %esi
xorl %r8d, %r8d
cmpq %rdi, %rsi
je 0x4f2de
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 0x4f2b9
cmpl 0x7d8(%rcx), %r8d
jle 0x4f3cf
movq 0x18(%rax), %rdi
callq 0x21f20
movq %rax, %r15
xorl %r12d, %r12d
xorl %r14d, %r14d
movq %r15, %rdi
callq 0x21f2f
testq %rax, %rax
je 0x4f396
movq 0x8(%rax), %r13
testb $0xa, 0x30(%r13)
jne 0x4f2fd
movq %r13, %rdi
callq 0x4cd6e
testl %eax, %eax
jne 0x4f33c
testq %r14, %r14
jne 0x4f33c
cmpl $0x0, 0x840(%r13)
movl $0x0, %r14d
cmovgq %r13, %r14
cmpl %ebp, %eax
jne 0x4f2fd
movl %ebp, 0xc(%rsp)
movl 0x844(%r13), %eax
testl %eax, %eax
cmovlel %r12d, %eax
movq %rax, 0x10(%rsp)
xorl %r12d, %r12d
cmpq %r12, 0x10(%rsp)
je 0x4f38a
movq 0x848(%r13), %rax
movq (%rax,%r12,8), %rbp
leaq 0x8(%rbp), %rdi
leaq 0x8(%rbx), %rsi
pushq $0x28
popq %rdx
callq 0x1f940
testl %eax, %eax
js 0x4f382
movq %rbx, %rbp
incq %r12
movq %rbp, %rbx
jmp 0x4f359
movl 0xc(%rsp), %ebp
xorl %r12d, %r12d
jmp 0x4f2fd
testq %r14, %r14
je 0x4f3cf
cmpq 0x8c286(%rip), %rbx     # 0xdb628
jne 0x4f3cf
leaq 0x8(%r14), %rdx
leaq 0x432cc(%rip), %rsi     # 0x9267b
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
movq %r14, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4d879
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq