// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movl 0x98(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x4eb63
jmp 0x4eed5
leaq 0x12966e(%rip), %rax    # 0x1781d8
cmpq $0x0, 0x828(%rax)
je 0x4eb99
leaq 0x12965d(%rip), %rax    # 0x1781d8
cmpl $0xf, 0x84c(%rax)
je 0x4eb99
movq -0x8(%rbp), %rdi
leaq 0xed383(%rip), %rsi     # 0x13bf12
callq 0x42f90
jmp 0x4eed5
movq -0x8(%rbp), %rdi
callq 0x424d0
cmpl $0x0, %eax
je 0x4ebbc
movq -0x8(%rbp), %rdi
leaq 0xed38e(%rip), %rsi     # 0x13bf40
callq 0x42f90
jmp 0x4eed5
movq -0x8(%rbp), %rdi
callq 0x4d700
movq %rax, %rdx
movl $0x2, %edi
leaq 0xed39b(%rip), %rsi     # 0x13bf6f
movb $0x0, %al
callq 0x2fc60
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdi
leaq 0xe8be3(%rip), %rsi     # 0x1377d4
callq 0x26300
cmpl $0x0, %eax
jne 0x4ec72
movq -0x8(%rbp), %rdi
callq 0x4e5e0
cmpl $0x0, %eax
jne 0x4ec2e
leaq 0x1295c8(%rip), %rax    # 0x1781d8
movq 0x3e8(%rax), %rcx
addq $0x1, %rcx
leaq 0x1295b6(%rip), %rax    # 0x1781d8
movq %rcx, 0x3e8(%rax)
jmp 0x4eed5
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x3f, %eax
je 0x4ec6e
leaq 0x129583(%rip), %rax    # 0x1781d8
movq 0x3f0(%rax), %rcx
addq $0x1, %rcx
leaq 0x129571(%rip), %rax    # 0x1781d8
movq %rcx, 0x3f0(%rax)
jmp 0x4ec70
jmp 0x4ec88
movq -0x8(%rbp), %rax
movl 0x98(%rax), %ecx
orl $0x10000, %ecx          # imm = 0x10000
movl %ecx, 0x98(%rax)
leaq 0x129549(%rip), %rax    # 0x1781d8
movq 0x3e0(%rax), %rcx
addq $0x1, %rcx
leaq 0x129537(%rip), %rax    # 0x1781d8
movq %rcx, 0x3e0(%rax)
movq -0x8(%rbp), %rax
movl $0x6, 0xa0(%rax)
leaq 0x12951b(%rip), %rax    # 0x1781d8
cmpl $0x0, 0x890(%rax)
je 0x4ecd6
movq -0x8(%rbp), %rax
movl 0x8(%rax), %esi
xorl %eax, %eax
movl %eax, %edi
callq 0x2bb10
movq -0x8(%rbp), %rax
movl $0xffffffff, 0xa8(%rax) # imm = 0xFFFFFFFF
movq -0x8(%rbp), %rax
movl 0x98(%rax), %ecx
orl $0x1, %ecx
movl %ecx, 0x98(%rax)
leaq 0x1294da(%rip), %rax    # 0x1781d8
movq 0x1e0(%rax), %rdi
movq -0x8(%rbp), %rsi
callq 0x27000
leaq 0x1294c3(%rip), %rax    # 0x1781d8
cmpl $-0x1, 0x730(%rax)
je 0x4edf2
leaq 0x1294af(%rip), %rax    # 0x1781d8
cmpl $0x1, 0x77c(%rax)
jne 0x4edf2
leaq 0x12949b(%rip), %rax    # 0x1781d8
movq 0x1e0(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x27320
leaq -0x30(%rbp), %rdi
callq 0x27380
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
je 0x4ed7d
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x7, 0xa0(%rax)
jne 0x4ed7b
jmp 0x4ed7d
jmp 0x4ed4d
cmpq $0x0, -0x20(%rbp)
je 0x4edda
movq -0x8(%rbp), %rax
movl 0x148(%rax), %eax
movq -0x18(%rbp), %rcx
andl 0x148(%rcx), %eax
movq -0x18(%rbp), %rcx
cmpl 0x148(%rcx), %eax
jne 0x4edda
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x43a30
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0xe0(%rax), %rsi
callq 0x4e500
movl $0x2, %edi
leaq 0xed1c0(%rip), %rsi     # 0x13bf91
movb $0x0, %al
callq 0x2fc60
jmp 0x4eded
movl $0x2, %edi
leaq 0xed1ce(%rip), %rsi     # 0x13bfb4
movb $0x0, %al
callq 0x2fc60
jmp 0x4eea8
leaq 0x1293df(%rip), %rax    # 0x1781d8
cmpl $-0x1, 0x730(%rax)
je 0x4ee27
leaq 0x1293cf(%rip), %rax    # 0x1781d8
cmpl $0x2, 0x77c(%rax)
jne 0x4ee27
movl $0x2, %edi
leaq 0xed1e5(%rip), %rsi     # 0x13c003
movb $0x0, %al
callq 0x2fc60
jmp 0x4eea6
leaq 0x1293aa(%rip), %rax    # 0x1781d8
cmpl $0x0, 0x814(%rax)
je 0x4ee6e
movq -0x8(%rbp), %rax
movl 0x148(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x4ee6e
leaq 0x129388(%rip), %rax    # 0x1781d8
cmpl $0x0, 0x818(%rax)
je 0x4ee6c
movl $0x2, %edi
leaq 0xed1e1(%rip), %rsi     # 0x13c046
movb $0x0, %al
callq 0x2fc60
jmp 0x4eea4
leaq 0x129363(%rip), %rax    # 0x1781d8
cmpl $-0x1, 0x69c(%rax)
jne 0x4ee8f
movq -0x8(%rbp), %rax
movl 0x148(%rax), %edi
callq 0x4e920
jmp 0x4eea2
movl $0x2, %edi
leaq 0xed1cf(%rip), %rsi     # 0x13c06a
movb $0x0, %al
callq 0x2fc60
jmp 0x4eea4
jmp 0x4eea6
jmp 0x4eea8
leaq 0x129329(%rip), %rax    # 0x1781d8
movq 0x1e0(%rax), %rax
cmpq $0x1, 0x28(%rax)
jne 0x4eed3
leaq 0x129314(%rip), %rax    # 0x1781d8
cmpq $0x0, 0x7d0(%rax)
jne 0x4eed3
callq 0x4d7f0
jmp 0x4eed5
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)