// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0x480f0
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x5, %eax
jne 0x6027b
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
xorl %esi, %esi
callq 0x32e50
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rsi
leaq -0x4c(%rbp), %rdx
leaq -0x58(%rbp), %rcx
callq 0x33490
cmpl $0x0, %eax
je 0x60276
cmpq $0x0, -0x28(%rbp)
jne 0x601ec
cmpq $0x40, -0x30(%rbp)
jle 0x6016a
movl $0x40, %eax
movq %rax, -0x98(%rbp)
jmp 0x60175
movq -0x30(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movl %eax, -0x5c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x5c(%rbp), %edx
addl $0x2, %edx
movl $0x2a, %esi
callq 0x75eb0
cmpq $0x0, %rax
jne 0x601a5
movl $0x0, -0x4(%rbp)
jmp 0x603ea
movq -0x10(%rbp), %rdi
leaq 0x84a2a(%rip), %rsi     # 0xe4bda
movl $0x5, %edx
callq 0x76050
cmpq $0x0, %rax
jne 0x601cc
movl $0x0, -0x4(%rbp)
jmp 0x603ea
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x60010
cmpl $0x0, %eax
jne 0x601ea
movl $0x0, -0x4(%rbp)
jmp 0x603ea
jmp 0x601ec
cmpq $0x0, -0x48(%rbp)
je 0x60219
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl -0x4c(%rbp), %eax
movl %eax, %edx
callq 0x76050
cmpq $0x0, %rax
jne 0x60217
movl $0x0, -0x4(%rbp)
jmp 0x603ea
jmp 0x6023a
movq -0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x76100
cmpq $0x0, %rax
jne 0x60238
movl $0x0, -0x4(%rbp)
jmp 0x603ea
jmp 0x6023a
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x33330
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
cmpq $0x40, %rax
jne 0x60265
movq $0x0, -0x28(%rbp)
movq -0x30(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x6012c
jmp 0x603e3
movq -0x20(%rbp), %rax
movl (%rax), %eax
shrl $0x4, %eax
andl $0xf, %eax
cmpl $0x4, %eax
jne 0x603bf
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x202b0
leaq -0x80(%rbp), %rdi
callq 0x20310
movq %rax, -0x70(%rbp)
cmpq $0x0, %rax
je 0x603bd
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x60374
cmpq $0x40, -0x30(%rbp)
jle 0x602ef
movl $0x40, %eax
movq %rax, -0xa0(%rbp)
jmp 0x602fa
movq -0x30(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movl %eax, -0x8c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x8c(%rbp), %edx
addl $0x2, %edx
movl $0x2a, %esi
callq 0x75eb0
cmpq $0x0, %rax
jne 0x60330
movl $0x0, -0x4(%rbp)
jmp 0x603ea
movq -0x10(%rbp), %rdi
leaq 0x8489f(%rip), %rsi     # 0xe4bda
movl $0x5, %edx
callq 0x76050
cmpq $0x0, %rax
jne 0x60357
movl $0x0, -0x4(%rbp)
jmp 0x603ea
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x60010
cmpl $0x0, %eax
jne 0x60372
movl $0x0, -0x4(%rbp)
jmp 0x603ea
jmp 0x60374
movq -0x10(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x60010
cmpl $0x0, %eax
jne 0x60392
movl $0x0, -0x4(%rbp)
jmp 0x603ea
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
cmpq $0x40, %rax
jne 0x603ac
movq $0x0, -0x28(%rbp)
movq -0x30(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x602a9
jmp 0x603e1
leaq 0x82786(%rip), %rdi     # 0xe2b4c
leaq 0x84308(%rip), %rsi     # 0xe46d5
movl $0x44a, %edx            # imm = 0x44A
callq 0x63d40
movl $0x1, %edi
callq 0x1f3b0
jmp 0x603e3
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)