// File: /workspace/github_c_makefile_star2_O1/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 0x310df
testl %eax, %eax
jne 0x3a6a0
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x310df
testl %eax, %eax
je 0x3a6af
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 0x90142(%rip), %r12     # 0xca800
movq 0x68(%r12), %rdx
movq %rbx, %rdi
callq 0x31475
testq %rax, %rax
je 0x3a6a0
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movl $0x1, %edx
callq 0x308e7
testl %eax, %eax
jne 0x3a6a0
movq %r14, %rdi
callq 0x398e9
movq %rax, %r15
movq (%rsp), %rax
testq %rax, %rax
jns 0x3a702
addq %r15, %rax
movq %rax, (%rsp)
movq 0x8(%rsp), %rax
testq %rax, %rax
jns 0x3a714
addq %r15, %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, (%rsp)
jns 0x3a723
movq $0x0, (%rsp)
movq (%rsp), %rbp
movq 0x8(%rsp), %rax
cmpq %rax, %rbp
jg 0x3a7f9
cmpq %r15, %rbp
jge 0x3a7f9
cmpq %r15, %rax
jl 0x3a74c
leaq -0x1(%r15), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %r12
movq %r12, %r13
subq %rbp, %r13
incq %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2ded8
movl (%r14), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x4, %eax
je 0x3a80b
cmpl $0x5, %eax
jne 0x3a861
movq 0x8(%r14), %rdi
movl (%rsp), %esi
callq 0x2bff5
testq %r13, %r13
je 0x3a6a0
movq %rax, %r15
notq %r12
addq %rbp, %r12
leaq 0x14(%rsp), %r13
leaq 0x18(%rsp), %rbp
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
movq %r13, %rdx
movq %rbp, %rcx
callq 0x2c351
movq 0x20(%rsp), %rsi
testq %rsi, %rsi
je 0x3a7d3
movl 0x14(%rsp), %edx
movq %rbx, %rdi
callq 0x2dfa7
jmp 0x3a7e0
movq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x2dfe7
movq 0x8(%r14), %rdi
movq %r15, %rsi
callq 0x2c2b3
movq %rax, %r15
incq %r12
jne 0x3a7a8
jmp 0x3a6a0
movq 0x68(%r12), %rsi
movq %rbx, %rdi
callq 0x2d6f0
jmp 0x3a6a0
movq (%rsp), %rax
movq %r15, %rcx
shrq $0x3f, %rcx
addq %r15, %rcx
sarq %rcx
cmpq %rcx, %rax
jle 0x3a828
subq %r15, %rax
movq %rax, (%rsp)
movq 0x8(%r14), %rdi
movq (%rsp), %rsi
callq 0x20750
testq %r13, %r13
je 0x3a6a0
movq %rax, %r14
notq %r12
addq %rbp, %r12
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x2df74
movq 0x8(%r14), %r14
incq %r12
jne 0x3a847
jmp 0x3a6a0
leaq 0x5e7ab(%rip), %rdi     # 0x99013
leaq 0x5e6f6(%rip), %rsi     # 0x98f65
movl $0x363, %edx            # imm = 0x363
callq 0x4d0e5
movl $0x1, %edi
callq 0x1f3b0