// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/src/replication.c
// Function: syncCommand
void syncCommand(client *c) {
    /* ignore SYNC if already slave or in monitor mode */
    if (c->flags & CLIENT_SLAVE) return;

    /* Refuse SYNC requests if we are a slave but the link with our master
     * is not ok... */
    if (server.masterhost && server.repl_state != REPL_STATE_CONNECTED) {
        addReplyError(c,"Can't SYNC while not connected with my master");
        return;
    }

    /* SYNC can't be issued when the server has pending data to send to
     * the client about already issued commands. We need a fresh reply
     * buffer registering the differences between the BGSAVE and the current
     * dataset, so that we can copy to other slaves if needed. */
    if (clientHasPendingReplies(c)) {
        addReplyError(c,"SYNC and PSYNC are invalid with pending output");
        return;
    }

    serverLog(LL_NOTICE,"Slave %s asks for synchronization",
        replicationGetSlaveName(c));

    /* Try a partial resynchronization if this is a PSYNC command.
     * If it fails, we continue with usual full resynchronization, however
     * when this happens masterTryPartialResynchronization() already
     * replied with:
     *
     * +FULLRESYNC <runid> <offset>
     *
     * So the slave knows the new runid and offset to try a PSYNC later
     * if the connection with the master is lost. */
    if (!strcasecmp(c->argv[0]->ptr,"psync")) {
        if (masterTryPartialResynchronization(c) == C_OK) {
            server.stat_sync_partial_ok++;
            return; /* No full resync needed, return. */
        } else {
            char *master_runid = c->argv[1]->ptr;

            /* Increment stats for failed PSYNCs, but only if the
             * runid is not "?", as this is used by slaves to force a full
             * resync on purpose when they are not albe to partially
             * resync. */
            if (master_runid[0] != '?') server.stat_sync_partial_err++;
        }
    } else {
        /* If a slave uses SYNC, we are dealing with an old implementation
         * of the replication protocol (like redis-cli --slave). Flag the client
         * so that we don't expect to receive REPLCONF ACK feedbacks. */
        c->flags |= CLIENT_PRE_PSYNC;
    }

    /* Full resynchronization. */
    server.stat_sync_full++;

    /* Setup the slave as one waiting for BGSAVE to start. The following code
     * paths will change the state if we handle the slave differently. */
    c->replstate = SLAVE_STATE_WAIT_BGSAVE_START;
    if (server.repl_disable_tcp_nodelay)
        anetDisableTcpNoDelay(NULL, c->fd); /* Non critical if it fails. */
    c->repldbfd = -1;
    c->flags |= CLIENT_SLAVE;
    listAddNodeTail(server.slaves,c);

    /* CASE 1: BGSAVE is in progress, with disk target. */
    if (server.rdb_child_pid != -1 &&
        server.rdb_child_type == RDB_CHILD_TYPE_DISK)
    {
        /* Ok a background save is in progress. Let's check if it is a good
         * one for replication, i.e. if there is another slave that is
         * registering differences since the server forked to save. */
        client *slave;
        listNode *ln;
        listIter li;

        listRewind(server.slaves,&li);
        while((ln = listNext(&li))) {
            slave = ln->value;
            if (slave->replstate == SLAVE_STATE_WAIT_BGSAVE_END) break;
        }
        /* To attach this slave, we check that it has at least all the
         * capabilities of the slave that triggered the current BGSAVE. */
        if (ln && ((c->slave_capa & slave->slave_capa) == slave->slave_capa)) {
            /* Perfect, the server is already registering differences for
             * another slave. Set the right state, and copy the buffer. */
            copyClientOutputBuffer(c,slave);
            replicationSetupSlaveForFullResync(c,slave->psync_initial_offset);
            serverLog(LL_NOTICE,"Waiting for end of BGSAVE for SYNC");
        } else {
            /* No way, we need to wait for the next BGSAVE in order to
             * register differences. */
            serverLog(LL_NOTICE,"Can't attach the slave to the current BGSAVE. Waiting for next BGSAVE for SYNC");
        }

    /* CASE 2: BGSAVE is in progress, with socket target. */
    } else if (server.rdb_child_pid != -1 &&
               server.rdb_child_type == RDB_CHILD_TYPE_SOCKET)
    {
        /* There is an RDB child process but it is writing directly to
         * children sockets. We need to wait for the next BGSAVE
         * in order to synchronize. */
        serverLog(LL_NOTICE,"Current BGSAVE has socket target. Waiting for next BGSAVE for SYNC");

    /* CASE 3: There is no BGSAVE is progress. */
    } else {
        if (server.repl_diskless_sync && (c->slave_capa & SLAVE_CAPA_EOF)) {
            /* Diskless replication RDB child is created inside
             * replicationCron() since we want to delay its start a
             * few seconds to wait for more slaves to arrive. */
            if (server.repl_diskless_sync_delay)
                serverLog(LL_NOTICE,"Delay next BGSAVE for diskless SYNC");
        } else {
            /* Target is disk (or the slave is not capable of supporting
             * diskless replication) and we don't have a BGSAVE in progress,
             * let's start one. */
            if (server.aof_child_pid == -1) {
                startBgsaveForReplication(c->slave_capa);
            } else {
                serverLog(LL_NOTICE,
                    "No BGSAVE in progress, but an AOF rewrite is active. "
                    "BGSAVE for replication delayed");
            }
        }
    }

    if (listLength(server.slaves) == 1 && server.repl_backlog == NULL)
        createReplicationBacklog();
    return;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x10, %rsp
testb $0x1, 0x98(%rdi)
jne 0x3dbb1
movq %rdi, %rbx
leaq 0xbbf0b(%rip), %r13     # 0xf98a8
cmpq $0x0, 0x828(%r13)
je 0x3d9ba
cmpl $0xf, 0x84c(%r13)
je 0x3d9ba
leaq 0x753de(%rip), %rsi     # 0xb2d96
jmp 0x3d9cd
movq %rbx, %rdi
callq 0x3655e
testl %eax, %eax
je 0x3d9e2
leaq 0x753f7(%rip), %rsi     # 0xb2dc4
movq %rbx, %rdi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x36b6f
movq %rbx, %rdi
callq 0x3cc38
leaq 0x75402(%rip), %rsi     # 0xb2df3
leaq 0xd0768(%rip), %rdx     # 0x10e160
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x2b699
movq 0x40(%rbx), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x70d40(%rip), %rsi     # 0xae754
callq 0x262f0
testl %eax, %eax
je 0x3db1d
orb $0x1, 0x9a(%rbx)
incq 0x3e0(%r13)
movl $0x6, 0xa0(%rbx)
cmpl $0x0, 0x890(%r13)
je 0x3da4d
movl 0x8(%rbx), %esi
xorl %edi, %edi
callq 0x293f4
orl $-0x1, 0xa8(%rbx)
orb $0x1, 0x98(%rbx)
movq 0x1e0(%r13), %rdi
movq %rbx, %rsi
callq 0x26ecc
movl 0x730(%r13), %ecx
movl 0x77c(%r13), %eax
cmpl $-0x1, %ecx
setne %dl
cmpl $0x1, %eax
sete %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x3db02
movq 0x1e0(%r13), %rdi
movq %rsp, %r15
movq %r15, %rsi
callq 0x2702c
leaq 0x75391(%rip), %r14     # 0xb2e38
movq %r15, %rdi
callq 0x27046
testq %rax, %rax
je 0x3daf7
movq 0x10(%rax), %r12
cmpl $0x7, 0xa0(%r12)
jne 0x3daa7
movl 0x148(%rbx), %eax
notl %eax
testl %eax, 0x148(%r12)
jne 0x3daf7
movq %rbx, %rdi
movq %r12, %rsi
callq 0x37237
movq 0xe0(%r12), %rsi
movq %rbx, %rdi
callq 0x3d4fe
leaq 0x7531e(%rip), %r14     # 0xb2e15
pushq $0x2
popq %rdi
movq %r14, %rsi
jmp 0x3db8d
cmpl $-0x1, %ecx
setne %cl
cmpl $0x2, %eax
sete %al
andb %cl, %al
cmpb $0x1, %al
jne 0x3db4a
leaq 0x7536c(%rip), %rsi     # 0xb2e87
jmp 0x3db8a
movq %rbx, %rdi
callq 0x3d58b
testl %eax, %eax
je 0x3db70
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
cmpb $0x3f, (%rax)
je 0x3da28
incq 0x3f0(%r13)
jmp 0x3da28
cmpl $0x0, 0x814(%r13)
je 0x3db5d
testb $0x1, 0x148(%rbx)
jne 0x3db79
cmpl $-0x1, 0x69c(%r13)
je 0x3dbbf
leaq 0x75380(%rip), %rsi     # 0xb2eee
jmp 0x3db8a
incq 0x3e8(%r13)
jmp 0x3dbb1
cmpl $0x0, 0x818(%r13)
je 0x3db94
leaq 0x75340(%rip), %rsi     # 0xb2eca
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x2b699
movq 0x1e0(%r13), %rax
cmpq $0x1, 0x28(%rax)
jne 0x3dbb1
cmpq $0x0, 0x7d0(%r13)
jne 0x3dbb1
callq 0x3ccec
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movl 0x148(%rbx), %edi
callq 0x3d7fb
jmp 0x3db94