// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/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 0xbedfb(%rip), %r12     # 0x106858
leaq 0x70(%r12), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x26300
testl %eax, %eax
je 0x47aa2
cmpb $0x3f, (%r14)
jne 0x47b4a
movq %rbx, %rdi
callq 0x47084
leaq 0x7c95f(%rip), %rsi     # 0xc43e9
leaq 0xd366f(%rip), %rdx     # 0x11b100
movl $0x2, %edi
xorl %eax, %eax
callq 0x32ea6
jmp 0x47b63
movq 0x10(%r13), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x44b44
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x47b68
cmpq $0x0, 0x7c0(%r12)
je 0x47af0
movq 0x8(%rsp), %rax
movq 0x7e0(%r12), %rcx
cmpq %rcx, %rax
jl 0x47af0
addq 0x7d0(%r12), %rcx
cmpq %rcx, %rax
jle 0x47b7c
movq %rbx, %rdi
callq 0x47084
movq 0x8(%rsp), %rcx
leaq 0x7c907(%rip), %rsi     # 0xc440b
leaq 0xd35f5(%rip), %rdx     # 0x11b100
movl $0x2, %edi
xorl %eax, %eax
callq 0x32ea6
movq 0x8(%rsp), %rax
cmpq 0x7b0(%r12), %rax
jle 0x47b68
movq %rbx, %rdi
callq 0x47084
leaq 0x7c92c(%rip), %rsi     # 0xc4461
leaq 0xd35c4(%rip), %rdx     # 0x11b100
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
jmp 0x47b68
leaq 0x7c831(%rip), %rsi     # 0xc4382
movl $0x2, %edi
movq %r14, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x32ea6
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
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 0x968(%r12), %rax
movq %rax, 0xd8(%rbx)
movl $0x0, 0xa4(%rbx)
movq 0x1e0(%r12), %rdi
movq %rbx, %rsi
callq 0x2e40f
movabsq $0x554e49544e4f432b, %rax # imm = 0x554E49544E4F432B
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
movl $0xa0d45, 0x8(%rsi)     # imm = 0xA0D45
movl 0x8(%rbx), %edi
movl $0xb, %edx
callq 0x26bc0
cmpq $0xb, %rax
jne 0x47c28
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x477ed
movq %rax, %r14
movq %rbx, %rdi
callq 0x47084
movq 0x8(%rsp), %r8
leaq 0x7c8bf(%rip), %rsi     # 0xc44c5
leaq 0xd34f3(%rip), %rdx     # 0x11b100
xorl %ebp, %ebp
movl $0x2, %edi
movq %r14, %rcx
xorl %eax, %eax
callq 0x32ea6
callq 0x47c37
jmp 0x47b68
movq %rbx, %rdi
callq 0x40ebf
xorl %ebp, %ebp
jmp 0x47b68