// File: /workspace/github_c_makefile_star2_O2/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 0x10(%rsp), %rdx
xorl %ecx, %ecx
callq 0x2f672
testl %eax, %eax
jne 0x3db2e
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
leaq 0x28(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x2f672
testl %eax, %eax
jne 0x3db2e
movl 0x30(%rbx), %r12d
cmpl $0x5, %r12d
jne 0x3dafc
movq 0x38(%rbx), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x51a13(%rip), %rsi     # 0x8f4ee
callq 0x1f2e0
testl %eax, %eax
je 0x3dafe
leaq 0x89d15(%rip), %rax     # 0xc7800
movq 0x80(%rax), %rsi
movq %rbx, %rdi
callq 0x2c10d
jmp 0x3db2e
jge 0x3dae4
leaq 0x89cfb(%rip), %r15     # 0xc7800
movq 0x68(%r15), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2f9a9
testq %rax, %rax
je 0x3db2e
movq %rax, %r14
pushq $0x3
popq %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2eec6
testl %eax, %eax
je 0x3db3d
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, 0x8(%rsp)
movq %r14, %rdi
callq 0x3bc1e
movq 0x10(%rsp), %r13
movslq %eax, %rcx
testq %r13, %r13
jns 0x3db5f
addq %rcx, %r13
movq %r13, 0x10(%rsp)
movq 0x28(%rsp), %r14
testq %r14, %r14
jns 0x3db71
addq %rcx, %r14
movq %r14, 0x28(%rsp)
testq %r13, %r13
jns 0x3db7f
andq $0x0, 0x10(%rsp)
xorl %r13d, %r13d
cmpq %r14, %r13
jg 0x3dcc7
cmpq %rcx, %r13
jge 0x3dcc7
movq %rcx, %r15
cmpq %rcx, %r14
jb 0x3dba3
decl %eax
movslq %eax, %r14
movq %r14, 0x28(%rsp)
cmpl $0x5, %r12d
sete %cl
movl %r14d, %eax
subl %r13d, %eax
incl %eax
shll %cl, %eax
movslq %eax, %rsi
movq %rbx, %rdi
callq 0x2c7c7
movq 0x8(%rsp), %rcx
movl (%rcx), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x3dcd0
cmpl $0x5, %eax
jne 0x3ddc0
movq 0x8(%rsp), %rax
movq 0x8(%rax), %r15
movl 0x10(%rsp), %eax
addl %eax, %eax
movl %eax, %esi
xorl $-0x2, %esi
testl %ebp, %ebp
cmovel %eax, %esi
movq %r15, %rdi
callq 0x2abfb
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x3ddda
movq %r15, %rdi
movq %rax, %rsi
callq 0x2ae0d
leaq 0x18(%rsp), %rcx
movq %rax, (%rcx)
notl %r14d
addl %r13d, %r14d
testl %r14d, %r14d
je 0x3db2e
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x3dd97
movq 0x18(%rsp), %r13
testq %r13, %r13
je 0x3dd97
leaq 0x40(%rsp), %rsi
leaq 0x34(%rsp), %rdx
leaq 0x38(%rsp), %rcx
callq 0x2aea9
testl %eax, %eax
je 0x3dd79
movq 0x40(%rsp), %rsi
testq %rsi, %rsi
je 0x3dc7f
movl 0x34(%rsp), %edx
movq %rbx, %rdi
callq 0x2c87d
jmp 0x3dc8c
movq 0x38(%rsp), %rsi
movq %rbx, %rdi
callq 0x2c8bb
cmpl $0x5, %r12d
jne 0x3dca2
movq %r13, %rdi
callq 0x3b09e
movq %rbx, %rdi
callq 0x2c607
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
leaq 0x18(%rsp), %rdx
testl %ebp, %ebp
je 0x3dcba
callq 0x3b2e1
jmp 0x3dcbf
callq 0x3b25c
incl %r14d
jmp 0x3dc26
movq 0x68(%r15), %rsi
jmp 0x3daf2
movq 0x8(%rcx), %rax
movq 0x8(%rax), %rdi
testl %ebp, %ebp
je 0x3dcf0
movq 0x10(%rsp), %rax
testq %rax, %rax
jle 0x3dd0a
movq %r15, %rsi
subq %rax, %rsi
pushq $0x10
jmp 0x3dcff
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
jle 0x3dd12
incq %rsi
pushq $0x18
popq %rbp
callq 0x3adcb
movq %rax, %r15
jmp 0x3dd1c
movq 0x8(%rdi), %r15
pushq $0x10
jmp 0x3dd1b
movq (%rdi), %rax
movq 0x18(%rax), %r15
pushq $0x18
popq %rbp
notl %r14d
addl %r13d, %r14d
movq 0x8(%rsp), %r13
testl %r14d, %r14d
je 0x3db2e
testq %r15, %r15
je 0x3dd5d
movq (%r15), %rsi
movq %rbx, %rdi
callq 0x2c84a
cmpl $0x5, %r12d
jne 0x3dd54
movsd 0x8(%r15), %xmm0
movq %rbx, %rdi
callq 0x2c607
movq (%r15,%rbp), %r15
incl %r14d
jmp 0x3dd27
leaq 0x51256(%rip), %rdx     # 0x8efba
leaq 0x51458(%rip), %rcx     # 0x8f1c3
movq %rbx, %rdi
movq %r13, %rsi
movl $0xbff, %r8d            # imm = 0xBFF
jmp 0x3ddb3
leaq 0x5147a(%rip), %rdx     # 0x8f1fa
leaq 0x5143c(%rip), %rcx     # 0x8f1c3
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movl $0xbdc, %r8d            # imm = 0xBDC
jmp 0x3ddb3
leaq 0x5175b(%rip), %rdx     # 0x8f4f9
leaq 0x5141e(%rip), %rcx     # 0x8f1c3
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movl $0xbdb, %r8d            # imm = 0xBDB
callq 0x4980c
pushq $0x1
popq %rdi
callq 0x1f3b0
leaq 0x4f5e0(%rip), %rdi     # 0x8d3a7
leaq 0x513f5(%rip), %rsi     # 0x8f1c3
movl $0xc07, %edx            # imm = 0xC07
callq 0x490e8
jmp 0x3ddb8
leaq 0x5146b(%rip), %rdx     # 0x8f24c
leaq 0x513db(%rip), %rcx     # 0x8f1c3
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
movl $0xbd6, %r8d            # imm = 0xBD6
jmp 0x3ddb3