// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/t_zset.c
// Function: zremCommand
void zremCommand(redisClient *c) {
    robj *key = c->argv[1];
    robj *zobj;
    int deleted = 0, keyremoved = 0, j;

    // 取出有序集合对象
    if ((zobj = lookupKeyWriteOrReply(c,key,shared.czero)) == NULL ||
        checkType(c,zobj,REDIS_ZSET)) return;

    // 从 ziplist 中删除
    if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
        unsigned char *eptr;

        // 遍历所有输入元素
        for (j = 2; j < c->argc; j++) {
            // 如果元素在 ziplist 中存在的话
            if ((eptr = zzlFind(zobj->ptr,c->argv[j],NULL)) != NULL) {
                // 元素存在时，删除计算器才增一
                deleted++;
                // 那么删除它们
                zobj->ptr = zzlDelete(zobj->ptr,eptr);
                
                // ziplist 已清空，将有序集合从数据库中删除
                if (zzlLength(zobj->ptr) == 0) {
                    dbDelete(c->db,key);
                    break;
                }
            }
        }

    // 从跳跃表和字典中删除
    } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) {
        zset *zs = zobj->ptr;
        dictEntry *de;
        double score;

        // 遍历所有输入元素
        for (j = 2; j < c->argc; j++) {

            // 查找元素
            de = dictFind(zs->dict,c->argv[j]);

            if (de != NULL) {
                // 元素存在时，删除计算器才增一
                deleted++;

                /* Delete from the skiplist */
                // 将元素从跳跃表中删除
                score = *(double*)dictGetVal(de);
                redisAssertWithInfo(c,c->argv[j],zslDelete(zs->zsl,score,c->argv[j]));

                /* Delete from the hash table */
                // 将元素从字典中删除
                dictDelete(zs->dict,c->argv[j]);

                // 检查是否需要缩小字典
                if (htNeedsResize(zs->dict)) dictResize(zs->dict);

                // 字典已被清空，有序集合已经被清空，将它从数据库中删除
                if (dictSize(zs->dict) == 0) {
                    dbDelete(c->db,key);
                    break;
                }
            }
        }
    } else {
        redisPanic("Unknown sorted set encoding");
    }

    // 如果有至少一个元素被删除的话，那么执行以下代码
    if (deleted) {

        notifyKeyspaceEvent(REDIS_NOTIFY_ZSET,"zrem",key,c->db->id);

        if (keyremoved)
            notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"del",key,c->db->id);

        signalModifiedKey(c->db,key);

        server.dirty += deleted;
    }

    // 回复被删除元素的数量
    addReplyLongLong(c,deleted);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movq 0x8(%rax), %r14
leaq 0x8b473(%rip), %rax     # 0xca800
movq 0x20(%rax), %rdx
movq %r14, %rsi
callq 0x317a6
testq %rax, %rax
je 0x3f537
movq %rax, %r12
movq %rbx, %rdi
movq %rax, %rsi
movl $0x3, %edx
callq 0x30bbf
testl %eax, %eax
jne 0x3f537
movl (%r12), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x3f449
cmpl $0x5, %eax
jne 0x3f574
xorl %r15d, %r15d
cmpl $0x3, 0x30(%rbx)
jl 0x3f52c
movl $0x2, %ebp
movq %rsp, %r13
movq 0x8(%r12), %rdi
movq 0x38(%rbx), %rax
movq (%rax,%rbp,8), %rsi
xorl %edx, %edx
callq 0x3e6af
testq %rax, %rax
je 0x3f438
incl %r15d
movq 0x8(%r12), %rdi
movq %rax, (%rsp)
movq %r13, %rsi
callq 0x2c713
movq %rax, %rdi
movq %r13, %rsi
callq 0x2c713
movq %rax, 0x8(%r12)
movq %rax, %rdi
callq 0x2cc17
cmpl $0x1, %eax
jbe 0x3f4de
incq %rbp
movslq 0x30(%rbx), %rax
cmpq %rax, %rbp
jl 0x3f3ea
jmp 0x3f4ea
xorl %r15d, %r15d
cmpl $0x3, 0x30(%rbx)
jl 0x3f52c
movq 0x8(%r12), %r13
movl $0x2, %r12d
movq (%r13), %rdi
movq 0x38(%rbx), %rax
movq (%rax,%r12,8), %rsi
callq 0x226ac
testq %rax, %rax
je 0x3f4d0
movq 0x8(%rax), %rax
movsd (%rax), %xmm0
movq 0x8(%r13), %rdi
movq 0x38(%rbx), %rax
movq (%rax,%r12,8), %rsi
callq 0x3d412
testl %eax, %eax
je 0x3f546
movq (%r13), %rdi
movq 0x38(%rbx), %rax
movq (%rax,%r12,8), %rsi
callq 0x22796
movq (%r13), %rdi
callq 0x2363c
testl %eax, %eax
je 0x3f4bf
movq (%r13), %rdi
callq 0x221ba
incl %r15d
movq (%r13), %rax
movq 0x28(%rax), %rcx
addq 0x48(%rax), %rcx
je 0x3f4de
incq %r12
movslq 0x30(%rbx), %rax
cmpq %rax, %r12
jl 0x3f461
jmp 0x3f4ea
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x31a36
testl %r15d, %r15d
je 0x3f529
movq 0x8(%rbx), %rax
movl 0x30(%rax), %ecx
leaq 0x54ee2(%rip), %rsi     # 0x943df
movl $0x80, %edi
movq %r14, %rdx
callq 0x60371
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x319b8
movslq %r15d, %r15
leaq 0x8aa88(%rip), %rax     # 0xc9fa8
addq %r15, 0x560(%rax)
jmp 0x3f52c
xorl %r15d, %r15d
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2e133
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38(%rbx), %rax
movq (%rax,%r12,8), %rsi
leaq 0x59e72(%rip), %rdx     # 0x993c7
leaq 0x59ca3(%rip), %rcx     # 0x991ff
movq %rbx, %rdi
movl $0x7ab, %r8d            # imm = 0x7AB
callq 0x4d277
movl $0x1, %edi
callq 0x1f3b0
leaq 0x57e38(%rip), %rdi     # 0x973b3
leaq 0x59c7d(%rip), %rsi     # 0x991ff
movl $0x7bc, %edx            # imm = 0x7BC
callq 0x4cae0
jmp 0x3f56a