// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/src/replication.c
// Function: replicationCron
void replicationCron(void) {
    static long long replication_cron_loops = 0;

    /* Non blocking connection timeout? */
    if (server.masterhost &&
        (server.repl_state == REPL_STATE_CONNECTING ||
         slaveIsInHandshakeState()) &&
         (time(NULL)-server.repl_transfer_lastio) > server.repl_timeout)
    {
        serverLog(LL_WARNING,"Timeout connecting to the MASTER...");
        cancelReplicationHandshake();
    }

    /* Bulk transfer I/O timeout? */
    if (server.masterhost && server.repl_state == REPL_STATE_TRANSFER &&
        (time(NULL)-server.repl_transfer_lastio) > server.repl_timeout)
    {
        serverLog(LL_WARNING,"Timeout receiving bulk data from MASTER... If the problem persists try to set the 'repl-timeout' parameter in redis.conf to a larger value.");
        cancelReplicationHandshake();
    }

    /* Timed out master when we are an already connected slave? */
    if (server.masterhost && server.repl_state == REPL_STATE_CONNECTED &&
        (time(NULL)-server.master->lastinteraction) > server.repl_timeout)
    {
        serverLog(LL_WARNING,"MASTER timeout: no data nor PING received...");
        freeClient(server.master);
    }

    /* Check if we should connect to a MASTER */
    if (server.repl_state == REPL_STATE_CONNECT) {
        serverLog(LL_NOTICE,"Connecting to MASTER %s:%d",
            server.masterhost, server.masterport);
        if (connectWithMaster() == C_OK) {
            serverLog(LL_NOTICE,"MASTER <-> SLAVE sync started");
        }
    }

    /* Send ACK to master from time to time.
     * Note that we do not send periodic acks to masters that don't
     * support PSYNC and replication offsets. */
    if (server.masterhost && server.master &&
        !(server.master->flags & CLIENT_PRE_PSYNC))
        replicationSendAck();

    /* If we have attached slaves, PING them from time to time.
     * So slaves can implement an explicit timeout to masters, and will
     * be able to detect a link disconnection even if the TCP connection
     * will not actually go down. */
    listIter li;
    listNode *ln;
    robj *ping_argv[1];

    /* First, send PING according to ping_slave_period. */
    if ((replication_cron_loops % server.repl_ping_slave_period) == 0) {
        ping_argv[0] = createStringObject("PING",4);
        replicationFeedSlaves(server.slaves, server.slaveseldb,
            ping_argv, 1);
        decrRefCount(ping_argv[0]);
    }

    /* Second, send a newline to all the slaves in pre-synchronization
     * stage, that is, slaves waiting for the master to create the RDB file.
     * The newline will be ignored by the slave but will refresh the
     * last-io timer preventing a timeout. In this case we ignore the
     * ping period and refresh the connection once per second since certain
     * timeouts are set at a few seconds (example: PSYNC response). */
    listRewind(server.slaves,&li);
    while((ln = listNext(&li))) {
        client *slave = ln->value;

        if (slave->replstate == SLAVE_STATE_WAIT_BGSAVE_START ||
            (slave->replstate == SLAVE_STATE_WAIT_BGSAVE_END &&
             server.rdb_child_type != RDB_CHILD_TYPE_SOCKET))
        {
            if (write(slave->fd, "\n", 1) == -1) {
                /* Don't worry, it's just a ping. */
            }
        }
    }

    /* Disconnect timedout slaves. */
    if (listLength(server.slaves)) {
        listIter li;
        listNode *ln;

        listRewind(server.slaves,&li);
        while((ln = listNext(&li))) {
            client *slave = ln->value;

            if (slave->replstate != SLAVE_STATE_ONLINE) continue;
            if (slave->flags & CLIENT_PRE_PSYNC) continue;
            if ((server.unixtime - slave->repl_ack_time) > server.repl_timeout)
            {
                serverLog(LL_WARNING, "Disconnecting timedout slave: %s",
                    replicationGetSlaveName(slave));
                freeClient(slave);
            }
        }
    }

    /* If we have no attached slaves and there is a replication backlog
     * using memory, free it after some (configured) time. */
    if (listLength(server.slaves) == 0 && server.repl_backlog_time_limit &&
        server.repl_backlog)
    {
        time_t idle = server.unixtime - server.repl_no_slaves_since;

        if (idle > server.repl_backlog_time_limit) {
            freeReplicationBacklog();
            serverLog(LL_NOTICE,
                "Replication backlog freed after %d seconds "
                "without connected slaves.",
                (int) server.repl_backlog_time_limit);
        }
    }

    /* If AOF is disabled and we no longer have attached slaves, we can
     * free our Replication Script Cache as there is no need to propagate
     * EVALSHA at all. */
    if (listLength(server.slaves) == 0 &&
        server.aof_state == AOF_OFF &&
        listLength(server.repl_scriptcache_fifo) != 0)
    {
        replicationScriptCacheFlush();
    }

    /* Start a BGSAVE good for replication if we have slaves in
     * WAIT_BGSAVE_START state.
     *
     * In case of diskless replication, we make sure to wait the specified
     * number of seconds (according to configuration) so that other slaves
     * have the time to arrive before we start streaming. */
    if (server.rdb_child_pid == -1 && server.aof_child_pid == -1) {
        time_t idle, max_idle = 0;
        int slaves_waiting = 0;
        int mincapa = -1;
        listNode *ln;
        listIter li;

        listRewind(server.slaves,&li);
        while((ln = listNext(&li))) {
            client *slave = ln->value;
            if (slave->replstate == SLAVE_STATE_WAIT_BGSAVE_START) {
                idle = server.unixtime - slave->lastinteraction;
                if (idle > max_idle) max_idle = idle;
                slaves_waiting++;
                mincapa = (mincapa == -1) ? slave->slave_capa :
                                            (mincapa & slave->slave_capa);
            }
        }

        if (slaves_waiting &&
            (!server.repl_diskless_sync ||
             max_idle > server.repl_diskless_sync_delay))
        {
            /* Start the BGSAVE. The called function may start a
             * BGSAVE with socket target or disk target depending on the
             * configuration and slaves capabilities. */
            startBgsaveForReplication(mincapa);
        }
    }

    /* Refresh the number of slaves with lag <= min-slaves-max-lag. */
    refreshGoodSlavesCount();
    replication_cron_loops++; /* Incremented with frequency 1 HZ. */
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x30, %rsp
leaq 0xbc035(%rip), %r13     # 0x106858
cmpq $0x0, 0x818(%r13)
je 0x4a86e
movl 0x83c(%r13), %eax
addl $-0xe, %eax
cmpl $-0xc, %eax
jb 0x4a86e
xorl %edi, %edi
callq 0x26ce0
subq 0x868(%r13), %rax
movslq 0x824(%r13), %rcx
cmpq %rcx, %rax
jle 0x4a86e
leaq 0x7ad6f(%rip), %rsi     # 0xc55cc
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
callq 0x48f1f
cmpq $0x0, 0x818(%r13)
je 0x4a8b4
cmpl $0xe, 0x83c(%r13)
jne 0x4a8b4
xorl %edi, %edi
callq 0x26ce0
subq 0x868(%r13), %rax
movslq 0x824(%r13), %rcx
cmpq %rcx, %rax
jle 0x4a8b4
leaq 0x7ad4d(%rip), %rsi     # 0xc55f0
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
callq 0x48f1f
cmpq $0x0, 0x818(%r13)
je 0x4a908
cmpl $0xf, 0x83c(%r13)
jne 0x4a908
xorl %edi, %edi
callq 0x26ce0
movq 0x828(%r13), %rcx
subq 0x88(%rcx), %rax
movslq 0x824(%r13), %rcx
cmpq %rcx, %rax
jle 0x4a908
leaq 0x7ad8c(%rip), %rsi     # 0xc567c
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
movq 0x828(%r13), %rdi
callq 0x40b04
cmpl $0x1, 0x83c(%r13)
jne 0x4a94f
movq 0x818(%r13), %rdx
movl 0x820(%r13), %ecx
leaq 0x7ad82(%rip), %rsi     # 0xc56a9
movl $0x2, %edi
xorl %eax, %eax
callq 0x32ea6
callq 0x49d5d
testl %eax, %eax
jne 0x4a94f
leaq 0x7ad81(%rip), %rsi     # 0xc56c4
movl $0x2, %edi
xorl %eax, %eax
callq 0x32ea6
cmpq $0x0, 0x818(%r13)
setne %cl
movq 0x828(%r13), %rax
testq %rax, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x4a97c
testb $0x1, 0x9a(%rax)
jne 0x4a97c
callq 0x4848c
movq 0xd083d(%rip), %rax     # 0x11b1c0
movslq 0x7b8(%r13), %rcx
cqto
idivq %rcx
testq %rdx, %rdx
jne 0x4a9d0
leaq 0x7a642(%rip), %rdi     # 0xc4fdd
movl $0x4, %esi
callq 0x43ba8
movq %rax, %rbx
leaq 0x18(%rsp), %rdx
movq %rax, (%rdx)
movq 0x1e0(%r13), %rdi
movl 0x7ac(%r13), %esi
movl $0x1, %ecx
callq 0x47393
movq %rbx, %rdi
callq 0x440f3
movq 0x1e0(%r13), %rdi
leaq 0x20(%rsp), %rbx
movq %rbx, %rsi
callq 0x2e582
movq %rbx, %rdi
callq 0x2e59f
testq %rax, %rax
je 0x4aa38
leaq 0x7745f(%rip), %rbx     # 0xc1e57
leaq 0x20(%rsp), %r14
movq 0x10(%rax), %rax
movl 0xa0(%rax), %ecx
cmpl $0x6, %ecx
je 0x4aa1b
cmpl $0x7, %ecx
jne 0x4aa2b
cmpl $0x2, 0x76c(%r13)
je 0x4aa2b
movl 0x8(%rax), %edi
movl $0x1, %edx
movq %rbx, %rsi
callq 0x26bc0
movq %r14, %rdi
callq 0x2e59f
testq %rax, %rax
jne 0x4a9fd
movq 0x1e0(%r13), %rdi
cmpq $0x0, 0x28(%rdi)
je 0x4aadb
leaq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x2e582
movq %rbx, %rdi
callq 0x2e59f
testq %rax, %rax
je 0x4aadb
leaq 0x7ac77(%rip), %rbx     # 0xc56e2
leaq 0xd068e(%rip), %r14     # 0x11b100
leaq 0x8(%rsp), %r15
movq 0x10(%rax), %r12
cmpl $0x9, 0xa0(%r12)
jne 0x4aace
testb $0x1, 0x9a(%r12)
jne 0x4aace
movq 0x968(%r13), %rax
subq 0xd8(%r12), %rax
movslq 0x824(%r13), %rcx
cmpq %rcx, %rax
jle 0x4aace
movq %r12, %rdi
callq 0x47084
movl $0x3, %edi
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x32ea6
movq %r12, %rdi
callq 0x40b04
movq %r15, %rdi
callq 0x2e59f
testq %rax, %rax
jne 0x4aa77
movq 0x1e0(%r13), %rax
cmpq $0x0, 0x28(%rax)
jne 0x4ab31
movq 0x7e8(%r13), %rax
testq %rax, %rax
je 0x4ab31
cmpq $0x0, 0x7c0(%r13)
je 0x4ab31
movq 0x968(%r13), %rcx
subq 0x7f0(%r13), %rcx
cmpq %rax, %rcx
jle 0x4ab31
callq 0x4720a
movl 0x7e8(%r13), %edx
leaq 0x7abde(%rip), %rsi     # 0xc5703
movl $0x2, %edi
xorl %eax, %eax
callq 0x32ea6
movq 0x1e0(%r13), %rax
cmpq $0x0, 0x28(%rax)
jne 0x4ab5c
cmpl $0x0, 0x658(%r13)
jne 0x4ab5c
movq 0x8d8(%r13), %rax
cmpq $0x0, 0x28(%rax)
je 0x4ab5c
callq 0x47e38
cmpl $-0x1, 0x720(%r13)
jne 0x4ac0f
cmpl $-0x1, 0x68c(%r13)
jne 0x4ac0f
movq 0x1e0(%r13), %rdi
leaq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x2e582
movq %rbx, %rdi
callq 0x2e59f
xorl %r12d, %r12d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x4abe9
leaq 0x8(%rsp), %r14
xorl %r15d, %r15d
movq 0x10(%rax), %rax
cmpl $0x6, 0xa0(%rax)
jne 0x4abd4
movq 0x968(%r13), %rcx
subq 0x88(%rax), %rcx
cmpq %r15, %rcx
cmovgq %rcx, %r15
incl %r12d
andl 0x148(%rax), %ebx
movq %r14, %rdi
callq 0x2e59f
testq %rax, %rax
jne 0x4aba9
testl %r12d, %r12d
sete %al
jmp 0x4abee
movb $0x1, %al
xorl %r15d, %r15d
testb %al, %al
jne 0x4ac0f
cmpl $0x0, 0x804(%r13)
je 0x4ac08
movslq 0x808(%r13), %rax
cmpq %rax, %r15
jle 0x4ac0f
movl %ebx, %edi
callq 0x47cc7
callq 0x47c37
incq 0xd05a5(%rip)           # 0x11b1c0
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
nopl (%rax)