// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/aof.c
// Function: rewriteListObject
int rewriteListObject(rio *r, robj *key, robj *o) {
    long long count = 0, items = listTypeLength(o);

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

        // 先构建一个 RPUSH key 
        // 然后从 ZIPLIST 中取出最多 REDIS_AOF_REWRITE_ITEMS_PER_CMD 个元素
        // 之后重复第一步，直到 ZIPLIST 为空
        while(ziplistGet(p,&vstr,&vlen,&vlong)) {
            if (count == 0) {
                int cmd_items = (items > REDIS_AOF_REWRITE_ITEMS_PER_CMD) ?
                    REDIS_AOF_REWRITE_ITEMS_PER_CMD : items;

                if (rioWriteBulkCount(r,'*',2+cmd_items) == 0) return 0;
                if (rioWriteBulkString(r,"RPUSH",5) == 0) return 0;
                if (rioWriteBulkObject(r,key) == 0) return 0;
            }
            // 取出值
            if (vstr) {
                if (rioWriteBulkString(r,(char*)vstr,vlen) == 0) return 0;
            } else {
                if (rioWriteBulkLongLong(r,vlong) == 0) return 0;
            }
            // 移动指针，并计算被取出元素的数量
            p = ziplistNext(zl,p);
            if (++count == REDIS_AOF_REWRITE_ITEMS_PER_CMD) count = 0;
            items--;
        }
    } else if (o->encoding == REDIS_ENCODING_LINKEDLIST) {
        list *list = o->ptr;
        listNode *ln;
        listIter li;

        // 先构建一个 RPUSH key 
        // 然后从双端链表中取出最多 REDIS_AOF_REWRITE_ITEMS_PER_CMD 个元素
        // 之后重复第一步，直到链表为空
        listRewind(list,&li);
        while((ln = listNext(&li))) {
            robj *eleobj = listNodeValue(ln);

            if (count == 0) {
                int cmd_items = (items > REDIS_AOF_REWRITE_ITEMS_PER_CMD) ?
                    REDIS_AOF_REWRITE_ITEMS_PER_CMD : items;

                if (rioWriteBulkCount(r,'*',2+cmd_items) == 0) return 0;
                if (rioWriteBulkString(r,"RPUSH",5) == 0) return 0;
                if (rioWriteBulkObject(r,key) == 0) return 0;
            }

            // 取出值
            if (rioWriteBulkObject(r,eleobj) == 0) return 0;

            // 元素计数
            if (++count == REDIS_AOF_REWRITE_ITEMS_PER_CMD) count = 0;

            items--;
        }
    } else {
        redisPanic("Unknown list encoding");
    }
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %rbx
movq %rsi, 0x8(%rsp)
movq %rdi, %r14
movq %rdx, %rdi
callq 0x3700e
movq %rax, %r15
movl (%rbx), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x4, %eax
je 0x46ae6
cmpl $0x5, %eax
jne 0x46b92
movq 0x8(%rbx), %r12
xorl %ebx, %ebx
movq %r12, %rdi
xorl %esi, %esi
callq 0x2abfb
movq %rax, %rbp
leaq 0x10(%rsp), %r13
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
leaq 0x4(%rsp), %rdx
movq %r13, %rcx
callq 0x2aea9
testl %eax, %eax
je 0x46b80
testq %rbx, %rbx
jne 0x46a92
cmpq $0x40, %r15
pushq $0x40
popq %rdx
cmovlq %r15, %rdx
addl $0x2, %edx
movq %r14, %rdi
pushq $0x2a
popq %rsi
callq 0x54281
testq %rax, %rax
je 0x46b7c
movq %r14, %rdi
leaq 0x4a0ae(%rip), %rsi     # 0x90b1a
pushq $0x5
popq %rdx
callq 0x54345
testq %rax, %rax
je 0x46b7c
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0x46979
testl %eax, %eax
je 0x46b7c
movq 0x18(%rsp), %rsi
testq %rsi, %rsi
je 0x46aaa
movl 0x4(%rsp), %edx
movq %r14, %rdi
callq 0x54345
jmp 0x46ab7
movq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x543ad
testq %rax, %rax
je 0x46b7c
movq %r12, %rdi
movq %rbp, %rsi
callq 0x2ae0d
movq %rax, %rbp
incq %rbx
cmpq $0x40, %rbx
movl $0x0, %eax
cmoveq %rax, %rbx
decq %r15
jmp 0x46a1e
movq 0x8(%rbx), %rdi
leaq 0x18(%rsp), %rbx
movq %rbx, %rsi
callq 0x1ffac
xorl %r13d, %r13d
pushq $0x2a
popq %rbp
movq %rbx, %rdi
callq 0x1ffc6
testq %rax, %rax
je 0x46b80
movq 0x10(%rax), %r12
testq %r13, %r13
jne 0x46b58
cmpq $0x40, %r15
pushq $0x40
popq %rdx
cmovlq %r15, %rdx
addl $0x2, %edx
movq %r14, %rdi
movl %ebp, %esi
callq 0x54281
testq %rax, %rax
je 0x46b7c
movq %r14, %rdi
leaq 0x49fe0(%rip), %rsi     # 0x90b1a
pushq $0x5
popq %rdx
callq 0x54345
testq %rax, %rax
je 0x46b7c
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0x46979
testl %eax, %eax
je 0x46b7c
movq %r14, %rdi
movq %r12, %rsi
callq 0x46979
testl %eax, %eax
je 0x46b7c
incq %r13
cmpq $0x40, %r13
movl $0x0, %eax
cmoveq %rax, %r13
decq %r15
jmp 0x46afd
xorl %eax, %eax
jmp 0x46b83
pushq $0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x47f0b(%rip), %rdi     # 0x8eaa4
leaq 0x49a75(%rip), %rsi     # 0x90615
movl $0x44a, %edx            # imm = 0x44A
callq 0x490e8
pushq $0x1
popq %rdi
callq 0x1f3b0