// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/redis.c
// Function: evictionPoolPopulate
void evictionPoolPopulate(dict *sampledict, dict *keydict, struct evictionPoolEntry *pool) {
    int j, k, count;
    dictEntry *_samples[EVICTION_SAMPLES_ARRAY_SIZE];
    dictEntry **samples;

    /* Try to use a static buffer: this function is a big hit...
     * Note: it was actually measured that this helps. */
    if (server.maxmemory_samples <= EVICTION_SAMPLES_ARRAY_SIZE) {
        samples = _samples;
    } else {
        samples = zmalloc(sizeof(samples[0])*server.maxmemory_samples);
    }

#if 1 /* Use bulk get by default. */
    count = dictGetRandomKeys(sampledict,samples,server.maxmemory_samples);
#else
    count = server.maxmemory_samples;
    for (j = 0; j < count; j++) samples[j] = dictGetRandomKey(sampledict);
#endif

    for (j = 0; j < count; j++) {
        unsigned long long idle;
        sds key;
        robj *o;
        dictEntry *de;

        de = samples[j];
        key = dictGetKey(de);
        /* If the dictionary we are sampling from is not the main
         * dictionary (but the expires one) we need to lookup the key
         * again in the key dictionary to obtain the value object. */
        if (sampledict != keydict) de = dictFind(keydict, key);
        o = dictGetVal(de);
        idle = estimateObjectIdleTime(o);

        /* Insert the element inside the pool.
         * First, find the first empty bucket or the first populated
         * bucket that has an idle time smaller than our idle time. */
        k = 0;
        while (k < REDIS_EVICTION_POOL_SIZE &&
               pool[k].key &&
               pool[k].idle < idle) k++;
        if (k == 0 && pool[REDIS_EVICTION_POOL_SIZE-1].key != NULL) {
            /* Can't insert if the element is < the worst element we have
             * and there are no empty buckets. */
            continue;
        } else if (k < REDIS_EVICTION_POOL_SIZE && pool[k].key == NULL) {
            /* Inserting into empty position. No setup needed before insert. */
        } else {
            /* Inserting in the middle. Now k points to the first element
             * greater than the element to insert.  */
            if (pool[REDIS_EVICTION_POOL_SIZE-1].key == NULL) {
                /* Free space on the right? Insert at k shifting
                 * all the elements from k to end to the right. */
                memmove(pool+k+1,pool+k,
                    sizeof(pool[0])*(REDIS_EVICTION_POOL_SIZE-k-1));
            } else {
                /* No free space on right? Insert at k-1 */
                k--;
                /* Shift all elements on the left of k (included) to the
                 * left, so we discard the element with smaller idle time. */
                sdsfree(pool[0].key);
                memmove(pool,pool+1,sizeof(pool[0])*k);
            }
        }
        pool[k].key = sdsdup(key);
        pool[k].idle = idle;
    }
    if (samples != _samples) zfree(samples);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %rbp
movslq 0xa3077(%rip), %rdi     # 0xca6d4
cmpq $0x11, %rdi
jge 0x2766a
leaq 0x30(%rsp), %r14
jmp 0x27676
shlq $0x3, %rdi
callq 0x29709
movq %rax, %r14
movl 0xa3058(%rip), %edx     # 0xca6d4
movq %rbp, %rdi
movq %r14, %rsi
callq 0x22ada
testl %eax, %eax
jle 0x277bf
leaq 0x10(%rbx), %rcx
movq %rcx, 0x8(%rsp)
movl %eax, %eax
movq %rax, 0x18(%rsp)
xorl %r12d, %r12d
movq %rbp, 0x10(%rsp)
movq %r14, 0x20(%rsp)
movq (%r14,%r12,8), %rax
movq (%rax), %r13
cmpq %r15, %rbp
je 0x276c3
movq %r15, %rdi
movq %r13, %rsi
callq 0x224dc
movq %r13, 0x28(%rsp)
movq 0x8(%rax), %rdi
callq 0x31127
movq %rax, %r13
cmpq $0x0, 0x8(%rbx)
je 0x27701
movb $0x1, %al
movq %rbx, %rcx
xorl %r14d, %r14d
cmpq %r13, (%rcx)
jae 0x2770e
cmpq $0xf, %r14
setb %al
je 0x27708
incq %r14
cmpq $0x0, 0x18(%rcx)
leaq 0x10(%rcx), %rcx
jne 0x276e3
jmp 0x2770e
movb $0x1, %al
xorl %r14d, %r14d
jmp 0x2770e
movl $0x10, %r14d
testl %r14d, %r14d
je 0x2775f
testb $0x1, %al
je 0x27726
movl %r14d, %eax
shlq $0x4, %rax
cmpq $0x0, 0x8(%rbx,%rax)
je 0x27792
cmpq $0x0, 0xf8(%rbx)
je 0x2776b
movq %r15, %rbp
movslq %r14d, %r15
decl %r14d
shlq $0x4, %r15
movq 0x8(%rbx), %rdi
callq 0x28204
addq $-0x10, %r15
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movq %r15, %rdx
movq %rbp, %r15
movq 0x10(%rsp), %rbp
jmp 0x2778d
cmpq $0x0, 0xf8(%rbx)
jne 0x277ac
jmp 0x27713
movl %r14d, %eax
shlq $0x4, %rax
leaq (%rbx,%rax), %rsi
leaq (%rbx,%rax), %rdi
addq $0x10, %rdi
movl $0xf, %eax
subl %r14d, %eax
movslq %eax, %rdx
shlq $0x4, %rdx
callq 0x1f780
movq 0x28(%rsp), %rdi
callq 0x281fb
movslq %r14d, %rcx
shlq $0x4, %rcx
movq %rax, 0x8(%rbx,%rcx)
movq %r13, (%rbx,%rcx)
incq %r12
cmpq 0x18(%rsp), %r12
movq 0x20(%rsp), %r14
jne 0x276ac
leaq 0x30(%rsp), %rax
cmpq %rax, %r14
je 0x277d1
movq %r14, %rdi
callq 0x2984b
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq