// File: /workspace/github_c_makefile_star2_O2/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 0xb996f(%rip), %r13     # 0xf98a8
cmpq $0x0, 0x828(%r13)
je 0x3ff82
movl 0x84c(%r13), %eax
addl $-0xe, %eax
cmpl $-0xc, %eax
jb 0x3ff82
xorl %edi, %edi
callq 0x26cd0
subq 0x878(%r13), %rax
movslq 0x834(%r13), %rcx
cmpq %rcx, %rax
jle 0x3ff82
leaq 0x73e3d(%rip), %rsi     # 0xb3db0
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
callq 0x3e882
cmpq $0x0, 0x828(%r13)
je 0x3ffc6
cmpl $0xe, 0x84c(%r13)
jne 0x3ffc6
xorl %edi, %edi
callq 0x26cd0
subq 0x878(%r13), %rax
movslq 0x834(%r13), %rcx
cmpq %rcx, %rax
jle 0x3ffc6
leaq 0x73e1d(%rip), %rsi     # 0xb3dd4
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
callq 0x3e882
cmpq $0x0, 0x828(%r13)
setne %cl
movl 0x84c(%r13), %eax
cmpl $0xf, %eax
sete %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x4002a
xorl %edi, %edi
callq 0x26cd0
movq 0x838(%r13), %rcx
subq 0x88(%rcx), %rax
movslq 0x834(%r13), %rcx
cmpq %rcx, %rax
jle 0x40023
leaq 0x73e53(%rip), %rsi     # 0xb3e60
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
movq 0x838(%r13), %rdi
callq 0x37527
movl 0x84c(%r13), %eax
cmpl $0x1, %eax
jne 0x40068
movq 0x828(%r13), %rdx
movl 0x830(%r13), %ecx
leaq 0x73e49(%rip), %rsi     # 0xb3e8d
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x2b699
callq 0x3f517
testl %eax, %eax
jne 0x40068
leaq 0x73e4a(%rip), %rsi     # 0xb3ea8
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x2b699
cmpq $0x0, 0x828(%r13)
setne %cl
movq 0x838(%r13), %rax
testq %rax, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x40095
testb $0x1, 0x9a(%rax)
jne 0x40095
callq 0x3de95
movq 0xce184(%rip), %rax     # 0x10e220
movslq 0x7c8(%r13), %rcx
cqto
idivq %rcx
testq %rdx, %rdx
jne 0x400e5
leaq 0x7370d(%rip), %rdi     # 0xb37c1
pushq $0x4
popq %rsi
callq 0x39e29
movq %rax, %rbx
leaq 0x10(%rsp), %rdx
movq %rax, (%rdx)
movq 0x1e0(%r13), %rdi
movl 0x7bc(%r13), %esi
pushq $0x1
popq %rcx
callq 0x3cf58
movq %rbx, %rdi
callq 0x3a265
movq 0x1e0(%r13), %rdi
leaq 0x18(%rsp), %rbx
movq %rbx, %rsi
callq 0x2702c
leaq 0x70cb9(%rip), %r14     # 0xb0db9
pushq $0x1
popq %r15
movq %rbx, %rdi
callq 0x27046
testq %rax, %rax
je 0x4013f
movq 0x10(%rax), %rax
movl 0xa0(%rax), %ecx
cmpl $0x6, %ecx
je 0x4012f
cmpl $0x7, %ecx
jne 0x40104
cmpl $0x2, 0x77c(%r13)
je 0x40104
movl 0x8(%rax), %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x26bb0
jmp 0x40104
movq 0x1e0(%r13), %rdi
cmpq $0x0, 0x28(%rdi)
je 0x401dd
movq %rsp, %rbx
movq %rbx, %rsi
callq 0x2702c
leaq 0x73d63(%rip), %r14     # 0xb3ec6
leaq 0xcdff6(%rip), %r15     # 0x10e160
pushq $0x3
popq %rbp
movq %rbx, %rdi
callq 0x27046
testq %rax, %rax
je 0x401d0
movq 0x10(%rax), %r12
cmpl $0x9, 0xa0(%r12)
jne 0x4016d
testb $0x1, 0x9a(%r12)
jne 0x4016d
movq 0x978(%r13), %rax
subq 0xd8(%r12), %rax
movslq 0x834(%r13), %rcx
cmpq %rcx, %rax
jle 0x4016d
movq %r12, %rdi
callq 0x3cc38
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2b699
movq %r12, %rdi
callq 0x37527
jmp 0x4016d
movq 0x1e0(%r13), %rax
movq 0x28(%rax), %rax
jmp 0x401df
xorl %eax, %eax
testq %rax, %rax
jne 0x40239
movq 0x7f8(%r13), %rcx
testq %rcx, %rcx
je 0x40239
cmpq $0x0, 0x7d0(%r13)
je 0x40239
movq 0x978(%r13), %rax
subq 0x800(%r13), %rax
cmpq %rcx, %rax
jle 0x40237
callq 0x3cdc1
movl 0x7f8(%r13), %edx
leaq 0x73cc7(%rip), %rsi     # 0xb3ee7
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x2b699
movq 0x1e0(%r13), %rax
movq 0x28(%rax), %rax
jmp 0x40239
xorl %eax, %eax
testq %rax, %rax
jne 0x4025b
cmpl $0x0, 0x668(%r13)
jne 0x4025b
movq 0x8e8(%r13), %rax
cmpq $0x0, 0x28(%rax)
je 0x4025b
callq 0x3d949
cmpl $-0x1, 0x730(%r13)
jne 0x402ea
cmpl $-0x1, 0x69c(%r13)
jne 0x402ea
movq 0x1e0(%r13), %rdi
movq %rsp, %r14
movq %r14, %rsi
callq 0x2702c
pushq $-0x1
popq %rbx
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %r14, %rdi
callq 0x27046
testq %rax, %rax
je 0x402c8
movq 0x10(%rax), %rax
cmpl $0x6, 0xa0(%rax)
jne 0x4028e
movq 0x978(%r13), %rcx
subq 0x88(%rax), %rcx
cmpq %r15, %rcx
cmovgq %rcx, %r15
incl %r12d
andl 0x148(%rax), %ebx
jmp 0x4028e
testl %r12d, %r12d
je 0x402ea
cmpl $0x0, 0x814(%r13)
je 0x402e3
movslq 0x818(%r13), %rax
cmpq %rax, %r15
jle 0x402ea
movl %ebx, %edi
callq 0x3d7fb
callq 0x3d779
incq 0xcdf2a(%rip)           # 0x10e220
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)