// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x10(%rax), %rsi
leaq -0x30(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x3b960
cmpl $0x0, %eax
jne 0x530ad
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x18(%rax), %rsi
leaq -0x38(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x3b960
cmpl $0x0, %eax
je 0x530b2
jmp 0x53561
movq -0x8(%rbp), %rax
cmpl $0x5, 0x30(%rax)
jne 0x530e6
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x904e6(%rip), %rsi     # 0xe35b9
callq 0x1f2f0
cmpl $0x0, %eax
jne 0x530e6
movl $0x1, -0x24(%rbp)
jmp 0x5310e
movq -0x8(%rbp), %rax
cmpl $0x5, 0x30(%rax)
jl 0x5310c
movq -0x8(%rbp), %rdi
leaq 0xc1055(%rip), %rax     # 0x114150
movq 0x80(%rax), %rsi
callq 0x35a70
jmp 0x53561
jmp 0x5310e
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq 0xc1033(%rip), %rax     # 0x114150
movq 0x68(%rax), %rdx
callq 0x3c0c0
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
je 0x53147
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x3, %edx
callq 0x3abb0
cmpl $0x0, %eax
je 0x5314c
jmp 0x53561
movq -0x20(%rbp), %rdi
callq 0x50080
movl %eax, -0x3c(%rbp)
cmpq $0x0, -0x30(%rbp)
jge 0x5316b
movslq -0x3c(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x38(%rbp)
jge 0x5317e
movslq -0x3c(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x30(%rbp)
jge 0x5318d
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
jg 0x531a4
movq -0x30(%rbp), %rax
movslq -0x3c(%rbp), %rcx
cmpq %rcx, %rax
jl 0x531bd
movq -0x8(%rbp), %rdi
leaq 0xc0fa1(%rip), %rax     # 0x114150
movq 0x68(%rax), %rsi
callq 0x35a70
jmp 0x53561
movq -0x38(%rbp), %rax
movslq -0x3c(%rbp), %rcx
cmpq %rcx, %rax
jl 0x531d6
movl -0x3c(%rbp), %eax
subl $0x1, %eax
cltq
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
subq -0x30(%rbp), %rax
addq $0x1, %rax
movl %eax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x98(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x53203
movl -0x40(%rbp), %eax
shll %eax
movl %eax, -0x9c(%rbp)
jmp 0x5320c
movl -0x40(%rbp), %eax
movl %eax, -0x9c(%rbp)
movq -0x98(%rbp), %rdi
movl -0x9c(%rbp), %eax
movslq %eax, %rsi
callq 0x364f0
movq -0x20(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x5, %eax
jne 0x533dd
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
cmpl $0x0, -0xc(%rbp)
je 0x5326a
movq -0x48(%rbp), %rdi
movq -0x30(%rbp), %rcx
shlq %rcx
movq $-0x2, %rax
subq %rcx, %rax
movl %eax, %esi
callq 0x32e50
movq %rax, -0x50(%rbp)
jmp 0x53280
movq -0x48(%rbp), %rdi
movq -0x30(%rbp), %rax
shlq %rax
movl %eax, %esi
callq 0x32e50
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x53289
jmp 0x532b4
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
leaq 0x9007f(%rip), %rdx     # 0xe3317
leaq 0x8ffef(%rip), %rcx     # 0xe328e
movl $0xbd6, %r8d            # imm = 0xBD6
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x33330
movq %rax, -0x58(%rbp)
movl -0x40(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x40(%rbp)
cmpl $0x0, %eax
je 0x533d8
cmpq $0x0, -0x50(%rbp)
je 0x532e9
cmpq $0x0, -0x58(%rbp)
je 0x532e9
jmp 0x53314
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
leaq 0x902cc(%rip), %rdx     # 0xe35c4
leaq 0x8ff8f(%rip), %rcx     # 0xe328e
movl $0xbdb, %r8d            # imm = 0xBDB
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
movq -0x50(%rbp), %rdi
leaq -0x60(%rbp), %rsi
leaq -0x64(%rbp), %rdx
leaq -0x70(%rbp), %rcx
callq 0x33490
cmpl $0x0, %eax
je 0x53330
jmp 0x5335b
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
leaq 0x8ff86(%rip), %rdx     # 0xe32c5
leaq 0x8ff48(%rip), %rcx     # 0xe328e
movl $0xbdc, %r8d            # imm = 0xBDC
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
cmpq $0x0, -0x60(%rbp)
jne 0x53371
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x366c0
jmp 0x53383
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
movl -0x64(%rbp), %eax
movl %eax, %edx
callq 0x36670
cmpl $0x0, -0x24(%rbp)
je 0x533a9
movq -0x8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x58(%rbp), %rdi
callq 0x4ee80
movq -0xa8(%rbp), %rdi
callq 0x36230
cmpl $0x0, -0xc(%rbp)
je 0x533c2
movq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
leaq -0x58(%rbp), %rdx
callq 0x4f1d0
jmp 0x533d3
movq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
leaq -0x58(%rbp), %rdx
callq 0x4f100
jmp 0x532c5
jmp 0x53561
movq -0x20(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x7, %eax
jne 0x5353d
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x80(%rbp)
cmpl $0x0, -0xc(%rbp)
je 0x53440
movq -0x80(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x30(%rbp)
jle 0x5343e
movq -0x80(%rbp), %rdi
movslq -0x3c(%rbp), %rsi
subq -0x30(%rbp), %rsi
callq 0x4e7c0
movq %rax, -0x88(%rbp)
jmp 0x53473
movq -0x80(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x30(%rbp)
jle 0x53471
movq -0x80(%rbp), %rdi
movq -0x30(%rbp), %rsi
addq $0x1, %rsi
callq 0x4e7c0
movq %rax, -0x88(%rbp)
jmp 0x53473
jmp 0x53475
movl -0x40(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x40(%rbp)
cmpl $0x0, %eax
je 0x5353b
cmpq $0x0, -0x88(%rbp)
je 0x53495
jmp 0x534c0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
leaq 0x8fbd5(%rip), %rdx     # 0xe3079
leaq 0x8fde3(%rip), %rcx     # 0xe328e
movl $0xbff, %r8d            # imm = 0xBFF
callq 0x648b0
movl $0x1, %edi
callq 0x1f3b0
movq -0x88(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x8(%rbp), %rdi
movq -0x90(%rbp), %rsi
callq 0x36620
cmpl $0x0, -0x24(%rbp)
je 0x534fc
movq -0x8(%rbp), %rdi
movq -0x88(%rbp), %rax
movsd 0x8(%rax), %xmm0
callq 0x36230
cmpl $0x0, -0xc(%rbp)
je 0x53516
movq -0x88(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xb0(%rbp)
jmp 0x53528
movq -0x88(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x53475
jmp 0x5355f
leaq 0x8df57(%rip), %rdi     # 0xe149b
leaq 0x8fd43(%rip), %rsi     # 0xe328e
movl $0xc07, %edx            # imm = 0xC07
callq 0x63d40
movl $0x1, %edi
callq 0x1f3b0
jmp 0x53561
addq $0xb0, %rsp
popq %rbp
retq
nopw (%rax,%rax)