// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movl %edi, -0x4(%rbp)
movl $0x0, -0xc(%rbp)
movq 0x10a527(%rip), %rax    # 0x18cb60
movq 0x850(%rax), %rax
movq %rax, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
leaq 0xf5b7d(%rip), %rax     # 0x1781d8
movq 0x9b0(%rax), %rax
cmpl $0x0, 0x10(%rax)
je 0x8266d
jmp 0x828d8
cmpq $0x0, -0x18(%rbp)
jne 0x82679
jmp 0x828d8
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x844(%rcx), %eax
jge 0x826e1
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 0x826d4
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 0x826d4
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x826d6
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x82680
movl -0xc(%rbp), %eax
leaq 0xf5aed(%rip), %rcx     # 0x1781d8
cmpl 0x9b8(%rcx), %eax
jg 0x826f8
jmp 0x828d8
movq 0x10a461(%rip), %rax    # 0x18cb60
movq %rax, -0x28(%rbp)
leaq 0xf5ace(%rip), %rax     # 0x1781d8
movq 0x9b0(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x2e470
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x2e4a0
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
je 0x8287f
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x1, -0x48(%rbp)
movq -0x40(%rbp), %rax
movl 0x30(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x8276d
movq -0x40(%rbp), %rax
movl 0x30(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x82774
movl $0x0, -0x48(%rbp)
movq -0x40(%rbp), %rax
movl 0x30(%rax), %eax
andl $0x100, %eax            # imm = 0x100
cmpl $0x0, %eax
jne 0x8278c
movl $0x0, -0x48(%rbp)
movq -0x40(%rbp), %rax
movl 0x30(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x827a7
movq -0x40(%rbp), %rdi
callq 0x7e340
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jle 0x827b4
movl $0x0, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0x827f9
cmpq $0x0, -0x20(%rbp)
jne 0x827d6
movq -0x40(%rbp), %rax
cmpl $0x0, 0x840(%rax)
jle 0x827d6
movq -0x40(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x40(%rbp), %rax
cmpq $0x0, 0x880(%rax)
jne 0x827f7
callq 0x2ff70
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x880(%rax)
jmp 0x82808
movq -0x40(%rbp), %rax
movq $0x0, 0x880(%rax)
movl -0x44(%rbp), %eax
cmpl -0x4(%rbp), %eax
jne 0x8287a
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
movq -0x40(%rbp), %rcx
cmpl 0x844(%rcx), %eax
jge 0x82878
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 0x269b0
cmpl $0x0, %eax
jge 0x8286b
movq -0x40(%rbp), %rax
movq 0x848(%rax), %rax
movslq -0x8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
jmp 0x8286d
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x82817
jmp 0x8287a
jmp 0x8271e
movq -0x30(%rbp), %rdi
callq 0x2e600
cmpq $0x0, -0x20(%rbp)
je 0x828d8
movq -0x28(%rbp), %rax
cmpq 0x10a2c6(%rip), %rax    # 0x18cb60
jne 0x828d8
callq 0x2ff70
movq -0x20(%rbp), %rcx
subq 0x880(%rcx), %rax
cmpq $0x1388, %rax           # imm = 0x1388
jle 0x828d8
movq -0x20(%rbp), %rdx
addq $0x8, %rdx
movl $0x3, %edi
leaq 0xbfc5f(%rip), %rsi     # 0x142527
movb $0x0, %al
callq 0x2fc60
movq -0x20(%rbp), %rdi
callq 0x7f5c0
addq $0x50, %rsp
popq %rbp
retq
nop