// File: /workspace/github_c_makefile_star2_O1/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
movq %rsp, %rdx
xorl %ecx, %ecx
callq 0x310df
testl %eax, %eax
jne 0x40ca3
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x310df
testl %eax, %eax
jne 0x40ca3
movl 0x30(%rbx), %r15d
cmpl $0x5, %r15d
jne 0x40aa7
movq 0x38(%rbx), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x58a43(%rip), %rsi     # 0x994e1
callq 0x1f2e0
testl %eax, %eax
je 0x40ac2
movb $0x1, %al
cmpl $0x5, %r15d
jl 0x40ac4
leaq 0x89d4a(%rip), %rax     # 0xca800
movq 0x80(%rax), %rsi
jmp 0x40c9b
xorl %eax, %eax
movl %eax, 0xc(%rsp)
leaq 0x89d31(%rip), %r15     # 0xca800
movq 0x68(%r15), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x31475
testq %rax, %rax
je 0x40ca3
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movl $0x3, %edx
callq 0x308e7
testl %eax, %eax
jne 0x40ca3
movq %r14, %rdi
callq 0x3ea49
movq (%rsp), %rcx
testq %rcx, %rcx
jns 0x40b1d
movslq %eax, %rdx
addq %rdx, %rcx
movq %rcx, (%rsp)
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
jns 0x40b32
movslq %eax, %rdx
addq %rdx, %rcx
movq %rcx, 0x10(%rsp)
cmpq $0x0, (%rsp)
jns 0x40b41
movq $0x0, (%rsp)
movq (%rsp), %r13
movq 0x10(%rsp), %rcx
cmpq %rcx, %r13
jg 0x40c97
movslq %eax, %r12
cmpq %r12, %r13
jge 0x40c97
cmpq %r12, %rcx
jl 0x40b6d
decl %eax
cltq
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %r15
movl %r15d, %edx
subl %r13d, %edx
incl %edx
movl 0xc(%rsp), %eax
movl %eax, %ecx
xorb $0x1, %cl
movl %edx, 0x1c(%rsp)
movl %edx, %eax
shll %cl, %eax
movslq %eax, %rsi
movq %rbx, %rdi
callq 0x2ded8
movl (%r14), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
je 0x40cb2
cmpl $0x5, %eax
jne 0x40e37
movq 0x8(%r14), %r12
movl (%rsp), %eax
addl %eax, %eax
movl %eax, %esi
xorl $-0x2, %esi
testl %ebp, %ebp
cmovel %eax, %esi
movq %r12, %rdi
callq 0x2bff5
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x40e51
movq %r12, %rdi
movq %rax, %rsi
callq 0x2c2b3
movq %rax, 0x20(%rsp)
cmpl $0x0, 0x1c(%rsp)
je 0x40ca3
notl %r15d
addl %r13d, %r15d
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x40e0e
movq 0x20(%rsp), %r13
testq %r13, %r13
je 0x40e0e
leaq 0x40(%rsp), %rsi
leaq 0x34(%rsp), %rdx
leaq 0x38(%rsp), %rcx
callq 0x2c351
testl %eax, %eax
je 0x40df2
movq 0x40(%rsp), %rsi
testq %rsi, %rsi
je 0x40c4b
movl 0x34(%rsp), %edx
movq %rbx, %rdi
callq 0x2dfa7
jmp 0x40c58
movq 0x38(%rsp), %rsi
movq %rbx, %rdi
callq 0x2dfe7
cmpb $0x0, 0xc(%rsp)
jne 0x40c6f
movq %r13, %rdi
callq 0x3ddbd
movq %rbx, %rdi
callq 0x2dd0b
testl %ebp, %ebp
movq %r12, %rdi
leaq 0x28(%rsp), %rsi
leaq 0x20(%rsp), %rdx
je 0x40c87
callq 0x3e004
jmp 0x40c8c
callq 0x3df7d
incl %r15d
jne 0x40bfb
jmp 0x40ca3
movq 0x68(%r15), %rsi
movq %rbx, %rdi
callq 0x2d6f0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r14), %rax
movq 0x8(%rax), %rcx
movq %r13, %r9
testl %ebp, %ebp
je 0x40d1d
movq (%rsp), %rax
testq %rax, %rax
jle 0x40d77
subq %rax, %r12
movq (%rcx), %r13
movslq 0x18(%rcx), %rax
xorl %ecx, %ecx
testq %rax, %rax
jle 0x40d72
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 0x40d16
leaq (%rdx,%r13), %rdi
addq $0x18, %rdi
movl 0x8(%rdi), %edi
addq %rcx, %rdi
cmpq %r12, %rdi
jbe 0x40cf3
cmpq %r12, %rcx
jne 0x40cda
jmp 0x40d81
movq (%rcx), %r13
movq (%rsp), %rax
testq %rax, %rax
jle 0x40d7d
incq %rax
movslq 0x18(%rcx), %rcx
xorl %edx, %edx
testq %rcx, %rcx
jle 0x40d72
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 0x40d6b
leaq (%rsi,%r13), %r8
addq $0x18, %r8
movl 0x8(%r8), %r8d
addq %rdx, %r8
cmpq %rax, %r8
jbe 0x40d47
cmpq %rax, %rdx
jne 0x40d32
jmp 0x40d81
xorl %r13d, %r13d
jmp 0x40d81
movq 0x8(%rcx), %r13
jmp 0x40d81
movq 0x18(%r13), %r13
cmpl $0x0, 0x1c(%rsp)
je 0x40ca3
xorl %eax, %eax
testl %ebp, %ebp
sete %al
leaq 0x10(,%rax,8), %r12
notl %r15d
addl %r9d, %r15d
testq %r13, %r13
je 0x40dd6
movq (%r13), %rsi
movq %rbx, %rdi
callq 0x2df74
cmpb $0x0, 0xc(%rsp)
jne 0x40dc7
movsd 0x8(%r13), %xmm0
movq %rbx, %rdi
callq 0x2dd0b
movq (%r13,%r12), %r13
incl %r15d
jne 0x40da1
jmp 0x40ca3
leaq 0x581d0(%rip), %rdx     # 0x98fad
leaq 0x583d2(%rip), %rcx     # 0x991b6
movq %rbx, %rdi
movq %r14, %rsi
movl $0xbff, %r8d            # imm = 0xBFF
jmp 0x40e28
leaq 0x583f4(%rip), %rdx     # 0x991ed
leaq 0x583b6(%rip), %rcx     # 0x991b6
movq %rbx, %rdi
movq %r14, %rsi
movl $0xbdc, %r8d            # imm = 0xBDC
jmp 0x40e28
leaq 0x586d7(%rip), %rdx     # 0x994ec
leaq 0x5839a(%rip), %rcx     # 0x991b6
movq %rbx, %rdi
movq %r14, %rsi
movl $0xbdb, %r8d            # imm = 0xBDB
callq 0x4d872
movl $0x1, %edi
callq 0x1f3b0
leaq 0x56515(%rip), %rdi     # 0x97353
leaq 0x58371(%rip), %rsi     # 0x991b6
movl $0xc07, %edx            # imm = 0xC07
callq 0x4d0e5
jmp 0x40e2d
leaq 0x583e7(%rip), %rdx     # 0x9923f
leaq 0x58357(%rip), %rcx     # 0x991b6
movq %rbx, %rdi
movq %r14, %rsi
movl $0xbd6, %r8d            # imm = 0xBD6
jmp 0x40e28