// File: /workspace/github_c_makefile_star2_O0/nilyang[P]redis-tdd-annotation/src/aof.c
// Function: rewriteAppendOnlyFileBackground
int rewriteAppendOnlyFileBackground(void) {
    pid_t childpid;
    long long start;

    // 已经有进程在进行 AOF 重写了
    if (server.aof_child_pid != -1) return REDIS_ERR;

    // 记录 fork 开始前的时间，计算 fork 耗时用
    start = ustime();

    if ((childpid = fork()) == 0) {
        char tmpfile[256];

        /* Child */

        // 关闭网络连接 fd
        closeListeningSockets(0);

        // 为进程设置名字，方便记认
        redisSetProcTitle("redis-aof-rewrite");

        // 创建临时文件，并进行 AOF 重写
        snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) getpid());
        if (rewriteAppendOnlyFile(tmpfile) == REDIS_OK) {
            size_t private_dirty = zmalloc_get_private_dirty();

            if (private_dirty) {
                redisLog(REDIS_NOTICE,
                    "AOF rewrite: %zu MB of memory used by copy-on-write",
                    private_dirty/(1024*1024));
            }
            // 发送重写成功信号
            exitFromChild(0);
        } else {
            // 发送重写失败信号
            exitFromChild(1);
        }
    } else {
        /* Parent */
        // 记录执行 fork 所消耗的时间
        server.stat_fork_time = ustime()-start;

        if (childpid == -1) {
            redisLog(REDIS_WARNING,
                "Can't rewrite append only file in background: fork: %s",
                strerror(errno));
            return REDIS_ERR;
        }

        redisLog(REDIS_NOTICE,
            "Background append only file rewriting started by pid %d",childpid);

        // 记录 AOF 重写的信息
        server.aof_rewrite_scheduled = 0;
        server.aof_rewrite_time_start = time(NULL);
        server.aof_child_pid = childpid;

        // 关闭字典自动 rehash
        updateDictResizePolicy();

        /* We set appendseldb to -1 in order to force the next call to the
         * feedAppendOnlyFile() to issue a SELECT command, so the differences
         * accumulated by the parent into server.aof_rewrite_buf will start
         * with a SELECT statement and it will be safe to merge. 
         *
         * 将 aof_selected_db 设为 -1 ，
         * 强制让 feedAppendOnlyFile() 下次执行时引发一个 SELECT 命令，
         * 从而确保之后新添加的命令会设置到正确的数据库中
         */
        server.aof_selected_db = -1;
        replicationScriptCacheFlush();
        return REDIS_OK;
    }
    return REDIS_OK; /* unreached */
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
leaq 0xb4656(%rip), %rax     # 0x1138f8
cmpl $-0x1, 0x504(%rax)
je 0x5f2b7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5f437
callq 0x252a0
movq %rax, -0x10(%rbp)
callq 0x1fa40
movl %eax, -0x8(%rbp)
cmpl $0x0, %eax
jne 0x5f370
xorl %edi, %edi
callq 0x298b0
leaq 0x85a4f(%rip), %rdi     # 0xe4d2e
callq 0x2ba60
leaq -0x110(%rbp), %rax
movq %rax, -0x120(%rbp)
callq 0x1f410
movq -0x120(%rbp), %rdi
movl %eax, %ecx
movl $0x100, %esi            # imm = 0x100
leaq 0x85a34(%rip), %rdx     # 0xe4d40
movb $0x0, %al
callq 0x1f120
leaq -0x110(%rbp), %rdi
callq 0x60d50
cmpl $0x0, %eax
jne 0x5f361
callq 0x2e750
movq %rax, -0x118(%rbp)
cmpq $0x0, -0x118(%rbp)
je 0x5f358
movq -0x118(%rbp), %rdx
shrq $0x14, %rdx
movl $0x2, %edi
leaq 0x85a09(%rip), %rsi     # 0xe4d5a
movb $0x0, %al
callq 0x24fb0
xorl %edi, %edi
callq 0x25300
jmp 0x5f36b
movl $0x1, %edi
callq 0x25300
jmp 0x5f430
callq 0x252a0
movq %rax, %rcx
subq -0x10(%rbp), %rcx
leaq 0xb4575(%rip), %rax     # 0x1138f8
movq %rcx, 0x380(%rax)
cmpl $-0x1, -0x8(%rbp)
jne 0x5f3bb
callq 0x1f820
movl (%rax), %edi
callq 0x1f630
movq %rax, %rdx
movl $0x3, %edi
leaq 0x859e3(%rip), %rsi     # 0xe4d8e
movb $0x0, %al
callq 0x24fb0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x5f437
movl -0x8(%rbp), %edx
movl $0x2, %edi
leaq 0x859fb(%rip), %rsi     # 0xe4dc5
movb $0x0, %al
callq 0x24fb0
leaq 0xb4520(%rip), %rax     # 0x1138f8
movl $0x0, 0x500(%rax)
xorl %eax, %eax
movl %eax, %edi
callq 0x1fc70
movq %rax, %rcx
leaq 0xb4503(%rip), %rax     # 0x1138f8
movq %rcx, 0x538(%rax)
movl -0x8(%rbp), %ecx
leaq 0xb44f2(%rip), %rax     # 0x1138f8
movl %ecx, 0x504(%rax)
callq 0x258c0
leaq 0xb44e0(%rip), %rax     # 0x1138f8
movl $0xffffffff, 0x51c(%rax) # imm = 0xFFFFFFFF
callq 0x40020
movl $0x0, -0x4(%rbp)
jmp 0x5f437
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopw %cs:(%rax,%rax)