// File: /workspace/github_c_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %esi, %ebx
movq %rdi, %r14
movq 0x38(%rdi), %rax
movq 0x8(%rax), %r15
testl %esi, %esi
je 0x3f587
cmpl $0x2, %ebx
je 0x3f5c1
cmpl $0x1, %ebx
jne 0x3f5f4
movq 0x10(%rax), %rdi
movq 0x18(%rax), %rsi
leaq 0x30(%rsp), %rdx
callq 0x3f8b1
testl %eax, %eax
je 0x3f5f4
leaq 0x59e1d(%rip), %rsi     # 0x993a2
jmp 0x3f5de
movq 0x10(%rax), %rsi
movq %rsp, %rdx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x310df
testl %eax, %eax
jne 0x3f84c
movq 0x38(%r14), %rax
movq 0x18(%rax), %rsi
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0x310df
testl %eax, %eax
jne 0x3f84c
jmp 0x3f5f4
movq 0x10(%rax), %rdi
movq 0x18(%rax), %rsi
leaq 0x18(%rsp), %rdx
callq 0x3f9d9
testl %eax, %eax
je 0x3f5f4
leaq 0x59dde(%rip), %rsi     # 0x993bc
movq %r14, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2d9d7
leaq 0x8b205(%rip), %r13     # 0xca800
movq 0x20(%r13), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x314a5
testq %rax, %rax
je 0x3f833
movq %rax, %r12
movq %r14, %rdi
movq %rax, %rsi
movl $0x3, %edx
callq 0x308e7
testl %eax, %eax
jne 0x3f833
testl %ebx, %ebx
je 0x3f6ce
movl (%r12), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x3f691
cmpl $0x5, %eax
jne 0x3f88f
cmpl $0x2, %ebx
je 0x3f85b
cmpl $0x1, %ebx
je 0x3f758
testl %ebx, %ebx
jne 0x3f874
movq 0x8(%r12), %rdi
movl (%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 0x2c61d
jmp 0x3f86f
movq 0x8(%r12), %r12
cmpl $0x2, %ebx
je 0x3f771
cmpl $0x1, %ebx
je 0x3f743
testl %ebx, %ebx
jne 0x3f789
movq (%r12), %rcx
movq 0x8(%r12), %rdi
movl (%rsp), %esi
incl %esi
movl 0x10(%rsp), %edx
incl %edx
callq 0x3d8a8
jmp 0x3f784
movq %r12, %rdi
callq 0x3ea49
movl %eax, %eax
movq (%rsp), %rcx
testq %rcx, %rcx
jns 0x3f6e8
addq %rax, %rcx
movq %rcx, (%rsp)
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
jns 0x3f6fa
addq %rax, %rcx
movq %rcx, 0x10(%rsp)
cmpq $0x0, (%rsp)
jns 0x3f709
movq $0x0, (%rsp)
movq (%rsp), %rdx
movq 0x10(%rsp), %rcx
cmpq %rcx, %rdx
jg 0x3f732
cmpq %rax, %rdx
jge 0x3f732
cmpq %rax, %rcx
jl 0x3f636
decq %rax
movq %rax, 0x10(%rsp)
jmp 0x3f636
movq 0x20(%r13), %rsi
movq %r14, %rdi
callq 0x2d6f0
jmp 0x3f833
movq (%r12), %rdx
movq 0x8(%r12), %rdi
leaq 0x30(%rsp), %rsi
callq 0x3d674
jmp 0x3f784
movq 0x8(%r12), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x3e89d
jmp 0x3f86f
movq (%r12), %rdx
movq 0x8(%r12), %rdi
leaq 0x18(%rsp), %rsi
callq 0x3d76e
movq %rax, 0x8(%rsp)
movq (%r12), %rdi
callq 0x2347e
testl %eax, %eax
je 0x3f79f
movq (%r12), %rdi
callq 0x21fd6
movq (%r12), %rax
movq 0x28(%rax), %rcx
addq 0x48(%rax), %rcx
setne %bpl
jne 0x3f7bf
movq 0x8(%r14), %rdi
movq %r15, %rsi
callq 0x3174b
xorl %ebp, %ebp
cmpq $0x0, 0x8(%rsp)
je 0x3f818
movq 0x8(%r14), %rdi
movq %r15, %rsi
callq 0x316b7
movslq %ebx, %rax
leaq 0x59d6b(%rip), %rcx     # 0x99548
movslq (%rcx,%rax,4), %rsi
addq %rcx, %rsi
movq 0x8(%r14), %rax
movl 0x30(%rax), %ecx
movl $0x80, %edi
movq %r15, %rdx
callq 0x608b5
testb %bpl, %bpl
jne 0x3f818
movq 0x8(%r14), %rax
movl 0x30(%rax), %ecx
leaq 0x54c82(%rip), %rsi     # 0x9448d
movl $0x4, %edi
movq %r15, %rdx
callq 0x608b5
movq 0x8(%rsp), %rsi
leaq 0x8a784(%rip), %rax     # 0xc9fa8
addq %rsi, 0x560(%rax)
movq %r14, %rdi
callq 0x2dea3
cmpl $0x2, %ebx
jne 0x3f84c
movq 0x18(%rsp), %rdi
callq 0x30834
movq 0x20(%rsp), %rdi
callq 0x30834
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r12), %rdi
leaq 0x18(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x3e964
movq %rax, 0x8(%r12)
movq 0x8(%r12), %rdi
callq 0x2c9a8
movb $0x1, %bpl
cmpl $0x2, %eax
jb 0x3f7b1
jmp 0x3f7bf
leaq 0x57abd(%rip), %rdi     # 0x97353
leaq 0x59919(%rip), %rsi     # 0x991b6
movl $0x82a, %edx            # imm = 0x82A
callq 0x4d0e5
movl $0x1, %edi
callq 0x1f3b0