// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/redis.c
// Function: initServer
void initServer() {
    int j;

    // 设置信号处理函数
    signal(SIGHUP, SIG_IGN);
    signal(SIGPIPE, SIG_IGN);
    setupSignalHandlers();

    // 设置 syslog
    if (server.syslog_enabled) {
        openlog(server.syslog_ident, LOG_PID | LOG_NDELAY | LOG_NOWAIT,
            server.syslog_facility);
    }

    // 初始化并创建数据结构
    server.current_client = NULL;
    server.clients = listCreate();
    server.clients_to_close = listCreate();
    server.slaves = listCreate();
    server.monitors = listCreate();
    server.slaveseldb = -1; /* Force to emit the first SELECT command. */
    server.unblocked_clients = listCreate();
    server.ready_keys = listCreate();
    server.clients_waiting_acks = listCreate();
    server.get_ack_from_slaves = 0;
    server.clients_paused = 0;

    // 创建共享对象
    createSharedObjects();
    adjustOpenFilesLimit();
    server.el = aeCreateEventLoop(server.maxclients+REDIS_EVENTLOOP_FDSET_INCR);
    server.db = zmalloc(sizeof(redisDb)*server.dbnum);

    /* Open the TCP listening socket for the user commands. */
    // 打开 TCP 监听端口，用于等待客户端的命令请求
    if (server.port != 0 &&
        listenToPort(server.port,server.ipfd,&server.ipfd_count) == REDIS_ERR)
        exit(1);

    /* Open the listening Unix domain socket. */
    // 打开 UNIX 本地端口
    if (server.unixsocket != NULL) {
        unlink(server.unixsocket); /* don't care if this fails */
        server.sofd = anetUnixServer(server.neterr,server.unixsocket,
            server.unixsocketperm, server.tcp_backlog);
        if (server.sofd == ANET_ERR) {
            redisLog(REDIS_WARNING, "Opening socket: %s", server.neterr);
            exit(1);
        }
        anetNonBlock(NULL,server.sofd);
    }

    /* Abort if there are no listening sockets at all. */
    if (server.ipfd_count == 0 && server.sofd < 0) {
        redisLog(REDIS_WARNING, "Configured to not listen anywhere, exiting.");
        exit(1);
    }

    /* Create the Redis databases, and initialize other internal state. */
    // 创建并初始化数据库结构
    for (j = 0; j < server.dbnum; j++) {
        server.db[j].dict = dictCreate(&dbDictType,NULL);
        server.db[j].expires = dictCreate(&keyptrDictType,NULL);
        server.db[j].blocking_keys = dictCreate(&keylistDictType,NULL);
        server.db[j].ready_keys = dictCreate(&setDictType,NULL);
        server.db[j].watched_keys = dictCreate(&keylistDictType,NULL);
        server.db[j].eviction_pool = evictionPoolAlloc();
        server.db[j].id = j;
        server.db[j].avg_ttl = 0;
    }

    // 创建 PUBSUB 相关结构
    server.pubsub_channels = dictCreate(&keylistDictType,NULL);
    server.pubsub_patterns = listCreate();
    listSetFreeMethod(server.pubsub_patterns,freePubsubPattern);
    listSetMatchMethod(server.pubsub_patterns,listMatchPubsubPattern);

    server.cronloops = 0;
    server.rdb_child_pid = -1;
    server.aof_child_pid = -1;
    aofRewriteBufferReset();
    server.aof_buf = sdsempty();
    server.lastsave = time(NULL); /* At startup we consider the DB saved. */
    server.lastbgsave_try = 0;    /* At startup we never tried to BGSAVE. */
    server.rdb_save_time_last = -1;
    server.rdb_save_time_start = -1;
    server.dirty = 0;
    resetServerStats();
    /* A few stats we don't want to reset: server startup time, and peak mem. */
    server.stat_starttime = time(NULL);
    server.stat_peak_memory = 0;
    server.resident_set_size = 0;
    server.lastbgsave_status = REDIS_OK;
    server.aof_last_write_status = REDIS_OK;
    server.aof_last_write_errno = 0;
    server.repl_good_slaves_count = 0;
    updateCachedTime();

    /* Create the serverCron() time event, that's our main way to process
     * background operations. */
    // 为 serverCron() 创建时间事件
    if(aeCreateTimeEvent(server.el, 1, serverCron, NULL, NULL) == AE_ERR) {
        redisPanic("Can't create the serverCron time event.");
        exit(1);
    }

    /* Create an event handler for accepting new connections in TCP and Unix
     * domain sockets. */
    // 为 TCP 连接关联连接应答（accept）处理器
    // 用于接受并应答客户端的 connect() 调用
    for (j = 0; j < server.ipfd_count; j++) {
        if (aeCreateFileEvent(server.el, server.ipfd[j], AE_READABLE,
            acceptTcpHandler,NULL) == AE_ERR)
            {
                redisPanic(
                    "Unrecoverable error creating server.ipfd file event.");
            }
    }

    // 为本地套接字关联应答处理器
    if (server.sofd > 0 && aeCreateFileEvent(server.el,server.sofd,AE_READABLE,
        acceptUnixHandler,NULL) == AE_ERR) redisPanic("Unrecoverable error creating server.sofd file event.");

    /* Open the AOF file if needed. */
    // 如果 AOF 持久化功能已经打开，那么打开或创建一个 AOF 文件
    if (server.aof_state == REDIS_AOF_ON) {
        server.aof_fd = open(server.aof_filename,
                               O_WRONLY|O_APPEND|O_CREAT,0644);
        if (server.aof_fd == -1) {
            redisLog(REDIS_WARNING, "Can't open the append-only file: %s",
                strerror(errno));
            exit(1);
        }
    }

    /* 32 bit instances are limited to 4GB of address space, so if there is
     * no explicit limit in the user provided configuration we set a limit
     * at 3 GB using maxmemory with 'noeviction' policy'. This avoids
     * useless crashes of the Redis instance for out of memory. */
    // 对于 32 位实例来说，默认将最大可用内存限制在 3 GB
    if (server.arch_bits == 32 && server.maxmemory == 0) {
        redisLog(REDIS_WARNING,"Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now.");
        server.maxmemory = 3072LL*(1024*1024); /* 3 GB */
        server.maxmemory_policy = REDIS_MAXMEMORY_NO_EVICTION;
    }

    // 如果服务器以 cluster 模式打开，那么初始化 cluster
    if (server.cluster_enabled) clusterInit();

    // 初始化复制功能有关的脚本缓存
    replicationScriptCacheInit();

    // 初始化脚本系统
    scriptingInit();

    // 初始化慢查询功能
    slowlogInit();

    // 初始化 BIO 系统
    bioInit();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl $0x1, %esi
movl $0x1, %edi
callq 0x1f770
movl $0x1, %esi
movl $0xd, %edi
callq 0x1f770
callq 0x25a66
cmpl $0x0, 0xa5075(%rip)     # 0xca580
je 0x25524
movq 0xa5074(%rip), %rdi     # 0xca588
movl 0xa5076(%rip), %edx     # 0xca590
movl $0x19, %esi
callq 0x1f290
movq $0x0, 0xa4c49(%rip) # 0xca178
callq 0x203fc
movq %rax, 0xa4c1d(%rip)     # 0xca158
callq 0x203fc
movq %rax, 0xa4c19(%rip)     # 0xca160
callq 0x203fc
movq %rax, 0xa4c15(%rip)     # 0xca168
callq 0x203fc
movq %rax, 0xa4c11(%rip)     # 0xca170
movl $0xffffffff, 0xa502b(%rip) # imm = 0xFFFFFFFF
callq 0x203fc
movq %rax, 0xa516b(%rip)     # 0xca6e0
callq 0x203fc
movq %rax, 0xa5167(%rip)     # 0xca6e8
callq 0x203fc
movq %rax, 0xa512b(%rip)     # 0xca6b8
xorl %eax, %eax
movl %eax, 0xa512b(%rip)     # 0xca6c0
movl %eax, 0xa4be5(%rip)     # 0xca180
callq 0x244e0
callq 0x2514a
movl 0xa5119(%rip), %edi     # 0xca6c4
subl $-0x80, %edi
callq 0x20820
movq %rax, 0xa4a16(%rip)     # 0xc9fd0
movslq 0xa4e67(%rip), %rdi     # 0xca428
shlq $0x6, %rdi
callq 0x299b5
movq %rax, 0xa49e7(%rip)     # 0xc9fb8
movl 0xa4a59(%rip), %edi     # 0xca030
testl %edi, %edi
je 0x255f7
leaq 0xa4aea(%rip), %rsi     # 0xca0cc
leaq 0xa4b23(%rip), %rdx     # 0xca10c
callq 0x252da
cmpl $-0x1, %eax
je 0x25a5c
movq 0xa4ac2(%rip), %rdi     # 0xca0c0
testq %rdi, %rdi
je 0x2563f
callq 0x1f3a0
movq 0xa4ab1(%rip), %rsi     # 0xca0c0
movl 0xa4ab3(%rip), %edx     # 0xca0c8
movl 0xa4a19(%rip), %ecx     # 0xca034
leaq 0xa4b6e(%rip), %rdi     # 0xca190
callq 0x21ce2
movl %eax, 0xa4ae3(%rip)     # 0xca110
cmpl $-0x1, %eax
je 0x25a10
xorl %edi, %edi
movl %eax, %esi
callq 0x21374
cmpl $0x0, 0xa4ac6(%rip)     # 0xca10c
jne 0x25655
cmpl $0x0, 0xa4ac1(%rip)     # 0xca110
js 0x259e6
cmpl $0x0, 0xa4dcc(%rip)     # 0xca428
jle 0x25738
xorl %ebp, %ebp
leaq 0xa42c5(%rip), %r14     # 0xc9930
leaq 0xa434e(%rip), %r15     # 0xc99c0
leaq 0xa41f7(%rip), %r12     # 0xc9870
xorl %ebx, %ebx
leaq 0xa424e(%rip), %rdi     # 0xc98d0
xorl %esi, %esi
callq 0x2217b
movq 0xa4928(%rip), %rcx     # 0xc9fb8
movq %rax, (%rcx,%rbp)
movq %r14, %rdi
xorl %esi, %esi
callq 0x2217b
movq 0xa4913(%rip), %rcx     # 0xc9fb8
movq %rax, 0x8(%rcx,%rbp)
movq %r15, %rdi
xorl %esi, %esi
callq 0x2217b
movq 0xa48fd(%rip), %rcx     # 0xc9fb8
movq %rax, 0x10(%rcx,%rbp)
movq %r12, %rdi
xorl %esi, %esi
callq 0x2217b
movq 0xa48e7(%rip), %rcx     # 0xc9fb8
movq %rax, 0x18(%rcx,%rbp)
movq %r15, %rdi
xorl %esi, %esi
callq 0x2217b
movq 0xa48d1(%rip), %rcx     # 0xc9fb8
movq %rax, 0x20(%rcx,%rbp)
movl $0x100, %edi            # imm = 0x100
callq 0x299b5
movq %rax, %r13
movl $0x100, %edx            # imm = 0x100
movq %rax, %rdi
xorl %esi, %esi
callq 0x1f110
movq 0xa48a9(%rip), %rax     # 0xc9fb8
movq %r13, 0x28(%rax,%rbp)
movl %ebx, 0x30(%rax,%rbp)
movq $0x0, 0x38(%rax,%rbp)
incq %rbx
movslq 0xa4cfd(%rip), %rax     # 0xca428
addq $0x40, %rbp
cmpq %rax, %rbx
jl 0x2567b
leaq 0xa4281(%rip), %rdi     # 0xc99c0
xorl %r14d, %r14d
xorl %esi, %esi
callq 0x2217b
movq %rax, 0xa5000(%rip)     # 0xca750
callq 0x203fc
movq %rax, 0xa4ffc(%rip)     # 0xca758
leaq 0x25a11(%rip), %rcx     # 0x4b174
movq %rcx, 0x18(%rax)
leaq 0x25a1c(%rip), %rcx     # 0x4b18a
movq %rcx, 0x20(%rax)
movl %r14d, 0xa4883(%rip)    # 0xc9ffc
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0xa4d94(%rip)     # 0xca518
movl %eax, 0xa4d22(%rip)     # 0xca4ac
callq 0x48e4c
callq 0x2845e
movq %rax, 0xa4d1d(%rip)     # 0xca4b8
xorl %edi, %edi
callq 0x1fc50
movq %rax, 0xa4d97(%rip)     # 0xca540
movq %r14, 0xa4d98(%rip)     # 0xca548
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, 0xa4d94(%rip)    # 0xca550
movq %r14, 0xa4d45(%rip)     # 0xca508
pxor %xmm0, %xmm0
movdqu %xmm0, 0xa4b21(%rip)    # 0xca2f0
movdqu %xmm0, 0xa4b29(%rip)    # 0xca300
movdqu %xmm0, 0xa4b31(%rip)    # 0xca310
movdqu %xmm0, 0xa4b41(%rip)    # 0xca328
movdqu %xmm0, 0xa4b49(%rip)    # 0xca338
movq %r14, 0xa4b52(%rip)     # 0xca348
leaq 0xa4b8b(%rip), %rdi     # 0xca388
movl $0x84, %edx
xorl %esi, %esi
callq 0x1f110
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1f2f0
imulq $0xf4240, (%rbx), %rax  # imm = 0xF4240
addq 0x8(%rbx), %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
movq %rdx, 0xa4b33(%rip)     # 0xca378
movq %r14, 0xa4b34(%rip)     # 0xca380
xorl %edi, %edi
callq 0x1fc50
movq %rax, 0xa4a8e(%rip)     # 0xca2e8
movq %r14, 0xa4abf(%rip)     # 0xca320
movq %r14, 0xa4b08(%rip)     # 0xca370
movl %r14d, 0xa4cf1(%rip)    # 0xca560
movq %r14, 0xa4c86(%rip)     # 0xca4fc
movl %r14d, 0xa4d6b(%rip)    # 0xca5e8
callq 0x23e34
movq 0xa4747(%rip), %rdi     # 0xc9fd0
leaq -0x1a09(%rip), %rdx     # 0x23e87
movl $0x1, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20dc2
cmpq $-0x1, %rax
je 0x259fb
cmpl $0x0, 0xa485c(%rip)     # 0xca10c
jle 0x258fa
xorl %r14d, %r14d
leaq 0xa46ec(%rip), %r15     # 0xc9fa8
leaq 0x8a3c(%rip), %rbx      # 0x2e2ff
movq 0xa4706(%rip), %rdi     # 0xc9fd0
movl 0x124(%r15,%r14,4), %esi
movl $0x1, %edx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x20c22
cmpl $-0x1, %eax
je 0x259c4
incq %r14
movslq 0xa4817(%rip), %rax     # 0xca10c
cmpq %rax, %r14
jl 0x258c3
movl 0xa4810(%rip), %esi     # 0xca110
testl %esi, %esi
jle 0x25928
movq 0xa46c5(%rip), %rdi     # 0xc9fd0
leaq 0x8b31(%rip), %rcx      # 0x2e443
movl $0x1, %edx
xorl %r8d, %r8d
callq 0x20c22
cmpl $-0x1, %eax
je 0x25a25
cmpl $0x1, 0xa4b49(%rip)     # 0xca478
jne 0x25958
movq 0xa4b48(%rip), %rdi     # 0xca480
movl $0x441, %esi            # imm = 0x441
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x1f640
movl %eax, 0xa4b71(%rip)     # 0xca4c0
cmpl $-0x1, %eax
je 0x25a3a
cmpl $0x20, 0xa4699(%rip)    # 0xc9ff8
jne 0x25994
cmpq $0x0, 0xa4d5f(%rip)     # 0xca6c8
jne 0x25994
leaq 0x6f7d6(%rip), %rsi     # 0x95148
movl $0x3, %edi
xorl %eax, %eax
callq 0x23223
movl $0xc0000000, %eax       # imm = 0xC0000000
movq %rax, 0xa4d3e(%rip)     # 0xca6c8
movl $0x5, 0xa4d3c(%rip)     # 0xca6d0
cmpl $0x0, 0xa4dc9(%rip)     # 0xca764
je 0x259a2
callq 0x502a9
callq 0x35aa4
callq 0x5740a
callq 0x562b9
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x584b0
leaq 0x6f6eb(%rip), %rdi     # 0x950b6
leaq 0x7ad3e(%rip), %rsi     # 0xa0710
movl $0x875, %edx            # imm = 0x875
callq 0x4cae0
movl $0x1, %edi
callq 0x1f3b0
leaq 0x6f673(%rip), %rsi     # 0x95060
movl $0x3, %edi
xorl %eax, %eax
callq 0x23223
jmp 0x25a5c
leaq 0x6f68a(%rip), %rdi     # 0x9508c
leaq 0x7ad07(%rip), %rsi     # 0xa0710
movl $0x868, %edx            # imm = 0x868
jmp 0x259d7
leaq 0x6f636(%rip), %rsi     # 0x9504d
leaq 0xa4772(%rip), %rdx     # 0xca190
movl $0x3, %edi
jmp 0x25a55
leaq 0x6f6c1(%rip), %rdi     # 0x950ed
leaq 0x7acdd(%rip), %rsi     # 0xa0710
movl $0x87b, %edx            # imm = 0x87B
jmp 0x259d7
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
leaq 0x6f6d7(%rip), %rsi     # 0x95124
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x23223
movl $0x1, %edi
callq 0x1f2b0