// File: /workspace/github_c_makefile_star2_O3/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 0x8b0a9(%rip), %r14     # 0xde628
movq 0x850(%r14), %rsi
leaq 0x76a1b(%rip), %rcx     # 0xc9fa8
movq 0x7d0(%rcx), %rax
cmpl $0x0, 0x10(%rax)
setne %dl
testq %rsi, %rsi
sete %dil
orb %dl, %dil
jne 0x5370a
movslq 0x844(%rsi), %rdx
testq %rdx, %rdx
jle 0x535e0
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 0x535c3
jmp 0x535e2
xorl %esi, %esi
cmpl 0x7d8(%rcx), %esi
jle 0x5370a
movq 0x18(%rax), %rdi
callq 0x22aa0
movq %rax, %r15
movq %rax, %rdi
callq 0x22acf
testq %rax, %rax
je 0x5370a
xorl %r12d, %r12d
movl %ebx, 0xc(%rsp)
movq 0x8(%rax), %rax
testb $0xa, 0x30(%rax)
jne 0x536bd
movslq 0x844(%rax), %r13
testq %r13, %r13
jle 0x53654
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 0x53637
jmp 0x53656
xorl %ecx, %ecx
testl %ecx, %ecx
jne 0x53670
testq %r12, %r12
jne 0x53670
cmpl $0x0, 0x840(%rax)
movl $0x0, %r12d
cmovgq %rax, %r12
cmpl %ebx, %ecx
jne 0x536bd
testl %r13d, %r13d
jle 0x536bd
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 0x536a6
movq %r14, %r12
incq %rbx
movq %r12, %r14
cmpq %rbx, %r13
jne 0x53687
movq %r12, %r14
movl 0xc(%rsp), %ebx
movq 0x10(%rsp), %r12
movq %r15, %rdi
callq 0x22acf
testq %rax, %rax
jne 0x53612
testq %r12, %r12
je 0x5370a
cmpq 0x8af4e(%rip), %r14     # 0xde628
jne 0x5370a
leaq 0x8(%r12), %rdx
leaq 0x48fd3(%rip), %rsi     # 0x9c6bb
movl $0x3, %edi
xorl %eax, %eax
callq 0x23223
movq %r12, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x51aac
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq