// File: /workspace/github_c_makefile_star2_O1/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 0x398e9
movq %rax, %r15
movl (%rbx), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x4, %eax
je 0x4a65c
cmpl $0x5, %eax
jne 0x4a71b
movq 0x8(%rbx), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x2bff5
movq %rax, %r13
leaq 0x18(%rsp), %rsi
leaq 0x4(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %rax, %rdi
callq 0x2c351
testl %eax, %eax
je 0x4a652
xorl %ebx, %ebx
leaq 0x10(%rsp), %rbp
testq %rbx, %rbx
jne 0x4a5e6
cmpq $0x40, %r15
movl $0x40, %edx
cmovlq %r15, %rdx
addl $0x2, %edx
movq %r14, %rdi
movl $0x2a, %esi
callq 0x58eae
testq %rax, %rax
je 0x4a70a
movl $0x5, %edx
movq %r14, %rdi
leaq 0x5054b(%rip), %rsi     # 0x9ab0e
callq 0x58f52
testq %rax, %rax
je 0x4a70a
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0x4a4c3
testl %eax, %eax
je 0x4a70a
movq 0x18(%rsp), %rsi
testq %rsi, %rsi
je 0x4a5fe
movl 0x4(%rsp), %edx
movq %r14, %rdi
callq 0x58f52
jmp 0x4a60b
movq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x59069
testq %rax, %rax
je 0x4a70a
movq %r12, %rdi
movq %r13, %rsi
callq 0x2c2b3
movq %rax, %r13
incq %rbx
cmpq $0x40, %rbx
movl $0x0, %eax
cmoveq %rax, %rbx
decq %r15
movq %r13, %rdi
leaq 0x18(%rsp), %rsi
leaq 0x4(%rsp), %rdx
movq %rbp, %rcx
callq 0x2c351
testl %eax, %eax
jne 0x4a589
movl $0x1, %eax
jmp 0x4a70c
movq 0x8(%rbx), %rdi
leaq 0x18(%rsp), %r12
movq %r12, %rsi
callq 0x205d2
xorl %ebp, %ebp
leaq 0x50498(%rip), %r13     # 0x9ab0e
movq %r12, %rdi
callq 0x205ef
testq %rax, %rax
je 0x4a652
movq 0x10(%rax), %rbx
testq %rbp, %rbp
je 0x4a6b2
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4a4c3
testl %eax, %eax
je 0x4a700
incq %rbp
cmpq $0x40, %rbp
movl $0x0, %eax
cmoveq %rax, %rbp
decq %r15
movb $0x1, %al
jmp 0x4a702
cmpq $0x40, %r15
movl $0x40, %edx
cmovlq %r15, %rdx
addl $0x2, %edx
movq %r14, %rdi
movl $0x2a, %esi
callq 0x58eae
testq %rax, %rax
je 0x4a6fa
movl $0x5, %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0x58f52
testq %rax, %rax
je 0x4a6fa
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0x4a4c3
testl %eax, %eax
jne 0x4a68c
xorl %eax, %eax
xorl %ebp, %ebp
jmp 0x4a702
xorl %eax, %eax
testb %al, %al
jne 0x4a676
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4e362(%rip), %rdi     # 0x98a84
leaq 0x4fee0(%rip), %rsi     # 0x9a609
movl $0x44a, %edx            # imm = 0x44A
callq 0x4d0e5
movl $0x1, %edi
callq 0x1f3b0