// File: /workspace/github_c_makefile_star2_O1/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 0x6ed11(%rip), %rsi     # 0x9537f
movl $0x3, %edi
xorl %eax, %eax
callq 0x23064
cmpl $-0x1, 0xa3e97(%rip)    # 0xca518
je 0x266b1
leaq 0x6ed10(%rip), %rsi     # 0x9539a
movl $0x3, %edi
xorl %eax, %eax
callq 0x23064
movl 0xa3e7c(%rip), %edi     # 0xca518
movl $0xa, %esi
callq 0x1f620
movl 0xa3e6c(%rip), %edi     # 0xca518
callq 0x3779c
cmpl $0x0, 0xa3dc0(%rip)     # 0xca478
je 0x26704
cmpl $-0x1, 0xa3deb(%rip)    # 0xca4ac
je 0x266e6
leaq 0x6ecfd(%rip), %rsi     # 0x953c7
movl $0x3, %edi
xorl %eax, %eax
callq 0x23064
movl 0xa3dd0(%rip), %edi     # 0xca4ac
movl $0xa, %esi
callq 0x1f620
leaq 0x6ed0a(%rip), %rsi     # 0x953f7
movl $0x2, %edi
xorl %eax, %eax
callq 0x23064
movl 0xa3dc1(%rip), %edi     # 0xca4c0
callq 0x1f560
cmpl $0x0, 0xa3e1d(%rip)     # 0xca528
setg %al
testb $0x2, %bl
sete %cl
testb $0x1, %bl
jne 0x2671d
andb %al, %cl
je 0x2675a
leaq 0x6ecf4(%rip), %rsi     # 0x95418
movl $0x2, %edi
xorl %eax, %eax
callq 0x23064
movq 0xa3df9(%rip), %rdi     # 0xca530
callq 0x3725a
testl %eax, %eax
je 0x2675a
leaq 0x6ecff(%rip), %rsi     # 0x95446
movl $0x3, %edi
xorl %eax, %eax
callq 0x23064
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x267ba
cmpl $0x0, 0xa3ccb(%rip)     # 0xca42c
je 0x26782
leaq 0x6ed05(%rip), %rsi     # 0x9546f
movl $0x2, %edi
xorl %eax, %eax
callq 0x23064
movq 0xa3873(%rip), %rdi     # 0xc9ff0
callq 0x1f3a0
movl $0x1, %edi
callq 0x265b5
cmpl $0x0, 0xa3899(%rip)     # 0xca02c
leaq 0x6ed19(%rip), %rax     # 0x954b3
leaq 0x6ed09(%rip), %rdx     # 0x954aa
cmoveq %rax, %rdx
leaq 0x6ecda(%rip), %rsi     # 0x95486
xorl %ebx, %ebx
movl $0x3, %edi
xorl %eax, %eax
callq 0x23064
movl %ebx, %eax
popq %rbx
retq