// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movq 0xce38e(%rip), %rax     # 0x101a88
movl 0x28(%rax), %r15d
callq 0x3e869
movq $0x0, (%rsp)
testl %eax, %eax
jne 0x33c17
callq 0x3847a
movq %rax, %rcx
testq %r15, %r15
je 0x3376c
movq %rcx, %r12
movq 0xce35e(%rip), %rdi     # 0x101a88
leaq 0x18(%rsp), %rbx
movq %rbx, %rsi
callq 0x2b3b7
movq %rbx, %rdi
callq 0x2b3d4
testq %rax, %rax
je 0x33769
xorl %r14d, %r14d
leaq 0x18(%rsp), %rbx
movq 0x10(%rax), %rdi
callq 0x3ed7f
subq %rax, %r12
cmovbq %r14, %r12
movq %rbx, %rdi
callq 0x2b3d4
testq %rax, %rax
jne 0x3374c
movq %r12, %rcx
cmpl $0x0, 0xce79d(%rip)     # 0x101f10
je 0x337c9
movq %rcx, %r14
movq 0xce7d1(%rip), %rax     # 0x101f50
movzbl -0x1(%rax), %ebx
movl %ebx, %ecx
andl $0x7, %ecx
cmpl $0x4, %ecx
ja 0x337b9
leaq 0x88d10(%rip), %rdx     # 0xbc4a4
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
shrl $0x3, %ebx
jmp 0x337bb
movq -0x11(%rax), %rbx
jmp 0x337bb
movzwl -0x5(%rax), %ebx
jmp 0x337bb
movl -0x9(%rax), %ebx
jmp 0x337bb
movzbl -0x3(%rax), %ebx
jmp 0x337bb
xorl %ebx, %ebx
callq 0x5cd26
addq %rbx, %rax
movq %r14, %rcx
subq %rax, %rcx
subq 0xce9e0(%rip), %rcx     # 0x1021b0
jbe 0x33c17
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, (%rsp)
cmpl $0x5, 0xce9d2(%rip)     # 0x1021b8
je 0x33c17
movq %r15, 0x48(%rsp)
cmpq $0x0, 0xceac7(%rip)     # 0x1022c0
movabsq $0x20c49ba5e353f7cf, %r15 # imm = 0x20C49BA5E353F7CF
je 0x3383d
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movq %rcx, %r14
callq 0x26300
movq %r14, %rcx
imulq $0xf4240, (%rbx), %rax  # imm = 0xF4240
addq 0x8(%rbx), %rax
imulq %r15
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
movq %rdx, 0x28(%rsp)
jmp 0x33846
movq $0x0, 0x28(%rsp)
movq $0x0, 0x38(%rsp)
movq %rcx, 0x40(%rsp)
cmpq %rcx, 0x38(%rsp)
jae 0x33ba1
cmpl $0x0, 0xce652(%rip)     # 0x101eb8
jle 0x33b5a
xorl %r15d, %r15d
movl $0x0, 0x14(%rsp)
movq %r15, %rsi
shlq $0x6, %rsi
addq 0xce04b(%rip), %rsi     # 0x1018d0
movl 0xce92d(%rip), %eax     # 0x1021b8
leal -0x3(%rax), %ecx
xorl %edx, %edx
cmpl $0x2, %ecx
setae %dl
movq %rsi, 0x8(%rsp)
movq (%rsi,%rdx,8), %rbp
movq 0x28(%rbp), %rcx
addq 0x48(%rbp), %rcx
je 0x33b37
cmpl $0x4, %eax
ja 0x33b37
movl %eax, %eax
leaq 0x88bf9(%rip), %rcx     # 0xbc4b8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movq %r15, 0x30(%rsp)
jmpq *%rax
movq 0x8(%rsp), %rax
movq 0x28(%rax), %rax
leaq 0xf0(%rax), %rbx
addq $0x100, %rax            # imm = 0x100
movq %rax, 0x50(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq 0x28(%rax), %rdx
movq %rbp, %rdi
callq 0x351d0
movq 0x50(%rsp), %r13
xorl %r12d, %r12d
leaq -0x10(%r13), %r15
movq -0x8(%r13), %rsi
testq %rsi, %rsi
je 0x3393e
movq %rbp, %rdi
callq 0x2ebca
movq %rax, %r14
movq -0x8(%r13), %rdi
callq 0x36330
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x267e0
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
testq %r14, %r14
jne 0x33953
addq $0x10, %r12
movq %r15, %r13
cmpq $0x100, %r12            # imm = 0x100
jne 0x33904
xorl %r12d, %r12d
jmp 0x33956
movq (%r14), %r12
testq %r12, %r12
je 0x338e8
jmp 0x339b2
movq %rbp, %rdi
callq 0x2f141
movq (%rax), %r12
jmp 0x339ad
cmpl $0x0, 0xce84b(%rip)     # 0x1021bc
jle 0x33b37
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %ebx, %ebx
movq %rbp, %rdi
callq 0x2f141
movq 0x8(%rax), %rcx
testq %r12, %r12
sete %dl
cmpq %rcx, %r14
setg %sil
orb %dl, %sil
je 0x339a3
movq (%rax), %r12
movq %rcx, %r14
incl %ebx
cmpl 0xce811(%rip), %ebx     # 0x1021bc
jl 0x3397f
testq %r12, %r12
je 0x339d7
movzbl -0x1(%r12), %esi
movl %esi, %eax
andl $0x7, %eax
cmpl $0x4, %eax
ja 0x339ff
leaq 0x88b03(%rip), %rcx     # 0xbc4cc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
shrl $0x3, %esi
jmp 0x33a01
movq 0x30(%rsp), %r15
jmp 0x33b37
movq -0x11(%r12), %rsi
jmp 0x33a01
movzwl -0x5(%r12), %esi
jmp 0x33a01
movl -0x9(%r12), %esi
jmp 0x33a01
movzbl -0x3(%r12), %esi
jmp 0x33a01
xorl %esi, %esi
movq %r12, %rdi
callq 0x40744
movq %rax, %rbp
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x432db
callq 0x3847a
movq %rax, %r12
cmpq $0x0, 0xce897(%rip)     # 0x1022c0
je 0x33a65
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x26300
imulq $0xf4240, 0x18(%rsp), %rax # imm = 0xF4240
addq 0x20(%rsp), %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
imulq %rcx
movq %rdx, %r15
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %r15
addq %rax, %r15
jmp 0x33a68
xorl %r15d, %r15d
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x41e5f
cmpq $0x0, 0xce843(%rip)     # 0x1022c0
je 0x33ae1
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x26300
imulq $0xf4240, 0x18(%rsp), %rax # imm = 0xF4240
addq 0x20(%rsp), %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
imulq %rcx
movq %rdx, %r13
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %r13
addq %rax, %r13
subq %r15, %r13
movq 0xce7ff(%rip), %rax     # 0x1022c0
testq %rax, %rax
je 0x33ae4
cmpq %rax, %r13
movq 0x30(%rsp), %r15
jl 0x33ae9
leaq 0x8aa48(%rip), %rdi     # 0xbe51f
movq %r13, %rsi
callq 0x7eb75
jmp 0x33ae9
movq %r15, %r13
movq 0x30(%rsp), %r15
addq %r13, 0x28(%rsp)
callq 0x3847a
subq %rax, %r12
addq %r12, 0x38(%rsp)
incq 0xce14e(%rip)           # 0x101c50
movq 0x8(%rsp), %rax
movl 0x30(%rax), %ecx
movl $0x200, %edi            # imm = 0x200
leaq 0x8aa16(%rip), %rsi     # 0xbe52c
movq %rbp, %rdx
callq 0x7c665
movq %rbp, %rdi
callq 0x40c8f
incl 0x14(%rsp)
cmpq $0x0, 0x48(%rsp)
je 0x33b37
callq 0x3f95d
incq %r15
movslq 0xce377(%rip), %rax     # 0x101eb8
cmpq %rax, %r15
jl 0x33877
cmpl $0x0, 0x14(%rsp)
movq 0x40(%rsp), %rcx
jne 0x33854
cmpq $0x0, 0xce75e(%rip)     # 0x1022c0
je 0x33c17
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x26300
imulq $0xf4240, (%rbx), %rax  # imm = 0xF4240
addq 0x8(%rbx), %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
imulq %rcx
movq 0xce72a(%rip), %rax     # 0x1022c0
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rcx, (%rsp)
jmp 0x33be1
cmpq $0x0, 0xce717(%rip)     # 0x1022c0
je 0x33c0f
movq $0x0, (%rsp)
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x26300
imulq $0xf4240, (%rbx), %rax  # imm = 0xF4240
addq 0x8(%rbx), %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
imulq %rcx
movq 0xce6df(%rip), %rax     # 0x1022c0
testq %rax, %rax
je 0x33c17
movq %rdx, %rcx
shrq $0x3f, %rcx
sarq $0x7, %rdx
addq %rcx, %rdx
subq 0x28(%rsp), %rdx
cmpq %rax, %rdx
jl 0x33c17
leaq 0x8a92f(%rip), %rdi     # 0xbe534
movq %rdx, %rsi
callq 0x7eb75
jmp 0x33c17
movq $0x0, (%rsp)
movq (%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq