// File: /workspace/github_c_makefile_star2_O3/gplessis[P]dotdeb-redis/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
leaq 0xba36a(%rip), %r13     # 0x1018a8
cmpq $0x0, 0x828(%r13)
je 0x47589
movl 0x84c(%r13), %eax
addl $-0xe, %eax
cmpl $-0xc, %eax
jb 0x47589
xorl %edi, %edi
callq 0x26cd0
subq 0x878(%r13), %rax
movslq 0x834(%r13), %rcx
cmpq %rcx, %rax
jle 0x47589
leaq 0x7b0e8(%rip), %rsi     # 0xc2660
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
callq 0x45c96
cmpq $0x0, 0x828(%r13)
je 0x475cf
cmpl $0xe, 0x84c(%r13)
jne 0x475cf
xorl %edi, %edi
callq 0x26cd0
subq 0x878(%r13), %rax
movslq 0x834(%r13), %rcx
cmpq %rcx, %rax
jle 0x475cf
leaq 0x7b0c6(%rip), %rsi     # 0xc2684
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
callq 0x45c96
cmpq $0x0, 0x828(%r13)
setne %cl
movl 0x84c(%r13), %eax
cmpl $0xf, %eax
sete %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x47635
xorl %edi, %edi
callq 0x26cd0
movq 0x838(%r13), %rcx
subq 0x88(%rcx), %rax
movslq 0x834(%r13), %rcx
cmpq %rcx, %rax
jle 0x4762e
leaq 0x7b0fa(%rip), %rsi     # 0xc2710
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
movq 0x838(%r13), %rdi
callq 0x3d73b
movl 0x84c(%r13), %eax
cmpl $0x1, %eax
jne 0x47677
movq 0x828(%r13), %rdx
movl 0x830(%r13), %ecx
leaq 0x7b0ee(%rip), %rsi     # 0xc273d
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
callq 0x46a7a
testl %eax, %eax
jne 0x47677
leaq 0x7b0ed(%rip), %rsi     # 0xc2758
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
cmpq $0x0, 0x828(%r13)
setne %cl
movq 0x838(%r13), %rax
testq %rax, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x476a4
testb $0x1, 0x9a(%rax)
jne 0x476a4
callq 0x45213
movq 0xceb75(%rip), %rax     # 0x116220
movslq 0x7c8(%r13), %rcx
cqto
idivq %rcx
testq %rdx, %rdx
jne 0x476f8
leaq 0x7a9ae(%rip), %rdi     # 0xc2071
movl $0x4, %esi
callq 0x40744
movq %rax, %rbx
leaq 0x10(%rsp), %rdx
movq %rax, (%rdx)
movq 0x1e0(%r13), %rdi
movl 0x7bc(%r13), %esi
movl $0x1, %ecx
callq 0x441a9
movq %rbx, %rdi
callq 0x40c8f
movq 0x1e0(%r13), %rdi
leaq 0x18(%rsp), %rbx
movq %rbx, %rsi
callq 0x2b3b7
movq %rbx, %rdi
callq 0x2b3d4
testq %rax, %rax
je 0x47760
leaq 0x777a9(%rip), %rbx     # 0xbeec9
leaq 0x18(%rsp), %r14
movq 0x10(%rax), %rax
movl 0xa0(%rax), %ecx
cmpl $0x6, %ecx
je 0x47743
cmpl $0x7, %ecx
jne 0x47753
cmpl $0x2, 0x77c(%r13)
je 0x47753
movl 0x8(%rax), %edi
movl $0x1, %edx
movq %rbx, %rsi
callq 0x26bb0
movq %r14, %rdi
callq 0x2b3d4
testq %rax, %rax
jne 0x47725
movq 0x1e0(%r13), %rdi
cmpq $0x0, 0x28(%rdi)
je 0x4780c
movq %rsp, %rbx
movq %rbx, %rsi
callq 0x2b3b7
movq %rbx, %rdi
callq 0x2b3d4
testq %rax, %rax
je 0x477ff
leaq 0x7afe5(%rip), %rbx     # 0xc2776
leaq 0xce9c8(%rip), %r14     # 0x116160
movq %rsp, %r15
movq 0x10(%rax), %r12
cmpl $0x9, 0xa0(%r12)
jne 0x477f2
testb $0x1, 0x9a(%r12)
jne 0x477f2
movq 0x978(%r13), %rax
subq 0xd8(%r12), %rax
movslq 0x834(%r13), %rcx
cmpq %rcx, %rax
jle 0x477f2
movq %r12, %rdi
callq 0x43e7c
movl $0x3, %edi
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2fdad
movq %r12, %rdi
callq 0x3d73b
movq %r15, %rdi
callq 0x2b3d4
testq %rax, %rax
jne 0x4779b
movq 0x1e0(%r13), %rax
movq 0x28(%rax), %rax
jmp 0x4780e
xorl %eax, %eax
testq %rax, %rax
jne 0x4786a
movq 0x7f8(%r13), %rcx
testq %rcx, %rcx
je 0x4786a
cmpq $0x0, 0x7d0(%r13)
je 0x4786a
movq 0x978(%r13), %rax
subq 0x800(%r13), %rax
cmpq %rcx, %rax
jle 0x47868
callq 0x4400c
movl 0x7f8(%r13), %edx
leaq 0x7af48(%rip), %rsi     # 0xc2797
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
movq 0x1e0(%r13), %rax
movq 0x28(%rax), %rax
jmp 0x4786a
xorl %eax, %eax
testq %rax, %rax
jne 0x4788c
cmpl $0x0, 0x668(%r13)
jne 0x4788c
movq 0x8e8(%r13), %rax
cmpq $0x0, 0x28(%rax)
je 0x4788c
callq 0x44c36
cmpl $-0x1, 0x730(%r13)
jne 0x4792c
cmpl $-0x1, 0x69c(%r13)
jne 0x4792c
movq 0x1e0(%r13), %rdi
movq %rsp, %rbx
movq %rbx, %rsi
callq 0x2b3b7
movq %rbx, %rdi
callq 0x2b3d4
testq %rax, %rax
je 0x4792c
xorl %r15d, %r15d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq %rsp, %r14
xorl %ebp, %ebp
movq 0x10(%rax), %rax
cmpl $0x6, 0xa0(%rax)
jne 0x478fe
movq 0x978(%r13), %rcx
subq 0x88(%rax), %rcx
cmpq %r15, %rcx
cmovgq %rcx, %r15
incl %ebp
andl 0x148(%rax), %ebx
movq %r14, %rdi
callq 0x2b3d4
testq %rax, %rax
jne 0x478d4
testl %ebp, %ebp
je 0x4792c
cmpl $0x0, 0x814(%r13)
je 0x47925
movslq 0x818(%r13), %rax
cmpq %rax, %r15
jle 0x4792c
movl %ebx, %edi
callq 0x44abb
callq 0x44a2b
incq 0xce8e8(%rip)           # 0x116220
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop