// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movl %edi, -0x4(%rbp)
movl $0x0, -0xc(%rbp)
movq 0xba04f(%rip), %rax     # 0x127f78
movq 0x850(%rax), %rax
movq %rax, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
leaq 0xa59ad(%rip), %rax     # 0x1138f8
movq 0x7d0(%rax), %rax
cmpl $0x0, 0x10(%rax)
je 0x6df5d
jmp 0x6e12e
cmpq $0x0, -0x18(%rbp)
jne 0x6df69
jmp 0x6e12e
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x844(%rcx), %eax
jge 0x6dfd1
movq -0x18(%rbp), %rax
movq 0x848(%rax), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x30(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
jne 0x6dfc4
movq -0x18(%rbp), %rax
movq 0x848(%rax), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl 0x30(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
jne 0x6dfc4
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x6dfc6
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x6df70
movl -0xc(%rbp), %eax
leaq 0xa591d(%rip), %rcx     # 0x1138f8
cmpl 0x7d8(%rcx), %eax
jg 0x6dfe8
jmp 0x6e12e
movq 0xb9f89(%rip), %rax     # 0x127f78
movq %rax, -0x28(%rbp)
leaq 0xa58fe(%rip), %rax     # 0x1138f8
movq 0x7d0(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x24240
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x24270
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
je 0x6e0f6
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl 0x30(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x6e04f
movq -0x40(%rbp), %rax
movl 0x30(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x6e051
jmp 0x6e00e
movq -0x40(%rbp), %rdi
callq 0x6a2f0
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jne 0x6e07f
cmpq $0x0, -0x20(%rbp)
jne 0x6e07f
movq -0x40(%rbp), %rax
cmpl $0x0, 0x840(%rax)
jle 0x6e07f
movq -0x40(%rbp), %rax
movq %rax, -0x20(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x4(%rbp), %eax
jne 0x6e0f1
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
movq -0x40(%rbp), %rcx
cmpl 0x844(%rcx), %eax
jge 0x6e0ef
movq -0x40(%rbp), %rax
movq 0x848(%rax), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
addq $0x8, %rdi
movq -0x28(%rbp), %rsi
addq $0x8, %rsi
movl $0x28, %edx
callq 0x1f950
cmpl $0x0, %eax
jge 0x6e0e2
movq -0x40(%rbp), %rax
movq 0x848(%rax), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
jmp 0x6e0e4
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x6e08e
jmp 0x6e0f1
jmp 0x6e00e
cmpq $0x0, -0x20(%rbp)
je 0x6e12e
movq -0x28(%rbp), %rax
cmpq 0xb9e70(%rip), %rax     # 0x127f78
jne 0x6e12e
movq -0x20(%rbp), %rdx
addq $0x8, %rdx
movl $0x3, %edi
leaq 0x78585(%rip), %rsi     # 0xe66a3
movb $0x0, %al
callq 0x24fb0
movq -0x20(%rbp), %rdi
callq 0x6b360
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)