// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/src/sentinel.c
// Function: sentinelSelectSlave
sentinelRedisInstance *sentinelSelectSlave(sentinelRedisInstance *master) {
    sentinelRedisInstance **instance =
        zmalloc(sizeof(instance[0])*dictSize(master->slaves));
    sentinelRedisInstance *selected = NULL;
    int instances = 0;
    dictIterator *di;
    dictEntry *de;
    mstime_t max_master_down_time = 0;

    if (master->flags & SRI_S_DOWN)
        max_master_down_time += mstime() - master->s_down_since_time;
    max_master_down_time += master->down_after_period * 10;

    di = dictGetIterator(master->slaves);
    while((de = dictNext(di)) != NULL) {
        sentinelRedisInstance *slave = dictGetVal(de);
        mstime_t info_validity_time;

        if (slave->flags & (SRI_S_DOWN|SRI_O_DOWN)) continue;
        if (slave->link->disconnected) continue;
        if (mstime() - slave->link->last_avail_time > SENTINEL_PING_PERIOD*5) continue;
        if (slave->slave_priority == 0) continue;

        /* If the master is in SDOWN state we get INFO for slaves every second.
         * Otherwise we get it with the usual period so we need to account for
         * a larger delay. */
        if (master->flags & SRI_S_DOWN)
            info_validity_time = SENTINEL_PING_PERIOD*5;
        else
            info_validity_time = SENTINEL_INFO_PERIOD*3;
        if (mstime() - slave->info_refresh > info_validity_time) continue;
        if (slave->master_link_down_time > max_master_down_time) continue;
        instance[instances++] = slave;
    }
    dictReleaseIterator(di);
    if (instances) {
        qsort(instance,instances,sizeof(sentinelRedisInstance*),
            compareSlavesForPromotion);
        selected = instance[0];
    }
    zfree(instance);
    return selected;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r14
movq 0x88(%rdi), %rax
movq 0x48(%rax), %rdi
addq 0x28(%rax), %rdi
shlq $0x3, %rdi
callq 0x327b9
movq %rax, %rbx
xorl %r13d, %r13d
testb $0x8, (%r14)
jne 0x6e4c4
xorl %eax, %eax
jmp 0x6e4cd
callq 0x2b8ae
subq 0x48(%r14), %rax
movq %rbx, 0x8(%rsp)
imulq $0xa, 0x58(%r14), %rcx
addq %rax, %rcx
movq %rcx, 0x10(%rsp)
movq 0x88(%r14), %rdi
callq 0x2a99f
movq %rax, %r15
movq %r15, %rdi
callq 0x2a9d3
movl %r13d, %r12d
testq %rax, %rax
je 0x6e56b
movq 0x8(%rax), %rbx
testb $0x18, (%rbx)
jne 0x6e4ee
movq 0x28(%rbx), %rax
cmpl $0x0, 0x4(%rax)
jne 0x6e4ee
callq 0x2b8ae
movq 0x28(%rbx), %rcx
subq 0x38(%rcx), %rax
cmpq $0x1388, %rax           # imm = 0x1388
jg 0x6e4ee
cmpl $0x0, 0xa8(%rbx)
je 0x6e4ee
testb $0x8, (%r14)
movl $0x1388, %ebp           # imm = 0x1388
movl $0x7530, %eax           # imm = 0x7530
cmoveq %rax, %rbp
callq 0x2b8ae
subq 0x60(%rbx), %rax
cmpq %rbp, %rax
jg 0x6e4ee
movq 0x10(%rsp), %rax
cmpq %rax, 0xa0(%rbx)
jg 0x6e4ee
incl %r13d
movq 0x8(%rsp), %rax
movq %rbx, (%rax,%r12,8)
jmp 0x6e4ee
movq %r15, %rdi
callq 0x2aa94
testl %r13d, %r13d
je 0x6e597
leaq 0xadd(%rip), %rcx       # 0x6f05c
pushq $0x8
popq %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x26890
movq (%rbx), %r14
jmp 0x6e59f
xorl %r14d, %r14d
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x328f9
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq