// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/t_list.c
// Function: lremCommand
void lremCommand(redisClient *c) {
    robj *subject, *obj;

    // 编码目标对象 elem
    obj = c->argv[3] = tryObjectEncoding(c->argv[3]);
    long toremove;
    long removed = 0;
    listTypeEntry entry;

    // 取出指定删除模式的 count 参数
    if ((getLongFromObjectOrReply(c, c->argv[2], &toremove, NULL) != REDIS_OK))
        return;

    // 取出列表对象
    subject = lookupKeyWriteOrReply(c,c->argv[1],shared.czero);
    if (subject == NULL || checkType(c,subject,REDIS_LIST)) return;

    /* Make sure obj is raw when we're dealing with a ziplist */
    if (subject->encoding == REDIS_ENCODING_ZIPLIST)
        obj = getDecodedObject(obj);

    listTypeIterator *li;

    // 根据 toremove 参数，决定是从表头还是表尾开始进行删除
    if (toremove < 0) {
        toremove = -toremove;
        li = listTypeInitIterator(subject,-1,REDIS_HEAD);
    } else {
        li = listTypeInitIterator(subject,0,REDIS_TAIL);
    }

    // 查找，比对对象，并进行删除
    while (listTypeNext(li,&entry)) {
        if (listTypeEqual(&entry,obj)) {
            listTypeDelete(&entry);
            server.dirty++;
            removed++;
            // 已经满足删除数量，停止
            if (toremove && removed == toremove) break;
        }
    }
    listTypeReleaseIterator(li);

    /* Clean up raw encoded object */
    if (subject->encoding == REDIS_ENCODING_ZIPLIST)
        decrRefCount(obj);

    // 删除空列表
    if (listTypeLength(subject) == 0) dbDelete(c->db,c->argv[1]);

    addReplyLongLong(c,removed);

    if (removed) signalModifiedKey(c->db,c->argv[1]);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movq 0x18(%rax), %rdi
callq 0x2ef4c
movq %rax, %r14
movq 0x38(%rbx), %rax
movq %r14, 0x18(%rax)
movq 0x38(%rbx), %rax
movq 0x10(%rax), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x2f672
testl %eax, %eax
je 0x38190
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
leaq 0x8f661(%rip), %rax     # 0xc7800
movq 0x20(%rax), %rdx
movq %rbx, %rdi
callq 0x2f9d9
testq %rax, %rax
je 0x38181
movq %rax, %r15
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2eec6
testl %eax, %eax
jne 0x38181
movl $0xf0, %eax
andl (%r15), %eax
cmpl $0x50, %eax
jne 0x381dd
movq %r14, %rdi
callq 0x2f087
movq %rax, %r14
movq 0x8(%rsp), %rax
testq %rax, %rax
js 0x381f1
pushq $0x1
popq %rdx
movq %r15, %rdi
xorl %esi, %esi
jmp 0x38201
negq %rax
movq %rax, 0x8(%rsp)
pushq $-0x1
popq %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x37058
movq %rax, %r13
xorl %r12d, %r12d
leaq 0x10(%rsp), %rbp
movq %r13, %rdi
movq %rbp, %rsi
callq 0x370e4
testl %eax, %eax
je 0x3825f
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3733b
testl %eax, %eax
je 0x38211
movq %rbp, %rdi
callq 0x373b8
leaq 0x8ed6a(%rip), %rax     # 0xc6fa8
incq 0x560(%rax)
incq %r12
movq 0x8(%rsp), %rax
testq %rax, %rax
setne %cl
cmpq %rax, %r12
sete %al
andb %cl, %al
cmpb $0x1, %al
jne 0x38211
movq %r13, %rdi
callq 0x283cd
movl $0xf0, %eax
andl (%r15), %eax
cmpl $0x50, %eax
jne 0x3827c
movq %r14, %rdi
callq 0x2ee1f
movq %r15, %rdi
callq 0x3700e
testq %rax, %rax
jne 0x3829a
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x2fc3b
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2c794
testq %r12, %r12
je 0x38181
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x2fbc0
jmp 0x38181