// File: /workspace/github_c_makefile_star2_O3/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 0x6eb40(%rip), %rsi     # 0x953cf
movl $0x3, %edi
xorl %eax, %eax
callq 0x23223
cmpl $-0x1, 0xa3c76(%rip)    # 0xca518
je 0x268d2
leaq 0x6eb3f(%rip), %rsi     # 0x953ea
movl $0x3, %edi
xorl %eax, %eax
callq 0x23223
movl 0xa3c5b(%rip), %edi     # 0xca518
movl $0xa, %esi
callq 0x1f620
movl 0xa3c4b(%rip), %edi     # 0xca518
callq 0x37964
cmpl $0x0, 0xa3b9f(%rip)     # 0xca478
je 0x26925
cmpl $-0x1, 0xa3bca(%rip)    # 0xca4ac
je 0x26907
leaq 0x6eb2c(%rip), %rsi     # 0x95417
movl $0x3, %edi
xorl %eax, %eax
callq 0x23223
movl 0xa3baf(%rip), %edi     # 0xca4ac
movl $0xa, %esi
callq 0x1f620
leaq 0x6eb39(%rip), %rsi     # 0x95447
movl $0x2, %edi
xorl %eax, %eax
callq 0x23223
movl 0xa3ba0(%rip), %edi     # 0xca4c0
callq 0x1f560
cmpl $0x0, 0xa3bfc(%rip)     # 0xca528
setg %al
testb $0x2, %bl
sete %cl
testb $0x1, %bl
jne 0x2693e
andb %al, %cl
je 0x2697b
leaq 0x6eb23(%rip), %rsi     # 0x95468
movl $0x2, %edi
xorl %eax, %eax
callq 0x23223
movq 0xa3bd8(%rip), %rdi     # 0xca530
callq 0x37460
testl %eax, %eax
je 0x2697b
leaq 0x6eb2e(%rip), %rsi     # 0x95496
movl $0x3, %edi
xorl %eax, %eax
callq 0x23223
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x269db
cmpl $0x0, 0xa3aaa(%rip)     # 0xca42c
je 0x269a3
leaq 0x6eb34(%rip), %rsi     # 0x954bf
movl $0x2, %edi
xorl %eax, %eax
callq 0x23223
movq 0xa3652(%rip), %rdi     # 0xc9ff0
callq 0x1f3a0
movl $0x1, %edi
callq 0x267d6
cmpl $0x0, 0xa3678(%rip)     # 0xca02c
leaq 0x6eb48(%rip), %rax     # 0x95503
leaq 0x6eb38(%rip), %rdx     # 0x954fa
cmoveq %rax, %rdx
leaq 0x6eb09(%rip), %rsi     # 0x954d6
xorl %ebx, %ebx
movl $0x3, %edi
xorl %eax, %eax
callq 0x23223
movl %ebx, %eax
popq %rbx
retq