// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/redis.c
// Function: prepareForShutdown
int prepareForShutdown(int flags) {
    int save = flags & REDIS_SHUTDOWN_SAVE;
    int nosave = flags & REDIS_SHUTDOWN_NOSAVE;

    redisLog(REDIS_WARNING,"User requested shutdown...");

    /* Kill the saving child if there is a background saving in progress.
       We want to avoid race conditions, for instance our saving child may
       overwrite the synchronous saving did by SHUTDOWN. */
    // 如果有 BGSAVE 正在执行，那么杀死子进程，避免竞争条件
    if (server.rdb_child_pid != -1) {
        redisLog(REDIS_WARNING,"There is a child saving an .rdb. Killing it!");
        kill(server.rdb_child_pid,SIGUSR1);
        rdbRemoveTempFile(server.rdb_child_pid);
    }

    // 同理，杀死正在执行 BGREWRITEAOF 的子进程
    if (server.aof_state != REDIS_AOF_OFF) {
        /* Kill the AOF saving child as the AOF we already have may be longer
         * but contains the full dataset anyway. */
        if (server.aof_child_pid != -1) {
            redisLog(REDIS_WARNING,
                "There is a child rewriting the AOF. Killing it!");
            kill(server.aof_child_pid,SIGUSR1);
        }
        /* Append only file: fsync() the AOF and exit */
        redisLog(REDIS_NOTICE,"Calling fsync() on the AOF file.");
        // 将缓冲区的内容写入到硬盘里面
        aof_fsync(server.aof_fd);
    }

    // 如果客户端执行的是 SHUTDOWN save ，或者 SAVE 功能被打开
    // 那么执行 SAVE 操作
    if ((server.saveparamslen > 0 && !nosave) || save) {
        redisLog(REDIS_NOTICE,"Saving the final RDB snapshot before exiting.");
        /* Snapshotting. Perform a SYNC SAVE and exit */
        if (rdbSave(server.rdb_filename) != REDIS_OK) {
            /* Ooops.. error saving! The best we can do is to continue
             * operating. Note that if there was a background saving process,
             * in the next cron() Redis will be notified that the background
             * saving aborted, handling special stuff like slaves pending for
             * synchronization... */
            redisLog(REDIS_WARNING,"Error trying to save the DB, can't exit.");
            return REDIS_ERR;
        }
    }

    // 移除 pidfile 文件
    if (server.daemonize) {
        redisLog(REDIS_NOTICE,"Removing the pid file.");
        unlink(server.pidfile);
    }

    /* Close the listening sockets. Apparently this allows faster restarts. */
    // 关闭监听套接字，这样在重启的时候会快一点
    closeListeningSockets(1);
    redisLog(REDIS_WARNING,"%s is now ready to exit, bye bye...",
        server.sentinel_mode ? "Sentinel" : "Redis");
    return REDIS_OK;
}

// Assembly:
pushq %rbx
movl %edi, %ebx
leaq 0x65c08(%rip), %rsi     # 0x8b38f
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
cmpl $-0x1, 0xa1d80(%rip)    # 0xc7518
je 0x257c4
leaq 0x65c09(%rip), %rsi     # 0x8b3aa
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
movl 0xa1d67(%rip), %edi     # 0xc7518
pushq $0xa
popq %rsi
callq 0x1f620
movl 0xa1d59(%rip), %edi     # 0xc7518
callq 0x351f9
cmpl $0x0, 0xa1cad(%rip)     # 0xc7478
je 0x25811
cmpl $-0x1, 0xa1cd8(%rip)    # 0xc74ac
je 0x257f5
leaq 0x65bfa(%rip), %rsi     # 0x8b3d7
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
movl 0xa1cbf(%rip), %edi     # 0xc74ac
pushq $0xa
popq %rsi
callq 0x1f620
leaq 0x65c0b(%rip), %rsi     # 0x8b407
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x22613
movl 0xa1cb4(%rip), %edi     # 0xc74c0
callq 0x1f560
cmpl $0x0, 0xa1d10(%rip)     # 0xc7528
setg %al
testb $0x2, %bl
sete %cl
testb $0x1, %bl
jne 0x2582a
andb %al, %cl
je 0x25861
leaq 0x65bf7(%rip), %rsi     # 0x8b428
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x22613
movq 0xa1cee(%rip), %rdi     # 0xc7530
callq 0x34d06
testl %eax, %eax
je 0x25861
leaq 0x65c04(%rip), %rsi     # 0x8b456
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
pushq $-0x1
popq %rbx
jmp 0x258bb
cmpl $0x0, 0xa1bc4(%rip)     # 0xc742c
je 0x25887
leaq 0x65c0e(%rip), %rsi     # 0x8b47f
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x22613
movq 0xa176e(%rip), %rdi     # 0xc6ff0
callq 0x1f3a0
pushq $0x1
popq %rdi
callq 0x256de
cmpl $0x0, 0xa1796(%rip)     # 0xc702c
leaq 0x65c26(%rip), %rax     # 0x8b4c3
leaq 0x65c16(%rip), %rdx     # 0x8b4ba
cmoveq %rax, %rdx
leaq 0x65be7(%rip), %rsi     # 0x8b496
xorl %ebx, %ebx
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
movl %ebx, %eax
popq %rbx
retq