// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/src/replication.c
// Function: masterTryPartialResynchronization
int masterTryPartialResynchronization(client *c) {
    long long psync_offset, psync_len;
    char *master_runid = c->argv[1]->ptr;
    char buf[128];
    int buflen;

    /* Is the runid of this master the same advertised by the wannabe slave
     * via PSYNC? If runid changed this master is a different instance and
     * there is no way to continue. */
    if (strcasecmp(master_runid, server.runid)) {
        /* Run id "?" is used by slaves that want to force a full resync. */
        if (master_runid[0] != '?') {
            serverLog(LL_NOTICE,"Partial resynchronization not accepted: "
                "Runid mismatch (Client asked for runid '%s', my runid is '%s')",
                master_runid, server.runid);
        } else {
            serverLog(LL_NOTICE,"Full resync requested by slave %s",
                replicationGetSlaveName(c));
        }
        goto need_full_resync;
    }

    /* We still have the data our slave is asking for? */
    if (getLongLongFromObjectOrReply(c,c->argv[2],&psync_offset,NULL) !=
       C_OK) goto need_full_resync;
    if (!server.repl_backlog ||
        psync_offset < server.repl_backlog_off ||
        psync_offset > (server.repl_backlog_off + server.repl_backlog_histlen))
    {
        serverLog(LL_NOTICE,
            "Unable to partial resync with slave %s for lack of backlog (Slave request was: %lld).", replicationGetSlaveName(c), psync_offset);
        if (psync_offset > server.master_repl_offset) {
            serverLog(LL_WARNING,
                "Warning: slave %s tried to PSYNC with an offset that is greater than the master replication offset.", replicationGetSlaveName(c));
        }
        goto need_full_resync;
    }

    /* If we reached this point, we are able to perform a partial resync:
     * 1) Set client state to make it a slave.
     * 2) Inform the client we can continue with +CONTINUE
     * 3) Send the backlog data (from the offset to the end) to the slave. */
    c->flags |= CLIENT_SLAVE;
    c->replstate = SLAVE_STATE_ONLINE;
    c->repl_ack_time = server.unixtime;
    c->repl_put_online_on_ack = 0;
    listAddNodeTail(server.slaves,c);
    /* We can't use the connection buffers since they are used to accumulate
     * new commands at this stage. But we are sure the socket send buffer is
     * empty so this write will never fail actually. */
    buflen = snprintf(buf,sizeof(buf),"+CONTINUE\r\n");
    if (write(c->fd,buf,buflen) != buflen) {
        freeClientAsync(c);
        return C_OK;
    }
    psync_len = addReplyReplicationBacklog(c,psync_offset);
    serverLog(LL_NOTICE,
        "Partial resynchronization request from %s accepted. Sending %lld bytes of backlog starting from offset %lld.",
            replicationGetSlaveName(c),
            psync_len, psync_offset);
    /* Note that we don't need to set the selected DB at server.slaveseldb
     * to -1 to force the master to emit SELECT, since the slave already
     * has this state from the previous connection with the master. */

    refreshGoodSlavesCount();
    return C_OK; /* The caller can return, no full resync needed. */

need_full_resync:
    /* We need a full resync for some reason... Note that we can't
     * reply to PSYNC right now if a full SYNC is needed. The reply
     * must include the master offset at the time the RDB file we transfer
     * is generated, so we need to delay the reply to that moment. */
    return C_ERR;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
movq 0x40(%rdi), %r13
movq 0x8(%r13), %rax
movq 0x8(%rax), %r14
leaq 0xbc2f6(%rip), %r12     # 0xf98a8
leaq 0x70(%r12), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x262f0
testl %eax, %eax
je 0x3d5f5
cmpb $0x3f, (%r14)
jne 0x3d697
movq %rbx, %rdi
callq 0x3cc38
leaq 0x755ee(%rip), %rsi     # 0xb2bcd
leaq 0xd0b7a(%rip), %rdx     # 0x10e160
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x2b699
jmp 0x3d6ae
movq 0x10(%r13), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x3ab03
pushq $-0x1
popq %rbp
testl %eax, %eax
jne 0x3d6b1
cmpq $0x0, 0x7d0(%r12)
je 0x3d641
movq 0x8(%rsp), %rax
movq 0x7f0(%r12), %rcx
cmpq %rcx, %rax
jl 0x3d641
addq 0x7e0(%r12), %rcx
cmpq %rcx, %rax
jle 0x3d6c5
movq %rbx, %rdi
callq 0x3cc38
movq 0x8(%rsp), %rcx
leaq 0x7559a(%rip), %rsi     # 0xb2bef
leaq 0xd0b04(%rip), %rdx     # 0x10e160
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x2b699
movq 0x8(%rsp), %rax
cmpq 0x7c0(%r12), %rax
jle 0x3d6b1
movq %rbx, %rdi
callq 0x3cc38
leaq 0x755c1(%rip), %rsi     # 0xb2c45
leaq 0xd0ad5(%rip), %rdx     # 0x10e160
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
jmp 0x3d6b1
leaq 0x754c8(%rip), %rsi     # 0xb2b66
pushq $0x2
popq %rdi
movq %r14, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2b699
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
orb $0x1, 0x98(%rbx)
movl $0x9, 0xa0(%rbx)
movq 0x978(%r12), %rax
movq %rax, 0xd8(%rbx)
andl $0x0, 0xa4(%rbx)
movq 0x1e0(%r12), %rdi
movq %rbx, %rsi
callq 0x26ecc
movabsq $0x554e49544e4f432b, %rax # imm = 0x554E49544E4F432B
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
movl $0xa0d45, 0x8(%rsi)     # imm = 0xA0D45
movl 0x8(%rbx), %edi
pushq $0xb
popq %rdx
callq 0x26bb0
cmpq $0xb, %rax
jne 0x3d76a
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x3d34e
movq %rax, %r14
movq %rbx, %rdi
callq 0x3cc38
movq 0x8(%rsp), %r8
leaq 0x7555f(%rip), %rsi     # 0xb2ca9
leaq 0xd0a0f(%rip), %rdx     # 0x10e160
xorl %ebp, %ebp
pushq $0x2
popq %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2b699
callq 0x3d779
jmp 0x3d6b1
movq %rbx, %rdi
callq 0x378cf
xorl %ebp, %ebp
jmp 0x3d6b1