// File: /workspace/github_c_makefile_star2_O1/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 0x80bba(%rip), %rbp     # 0xc9fa8
movq 0x508(%rbp), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x49404
movq 0x10(%rax), %r15
jmp 0x49407
xorl %r15d, %r15d
testq %rbx, %rbx
je 0x4950b
leaq 0x511ad(%rip), %r12     # 0x9a5c4
testq %r15, %r15
je 0x4944f
movq 0x8(%r15), %r13
testq %r13, %r13
je 0x4944f
cmpq %rbx, %r13
cmovaeq %rbx, %r13
movq (%r15), %rax
leaq (%r15,%rax), %rdi
addq $0x10, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x1fbd0
addq %r13, (%r15)
subq %r13, 0x8(%r15)
addq %r13, %r14
subq %r13, %rbx
testq %rbx, %rbx
je 0x49502
movl $0xa00010, %edi         # imm = 0xA00010
callq 0x29709
movq %rax, %r15
movq $0xa00000, 0x8(%rax)    # imm = 0xA00000
movq $0x0, (%rax)
movq 0x508(%rbp), %rdi
movq %rax, %rsi
callq 0x2045f
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 0x49502
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 0x494d3
movq 0x10(%rcx), %rcx
jmp 0x494d5
xorl %ecx, %ecx
testq %rcx, %rcx
je 0x494f6
leaq (%rax,%rax,4), %rax
shlq $0x15, %rax
movq (%rcx), %rcx
leaq (%rax,%rcx), %rdx
addq $-0xa00000, %rdx        # imm = 0xFF600000
shrq $0x14, %rdx
jmp 0x494f8
xorl %edx, %edx
movq %r12, %rsi
xorl %eax, %eax
callq 0x23064
testq %rbx, %rbx
jne 0x49417
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq