// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/src/server.c
// Function: freeMemoryIfNeeded
int freeMemoryIfNeeded(void) {
    size_t mem_used, mem_tofree, mem_freed;
    int slaves = listLength(server.slaves);
    mstime_t latency, eviction_latency;

    /* When clients are paused the dataset should be static not just from the
     * POV of clients not being able to write, but also from the POV of
     * expires and evictions of keys not being performed. */
    if (clientsArePaused()) return C_OK;

    /* Remove the size of slaves output buffers and AOF buffer from the
     * count of used memory. */
    mem_used = zmalloc_used_memory();
    if (slaves) {
        listIter li;
        listNode *ln;

        listRewind(server.slaves,&li);
        while((ln = listNext(&li))) {
            client *slave = listNodeValue(ln);
            unsigned long obuf_bytes = getClientOutputBufferMemoryUsage(slave);
            if (obuf_bytes > mem_used)
                mem_used = 0;
            else
                mem_used -= obuf_bytes;
        }
    }
    if (server.aof_state != AOF_OFF) {
        mem_used -= sdslen(server.aof_buf);
        mem_used -= aofRewriteBufferSize();
    }

    /* Check if we are over the memory limit. */
    if (mem_used <= server.maxmemory) return C_OK;

    if (server.maxmemory_policy == MAXMEMORY_NO_EVICTION)
        return C_ERR; /* We need to free memory, but policy forbids. */

    /* Compute how much memory we need to free. */
    mem_tofree = mem_used - server.maxmemory;
    mem_freed = 0;
    latencyStartMonitor(latency);
    while (mem_freed < mem_tofree) {
        int j, k, keys_freed = 0;

        for (j = 0; j < server.dbnum; j++) {
            long bestval = 0; /* just to prevent warning */
            sds bestkey = NULL;
            dictEntry *de;
            redisDb *db = server.db+j;
            dict *dict;

            if (server.maxmemory_policy == MAXMEMORY_ALLKEYS_LRU ||
                server.maxmemory_policy == MAXMEMORY_ALLKEYS_RANDOM)
            {
                dict = server.db[j].dict;
            } else {
                dict = server.db[j].expires;
            }
            if (dictSize(dict) == 0) continue;

            /* volatile-random and allkeys-random policy */
            if (server.maxmemory_policy == MAXMEMORY_ALLKEYS_RANDOM ||
                server.maxmemory_policy == MAXMEMORY_VOLATILE_RANDOM)
            {
                de = dictGetRandomKey(dict);
                bestkey = dictGetKey(de);
            }

            /* volatile-lru and allkeys-lru policy */
            else if (server.maxmemory_policy == MAXMEMORY_ALLKEYS_LRU ||
                server.maxmemory_policy == MAXMEMORY_VOLATILE_LRU)
            {
                struct evictionPoolEntry *pool = db->eviction_pool;

                while(bestkey == NULL) {
                    evictionPoolPopulate(dict, db->dict, db->eviction_pool);
                    /* Go backward from best to worst element to evict. */
                    for (k = MAXMEMORY_EVICTION_POOL_SIZE-1; k >= 0; k--) {
                        if (pool[k].key == NULL) continue;
                        de = dictFind(dict,pool[k].key);

                        /* Remove the entry from the pool. */
                        sdsfree(pool[k].key);
                        /* Shift all elements on its right to left. */
                        memmove(pool+k,pool+k+1,
                            sizeof(pool[0])*(MAXMEMORY_EVICTION_POOL_SIZE-k-1));
                        /* Clear the element on the right which is empty
                         * since we shifted one position to the left.  */
                        pool[MAXMEMORY_EVICTION_POOL_SIZE-1].key = NULL;
                        pool[MAXMEMORY_EVICTION_POOL_SIZE-1].idle = 0;

                        /* If the key exists, is our pick. Otherwise it is
                         * a ghost and we need to try the next element. */
                        if (de) {
                            bestkey = dictGetKey(de);
                            break;
                        } else {
                            /* Ghost... */
                            continue;
                        }
                    }
                }
            }

            /* volatile-ttl */
            else if (server.maxmemory_policy == MAXMEMORY_VOLATILE_TTL) {
                for (k = 0; k < server.maxmemory_samples; k++) {
                    sds thiskey;
                    long thisval;

                    de = dictGetRandomKey(dict);
                    thiskey = dictGetKey(de);
                    thisval = (long) dictGetVal(de);

                    /* Expire sooner (minor expire unix timestamp) is better
                     * candidate for deletion */
                    if (bestkey == NULL || thisval < bestval) {
                        bestkey = thiskey;
                        bestval = thisval;
                    }
                }
            }

            /* Finally remove the selected key. */
            if (bestkey) {
                long long delta;

                robj *keyobj = createStringObject(bestkey,sdslen(bestkey));
                propagateExpire(db,keyobj);
                /* We compute the amount of memory freed by dbDelete() alone.
                 * It is possible that actually the memory needed to propagate
                 * the DEL in AOF and replication link is greater than the one
                 * we are freeing removing the key, but we can't account for
                 * that otherwise we would never exit the loop.
                 *
                 * AOF and Output buffer memory will be freed eventually so
                 * we only care about memory used by the key space. */
                delta = (long long) zmalloc_used_memory();
                latencyStartMonitor(eviction_latency);
                dbDelete(db,keyobj);
                latencyEndMonitor(eviction_latency);
                latencyAddSampleIfNeeded("eviction-del",eviction_latency);
                latencyRemoveNestedEvent(latency,eviction_latency);
                delta -= (long long) zmalloc_used_memory();
                mem_freed += delta;
                server.stat_evictedkeys++;
                notifyKeyspaceEvent(NOTIFY_EVICTED, "evicted",
                    keyobj, db->id);
                decrRefCount(keyobj);
                keys_freed++;

                /* When the memory to free starts to be big enough, we may
                 * start spending so much time here that is impossible to
                 * deliver data to the slaves fast enough, so we force the
                 * transmission here inside the loop. */
                if (slaves) flushSlavesOutputBuffers();
            }
        }
        if (!keys_freed) {
            latencyEndMonitor(latency);
            latencyAddSampleIfNeeded("eviction-cycle",latency);
            return C_ERR; /* nothing to free... */
        }
    }
    latencyEndMonitor(latency);
    latencyAddSampleIfNeeded("eviction-cycle",latency);
    return C_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq 0xcb060(%rip), %rax     # 0xf9a88
movl 0x28(%rax), %ebx
callq 0x38320
movl $0x0, 0xc(%rsp)
testl %eax, %eax
jne 0x2ede3
callq 0x32960
movq %rbx, 0x48(%rsp)
testq %rbx, %rbx
je 0x2ea89
movq %rax, %r14
movq 0xcb02f(%rip), %rdi     # 0xf9a88
leaq 0x58(%rsp), %rbx
movq %rbx, %rsi
callq 0x2702c
movq %rbx, %rdi
callq 0x27046
testq %rax, %rax
je 0x2ea86
movq 0x10(%rax), %rdi
callq 0x3876d
subq %rax, %r14
jae 0x2ea66
xorl %r14d, %r14d
jmp 0x2ea66
movq %r14, %rax
cmpl $0x0, 0xcb480(%rip)     # 0xf9f10
je 0x2eab2
movq 0xcb4b7(%rip), %rdi     # 0xf9f50
movq %rax, %r14
callq 0x2b91b
movq %rax, %rbx
callq 0x537ca
addq %rbx, %rax
subq %rax, %r14
movq %r14, %rax
subq 0xcb6f7(%rip), %rax     # 0xfa1b0
jbe 0x2ede3
movq %rax, 0x28(%rsp)
pushq $-0x1
popq %rax
cmpl $0x5, 0xcb6ea(%rip)     # 0xfa1b8
movl %eax, 0xc(%rsp)
je 0x2ede3
cmpq $0x0, 0xcb7e0(%rip)     # 0xfa2c0
je 0x2eaec
callq 0x2b8ae
movq %rax, %r13
jmp 0x2eaef
xorl %r13d, %r13d
movq 0x28(%rsp), %rax
xorl %ebx, %ebx
cmpq %rax, %rbx
jae 0x2ed9f
xorl %ebp, %ebp
movl $0x0, 0x1c(%rsp)
movslq 0xcb3a8(%rip), %rax     # 0xf9eb8
cmpq %rax, %rbp
jge 0x2ed70
movq %rbp, %rsi
shlq $0x6, %rsi
addq 0xcada9(%rip), %rsi     # 0xf98d0
movl 0xcb68b(%rip), %eax     # 0xfa1b8
leal -0x3(%rax), %ecx
xorl %edx, %edx
cmpl $0x2, %ecx
setae %dl
movq %rsi, 0x10(%rsp)
movq (%rsi,%rdx,8), %rdx
movq 0x28(%rdx), %rcx
movq %rdx, 0x20(%rsp)
addq 0x48(%rdx), %rcx
je 0x2ed2a
cmpl $0x4, %eax
ja 0x2ed2a
movq %rbx, 0x38(%rsp)
movl %eax, %eax
leaq 0x7f855(%rip), %rcx     # 0xae3c0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq %r13, 0x40(%rsp)
movq %rbp, 0x30(%rsp)
jmpq *%rax
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rax
leaq 0xf0(%rax), %rbp
addq $0x100, %rax            # imm = 0x100
movq %rax, 0x50(%rsp)
xorl %r12d, %r12d
testq %r12, %r12
jne 0x2ec2f
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq 0x28(%rax), %rdx
movq 0x20(%rsp), %rdi
callq 0x3018f
movq 0x50(%rsp), %r13
xorl %r12d, %r12d
pushq $0xf
popq %rbx
testq %rbx, %rbx
js 0x2eb99
leaq -0x10(%r13), %r14
movq -0x8(%r13), %rsi
testq %rsi, %rsi
je 0x2ec08
movq 0x20(%rsp), %rdi
callq 0x2a5f9
movq %rax, %r15
movq -0x8(%r13), %rdi
callq 0x311c8
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x267e0
xorps %xmm0, %xmm0
movups %xmm0, (%rbp)
testq %r15, %r15
jne 0x2ec14
decq %rbx
addq $0x10, %r12
movq %r14, %r13
jmp 0x2ebc6
movq (%r15), %r12
jmp 0x2eb9c
movq 0x20(%rsp), %rdi
callq 0x2aaf6
movq (%rax), %r12
testq %r12, %r12
je 0x2ed1b
movq %r12, %rdi
callq 0x2b91b
movq %r12, %rdi
movq %rax, %rsi
callq 0x39e29
movq %rax, %r12
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x3c34a
callq 0x32960
movq %rax, %rbx
cmpq $0x0, 0xcb65e(%rip)     # 0xfa2c0
je 0x2ec6e
callq 0x2b8ae
movq %rax, %r14
jmp 0x2ec71
xorl %r14d, %r14d
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x3b1df
cmpq $0x0, 0xcb63a(%rip)     # 0xfa2c0
movq 0x40(%rsp), %r15
je 0x2ecc5
callq 0x2b8ae
movq %rax, %r13
subq %r14, %r13
movq 0xcb621(%rip), %rax     # 0xfa2c0
testq %rax, %rax
sete %cl
cmpq %rax, %r13
setl %al
orb %cl, %al
movq 0x30(%rsp), %rbp
jne 0x2eccd
leaq 0x81754(%rip), %rdi     # 0xb040f
movq %r13, %rsi
callq 0x71e85
jmp 0x2eccd
movq %r14, %r13
movq 0x30(%rsp), %rbp
addq %r15, %r13
callq 0x32960
subq %rax, %rbx
addq 0x38(%rsp), %rbx
incq 0xcaf6c(%rip)           # 0xf9c50
movq 0x10(%rsp), %rax
movl 0x30(%rax), %ecx
movl $0x200, %edi            # imm = 0x200
leaq 0x81724(%rip), %rsi     # 0xb041c
movq %r12, %rdx
callq 0x6fb58
movq %r12, %rdi
callq 0x3a265
incl 0x1c(%rsp)
cmpq $0x0, 0x48(%rsp)
je 0x2ed2a
callq 0x391d7
jmp 0x2ed2a
movq 0x40(%rsp), %r13
movq 0x38(%rsp), %rbx
movq 0x30(%rsp), %rbp
incq %rbp
jmp 0x2eb09
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r12d, %r12d
cmpl 0xcb47c(%rip), %ebx     # 0xfa1bc
jge 0x2ec26
movq 0x20(%rsp), %rdi
callq 0x2aaf6
movq 0x8(%rax), %rcx
testq %r12, %r12
sete %dl
cmpq %rcx, %r14
setg %sil
orb %dl, %sil
je 0x2ed6c
movq (%rax), %r12
movq %rcx, %r14
incl %ebx
jmp 0x2ed3a
cmpl $0x0, 0x1c(%rsp)
movq 0x28(%rsp), %rax
jne 0x2eaf6
cmpq $0x0, 0xcb538(%rip)     # 0xfa2c0
je 0x2ede3
callq 0x2b8ae
movq 0xcb52a(%rip), %rcx     # 0xfa2c0
pushq $-0x1
popq %rdx
movl %edx, 0xc(%rsp)
jmp 0x2edbd
cmpq $0x0, 0xcb519(%rip)     # 0xfa2c0
je 0x2eddb
callq 0x2b8ae
movq 0xcb50b(%rip), %rcx     # 0xfa2c0
movl $0x0, 0xc(%rsp)
testq %rcx, %rcx
je 0x2ede3
subq %r13, %rax
cmpq %rcx, %rax
jl 0x2ede3
leaq 0x81653(%rip), %rdi     # 0xb0424
movq %rax, %rsi
callq 0x71e85
jmp 0x2ede3
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq