// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/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;

    /* 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 0xd02d3(%rip), %rax     # 0x106a38
movl 0x28(%rax), %ebx
callq 0x3b446
movq %rbx, 0x48(%rsp)
testq %rbx, %rbx
je 0x367c2
movq %rax, %r15
movq 0xd02b7(%rip), %rdi     # 0x106a38
leaq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0x2e582
movq %rbx, %rdi
callq 0x2e59f
testq %rax, %rax
je 0x367bf
xorl %ebx, %ebx
leaq 0x10(%rsp), %r14
movq 0x10(%rax), %rdi
callq 0x42152
subq %rax, %r15
cmovbq %rbx, %r15
movq %r14, %rdi
callq 0x2e59f
testq %rax, %rax
jne 0x367a2
movq %r15, %rax
cmpl $0x0, 0xd06e7(%rip)     # 0x106eb0
je 0x3681f
movq %rax, %r14
movq 0xd071b(%rip), %rax     # 0x106ef0
movzbl -0x1(%rax), %ebx
movl %ebx, %ecx
andl $0x7, %ecx
cmpl $0x4, %ecx
ja 0x3680f
leaq 0x88c72(%rip), %rdx     # 0xbf45c
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
shrl $0x3, %ebx
jmp 0x36811
movq -0x11(%rax), %rbx
jmp 0x36811
movzwl -0x5(%rax), %ebx
jmp 0x36811
movl -0x9(%rax), %ebx
jmp 0x36811
movzbl -0x3(%rax), %ebx
jmp 0x36811
xorl %ebx, %ebx
callq 0x60942
addq %rbx, %rax
subq %rax, %r14
movq %r14, %rax
xorl %ebp, %ebp
subq 0xd0928(%rip), %rax     # 0x107150
jbe 0x36c87
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x5, 0xd091e(%rip)     # 0x107158
je 0x36c87
cmpq $0x0, 0xd0a18(%rip)     # 0x107260
movabsq $0x20c49ba5e353f7cf, %r14 # imm = 0x20C49BA5E353F7CF
movq %rax, 0x20(%rsp)
je 0x36890
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x26310
imulq $0xf4240, (%rbx), %rax  # imm = 0xF4240
addq 0x8(%rbx), %rax
imulq %r14
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
movq %rdx, 0x8(%rsp)
movq 0x20(%rsp), %rax
jmp 0x36899
movq $0x0, 0x8(%rsp)
movq $0x0, 0x40(%rsp)
cmpq %rax, 0x40(%rsp)
jae 0x36c1a
cmpl $0x0, 0xd05a4(%rip)     # 0x106e58
jle 0x36b9a
xorl %esi, %esi
movl $0x0, 0x34(%rsp)
movq %rsi, %rdi
shlq $0x6, %rdi
addq 0xcffae(%rip), %rdi     # 0x106880
movl 0xd0880(%rip), %eax     # 0x107158
leal -0x3(%rax), %ecx
xorl %edx, %edx
cmpl $0x2, %ecx
setae %dl
movq %rdi, 0x28(%rsp)
movq (%rdi,%rdx,8), %r12
movq 0x28(%r12), %rcx
addq 0x48(%r12), %rcx
je 0x36b78
cmpl $0x4, %eax
movq %rsi, 0x38(%rsp)
ja 0x369fb
movl %eax, %eax
leaq 0x88b5d(%rip), %rcx     # 0xbf470
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x28(%rsp), %rax
movq 0x28(%rax), %rax
leaq 0xf0(%rax), %rbp
addq $0x100, %rax            # imm = 0x100
movq %rax, 0x50(%rsp)
movq 0x28(%rsp), %rax
movq (%rax), %rsi
movq 0x28(%rax), %rdx
movq %r12, %rdi
callq 0x38160
movq 0x50(%rsp), %r13
xorl %r15d, %r15d
leaq -0x10(%r13), %r14
movq -0x8(%r13), %rsi
testq %rsi, %rsi
je 0x3698e
movq %r12, %rdi
callq 0x31cca
movq %rax, %rbx
movq -0x8(%r13), %rdi
callq 0x392b6
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x267f0
xorps %xmm0, %xmm0
movups %xmm0, (%rbp)
testq %rbx, %rbx
jne 0x369a3
addq $0x10, %r15
movq %r14, %r13
cmpq $0x100, %r15            # imm = 0x100
jne 0x36953
xorl %r15d, %r15d
jmp 0x369a6
movq (%rbx), %r15
testq %r15, %r15
je 0x36937
jmp 0x369fe
movq %r12, %rdi
callq 0x32237
movq (%rax), %r15
jmp 0x369fe
cmpl $0x0, 0xd079b(%rip)     # 0x10715c
jle 0x369fb
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %ebx, %ebx
movq %r12, %rdi
callq 0x32237
movq 0x8(%rax), %rcx
testq %r15, %r15
sete %dl
cmpq %rcx, %r14
setg %sil
orb %dl, %sil
je 0x369ef
movq (%rax), %r15
movq %rcx, %r14
incl %ebx
cmpl 0xd0765(%rip), %ebx     # 0x10715c
jl 0x369cb
jmp 0x369fe
xorl %r15d, %r15d
testq %r15, %r15
je 0x36b6e
movzbl -0x1(%r15), %esi
movl %esi, %eax
andl $0x7, %eax
cmpl $0x4, %eax
ja 0x36a4f
leaq 0x88a67(%rip), %rcx     # 0xbf484
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
movabsq $0x20c49ba5e353f7cf, %rbx # imm = 0x20C49BA5E353F7CF
jmpq *%rax
shrl $0x3, %esi
jmp 0x36a5b
movq -0x11(%r15), %rsi
jmp 0x36a5b
movzwl -0x5(%r15), %esi
jmp 0x36a5b
movl -0x9(%r15), %esi
jmp 0x36a5b
movzbl -0x3(%r15), %esi
jmp 0x36a5b
xorl %esi, %esi
movabsq $0x20c49ba5e353f7cf, %rbx # imm = 0x20C49BA5E353F7CF
movq %r15, %rdi
callq 0x43ba8
movq %rax, %r12
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x4672b
callq 0x3b446
movq %rax, %r15
cmpq $0x0, 0xd07dd(%rip)     # 0x107260
je 0x36ab5
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x26310
imulq $0xf4240, 0x10(%rsp), %rax # imm = 0xF4240
addq 0x18(%rsp), %rax
imulq %rbx
movq %rdx, %r13
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %r13
addq %rax, %r13
jmp 0x36ab8
xorl %r13d, %r13d
movq 0x28(%rsp), %rdi
movq %r12, %rsi
callq 0x452f3
cmpq $0x0, 0xd0793(%rip)     # 0x107260
je 0x36b00
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x26310
imulq $0xf4240, 0x10(%rsp), %rax # imm = 0xF4240
addq 0x18(%rsp), %rax
imulq %rbx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
subq %r13, %rdx
movq %rdx, %r13
movq 0xd0759(%rip), %rax     # 0x107260
testq %rax, %rax
je 0x36b20
cmpq %rax, %r13
jl 0x36b20
leaq 0x8a995(%rip), %rdi     # 0xc14ad
movq %r13, %rsi
callq 0x825dd
addq %r13, 0x8(%rsp)
callq 0x3b446
subq %rax, %r15
addq %r15, 0x40(%rsp)
incq 0xd00b7(%rip)           # 0x106bf0
movq 0x28(%rsp), %rax
movl 0x30(%rax), %ecx
movl $0x200, %edi            # imm = 0x200
leaq 0x8a96d(%rip), %rsi     # 0xc14ba
movq %r12, %rdx
callq 0x800c9
movq %r12, %rdi
callq 0x440f3
incl 0x34(%rsp)
cmpq $0x0, 0x48(%rsp)
je 0x36b6e
callq 0x42da9
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq 0x38(%rsp), %rsi
incq %rsi
movslq 0xd02d6(%rip), %rax     # 0x106e58
cmpq %rax, %rsi
jl 0x368c4
cmpl $0x0, 0x34(%rsp)
sete %bl
movq 0x20(%rsp), %rax
jmp 0x36b9c
movb $0x1, %bl
testb %bl, %bl
je 0x36c10
cmpq $0x0, 0xd06b8(%rip)     # 0x107260
je 0x36be9
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x26310
imulq $0xf4240, 0x10(%rsp), %rax # imm = 0xF4240
addq 0x18(%rsp), %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
subq 0x8(%rsp), %rdx
movq %rdx, 0x8(%rsp)
movq 0xd0670(%rip), %rax     # 0x107260
testq %rax, %rax
je 0x36c0b
movq 0x8(%rsp), %rsi
cmpq %rax, %rsi
jl 0x36c0b
leaq 0x8a8bc(%rip), %rdi     # 0xc14c2
callq 0x825dd
movq 0x20(%rsp), %rax
testb %bl, %bl
je 0x368a2
jmp 0x36c87
cmpq $0x0, 0xd063e(%rip)     # 0x107260
je 0x36c63
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x26310
imulq $0xf4240, (%rbx), %rax  # imm = 0xF4240
addq 0x8(%rbx), %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
subq 0x8(%rsp), %rdx
movq %rdx, 0x8(%rsp)
movq 0xd05f6(%rip), %rax     # 0x107260
xorl %ebp, %ebp
testq %rax, %rax
je 0x36c87
movq 0x8(%rsp), %rsi
cmpq %rax, %rsi
jl 0x36c87
leaq 0x8a840(%rip), %rdi     # 0xc14c2
callq 0x825dd
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq