// File: /workspace/github_c_makefile_star2_O3/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 0x44ea7
movq %rdi, %rbx
leaq 0xbcc1e(%rip), %r13     # 0x1018a8
cmpq $0x0, 0x828(%r13)
je 0x44ca7
cmpl $0xf, 0x84c(%r13)
je 0x44ca7
leaq 0x7c9a1(%rip), %rsi     # 0xc1646
jmp 0x44cba
movq %rbx, %rdi
callq 0x3c29e
testl %eax, %eax
je 0x44ccf
leaq 0x7c9ba(%rip), %rsi     # 0xc1674
movq %rbx, %rdi
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0x3cc06
movq %rbx, %rdi
callq 0x43e7c
leaq 0x7c9c5(%rip), %rsi     # 0xc16a3
leaq 0xd147b(%rip), %rdx     # 0x116160
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
movq 0x40(%rbx), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x77b61(%rip), %rsi     # 0xbc864
callq 0x262f0
testl %eax, %eax
je 0x44e11
orb $0x1, 0x9a(%rbx)
incq 0x3e0(%r13)
movl $0x6, 0xa0(%rbx)
cmpl $0x0, 0x890(%r13)
je 0x44d3c
movl 0x8(%rbx), %esi
xorl %edi, %edi
callq 0x2d834
movl $0xffffffff, 0xa8(%rbx) # imm = 0xFFFFFFFF
orb $0x1, 0x98(%rbx)
movq 0x1e0(%r13), %rdi
movq %rbx, %rsi
callq 0x2b24b
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 0x44df6
movq 0x1e0(%r13), %rdi
movq %rsp, %r15
movq %r15, %rsi
callq 0x2b3b7
leaq 0x7c94f(%rip), %r14     # 0xc16e8
movq %r15, %rdi
callq 0x2b3d4
testq %rax, %rax
je 0x44de9
movq 0x10(%rax), %r12
cmpl $0x7, 0xa0(%r12)
jne 0x44d99
movl 0x148(%rbx), %eax
notl %eax
testl %eax, 0x148(%r12)
jne 0x44de9
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3d448
movq 0xe0(%r12), %rsi
movq %rbx, %rdi
callq 0x44798
leaq 0x7c8dc(%rip), %r14     # 0xc16c5
movl $0x2, %edi
movq %r14, %rsi
jmp 0x44e83
cmpl $-0x1, %ecx
setne %cl
cmpl $0x2, %eax
sete %al
andb %cl, %al
cmpb $0x1, %al
jne 0x44e3e
leaq 0x7c928(%rip), %rsi     # 0xc1737
jmp 0x44e7e
movq %rbx, %rdi
callq 0x4482a
testl %eax, %eax
je 0x44e64
movq 0x40(%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
cmpb $0x3f, (%rax)
je 0x44d17
incq 0x3f0(%r13)
jmp 0x44d17
cmpl $0x0, 0x814(%r13)
je 0x44e51
testb $0x1, 0x148(%rbx)
jne 0x44e6d
cmpl $-0x1, 0x69c(%r13)
je 0x44eb5
leaq 0x7c93c(%rip), %rsi     # 0xc179e
jmp 0x44e7e
incq 0x3e8(%r13)
jmp 0x44ea7
cmpl $0x0, 0x818(%r13)
je 0x44e8a
leaq 0x7c8fc(%rip), %rsi     # 0xc177a
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
movq 0x1e0(%r13), %rax
cmpq $0x1, 0x28(%rax)
jne 0x44ea7
cmpq $0x0, 0x7d0(%r13)
jne 0x44ea7
callq 0x43f33
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movl 0x148(%rbx), %edi
callq 0x44abb
jmp 0x44e8a