// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0xb5091(%rip), %rax     # 0x1138f8
movq 0x508(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x5e88b
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x5e893
xorl %eax, %eax
movq %rax, -0x38(%rbp)
jmp 0x5e893
movq -0x38(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x5e9f8
cmpq $0x0, -0x20(%rbp)
je 0x5e93e
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
cmpq -0x10(%rbp), %rax
jae 0x5e8cd
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x5e8d5
movq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x5e93c
movq -0x20(%rbp), %rdi
addq $0x10, %rdi
movq -0x20(%rbp), %rax
addq (%rax), %rdi
movq -0x8(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x1fbf0
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0x28(%rbp), %rdx
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
addq -0x8(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x10(%rbp)
jmp 0x5e93e
cmpq $0x0, -0x10(%rbp)
je 0x5e9f3
movl $0xa00010, %edi         # imm = 0xA00010
callq 0x2e130
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq $0xa00000, 0x8(%rax)    # imm = 0xA00000
movq -0x20(%rbp), %rax
movq $0x0, (%rax)
leaq 0xb4f83(%rip), %rax     # 0x1138f8
movq 0x508(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x1ff90
leaq 0xb4f6c(%rip), %rax     # 0x1138f8
movq 0x508(%rax), %rax
movq 0x28(%rax), %rax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl $0xa, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x5e9f1
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl $0x64, %ecx
cltd
idivl %ecx
movl $0x2, %eax
movl $0x3, %ecx
cmpl $0x0, %edx
cmovel %ecx, %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
movl %eax, -0x44(%rbp)
callq 0x5e7c0
movl -0x44(%rbp), %edi
movq %rax, %rdx
shrq $0x14, %rdx
leaq 0x85ca6(%rip), %rsi     # 0xe4690
movb $0x0, %al
callq 0x24fb0
jmp 0x5e9f3
jmp 0x5e89b
addq $0x50, %rsp
popq %rbp
retq
nop