// File: /workspace/github_c_makefile_star2_O0/nilyang[P]redis-tdd-annotation/src/redis.c
// Function: main
int main(int argc, char **argv) {
    struct timeval tv;

    /* We need to initialize our libraries, and the server configuration. */
    // 初始化库
#ifdef INIT_SETPROCTITLE_REPLACEMENT
    spt_init(argc, argv);
#endif
    setlocale(LC_COLLATE,"");
    zmalloc_enable_thread_safeness();
    zmalloc_set_oom_handler(redisOutOfMemoryHandler);
    srand(time(NULL)^getpid());
    gettimeofday(&tv,NULL);
    dictSetHashFunctionSeed(tv.tv_sec^tv.tv_usec^getpid());

    // 检查服务器是否以 Sentinel 模式启动
    server.sentinel_mode = checkForSentinelMode(argc,argv);

    // 初始化服务器
    initServerConfig();

    /* We need to init sentinel right now as parsing the configuration file
     * in sentinel mode will have the effect of populating the sentinel
     * data structures with master nodes to monitor. */
    // 如果服务器以 Sentinel 模式启动，那么进行 Sentinel 功能相关的初始化
    // 并为要监视的主服务器创建一些相应的数据结构
    if (server.sentinel_mode) {
        initSentinelConfig();
        initSentinel();
    }

    // 检查用户是否指定了配置文件，或者配置选项
    if (argc >= 2) {
        int j = 1; /* First option to parse in argv[] */
        sds options = sdsempty();
        char *configfile = NULL;

        /* Handle special options --help and --version */
        // 处理特殊选项 -h 、-v 和 --test-memory
        if (strcmp(argv[1], "-v") == 0 ||
            strcmp(argv[1], "--version") == 0) version();
        if (strcmp(argv[1], "--help") == 0 ||
            strcmp(argv[1], "-h") == 0) usage();
        if (strcmp(argv[1], "--test-memory") == 0) {
            if (argc == 3) {
                memtest(atoi(argv[2]),50);
                exit(0);
            } else {
                fprintf(stderr,"Please specify the amount of memory to test in megabytes.\n");
                fprintf(stderr,"Example: ./redis-server --test-memory 4096\n\n");
                exit(1);
            }
        }

        /* First argument is the config file name? */
        // 如果第一个参数（argv[1]）不是以 "--" 开头
        // 那么它应该是一个配置文件
        if (argv[j][0] != '-' || argv[j][1] != '-')
            configfile = argv[j++];

        /* All the other options are parsed and conceptually appended to the
         * configuration file. For instance --port 6380 will generate the
         * string "port 6380\n" to be parsed after the actual file name
         * is parsed, if any. */
        // 对用户给定的其余选项进行分析，并将分析所得的字符串追加稍后载入的配置文件的内容之后
        // 比如 --port 6380 会被分析为 "port 6380\n"
        while(j != argc) {
            if (argv[j][0] == '-' && argv[j][1] == '-') {
                /* Option name */
                if (sdslen(options)) options = sdscat(options,"\n");
                options = sdscat(options,argv[j]+2);
                options = sdscat(options," ");
            } else {
                /* Option argument */
                options = sdscatrepr(options,argv[j],strlen(argv[j]));
                options = sdscat(options," ");
            }
            j++;
        }
        if (configfile) server.configfile = getAbsolutePath(configfile);
        // 重置保存条件
        resetServerSaveParams();

        // 载入配置文件， options 是前面分析出的给定选项
        loadServerConfig(configfile,options);
        sdsfree(options);

        // 获取配置文件的绝对路径
        if (configfile) server.configfile = getAbsolutePath(configfile);
    } else {
        redisLog(REDIS_WARNING, "Warning: no config file specified, using the default config. In order to specify a config file use %s /path/to/%s.conf", argv[0], server.sentinel_mode ? "sentinel" : "redis");
    }

    // 将服务器设置为守护进程
    if (server.daemonize) daemonize();

    // 创建并初始化服务器数据结构
    initServer();

    // 如果服务器是守护进程，那么创建 PID 文件
    if (server.daemonize) createPidFile();

    // 为服务器进程设置名字
    redisSetProcTitle(argv[0]);

    // 打印 ASCII LOGO
    redisAsciiArt();

    // 如果服务器不是运行在 SENTINEL 模式，那么执行以下代码
    if (!server.sentinel_mode) {
        /* Things not needed when running in Sentinel mode. */
        // 打印问候语
        redisLog(REDIS_WARNING,"Server started, Redis version " REDIS_VERSION);
    #ifdef __linux__
        // 打印内存警告
        linuxOvercommitMemoryWarning();
    #endif
        // 从 AOF 文件或者 RDB 文件中载入数据
        loadDataFromDisk();
        // 启动集群？
        if (server.cluster_enabled) {
            if (verifyClusterConfigWithData() == REDIS_ERR) {
                redisLog(REDIS_WARNING,
                    "You can't have keys in a DB different than DB 0 when in "
                    "Cluster mode. Exiting.");
                exit(1);
            }
        }
        // 打印 TCP 端口
        if (server.ipfd_count > 0)
            redisLog(REDIS_NOTICE,"The server is now ready to accept connections on port %d", server.port);
        // 打印本地套接字端口
        if (server.sofd > 0)
            redisLog(REDIS_NOTICE,"The server is now ready to accept connections at %s", server.unixsocket);
    } else {
        sentinelIsRunning();
    }

    /* Warning the user about suspicious maxmemory setting. */
    // 检查不正常的 maxmemory 配置
    if (server.maxmemory > 0 && server.maxmemory < 1024*1024) {
        redisLog(REDIS_WARNING,"WARNING: You specified a maxmemory value that is less than 1MB (current value is %llu bytes). Are you sure this is what you really want?", server.maxmemory);
    }

    // 运行事件处理器，一直到服务器关闭为止
    aeSetBeforeSleepProc(server.el,beforeSleep);
    aeMain(server.el);

    // 服务器关闭，停止事件循环
    aeDeleteEventLoop(server.el);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x81670
movl $0x3, %edi
leaq 0xb58cb(%rip), %rsi     # 0xe13f9
callq 0x1fae0
callq 0x2e4c0
leaq -0x12f(%rip), %rdi      # 0x2ba10
callq 0x2e4d0
xorl %eax, %eax
movl %eax, %edi
callq 0x1fc70
movq %rax, -0x48(%rbp)
callq 0x1f410
movl %eax, %ecx
movq -0x48(%rbp), %rax
movslq %ecx, %rcx
xorq %rcx, %rax
movl %eax, %edi
callq 0x1f910
leaq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f300
movq -0x20(%rbp), %rax
xorq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
callq 0x1f410
movl %eax, %ecx
movq -0x40(%rbp), %rax
movslq %ecx, %rcx
xorq %rcx, %rax
movl %eax, %edi
callq 0x23000
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x2b890
movl %eax, 0xe7dd0(%rip)     # 0x11397c
callq 0x27000
cmpl $0x0, 0xe7dc4(%rip)     # 0x11397c
je 0x2bbc4
callq 0x79720
callq 0x79740
cmpl $0x2, -0x8(%rbp)
jl 0x2be4a
movl $0x1, -0x24(%rbp)
callq 0x2c0c0
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xb4cd1(%rip), %rsi     # 0xe08c6
callq 0x1f890
cmpl $0x0, %eax
je 0x2bc18
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xb4cbb(%rip), %rsi     # 0xe08c9
callq 0x1f890
cmpl $0x0, %eax
jne 0x2bc1d
callq 0x2b580
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xb4ca7(%rip), %rsi     # 0xe08d3
callq 0x1f890
cmpl $0x0, %eax
je 0x2bc4f
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xb4c95(%rip), %rsi     # 0xe08da
callq 0x1f890
cmpl $0x0, %eax
jne 0x2bc54
callq 0x2b5f0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xb4c7a(%rip), %rsi     # 0xe08dd
callq 0x1f890
cmpl $0x0, %eax
jne 0x2bcce
cmpl $0x3, -0x8(%rbp)
jne 0x2bc94
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1f700
movslq %eax, %rdi
movl $0x32, %esi
callq 0x76fa0
xorl %edi, %edi
callq 0x1f2c0
movq 0xe3345(%rip), %rax     # 0x10efe0
movq (%rax), %rdi
leaq 0xb4c46(%rip), %rsi     # 0xe08eb
movb $0x0, %al
callq 0x1fb30
movq 0xe332d(%rip), %rax     # 0x10efe0
movq (%rax), %rdi
leaq 0xb4c69(%rip), %rsi     # 0xe0926
movb $0x0, %al
callq 0x1fb30
movl $0x1, %edi
callq 0x1f2c0
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x2bcf7
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl 0x1(%rax), %eax
cmpl $0x2d, %eax
je 0x2bd11
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x24(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x38(%rbp)
jmp 0x2bd13
movl -0x24(%rbp), %eax
cmpl -0x8(%rbp), %eax
je 0x2bdfd
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x2bd9e
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movsbl 0x1(%rax), %eax
cmpl $0x2d, %eax
jne 0x2bd9e
movq -0x30(%rbp), %rdi
callq 0x24b20
cmpq $0x0, %rax
je 0x2bd6b
movq -0x30(%rbp), %rdi
leaq 0xb4a57(%rip), %rsi     # 0xe07b9
callq 0x2c5b0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
addq $0x2, %rsi
callq 0x2c5b0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
leaq 0xb874b(%rip), %rsi     # 0xe44de
callq 0x2c5b0
movq %rax, -0x30(%rbp)
jmp 0x2bdef
movq -0x30(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1f4d0
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq %rax, %rdx
callq 0x2d820
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
leaq 0xb86f8(%rip), %rsi     # 0xe44de
callq 0x2c5b0
movq %rax, -0x30(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x2bd13
cmpq $0x0, -0x38(%rbp)
je 0x2be14
movq -0x38(%rbp), %rdi
callq 0x39dd0
movq %rax, 0xe7ae4(%rip)     # 0x1138f8
movb $0x0, %al
callq 0x571f0
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x59090
movq -0x30(%rbp), %rdi
callq 0x2c160
cmpq $0x0, -0x38(%rbp)
je 0x2be48
movq -0x38(%rbp), %rdi
callq 0x39dd0
movq %rax, 0xe7ab0(%rip)     # 0x1138f8
jmp 0x2be7f
movq -0x10(%rbp), %rax
movq (%rax), %rdx
movl 0xe7b25(%rip), %esi     # 0x11397c
leaq 0xb2f1b(%rip), %rcx     # 0xded79
leaq 0xbd839(%rip), %rax     # 0xe969e
cmpl $0x0, %esi
cmovneq %rax, %rcx
movl $0x3, %edi
leaq 0xb4adb(%rip), %rsi     # 0xe0953
movb $0x0, %al
callq 0x24fb0
cmpl $0x0, 0xe7ef6(%rip)     # 0x113d7c
je 0x2be8d
callq 0x2b500
callq 0x27e00
cmpl $0x0, 0xe7ee3(%rip)     # 0x113d7c
je 0x2bea0
callq 0x2b4a0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x2ba60
callq 0x2b740
cmpl $0x0, 0xe7ac4(%rip)     # 0x11397c
jne 0x2bf54
movl $0x3, %edi
leaq 0xb4b00(%rip), %rsi     # 0xe09ca
movb $0x0, %al
callq 0x24fb0
callq 0x2b470
callq 0x2b920
cmpl $0x0, 0xe81d2(%rip)     # 0x1140b4
je 0x2bf0d
callq 0x6ec10
cmpl $-0x1, %eax
jne 0x2bf0b
movl $0x3, %edi
leaq 0xb4af5(%rip), %rsi     # 0xe09ef
movb $0x0, %al
callq 0x24fb0
movl $0x1, %edi
callq 0x1f2c0
jmp 0x2bf0d
cmpl $0x0, 0xe7b48(%rip)     # 0x113a5c
jle 0x2bf2f
movl 0xe7a64(%rip), %edx     # 0x113980
movl $0x2, %edi
leaq 0xb4b16(%rip), %rsi     # 0xe0a3e
movb $0x0, %al
callq 0x24fb0
cmpl $0x0, 0xe7b2a(%rip)     # 0x113a60
jle 0x2bf52
movq 0xe7ad1(%rip), %rdx     # 0x113a10
movl $0x2, %edi
leaq 0xb4b2c(%rip), %rsi     # 0xe0a77
movb $0x0, %al
callq 0x24fb0
jmp 0x2bf59
callq 0x79850
cmpq $0x0, 0xe80b7(%rip)     # 0x114018
jbe 0x2bf8a
cmpq $0x100000, 0xe80aa(%rip) # imm = 0x100000
jae 0x2bf8a
movq 0xe80a1(%rip), %rdx     # 0x114018
movl $0x3, %edi
leaq 0xb4b28(%rip), %rsi     # 0xe0aab
movb $0x0, %al
callq 0x24fb0
movq 0xe798f(%rip), %rdi     # 0x113920
leaq -0x56c8(%rip), %rsi     # 0x268d0
callq 0x21870
movq 0xe797c(%rip), %rdi     # 0x113920
callq 0x217f0
movq 0xe7970(%rip), %rdi     # 0x113920
callq 0x209e0
xorl %eax, %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)