// File: /workspace/github_c_makefile_star2_O1/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 0x8b4c2(%rip), %rax     # 0xca800
movq 0x20(%rax), %rdx
movq %r14, %rsi
callq 0x314a5
testq %rax, %rax
je 0x3f4e4
movq %rax, %r15
movq %rbx, %rdi
movq %rax, %rsi
movl $0x3, %edx
callq 0x308e7
testl %eax, %eax
jne 0x3f4e4
movl (%r15), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x3f3f6
cmpl $0x5, %eax
jne 0x3f521
cmpl $0x3, 0x30(%rbx)
jl 0x3f48b
xorl %r13d, %r13d
movl $0x2, %ebp
movq %rsp, %r12
movq 0x8(%r15), %rdi
movq 0x38(%rbx), %rax
movq (%rax,%rbp,8), %rsi
xorl %edx, %edx
callq 0x3e5cc
testq %rax, %rax
je 0x3f3e5
incl %r13d
movq 0x8(%r15), %rdi
movq %rax, (%rsp)
movq %r12, %rsi
callq 0x2c46e
movq %rax, %rdi
movq %r12, %rsi
callq 0x2c46e
movq %rax, 0x8(%r15)
movq %rax, %rdi
callq 0x2c9a8
cmpl $0x2, %eax
jb 0x3f490
incq %rbp
movslq 0x30(%rbx), %rax
cmpq %rax, %rbp
jl 0x3f39a
jmp 0x3f49c
cmpl $0x3, 0x30(%rbx)
jl 0x3f48b
movq 0x8(%r15), %r12
xorl %r13d, %r13d
movl $0x2, %r15d
movq (%r12), %rdi
movq 0x38(%rbx), %rax
movq (%rax,%r15,8), %rsi
callq 0x224dc
testq %rax, %rax
je 0x3f47d
movq 0x8(%rax), %rax
movsd (%rax), %xmm0
movq 0x8(%r12), %rdi
movq 0x38(%rbx), %rax
movq (%rax,%r15,8), %rsi
callq 0x3d388
testl %eax, %eax
je 0x3f4f3
movq (%r12), %rdi
movq 0x38(%rbx), %rax
movq (%rax,%r15,8), %rsi
callq 0x225d3
movq (%r12), %rdi
callq 0x2347e
testl %eax, %eax
je 0x3f46c
movq (%r12), %rdi
callq 0x21fd6
incl %r13d
movq (%r12), %rax
movq 0x28(%rax), %rcx
addq 0x48(%rax), %rcx
je 0x3f490
incq %r15
movslq 0x30(%rbx), %rax
cmpq %rax, %r15
jl 0x3f40d
jmp 0x3f49c
xorl %r13d, %r13d
jmp 0x3f49c
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x3174b
movslq %r13d, %r15
testl %r13d, %r13d
je 0x3f4d9
movq 0x8(%rbx), %rax
movl 0x30(%rax), %ecx
leaq 0x54edd(%rip), %rsi     # 0x9438f
movl $0x80, %edi
movq %r14, %rdx
callq 0x608b5
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x316b7
leaq 0x8aad6(%rip), %rax     # 0xc9fa8
addq %r15, 0x560(%rax)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2dea3
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38(%rbx), %rax
movq (%rax,%r15,8), %rsi
leaq 0x59e7c(%rip), %rdx     # 0x9937e
leaq 0x59cad(%rip), %rcx     # 0x991b6
movq %rbx, %rdi
movl $0x7ab, %r8d            # imm = 0x7AB
callq 0x4d872
movl $0x1, %edi
callq 0x1f3b0
leaq 0x57e2b(%rip), %rdi     # 0x97353
leaq 0x59c87(%rip), %rsi     # 0x991b6
movl $0x7bc, %edx            # imm = 0x7BC
callq 0x4d0e5
jmp 0x3f517