// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/t_zset.c
// Function: zrangeGenericCommand
void zrangeGenericCommand(redisClient *c, int reverse) {
    robj *key = c->argv[1];
    robj *zobj;
    int withscores = 0;
    long start;
    long end;
    int llen;
    int rangelen;

    // 取出 start 和 end 参数
    if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != REDIS_OK) ||
        (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != REDIS_OK)) return;

    // 确定是否显示分值
    if (c->argc == 5 && !strcasecmp(c->argv[4]->ptr,"withscores")) {
        withscores = 1;
    } else if (c->argc >= 5) {
        addReply(c,shared.syntaxerr);
        return;
    }

    // 取出有序集合对象
    if ((zobj = lookupKeyReadOrReply(c,key,shared.emptymultibulk)) == NULL
         || checkType(c,zobj,REDIS_ZSET)) return;

    /* 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.emptymultibulk);
        return;
    }
    if (end >= llen) end = llen-1;
    rangelen = (end-start)+1;

    /* Return the result in form of a multi-bulk reply */
    addReplyMultiBulkLen(c, withscores ? (rangelen*2) : rangelen);

    if (zobj->encoding == REDIS_ENCODING_ZIPLIST) {
        unsigned char *zl = zobj->ptr;
        unsigned char *eptr, *sptr;
        unsigned char *vstr;
        unsigned int vlen;
        long long vlong;

        // 决定迭代的方向
        if (reverse)
            eptr = ziplistIndex(zl,-2-(2*start));
        else
            eptr = ziplistIndex(zl,2*start);

        redisAssertWithInfo(c,zobj,eptr != NULL);
        sptr = ziplistNext(zl,eptr);

        // 取出元素
        while (rangelen--) {
            redisAssertWithInfo(c,zobj,eptr != NULL && sptr != NULL);
            redisAssertWithInfo(c,zobj,ziplistGet(eptr,&vstr,&vlen,&vlong));
            if (vstr == NULL)
                addReplyBulkLongLong(c,vlong);
            else
                addReplyBulkCBuffer(c,vstr,vlen);

            if (withscores)
                addReplyDouble(c,zzlGetScore(sptr));

            if (reverse)
                zzlPrev(zl,&eptr,&sptr);
            else
                zzlNext(zl,&eptr,&sptr);
        }

    } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) {
        zset *zs = zobj->ptr;
        zskiplist *zsl = zs->zsl;
        zskiplistNode *ln;
        robj *ele;

        /* Check if starting point is trivial, before doing log(N) lookup. */
        // 迭代的方向
        if (reverse) {
            ln = zsl->tail;
            if (start > 0)
                ln = zslGetElementByRank(zsl,llen-start);
        } else {
            ln = zsl->header->level[0].forward;
            if (start > 0)
                ln = zslGetElementByRank(zsl,start+1);
        }

        // 取出元素
        while(rangelen--) {
            redisAssertWithInfo(c,zobj,ln != NULL);
            ele = ln->obj;
            addReplyBulk(c,ele);
            if (withscores)
                addReplyDouble(c,ln->score);
            ln = reverse ? ln->backward : ln->level[0].forward;
        }
    } else {
        redisPanic("Unknown sorted set encoding");
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %esi, %ebp
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movq 0x8(%rax), %r14
movq 0x10(%rax), %rsi
leaq 0x8(%rsp), %rdx
xorl %ecx, %ecx
callq 0x313b2
testl %eax, %eax
jne 0x40b38
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x313b2
testl %eax, %eax
jne 0x40b38
movl 0x30(%rbx), %r12d
cmpl $0x5, %r12d
jne 0x40aff
movq 0x38(%rbx), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x58a4c(%rip), %rsi     # 0x9952a
callq 0x1f2e0
testl %eax, %eax
je 0x40b01
leaq 0x89d12(%rip), %rax     # 0xca800
movq 0x80(%rax), %rsi
movq %rbx, %rdi
callq 0x2d97c
jmp 0x40b38
jge 0x40ae7
leaq 0x89cf8(%rip), %r13     # 0xca800
movq 0x68(%r13), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x31745
testq %rax, %rax
je 0x40b38
movq %rbx, %rdi
movq %rax, %rsi
movl $0x3, %edx
movq %rax, (%rsp)
callq 0x30bbf
movq (%rsp), %rdi
testl %eax, %eax
je 0x40b47
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3eadd
movq 0x8(%rsp), %rdx
testq %rdx, %rdx
jns 0x40b61
movslq %eax, %rcx
addq %rcx, %rdx
movq %rdx, 0x8(%rsp)
movq 0x20(%rsp), %r15
testq %r15, %r15
jns 0x40b76
movslq %eax, %rcx
addq %rcx, %r15
movq %r15, 0x20(%rsp)
testq %rdx, %rdx
jns 0x40b86
movq $0x0, 0x8(%rsp)
xorl %edx, %edx
cmpq %r15, %rdx
jg 0x40cd6
movslq %eax, %r14
cmpq %r14, %rdx
jge 0x40cd6
cmpq %r14, %r15
jb 0x40baa
decl %eax
movslq %eax, %r15
movq %r15, 0x20(%rsp)
cmpl $0x5, %r12d
sete %cl
movl %r15d, %r13d
movq %rdx, 0x30(%rsp)
subl %edx, %r13d
incl %r13d
movl %r13d, %eax
shll %cl, %eax
movslq %eax, %rsi
movq %rbx, %rdi
callq 0x2e168
movq (%rsp), %rcx
movl (%rcx), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x40cdf
cmpl $0x5, %eax
jne 0x40e68
movq 0x8(%rcx), %r14
movl 0x8(%rsp), %eax
addl %eax, %eax
movl %eax, %esi
xorl $-0x2, %esi
testl %ebp, %ebp
cmovel %eax, %esi
movq %r14, %rdi
callq 0x2c28b
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x40e82
movq %r14, %rdi
movq %rax, %rsi
callq 0x2c558
movq %rax, 0x10(%rsp)
testl %r13d, %r13d
je 0x40b38
notl %r15d
addl 0x30(%rsp), %r15d
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x40e3e
movq 0x10(%rsp), %r13
testq %r13, %r13
je 0x40e3e
leaq 0x40(%rsp), %rsi
leaq 0x2c(%rsp), %rdx
leaq 0x38(%rsp), %rcx
callq 0x2c5f6
testl %eax, %eax
je 0x40e21
movq 0x40(%rsp), %rsi
testq %rsi, %rsi
je 0x40c88
movl 0x2c(%rsp), %edx
movq %rbx, %rdi
callq 0x2e237
jmp 0x40c95
movq 0x38(%rsp), %rsi
movq %rbx, %rdi
callq 0x2e277
cmpl $0x5, %r12d
jne 0x40cab
movq %r13, %rdi
callq 0x3ded9
movq %rbx, %rdi
callq 0x2df9b
movq %r14, %rdi
leaq 0x18(%rsp), %rsi
leaq 0x10(%rsp), %rdx
testl %ebp, %ebp
je 0x40cc3
callq 0x3e126
jmp 0x40cc8
callq 0x3e09f
incl %r15d
jne 0x40c38
jmp 0x40b38
movq 0x68(%r13), %rsi
jmp 0x40af5
movq 0x8(%rcx), %rax
movq 0x8(%rax), %rcx
movl %r13d, %r9d
testl %ebp, %ebp
je 0x40d4b
movq 0x8(%rsp), %rax
testq %rax, %rax
jle 0x40da6
subq %rax, %r14
movq (%rcx), %r13
movslq 0x18(%rcx), %rax
xorl %ecx, %ecx
testq %rax, %rax
jle 0x40da1
decq %rax
movq %rax, %rdx
shlq $0x4, %rdx
movq %r13, %rsi
movq %rcx, %rdi
movq %rdi, %rcx
movq %rsi, %r13
movq 0x18(%rsi,%rdx), %rsi
testq %rsi, %rsi
je 0x40d44
leaq (%rdx,%r13), %rdi
addq $0x18, %rdi
movl 0x8(%rdi), %edi
addq %rcx, %rdi
cmpq %r14, %rdi
jbe 0x40d21
cmpq %r14, %rcx
jne 0x40d08
jmp 0x40db0
movq (%rcx), %r13
movq 0x8(%rsp), %rax
testq %rax, %rax
jle 0x40dac
incq %rax
movslq 0x18(%rcx), %rcx
xorl %edx, %edx
testq %rcx, %rcx
jle 0x40da1
decq %rcx
movq %rcx, %rsi
shlq $0x4, %rsi
movq %r13, %rdi
movq %rdx, %r8
movq %r8, %rdx
movq %rdi, %r13
movq 0x18(%rdi,%rsi), %rdi
testq %rdi, %rdi
je 0x40d9a
leaq (%rsi,%r13), %r8
addq $0x18, %r8
movl 0x8(%r8), %r8d
addq %rdx, %r8
cmpq %rax, %r8
jbe 0x40d76
cmpq %rax, %rdx
jne 0x40d61
jmp 0x40db0
xorl %r13d, %r13d
jmp 0x40db0
movq 0x8(%rcx), %r13
jmp 0x40db0
movq 0x18(%r13), %r13
testl %r9d, %r9d
je 0x40b38
xorl %eax, %eax
testl %ebp, %ebp
sete %al
leaq 0x10(,%rax,8), %r14
notl %r15d
addl 0x30(%rsp), %r15d
testq %r13, %r13
je 0x40e04
movq (%r13), %rsi
movq %rbx, %rdi
callq 0x2e204
cmpl $0x5, %r12d
jne 0x40df5
movsd 0x8(%r13), %xmm0
movq %rbx, %rdi
callq 0x2df9b
movq (%r13,%r14), %r13
incl %r15d
jne 0x40dd0
jmp 0x40b38
leaq 0x581eb(%rip), %rdx     # 0x98ff6
leaq 0x583ed(%rip), %rcx     # 0x991ff
movq %rbx, %rdi
movq (%rsp), %rsi
movl $0xbff, %r8d            # imm = 0xBFF
jmp 0x40e59
leaq 0x5840e(%rip), %rdx     # 0x99236
leaq 0x583d0(%rip), %rcx     # 0x991ff
movq %rbx, %rdi
movq (%rsp), %rsi
movl $0xbdc, %r8d            # imm = 0xBDC
jmp 0x40e59
leaq 0x586f0(%rip), %rdx     # 0x99535
leaq 0x583b3(%rip), %rcx     # 0x991ff
movq %rbx, %rdi
movq (%rsp), %rsi
movl $0xbdb, %r8d            # imm = 0xBDB
callq 0x4d277
movl $0x1, %edi
callq 0x1f3b0
leaq 0x56544(%rip), %rdi     # 0x973b3
leaq 0x58389(%rip), %rsi     # 0x991ff
movl $0xc07, %edx            # imm = 0xC07
callq 0x4cae0
jmp 0x40e5e
leaq 0x583ff(%rip), %rdx     # 0x99288
leaq 0x5836f(%rip), %rcx     # 0x991ff
movq %rbx, %rdi
movq (%rsp), %rsi
movl $0xbd6, %r8d            # imm = 0xBD6
jmp 0x40e59