// File: /workspace/github_c_makefile_star2_O2/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 0x3c6e0
cmpl $0x2, %ebp
je 0x3c71c
cmpl $0x1, %ebp
jne 0x3c74d
movq 0x10(%rax), %rdi
movq 0x18(%rax), %rsi
leaq 0x38(%rsp), %rdx
callq 0x3c9cf
testl %eax, %eax
je 0x3c74d
leaq 0x52cd1(%rip), %rsi     # 0x8f3af
jmp 0x3c739
movq 0x10(%rax), %rsi
leaq 0x18(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x2f672
testl %eax, %eax
jne 0x3c977
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x2f672
testl %eax, %eax
jne 0x3c977
jmp 0x3c74d
movq 0x10(%rax), %rdi
movq 0x18(%rax), %rsi
leaq 0x20(%rsp), %rdx
callq 0x3cae5
testl %eax, %eax
je 0x3c74d
leaq 0x52c90(%rip), %rsi     # 0x8f3c9
movq %rbx, %rdi
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2c2da
leaq 0x8b0ac(%rip), %r12     # 0xc7800
movq 0x20(%r12), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2f9d9
testq %rax, %rax
je 0x3c968
movq %rax, %r15
pushq $0x3
popq %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2eec6
testl %eax, %eax
jne 0x3c968
testl %ebp, %ebp
je 0x3c819
movl (%r15), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x3c7de
cmpl $0x5, %eax
jne 0x3c9af
movq 0x8(%r15), %rdi
cmpl $0x2, %ebp
je 0x3c984
cmpl $0x1, %ebp
je 0x3c899
testl %ebp, %ebp
jne 0x3c99a
movl 0x18(%rsp), %esi
incl %esi
movl 0x10(%rsp), %edx
incl %edx
leaq 0x8(%rsp), %rcx
callq 0x3bc02
jmp 0x3c993
movq 0x8(%r15), %r15
cmpl $0x2, %ebp
je 0x3c8ad
cmpl $0x1, %ebp
je 0x3c886
testl %ebp, %ebp
jne 0x3c8c3
movq (%r15), %rcx
movq 0x8(%r15), %rdi
movl 0x18(%rsp), %esi
incl %esi
movl 0x10(%rsp), %edx
incl %edx
callq 0x3ac63
jmp 0x3c8be
movq %r15, %rdi
callq 0x3bc1e
movl %eax, %eax
movq 0x18(%rsp), %rcx
testq %rcx, %rcx
jns 0x3c835
addq %rax, %rcx
movq %rcx, 0x18(%rsp)
movq 0x10(%rsp), %rdx
testq %rdx, %rdx
jns 0x3c847
addq %rax, %rdx
movq %rdx, 0x10(%rsp)
testq %rcx, %rcx
jns 0x3c854
andq $0x0, 0x18(%rsp)
xorl %ecx, %ecx
cmpq %rdx, %rcx
jg 0x3c874
cmpq %rax, %rcx
jae 0x3c874
cmpq %rax, %rdx
jb 0x3c78e
decq %rax
movq %rax, 0x10(%rsp)
jmp 0x3c78e
movq 0x20(%r12), %rsi
movq %rbx, %rdi
callq 0x2c10d
jmp 0x3c977
movq (%r15), %rdx
movq 0x8(%r15), %rdi
leaq 0x38(%rsp), %rsi
callq 0x3aa93
jmp 0x3c8be
leaq 0x38(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x3bab9
jmp 0x3c993
movq (%r15), %rdx
movq 0x8(%r15), %rdi
leaq 0x20(%rsp), %rsi
callq 0x3ab67
movq %rax, 0x8(%rsp)
movq (%r15), %rdi
callq 0x229c3
testl %eax, %eax
je 0x3c8d7
movq (%r15), %rdi
callq 0x2172d
movq (%r15), %rax
movq 0x28(%rax), %rcx
movb $0x1, %r15b
addq 0x48(%rax), %rcx
jne 0x3c8f6
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x2fc3b
xorl %r15d, %r15d
cmpq $0x0, 0x8(%rsp)
je 0x3c94d
movq 0x8(%rbx), %rdi
movq %r14, %rsi
callq 0x2fbc0
movslq %ebp, %rax
leaq 0x52c40(%rip), %rcx     # 0x8f554
movslq (%rcx,%rax,4), %rsi
addq %rcx, %rsi
movq 0x8(%rbx), %rax
movl 0x30(%rax), %ecx
movl $0x80, %edi
movq %r14, %rdx
callq 0x5b488
testb %r15b, %r15b
jne 0x3c94d
movq 0x8(%rbx), %rax
movl 0x30(%rax), %ecx
leaq 0x4db5b(%rip), %rsi     # 0x8a49d
pushq $0x4
popq %rdi
movq %r14, %rdx
callq 0x5b488
movq 0x8(%rsp), %rsi
leaq 0x8a64f(%rip), %rax     # 0xc6fa8
addq %rsi, 0x560(%rax)
movq %rbx, %rdi
callq 0x2c794
cmpl $0x2, %ebp
jne 0x3c977
leaq 0x20(%rsp), %rdi
callq 0x3ae93
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x3bb69
movq %rax, %rdi
movq %rax, 0x8(%r15)
callq 0x3b252
movb $0x1, %r15b
testl %eax, %eax
jne 0x3c8f6
jmp 0x3c8e7
leaq 0x509f1(%rip), %rdi     # 0x8d3a7
leaq 0x52806(%rip), %rsi     # 0x8f1c3
movl $0x82a, %edx            # imm = 0x82A
callq 0x490e8
pushq $0x1
popq %rdi
callq 0x1f3b0