// File: /workspace/github_c_makefile_star2_O3/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 0xbd057(%rip), %r12     # 0x1018a8
leaq 0x70(%r12), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x262f0
testl %eax, %eax
je 0x44896
cmpb $0x3f, (%r14)
jne 0x4493e
movq %rbx, %rdi
callq 0x43e7c
leaq 0x7cbff(%rip), %rsi     # 0xc147d
leaq 0xd18db(%rip), %rdx     # 0x116160
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
jmp 0x44957
movq 0x10(%r13), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x416bb
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x4495c
cmpq $0x0, 0x7d0(%r12)
je 0x448e4
movq 0x8(%rsp), %rax
movq 0x7f0(%r12), %rcx
cmpq %rcx, %rax
jl 0x448e4
addq 0x7e0(%r12), %rcx
cmpq %rcx, %rax
jle 0x44970
movq %rbx, %rdi
callq 0x43e7c
movq 0x8(%rsp), %rcx
leaq 0x7cba7(%rip), %rsi     # 0xc149f
leaq 0xd1861(%rip), %rdx     # 0x116160
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
movq 0x8(%rsp), %rax
cmpq 0x7c0(%r12), %rax
jle 0x4495c
movq %rbx, %rdi
callq 0x43e7c
leaq 0x7cbcc(%rip), %rsi     # 0xc14f5
leaq 0xd1830(%rip), %rdx     # 0x116160
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
jmp 0x4495c
leaq 0x7cad1(%rip), %rsi     # 0xc1416
movl $0x2, %edi
movq %r14, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2fdad
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 0x978(%r12), %rax
movq %rax, 0xd8(%rbx)
movl $0x0, 0xa4(%rbx)
movq 0x1e0(%r12), %rdi
movq %rbx, %rsi
callq 0x2b24b
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 0x26bb0
cmpq $0xb, %rax
jne 0x44a1c
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x445e1
movq %rax, %r14
movq %rbx, %rdi
callq 0x43e7c
movq 0x8(%rsp), %r8
leaq 0x7cb5f(%rip), %rsi     # 0xc1559
leaq 0xd175f(%rip), %rdx     # 0x116160
xorl %ebp, %ebp
movl $0x2, %edi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2fdad
callq 0x44a2b
jmp 0x4495c
movq %rbx, %rdi
callq 0x3db03
xorl %ebp, %ebp
jmp 0x4495c