// File: /workspace/github_c_makefile_star2_O1/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 0x30971
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 0x310df
testl %eax, %eax
je 0x3aaf5
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 0x8fcfc(%rip), %rax     # 0xca800
movq 0x20(%rax), %rdx
movq %rbx, %rdi
callq 0x314a5
testq %rax, %rax
je 0x3aae6
movq %rax, %r15
movq %rbx, %rdi
movq %rax, %rsi
movl $0x1, %edx
callq 0x308e7
testl %eax, %eax
jne 0x3aae6
movl $0xf0, %eax
andl (%r15), %eax
cmpl $0x50, %eax
jne 0x3ab44
movq %r14, %rdi
callq 0x30ac3
movq %rax, %r14
movq 0x8(%rsp), %rax
testq %rax, %rax
js 0x3ab5a
movq %r15, %rdi
xorl %esi, %esi
movl $0x1, %edx
jmp 0x3ab6e
negq %rax
movq %rax, 0x8(%rsp)
movq %r15, %rdi
movq $-0x1, %rsi
xorl %edx, %edx
callq 0x39935
movq %rax, %r13
leaq 0x10(%rsp), %rsi
movq %rax, %rdi
callq 0x399ca
xorl %r12d, %r12d
testl %eax, %eax
je 0x3abdb
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x39c29
testl %eax, %eax
je 0x3abcc
movq %rbp, %rdi
callq 0x39ca8
leaq 0x8f3fb(%rip), %rax     # 0xc9fa8
incq 0x560(%rax)
incq %r12
movq 0x8(%rsp), %rax
testq %rax, %rax
setne %cl
cmpq %rax, %r12
sete %al
testb %al, %cl
jne 0x3abdb
movq %r13, %rdi
movq %rbp, %rsi
callq 0x399ca
testl %eax, %eax
jne 0x3ab8f
movq %r13, %rdi
callq 0x2984b
movl $0xf0, %eax
andl (%r15), %eax
cmpl $0x50, %eax
jne 0x3abf8
movq %r14, %rdi
callq 0x30834
movq %r15, %rdi
callq 0x398e9
testq %rax, %rax
jne 0x3ac16
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x3174b
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2dea3
testq %r12, %r12
je 0x3aae6
movq 0x8(%rbx), %rdi
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
callq 0x316b7
jmp 0x3aae6