// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/t_zset.c
// Function: zremrangeGenericCommand
void zremrangeGenericCommand(redisClient *c, int rangetype) {
    robj *key = c->argv[1];
    robj *zobj;
    int keyremoved = 0;
    unsigned long deleted;
    zrangespec range;
    zlexrangespec lexrange;
    long start, end, llen;

    /* Step 1: Parse the range. */
    if (rangetype == ZRANGE_RANK) {
        if ((getLongFromObjectOrReply(c,c->argv[2],&start,NULL) != REDIS_OK) ||
            (getLongFromObjectOrReply(c,c->argv[3],&end,NULL) != REDIS_OK))
            return;
    } else if (rangetype == ZRANGE_SCORE) {
        if (zslParseRange(c->argv[2],c->argv[3],&range) != REDIS_OK) {
            addReplyError(c,"min or max is not a float");
            return;
        }
    } else if (rangetype == ZRANGE_LEX) {
        if (zslParseLexRange(c->argv[2],c->argv[3],&lexrange) != REDIS_OK) {
            addReplyError(c,"min or max not valid string range item");
            return;
        }
    }

    /* Step 2: Lookup & range sanity checks if needed. */
    if ((zobj = lookupKeyWriteOrReply(c,key,shared.czero)) == NULL ||
        checkType(c,zobj,REDIS_ZSET)) goto cleanup;

    if (rangetype == ZRANGE_RANK) {
        /* Sanitize indexes. */
        llen = zsetLength(zobj);
        if (start < 0) start = llen+start;
        if (end < 0) end = llen+end;
        if (start < 0) start = 0;

        /* Invariant: start >= 0, so this test will be true when end < 0.
         * The range is empty when start > end or start >= length. */
        if (start > end || start >= llen) {
            addReply(c,shared.czero);
            goto cleanup;
        }
        if (end >= llen) end = llen-1;
    }

    /* Step 3: Perform the range deletion operation. */
    if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
        switch(rangetype) {
        case ZRANGE_RANK:
            zobj->ptr = zzlDeleteRangeByRank(zobj->ptr,start+1,end+1,&deleted);
            break;
        case ZRANGE_SCORE:
            zobj->ptr = zzlDeleteRangeByScore(zobj->ptr,&range,&deleted);
            break;
        case ZRANGE_LEX:
            zobj->ptr = zzlDeleteRangeByLex(zobj->ptr,&lexrange,&deleted);
            break;
        }
        if (zzlLength(zobj->ptr) == 0) {
            dbDelete(c->db,key);
            keyremoved = 1;
        }

    // 从跳跃表和字典中删除
    } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) {
        zset *zs = zobj->ptr;
        switch(rangetype) {
        case ZRANGE_RANK:
            deleted = zslDeleteRangeByRank(zs->zsl,start+1,end+1,zs->dict);
            break;
        case ZRANGE_SCORE:
            deleted = zslDeleteRangeByScore(zs->zsl,&range,zs->dict);
            break;
        case ZRANGE_LEX:
            deleted = zslDeleteRangeByLex(zs->zsl,&lexrange,zs->dict);
            break;
        }
        if (htNeedsResize(zs->dict)) dictResize(zs->dict);

        // 对象已清空，从数据库中删除
        if (dictSize(zs->dict) == 0) {
            dbDelete(c->db,key);
            keyremoved = 1;
        }
    } else {
        redisPanic("Unknown sorted set encoding");
    }

    /* Step 4: Notifications and reply. */
    if (deleted) {
        char *event[3] = {"zremrangebyrank","zremrangebyscore","zremrangebylex"};
        signalModifiedKey(c->db,key);
        notifyKeyspaceEvent(REDIS_NOTIFY_ZSET,event[rangetype],key,c->db->id);
        if (keyremoved)
            notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"del",key,c->db->id);
    }

    server.dirty += deleted;

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

cleanup:
    if (rangetype == ZRANGE_LEX) zslFreeLexRange(&lexrange);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x50, %rsp
