// File: /workspace/github_c_makefile_star2_O1/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, %ebx
movq 0x8aae8(%rip), %r14     # 0xde628
movq 0x850(%r14), %rsi
leaq 0x7645a(%rip), %rcx     # 0xc9fa8
movq 0x7d0(%rcx), %rax
cmpl $0x0, 0x10(%rax)
setne %dl
testq %rsi, %rsi
sete %dil
orb %dl, %dil
jne 0x53ccb
movslq 0x844(%rsi), %rdx
testq %rdx, %rdx
jle 0x53ba1
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 0x53b84
jmp 0x53ba3
xorl %esi, %esi
cmpl 0x7d8(%rcx), %esi
jle 0x53ccb
movq 0x18(%rax), %rdi
callq 0x228e4
movq %rax, %r15
movq %rax, %rdi
callq 0x22913
xorl %r12d, %r12d
testq %rax, %rax
je 0x53c8f
movl %ebx, 0xc(%rsp)
movq 0x8(%rax), %rax
testb $0xa, 0x30(%rax)
jne 0x53c7e
movslq 0x844(%rax), %r13
testq %r13, %r13
jle 0x53c15
movq 0x848(%rax), %rdx
xorl %esi, %esi
xorl %ecx, %ecx
movq (%rdx,%rsi,8), %rdi
movb 0x30(%rdi), %dil
andb $0x8, %dil
cmpb $0x1, %dil
adcl $0x0, %ecx
incq %rsi
cmpq %rsi, %r13
jne 0x53bf8
jmp 0x53c17
xorl %ecx, %ecx
testl %ecx, %ecx
jne 0x53c31
testq %r12, %r12
jne 0x53c31
cmpl $0x0, 0x840(%rax)
movl $0x0, %r12d
cmovgq %rax, %r12
cmpl %ebx, %ecx
jne 0x53c7e
testl %r13d, %r13d
jle 0x53c7e
movq %r12, 0x10(%rsp)
movq 0x848(%rax), %rbp
xorl %ebx, %ebx
movq (%rbp,%rbx,8), %r12
leaq 0x8(%r12), %rdi
leaq 0x8(%r14), %rsi
movl $0x28, %edx
callq 0x1f940
testl %eax, %eax
js 0x53c67
movq %r14, %r12
incq %rbx
movq %r12, %r14
cmpq %rbx, %r13
jne 0x53c48
movq %r12, %r14
movl 0xc(%rsp), %ebx
movq 0x10(%rsp), %r12
movq %r15, %rdi
callq 0x22913
testq %rax, %rax
jne 0x53bd3
testq %r12, %r12
je 0x53ccb
cmpq 0x8a98d(%rip), %r14     # 0xde628
jne 0x53ccb
leaq 0x8(%r12), %rdx
leaq 0x489d2(%rip), %rsi     # 0x9c67b
movl $0x3, %edi
xorl %eax, %eax
callq 0x23064
movq %r12, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5202e
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq