// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movl %edi, %ebp
callq 0x5b5f0
leaq 0x66580(%rip), %rsi     # 0x8d311
pushq $0x3
popq %rdi
callq 0x1fac0
callq 0x28448
leaq -0xc3(%rip), %rdi       # 0x26ce2
callq 0x28450
xorl %edi, %edi
callq 0x1fc50
movq %rax, %r14
callq 0x1f410
xorl %eax, %r14d
movl %r14d, %edi
callq 0x1f8f0
leaq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x1f2f0
movl 0x8(%r14), %r15d
xorl (%r14), %r15d
callq 0x1f410
xorl %eax, %r15d
movl %r15d, %edi
callq 0x2162e
movl %ebp, %edi
movq %rbx, %rsi
callq 0x26be1
movl %eax, 0xa0232(%rip)     # 0xc702c
callq 0x23bb5
cmpl $0x0, 0xa0226(%rip)     # 0xc702c
je 0x26e12
callq 0x564fa
callq 0x5650c
cmpl $0x2, %ebp
jl 0x26ec0
callq 0x271bf
movq %rax, %r15
movq 0x8(%rbx), %r12
movb (%r12), %r14b
cmpb $0x2d, %r14b
jne 0x26e45
cmpb $0x76, 0x1(%r12)
jne 0x26e45
cmpb $0x0, 0x2(%r12)
je 0x270dd
leaq 0x65a12(%rip), %rsi     # 0x8c85e
movq %r12, %rdi
callq 0x1f880
testl %eax, %eax
je 0x270dd
leaq 0x65a05(%rip), %rsi     # 0x8c868
movq %r12, %rdi
callq 0x1f880
testl %eax, %eax
je 0x270e2
cmpb $0x2d, %r14b
jne 0x26e8d
cmpb $0x68, 0x1(%r12)
jne 0x26e8d
cmpb $0x0, 0x2(%r12)
je 0x270e2
leaq 0x659db(%rip), %rsi     # 0x8c86f
movq %r12, %rdi
callq 0x1f880
testl %eax, %eax
je 0x270e7
cmpb $0x2d, %r14b
jne 0x26ef2
cmpb $0x2d, 0x1(%r12)
jne 0x26ef2
pushq $0x1
popq %r12
movq $0x0, (%rsp)
jmp 0x26efa
movq (%rbx), %rdx
cmpl $0x0, 0xa0162(%rip)     # 0xc702c
leaq 0x63e40(%rip), %rax     # 0x8ad11
leaq 0x6e776(%rip), %rcx     # 0x9564e
cmoveq %rax, %rcx
leaq 0x65a02(%rip), %rsi     # 0x8c8e5
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
jmp 0x26fcc
movq %r12, (%rsp)
pushq $0x2
popq %r12
movl %ebp, %r14d
leaq 0x694cf(%rip), %r13     # 0x903d3
cmpq %r12, %r14
je 0x26f70
movq (%rbx,%r12,8), %rbp
cmpb $0x2d, (%rbp)
jne 0x26f47
cmpb $0x2d, 0x1(%rbp)
jne 0x26f47
cmpl $0x0, -0x8(%r15)
je 0x26f36
movq %r15, %rdi
leaq 0x65827(%rip), %rsi     # 0x8c751
callq 0x273bf
movq %rax, %r15
movq (%rbx,%r12,8), %rbp
addq $0x2, %rbp
movq %r15, %rdi
movq %rbp, %rsi
callq 0x273bf
jmp 0x26f5d
movq %rbp, %rdi
callq 0x1f4f0
movq %r15, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x27d51
movq %rax, %rdi
movq %r13, %rsi
callq 0x273bf
movq %rax, %r15
incq %r12
jmp 0x26f04
movq (%rsp), %r14
testq %r14, %r14
je 0x26fb3
movq %r14, %rdi
callq 0x2e88a
movq %rax, 0xa0020(%rip)     # 0xc6fa8
xorl %eax, %eax
callq 0x405e4
movq %r14, %rdi
movq %r15, %rsi
callq 0x41ba6
movq %r15, %rdi
callq 0x271f7
movq %r14, %rdi
callq 0x2e88a
movq %rax, 0x9fff7(%rip)     # 0xc6fa8
jmp 0x26fcc
xorl %eax, %eax
callq 0x405e4
xorl %edi, %edi
movq %r15, %rsi
callq 0x41ba6
movq %r15, %rdi
callq 0x271f7
cmpl $0x0, 0xa0459(%rip)     # 0xc742c
je 0x26fda
callq 0x26923
callq 0x24565
cmpl $0x0, 0xa0446(%rip)     # 0xc742c
je 0x26fed
callq 0x268e4
movq (%rbx), %rdi
callq 0x26d17
callq 0x26b00
cmpl $0x0, 0xa002b(%rip)     # 0xc702c
je 0x2700a
callq 0x565e0
jmp 0x2707d
leaq 0x6594b(%rip), %rsi     # 0x8c95c
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
callq 0x268c6
callq 0x26c3b
cmpl $0x0, 0xa0738(%rip)     # 0xc7764
je 0x2703c
callq 0x4fa86
cmpl $-0x1, %eax
je 0x2713d
cmpl $0x0, 0xa00c9(%rip)     # 0xc710c
jle 0x2705c
movl 0x9ffe5(%rip), %edx     # 0xc7030
leaq 0x6597e(%rip), %rsi     # 0x8c9d0
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x22613
cmpl $0x0, 0xa00ad(%rip)     # 0xc7110
jle 0x2707d
movq 0xa0054(%rip), %rdx     # 0xc70c0
leaq 0x65996(%rip), %rsi     # 0x8ca09
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x22613
movq 0xa0644(%rip), %rdx     # 0xc76c8
leaq -0x1(%rdx), %rax
cmpq $0xffffe, %rax          # imm = 0xFFFFE
ja 0x270a1
leaq 0x659a6(%rip), %rsi     # 0x8ca3d
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
movq 0x9ff28(%rip), %rdi     # 0xc6fd0
leaq -0x3b99(%rip), %rsi     # 0x23516
callq 0x20998
movq 0x9ff15(%rip), %rdi     # 0xc6fd0
callq 0x20959
movq 0x9ff09(%rip), %rdi     # 0xc6fd0
callq 0x202bf
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2697f
callq 0x269d4
cmpl $0x3, %ebp
jne 0x27107
movq 0x10(%rbx), %rdi
callq 0x1f710
movslq %eax, %rdi
pushq $0x32
popq %rsi
callq 0x54c25
xorl %edi, %edi
callq 0x1f2b0
movq 0x9beba(%rip), %r14     # 0xc2fc8
movq (%r14), %rcx
leaq 0x65765(%rip), %rdi     # 0x8c87d
pushq $0x3a
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x1fa90
movq (%r14), %rcx
leaq 0x65788(%rip), %rdi     # 0x8c8b8
pushq $0x2c
popq %rsi
movq %rbx, %rdx
callq 0x1fa90
jmp 0x2714e
leaq 0x6583d(%rip), %rsi     # 0x8c981
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
pushq $0x1
popq %rdi
callq 0x1f2b0
nop