movl %esi, %ebp
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movq 0x8(%rax), %r14
testl %esi, %esi
je 0x3f5d8
cmpl $0x2, %ebp
je 0x3f614
cmpl $0x1, %ebp
jne 0x3f645
movq 0x10(%rax), %rdi
movq 0x18(%rax), %rsi
leaq 0x38(%rsp), %rdx
callq 0x3f8e5
testl %eax, %eax
je 0x3f645
leaq 0x59e15(%rip), %rsi     # 0x993eb
jmp 0x3f631
movq 0x10(%rax), %rsi
leaq 0x18(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x313b2
testl %eax, %eax
jne 0x3f88a
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x313b2
testl %eax, %eax
jne 0x3f88a
jmp 0x3f645
movq 0x10(%rax), %rdi
movq 0x18(%rax), %rsi
leaq 0x20(%rsp), %rdx
callq 0x3fa0d
testl %eax, %eax
je 0x3f645
leaq 0x59dd4(%rip), %rsi     # 0x99405
movq %rbx, %rdi
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2dc63
leaq 0x8b1b4(%rip), %r12     # 0xca800
movq 0x20(%r12), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x317a6
testq %rax, %rax
je 0x3f871
movq %rax, %r15
movq %rbx, %rdi
movq %rax, %rsi
movl $0x3, %edx
callq 0x30bbf
testl %eax, %eax
jne 0x3f871
testl %ebp, %ebp
je 0x3f71d
movl (%r15), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x3f6e2
cmpl $0x5, %eax
jne 0x3f8c3
movq 0x8(%r15), %rdi
cmpl $0x2, %ebp
je 0x3f897
cmpl $0x1, %ebp
je 0x3f7a0
testl %ebp, %ebp
jne 0x3f8ad
movl 0x18(%rsp), %esi
movl 0x10(%rsp), %eax
subl %esi, %eax
leal 0x1(%rax), %ecx
movq %rcx, 0x8(%rsp)
addl %esi, %esi
leal 0x2(,%rax,2), %edx
callq 0x2c8cb
jmp 0x3f8a6
movq 0x8(%r15), %r15
cmpl $0x2, %ebp
je 0x3f7b4
cmpl $0x1, %ebp
je 0x3f78d
testl %ebp, %ebp
jne 0x3f7ca
movq (%r15), %rcx
movq 0x8(%r15), %rdi
movl 0x18(%rsp), %esi
incl %esi
movl 0x10(%rsp), %edx
incl %edx
callq 0x3d923
jmp 0x3f7c5
movq %r15, %rdi
callq 0x3eadd
movl %eax, %eax
movq 0x18(%rsp), %rcx
testq %rcx, %rcx
jns 0x3f739
addq %rax, %rcx
movq %rcx, 0x18(%rsp)
movq 0x10(%rsp), %rdx
testq %rdx, %rdx
jns 0x3f74b
addq %rax, %rdx
movq %rdx, 0x10(%rsp)
testq %rcx, %rcx
jns 0x3f75b
movq $0x0, 0x18(%rsp)
xorl %ecx, %ecx
cmpq %rdx, %rcx
jg 0x3f77b
cmpq %rax, %rcx
jae 0x3f77b
cmpq %rax, %rdx
jb 0x3f688
decq %rax
movq %rax, 0x10(%rsp)
jmp 0x3f688
movq 0x20(%r12), %rsi
movq %rbx, %rdi
callq 0x2d97c
jmp 0x3f88a
movq (%r15), %rdx
movq 0x8(%r15), %rdi
leaq 0x38(%rsp), %rsi
callq 0x3d6f9
jmp 0x3f7c5
leaq 0x38(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x3e95c
jmp 0x3f8a6
movq (%r15), %rdx
movq 0x8(%r15), %rdi
leaq 0x20(%rsp), %rsi
callq 0x3d7ed
movq %rax, 0x8(%rsp)
movq (%r15), %rdi
callq 0x2363c
testl %eax, %eax
je 0x3f7de
movq (%r15), %rdi
callq 0x221ba
movq (%r15), %rax
movq 0x28(%rax), %rcx
movb $0x1, %r15b
addq 0x48(%rax), %rcx
jne 0x3f7fd
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x31a36
xorl %r15d, %r15d
cmpq $0x0, 0x8(%rsp)
je 0x3f856
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x319b8
movslq %ebp, %rax
leaq 0x59d75(%rip), %rcx     # 0x99590
movslq (%rcx,%rax,4), %rsi
addq %rcx, %rsi
movq 0x8(%rbx), %rax
movl 0x30(%rax), %ecx
movl $0x80, %edi
movq %r14, %rdx
callq 0x60371
testb %r15b, %r15b
jne 0x3f856
movq 0x8(%rbx), %rax
movl 0x30(%rax), %ecx
leaq 0x54c94(%rip), %rsi     # 0x944dd
movl $0x4, %edi
movq %r14, %rdx
callq 0x60371
movq 0x8(%rsp), %rsi
leaq 0x8a746(%rip), %rax     # 0xc9fa8
addq %rsi, 0x560(%rax)
movq %rbx, %rdi
callq 0x2e133
cmpl $0x2, %ebp
jne 0x3f88a
movq 0x20(%rsp), %rdi
callq 0x30b0c
movq 0x28(%rsp), %rdi
callq 0x30b0c
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x3ea1a
movq %rax, %rdi
movq %rax, 0x8(%r15)
callq 0x2cc17
movb $0x1, %r15b
cmpl $0x2, %eax
jb 0x3f7ee
jmp 0x3f7fd
leaq 0x57ae9(%rip), %rdi     # 0x973b3
leaq 0x5992e(%rip), %rsi     # 0x991ff
movl $0x82a, %edx            # imm = 0x82A
callq 0x4cae0
movl $0x1, %edi
callq 0x1f3b0