// File: /workspace/github_c_makefile_star2_O3/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 0x313b2
testl %eax, %eax
jne 0x3a727
movq 0x38(%rbx), %rax
movq 0x18(%rax), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x313b2
testl %eax, %eax
je 0x3a736
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 0x900bb(%rip), %r13     # 0xca800
movq 0x68(%r13), %rdx
movq %rbx, %rdi
callq 0x31745
testq %rax, %rax
je 0x3a727
movq %rax, %r14
movq %rbx, %rdi
movq %rax, %rsi
movl $0x1, %edx
callq 0x30bbf
testl %eax, %eax
jne 0x3a727
movq %r14, %rdi
callq 0x39979
movq %rax, %r15
movq (%rsp), %rbp
testq %rbp, %rbp
jns 0x3a788
addq %r15, %rbp
movq %rbp, (%rsp)
movq 0x8(%rsp), %r12
testq %r12, %r12
jns 0x3a79a
addq %r15, %r12
movq %r12, 0x8(%rsp)
testq %rbp, %rbp
jns 0x3a7a9
movq $0x0, (%rsp)
xorl %ebp, %ebp
cmpq %r12, %rbp
jg 0x3a871
cmpq %r15, %rbp
jge 0x3a871
cmpq %r15, %r12
jb 0x3a7c9
leaq -0x1(%r15), %r12
movq %r12, 0x8(%rsp)
movq %r12, %r13
subq %rbp, %r13
incq %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2e168
movl (%r14), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x4, %eax
je 0x3a882
cmpl $0x5, %eax
jne 0x3a8cd
movq 0x8(%r14), %rdi
movl (%rsp), %esi
callq 0x2c28b
testq %r13, %r13
je 0x3a727
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 0x2c5f6
movq 0x20(%rsp), %rsi
testq %rsi, %rsi
je 0x3a84b
movl 0x14(%rsp), %edx
movq %rbx, %rdi
callq 0x2e237
jmp 0x3a858
movq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x2e277
movq 0x8(%r14), %rdi
movq %r15, %rsi
callq 0x2c558
movq %rax, %r15
incq %r12
jne 0x3a820
jmp 0x3a727
movq 0x68(%r13), %rsi
movq %rbx, %rdi
callq 0x2d97c
jmp 0x3a727
movq (%rsp), %rsi
movq %r15, %rax
shrq %rax
cmpq %rax, %rsi
jle 0x3a898
subq %r15, %rsi
movq %rsi, (%rsp)
movq 0x8(%r14), %rdi
callq 0x207b0
testq %r13, %r13
je 0x3a727
movq %rax, %r14
notq %r12
addq %rbp, %r12
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x2e204
movq 0x8(%r14), %r14
incq %r12
jne 0x3a8b3
jmp 0x3a727
leaq 0x5e788(%rip), %rdi     # 0x9905c
leaq 0x5e6d3(%rip), %rsi     # 0x98fae
movl $0x363, %edx            # imm = 0x363
callq 0x4cae0
movl $0x1, %edi
callq 0x1f3b0