// File: /workspace/github_c_makefile_star2_O3/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
pushq %rax
movq %rdi, %r14
movq 0x88(%rdi), %rax
movq 0x48(%rax), %rdi
addq 0x28(%rax), %rdi
shlq $0x3, %rdi
callq 0x382d1
movq %rax, %rbx
testb $0x8, (%r14)
jne 0x7adc8
xorl %r12d, %r12d
jmp 0x7add4
callq 0x2ffce
movq %rax, %r12
subq 0x48(%r14), %r12
movq 0x58(%r14), %r13
movq 0x88(%r14), %rdi
callq 0x2efbb
movq %rax, %r15
movq %rax, %rdi
callq 0x2f015
testq %rax, %rax
je 0x7aead
leaq (,%r13,4), %rcx
addq %r13, %rcx
leaq (%r12,%rcx,2), %rcx
movq %rcx, (%rsp)
xorl %ebp, %ebp
movq 0x8(%rax), %r13
testb $0x18, (%r13)
jne 0x7ae78
movq 0x28(%r13), %rax
cmpl $0x0, 0x4(%rax)
jne 0x7ae78
callq 0x2ffce
movq 0x28(%r13), %rcx
subq 0x38(%rcx), %rax
cmpq $0x1388, %rax           # imm = 0x1388
jg 0x7ae78
cmpl $0x0, 0xa8(%r13)
je 0x7ae78
testb $0x8, (%r14)
movl $0x1388, %r12d          # imm = 0x1388
movl $0x7530, %eax           # imm = 0x7530
cmoveq %rax, %r12
callq 0x2ffce
subq 0x60(%r13), %rax
cmpq %r12, %rax
jg 0x7ae78
movq (%rsp), %rax
cmpq %rax, 0xa0(%r13)
jg 0x7ae78
movslq %ebp, %rax
incl %ebp
movq %r13, (%rbx,%rax,8)
movq %r15, %rdi
callq 0x2f015
testq %rax, %rax
jne 0x7ae0d
movq %r15, %rdi
callq 0x2f0dd
testl %ebp, %ebp
je 0x7aeb5
movslq %ebp, %rsi
leaq 0xbba(%rip), %rcx       # 0x7ba55
movl $0x8, %edx
movq %rbx, %rdi
callq 0x26890
movq (%rbx), %r14
jmp 0x7aeb8
movq %r15, %rdi
callq 0x2f0dd
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0x38413
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq