// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/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 %r12
pushq %rbx
subq $0x18, %rsp
testb $0x1, 0x98(%rdi)
jne 0x480eb
movq %rdi, %rbx
leaq 0xbe9ce(%rip), %r12     # 0x106858
cmpq $0x0, 0x818(%r12)
je 0x47ea9
cmpl $0xf, 0x83c(%r12)
je 0x47ea9
leaq 0x7c70b(%rip), %rsi     # 0xc45b2
jmp 0x47ebc
movq %rbx, %rdi
callq 0x3f665
testl %eax, %eax
je 0x47ecf
leaq 0x7c724(%rip), %rsi     # 0xc45e0
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x3ffcb
movq %rbx, %rdi
callq 0x47084
leaq 0x7c731(%rip), %rsi     # 0xc460f
leaq 0xd321b(%rip), %rdx     # 0x11b100
movl $0x2, %edi
xorl %eax, %eax
callq 0x32ea6
movq 0x40(%rbx), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x77911(%rip), %rsi     # 0xbf814
callq 0x26300
testl %eax, %eax
je 0x48007
orb $0x1, 0x9a(%rbx)
incq 0x3d0(%r12)
movl $0x6, 0xa0(%rbx)
cmpl $0x0, 0x880(%r12)
je 0x47f3e
movl 0x8(%rbx), %esi
xorl %edi, %edi
callq 0x3090c
movl $0xffffffff, 0xa8(%rbx) # imm = 0xFFFFFFFF
orb $0x1, 0x98(%rbx)
movq 0x1e0(%r12), %rdi
movq %rbx, %rsi
callq 0x2e40f
movl 0x720(%r12), %ecx
movl 0x76c(%r12), %eax
cmpl $-0x1, %ecx
setne %dl
cmpl $0x1, %eax
sete %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x47fe9
movq 0x1e0(%r12), %rdi
leaq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0x2e582
movq %r14, %rdi
callq 0x2e59f
testq %rax, %rax
sete %cl
je 0x48065
movq 0x10(%rax), %r14
cmpl $0x7, 0xa0(%r14)
je 0x48065
leaq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x2e59f
testq %rax, %rax
sete %cl
je 0x48065
movq 0x10(%rax), %r14
cmpl $0x7, 0xa0(%r14)
jne 0x47fc5
jmp 0x48065
cmpl $-0x1, %ecx
setne %cl
cmpl $0x2, %eax
sete %al
andb %cl, %al
cmpb $0x1, %al
jne 0x48039
leaq 0x7c6a1(%rip), %rsi     # 0xc46a3
jmp 0x480c0
movq %rbx, %rdi
callq 0x47a36
testl %eax, %eax
je 0x480a4
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
cmpb $0x3f, (%rax)
je 0x47f17
incq 0x3e0(%r12)
jmp 0x47f17
cmpl $0x0, 0x804(%r12)
je 0x4804d
testb $0x1, 0x148(%rbx)
jne 0x480ae
cmpl $-0x1, 0x68c(%r12)
je 0x480f7
leaq 0x7c6a7(%rip), %rsi     # 0xc470a
jmp 0x480c0
leaq 0x7c5e8(%rip), %rsi     # 0xc4654
testb %cl, %cl
jne 0x480c0
movl 0x148(%rbx), %eax
notl %eax
testl %eax, 0x148(%r14)
jne 0x480c0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x40811
movq 0xe0(%r14), %rsi
movq %rbx, %rdi
callq 0x479a4
leaq 0x7c58f(%rip), %rsi     # 0xc4631
jmp 0x480c0
incq 0x3d8(%r12)
jmp 0x480eb
cmpl $0x0, 0x808(%r12)
je 0x480cc
leaq 0x7c626(%rip), %rsi     # 0xc46e6
movl $0x2, %edi
xorl %eax, %eax
callq 0x32ea6
movq 0x1e0(%r12), %rax
cmpq $0x1, 0x28(%rax)
jne 0x480eb
cmpq $0x0, 0x7c0(%r12)
jne 0x480eb
callq 0x47131
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movl 0x148(%rbx), %edi
callq 0x47cc7
jmp 0x480cc