// File: /workspace/github_c_makefile_star2_O2/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movq 0x8(%rax), %r14
leaq 0x8b338(%rip), %rax     # 0xc7800
movq 0x20(%rax), %rdx
movq %r14, %rsi
callq 0x2f9d9
testq %rax, %rax
je 0x3c4ee
movq %rax, %r15
pushq $0x3
popq %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2eec6
testl %eax, %eax
je 0x3c4f8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movl (%r15), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x3c563
cmpl $0x5, %eax
jne 0x3c67c
xorl %r12d, %r12d
pushq $0x2
popq %r13
movslq 0x30(%rbx), %rax
cmpq %rax, %r13
jge 0x3c5fe
movq 0x8(%r15), %rdi
movq 0x38(%rbx), %rax
movq (%rax,%r13,8), %rsi
xorl %edx, %edx
callq 0x3b828
testq %rax, %rax
je 0x3c55e
incl %r12d
movq 0x8(%r15), %rdi
movq %rax, %rsi
callq 0x3b8d6
movq %rax, 0x8(%r15)
movq %rax, %rdi
callq 0x3b252
testl %eax, %eax
je 0x3c5f2
incq %r13
jmp 0x3c516
movq 0x8(%r15), %r13
xorl %r12d, %r12d
pushq $0x2
popq %r15
movslq 0x30(%rbx), %rax
cmpq %rax, %r15
jge 0x3c5fe
movq (%r13), %rdi
movq 0x38(%rbx), %rax
movq (%rax,%r15,8), %rsi
callq 0x21b64
testq %rax, %rax
je 0x3c5ea
movq 0x8(%rax), %rax
movsd (%rax), %xmm0
movq 0x8(%r13), %rdi
movq 0x38(%rbx), %rax
movq (%rax,%r15,8), %rsi
callq 0x3a7e5
testl %eax, %eax
je 0x3c650
movq (%r13), %rdi
movq 0x38(%rbx), %rax
movq (%rax,%r15,8), %rsi
callq 0x21c36
movq (%r13), %rdi
callq 0x229c3
testl %eax, %eax
je 0x3c5d9
movq (%r13), %rdi
callq 0x2172d
incl %r12d
movq (%r13), %rax
movq 0x28(%rax), %rcx
addq 0x48(%rax), %rcx
je 0x3c5f2
incq %r15
jmp 0x3c56e
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x2fc3b
testl %r12d, %r12d
je 0x3c63d
movq 0x8(%rbx), %rax
movl 0x30(%rax), %ecx
leaq 0x4dd8e(%rip), %rsi     # 0x8a39f
movl $0x80, %edi
movq %r14, %rdx
callq 0x5b488
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x2fbc0
movslq %r12d, %rsi
leaq 0x8a974(%rip), %rax     # 0xc6fa8
addq %rsi, 0x560(%rax)
jmp 0x3c63f
xorl %esi, %esi
movq %rbx, %rdi
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x2c794
movq 0x38(%rbx), %rax
movq (%rax,%r15,8), %rsi
leaq 0x52d2c(%rip), %rdx     # 0x8f38b
leaq 0x52b5d(%rip), %rcx     # 0x8f1c3
movq %rbx, %rdi
movl $0x7ab, %r8d            # imm = 0x7AB
callq 0x4980c
pushq $0x1
popq %rdi
callq 0x1f3b0
leaq 0x50d24(%rip), %rdi     # 0x8d3a7
leaq 0x52b39(%rip), %rsi     # 0x8f1c3
movl $0x7bc, %edx            # imm = 0x7BC
callq 0x490e8
jmp 0x3c674