// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/t_list.c
// Function: lrangeCommand
void lrangeCommand(redisClient *c) {
    robj *o;
    long start, end, llen, rangelen;

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

    // 取出列表对象
    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk)) == NULL
         || checkType(c,o,REDIS_LIST)) return;

    // 取出列表长度
    llen = listTypeLength(o);

    /* convert negative indexes */
    // 将负数索引转换成正数索引
    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,rangelen);

    if (o->encoding == REDIS_ENCODING_ZIPLIST) {
        unsigned char *p = ziplistIndex(o->ptr,start);
        unsigned char *vstr;
        unsigned int vlen;
        long long vlong;

        // 遍历 ziplist ，并将指定索引上的值添加到回复中
        while(rangelen--) {
            ziplistGet(p,&vstr,&vlen,&vlong);
            if (vstr) {
                addReplyBulkCBuffer(c,vstr,vlen);
            } else {
                addReplyBulkLongLong(c,vlong);
            }
            p = ziplistNext(o->ptr,p);
        }

    } else if (o->encoding == REDIS_ENCODING_LINKEDLIST) {
        listNode *ln;

        /* If we are nearest to the end of the list, reach the element
         * starting from tail and going backward, as it is faster. */
        if (start > llen/2) start -= llen;
        ln = listIndex(o->ptr,start);

        // 遍历双端链表，将指定索引上的值添加到回复
        while(rangelen--) {
            addReplyBulk(c,ln->value);
            ln = ln->next;
        }

    } else {
        redisPanic("List encoding is not LINKEDLIST nor ZIPLIST!");
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movq 0x10(%rax), %rsi
movq %rsp, %rdx
xorl %ecx, %ecx
callq 0x2f672
testl %eax, %eax
jne 0x37d73
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x2f672
testl %eax, %eax
je 0x37d82
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38(%rbx), %rax
movq 0x8(%rax), %rsi
leaq 0x8fa6f(%rip), %rbp     # 0xc7800
movq 0x68(%rbp), %rdx
movq %rbx, %rdi
callq 0x2f9a9
testq %rax, %rax
je 0x37d73
movq %rax, %r14
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2eec6
testl %eax, %eax
jne 0x37d73
movq %r14, %rdi
callq 0x3700e
movq %rax, %r15
movq (%rsp), %r13
testq %r13, %r13
jns 0x37dd2
addq %r15, %r13
movq %r13, (%rsp)
movq 0x8(%rsp), %r12
testq %r12, %r12
jns 0x37de4
addq %r15, %r12
movq %r12, 0x8(%rsp)
testq %r13, %r13
jns 0x37df1
andq $0x0, (%rsp)
xorl %r13d, %r13d
cmpq %r12, %r13
jg 0x37eb1
cmpq %r15, %r13
jge 0x37eb1
cmpq %r15, %r12
jb 0x37e11
leaq -0x1(%r15), %r12
movq %r12, 0x8(%rsp)
movq %r12, %rsi
subq %r13, %rsi
incq %rsi
movq %rbx, %rdi
callq 0x2c7c7
movl (%r14), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x4, %eax
je 0x37ec2
cmpl $0x5, %eax
jne 0x37f08
movq 0x8(%r14), %rdi
movl (%rsp), %esi
callq 0x2abfb
movq %rax, %r15
notq %r12
addq %r13, %r12
leaq 0x14(%rsp), %r13
leaq 0x18(%rsp), %rbp
testq %r12, %r12
je 0x37d73
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x2aea9
movq 0x20(%rsp), %rsi
testq %rsi, %rsi
je 0x37e90
movl 0x14(%rsp), %edx
movq %rbx, %rdi
callq 0x2c87d
jmp 0x37e9d
movq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x2c8bb
movq 0x8(%r14), %rdi
movq %r15, %rsi
callq 0x2ae0d
movq %rax, %r15
incq %r12
jmp 0x37e5c
movq 0x68(%rbp), %rsi
movq %rbx, %rdi
callq 0x2c10d
jmp 0x37d73
movq (%rsp), %rsi
movq %r15, %rax
shrq %rax
cmpq %rax, %rsi
jle 0x37ed8
subq %r15, %rsi
movq %rsi, (%rsp)
movq 0x8(%r14), %rdi
callq 0x200c3
movq %rax, %r14
notq %r12
addq %r13, %r12
testq %r12, %r12
je 0x37d73
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x2c84a
movq 0x8(%r14), %r14
incq %r12
jmp 0x37eea
leaq 0x57111(%rip), %rdi     # 0x8f020
leaq 0x5705c(%rip), %rsi     # 0x8ef72
movl $0x363, %edx            # imm = 0x363
callq 0x490e8
pushq $0x1
popq %rdi
callq 0x1f3b0