// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/aof.c
// Function: aofRewriteBufferAppend
void aofRewriteBufferAppend(unsigned char *s, unsigned long len) {

    // 指向最后一个缓存块
    listNode *ln = listLast(server.aof_rewrite_buf_blocks);
    aofrwblock *block = ln ? ln->value : NULL;

    while(len) {
        /* If we already got at least an allocated block, try appending
         * at least some piece into it. 
         *
         * 如果已经有至少一个缓存块，那么尝试将内容追加到这个缓存块里面
         */
        if (block) {
            unsigned long thislen = (block->free < len) ? block->free : len;
            if (thislen) {  /* The current block is not already full. */
                memcpy(block->buf+block->used, s, thislen);
                block->used += thislen;
                block->free -= thislen;
                s += thislen;
                len -= thislen;
            }
        }

        // 如果 block != NULL ，那么这里是创建另一个缓存块买容纳 block 装不下的内容
        // 如果 block == NULL ，那么这里是创建缓存链表的第一个缓存块
        if (len) { /* First block to allocate, or need another block. */
            int numblocks;

            // 分配缓存块
            block = zmalloc(sizeof(*block));
            block->free = AOF_RW_BUF_BLOCK_SIZE;
            block->used = 0;

            // 链接到链表末尾
            listAddNodeTail(server.aof_rewrite_buf_blocks,block);

            /* Log every time we cross more 10 or 100 blocks, respectively
             * as a notice or warning. 
             *
             * 每次创建 10 个缓存块就打印一个日志，用作标记或者提醒
             */
            numblocks = listLength(server.aof_rewrite_buf_blocks);
            if (((numblocks+1) % 10) == 0) {
                int level = ((numblocks+1) % 100) == 0 ? REDIS_WARNING :
                                                         REDIS_NOTICE;
                redisLog(level,"Background AOF buffer size: %lu MB",
                    aofRewriteBufferSize()/(1024*1024));
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x810d6(%rip), %rbp     # 0xc9fa8
movq 0x508(%rbp), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x48ee8
movq 0x10(%rax), %r12
jmp 0x48eeb
xorl %r12d, %r12d
testq %rbx, %rbx
je 0x48fec
leaq 0x51711(%rip), %r15     # 0x9a60c
testq %r12, %r12
je 0x48f3d
movq 0x8(%r12), %r13
testq %r13, %r13
je 0x48f3d
cmpq %rbx, %r13
cmovaeq %rbx, %r13
movq (%r12), %rax
leaq (%r12,%rax), %rdi
addq $0x10, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x1fbd0
addq %r13, (%r12)
subq %r13, 0x8(%r12)
subq %r13, %rbx
je 0x48fec
addq %r13, %r14
movl $0xa00010, %edi         # imm = 0xA00010
callq 0x299b5
movq %rax, %r12
movq $0xa00000, 0x8(%rax)    # imm = 0xA00000
movq $0x0, (%rax)
movq 0x508(%rbp), %rdi
movq %rax, %rsi
callq 0x204cb
movq 0x508(%rbp), %rcx
movq 0x28(%rcx), %rax
imull $0xcccccccd, %eax, %edx # imm = 0xCCCCCCCD
addl $0xe6666665, %edx       # imm = 0xE6666665
rorl %edx
cmpl $0x19999998, %edx       # imm = 0x19999998
ja 0x48efb
leal 0x1(%rax), %edx
imull $0xc28f5c29, %edx, %edx # imm = 0xC28F5C29
addl $0x51eb850, %edx        # imm = 0x51EB850
rorl $0x2, %edx
cmpl $0x28f5c29, %edx        # imm = 0x28F5C29
movl $0x0, %edi
adcl $0x2, %edi
movq 0x8(%rcx), %rcx
testq %rcx, %rcx
je 0x48fdb
movq 0x10(%rcx), %rcx
testq %rcx, %rcx
je 0x48fdb
leaq (%rax,%rax,4), %rax
shlq $0x15, %rax
movq (%rcx), %rcx
leaq (%rax,%rcx), %rdx
addq $-0xa00000, %rdx        # imm = 0xFF600000
shrq $0x14, %rdx
jmp 0x48fdd
xorl %edx, %edx
movq %r15, %rsi
xorl %eax, %eax
callq 0x23223
jmp 0x48efb
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq