// File: /workspace/github_c_makefile_star2_O3/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 0x39979
movq %rax, %r15
movl (%rbx), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x4, %eax
je 0x4a0ce
cmpl $0x5, %eax
jne 0x4a196
movq 0x8(%rbx), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x2c28b
movq %rax, %r13
leaq 0x18(%rsp), %rsi
leaq 0x4(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %rax, %rdi
callq 0x2c5f6
testl %eax, %eax
je 0x4a17e
xorl %ebx, %ebx
leaq 0x10(%rsp), %rbp
testq %rbx, %rbx
jne 0x4a05d
cmpq $0x40, %r15
movl $0x40, %edx
cmovlq %r15, %rdx
addl $0x2, %edx
movq %r14, %rdi
movl $0x2a, %esi
callq 0x5891c
testq %rax, %rax
je 0x4a185
movl $0x5, %edx
movq %r14, %rdi
leaq 0x50b1c(%rip), %rsi     # 0x9ab56
callq 0x589bc
testq %rax, %rax
je 0x4a185
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0x49f3a
testl %eax, %eax
je 0x4a185
movq 0x18(%rsp), %rsi
testq %rsi, %rsi
je 0x4a075
movl 0x4(%rsp), %edx
movq %r14, %rdi
callq 0x589bc
jmp 0x4a082
movq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x58ab3
testq %rax, %rax
je 0x4a185
movq %r12, %rdi
movq %r13, %rsi
callq 0x2c558
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 0x2c5f6
testl %eax, %eax
jne 0x4a000
jmp 0x4a17e
movq 0x8(%rbx), %rdi
leaq 0x18(%rsp), %rbx
movq %rbx, %rsi
callq 0x20637
movq %rbx, %rdi
callq 0x20654
testq %rax, %rax
je 0x4a17e
xorl %ebp, %ebp
leaq 0x50a5d(%rip), %rbx     # 0x9ab56
leaq 0x18(%rsp), %r12
movq 0x10(%rax), %r13
testq %rbp, %rbp
jne 0x4a14f
cmpq $0x40, %r15
movl $0x40, %edx
cmovlq %r15, %rdx
addl $0x2, %edx
movq %r14, %rdi
movl $0x2a, %esi
callq 0x5891c
testq %rax, %rax
je 0x4a185
movl $0x5, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x589bc
testq %rax, %rax
je 0x4a185
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0x49f3a
testl %eax, %eax
je 0x4a185
movq %r14, %rdi
movq %r13, %rsi
callq 0x49f3a
testl %eax, %eax
je 0x4a185
incq %rbp
cmpq $0x40, %rbp
movl $0x0, %eax
cmoveq %rax, %rbp
decq %r15
movq %r12, %rdi
callq 0x20654
testq %rax, %rax
jne 0x4a0fe
movl $0x1, %eax
jmp 0x4a187
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4e943(%rip), %rdi     # 0x98ae0
leaq 0x504ad(%rip), %rsi     # 0x9a651
movl $0x44a, %edx            # imm = 0x44A
callq 0x4cae0
movl $0x1, %edi
callq 0x1f3b0