// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x10, %rsp
movl %edi, -0x8(%rbp)
movl -0x8(%rbp), %eax
andl $0x1, %eax
movl %eax, -0xc(%rbp)
movl -0x8(%rbp), %eax
andl $0x2, %eax
movl %eax, -0x10(%rbp)
movl $0x3, %edi
leaq 0xb5a4e(%rip), %rsi     # 0xdf3f7
movb $0x0, %al
callq 0x24fb0
cmpl $-0x1, 0xea4b1(%rip)    # 0x113e68
je 0x299e7
movl $0x3, %edi
leaq 0xb5a4d(%rip), %rsi     # 0xdf412
movb $0x0, %al
callq 0x24fb0
movl 0xea496(%rip), %edi     # 0x113e68
movl $0xa, %esi
callq 0x1f620
movl 0xea486(%rip), %edi     # 0x113e68
callq 0x45090
cmpl $0x0, 0xea3da(%rip)     # 0x113dc8
je 0x29a3a
cmpl $-0x1, 0xea405(%rip)    # 0x113dfc
je 0x29a1c
movl $0x3, %edi
leaq 0xb5a3a(%rip), %rsi     # 0xdf43f
movb $0x0, %al
callq 0x24fb0
movl 0xea3ea(%rip), %edi     # 0x113dfc
movl $0xa, %esi
callq 0x1f620
movl $0x2, %edi
leaq 0xb5a47(%rip), %rsi     # 0xdf46f
movb $0x0, %al
callq 0x24fb0
movl 0xea3db(%rip), %edi     # 0x113e10
callq 0x1f540
cmpl $0x0, 0xea437(%rip)     # 0x113e78
jle 0x29a49
cmpl $0x0, -0x10(%rbp)
je 0x29a4f
cmpl $0x0, -0xc(%rbp)
je 0x29a91
movl $0x2, %edi
leaq 0xb5a35(%rip), %rsi     # 0xdf490
movb $0x0, %al
callq 0x24fb0
movq 0xea417(%rip), %rdi     # 0x113e80
callq 0x449a0
cmpl $0x0, %eax
je 0x29a8f
movl $0x3, %edi
leaq 0xb5a3f(%rip), %rsi     # 0xdf4be
movb $0x0, %al
callq 0x24fb0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x29af8
jmp 0x29a91
cmpl $0x0, 0xea2e4(%rip)     # 0x113d7c
je 0x29ab9
movl $0x2, %edi
leaq 0xb5a41(%rip), %rsi     # 0xdf4e7
movb $0x0, %al
callq 0x24fb0
movq 0xe9e8c(%rip), %rdi     # 0x113940
callq 0x1f3a0
movl $0x1, %edi
callq 0x298b0
movl 0xe9eb3(%rip), %ecx     # 0x11397c
leaq 0xb5a5b(%rip), %rdx     # 0xdf52b
leaq 0xb5a4b(%rip), %rax     # 0xdf522
cmpl $0x0, %ecx
cmovneq %rax, %rdx
movl $0x3, %edi
leaq 0xb5a14(%rip), %rsi     # 0xdf4fe
movb $0x0, %al
callq 0x24fb0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x10, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)