// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/src/replication.c
// Function: slaveTryPartialResynchronization
int slaveTryPartialResynchronization(int fd, int read_reply) {
    char *psync_runid;
    char psync_offset[32];
    sds reply;

    /* Writing half */
    if (!read_reply) {
        /* Initially set repl_master_initial_offset to -1 to mark the current
         * master run_id and offset as not valid. Later if we'll be able to do
         * a FULL resync using the PSYNC command we'll set the offset at the
         * right value, so that this information will be propagated to the
         * client structure representing the master into server.master. */
        server.repl_master_initial_offset = -1;

        if (server.cached_master) {
            psync_runid = server.cached_master->replrunid;
            snprintf(psync_offset,sizeof(psync_offset),"%lld", server.cached_master->reploff+1);
            serverLog(LL_NOTICE,"Trying a partial resynchronization (request %s:%s).", psync_runid, psync_offset);
        } else {
            serverLog(LL_NOTICE,"Partial resynchronization not possible (no cached master)");
            psync_runid = "?";
            memcpy(psync_offset,"-1",3);
        }

        /* Issue the PSYNC command */
        reply = sendSynchronousCommand(SYNC_CMD_WRITE,fd,"PSYNC",psync_runid,psync_offset,NULL);
        if (reply != NULL) {
            serverLog(LL_WARNING,"Unable to send PSYNC to master: %s",reply);
            sdsfree(reply);
            aeDeleteFileEvent(server.el,fd,AE_READABLE);
            return PSYNC_WRITE_ERROR;
        }
        return PSYNC_WAIT_REPLY;
    }

    /* Reading half */
    reply = sendSynchronousCommand(SYNC_CMD_READ,fd,NULL);
    if (sdslen(reply) == 0) {
        /* The master may send empty newlines after it receives PSYNC
         * and before to reply, just to keep the connection alive. */
        sdsfree(reply);
        return PSYNC_WAIT_REPLY;
    }

    aeDeleteFileEvent(server.el,fd,AE_READABLE);

    if (!strncmp(reply,"+FULLRESYNC",11)) {
        char *runid = NULL, *offset = NULL;

        /* FULL RESYNC, parse the reply in order to extract the run id
         * and the replication offset. */
        runid = strchr(reply,' ');
        if (runid) {
            runid++;
            offset = strchr(runid,' ');
            if (offset) offset++;
        }
        if (!runid || !offset || (offset-runid-1) != CONFIG_RUN_ID_SIZE) {
            serverLog(LL_WARNING,
                "Master replied with wrong +FULLRESYNC syntax.");
            /* This is an unexpected condition, actually the +FULLRESYNC
             * reply means that the master supports PSYNC, but the reply
             * format seems wrong. To stay safe we blank the master
             * runid to make sure next PSYNCs will fail. */
            memset(server.repl_master_runid,0,CONFIG_RUN_ID_SIZE+1);
        } else {
            memcpy(server.repl_master_runid, runid, offset-runid-1);
            server.repl_master_runid[CONFIG_RUN_ID_SIZE] = '\0';
            server.repl_master_initial_offset = strtoll(offset,NULL,10);
            serverLog(LL_NOTICE,"Full resync from master: %s:%lld",
                server.repl_master_runid,
                server.repl_master_initial_offset);
        }
        /* We are going to full resync, discard the cached master structure. */
        replicationDiscardCachedMaster();
        sdsfree(reply);
        return PSYNC_FULLRESYNC;
    }

    if (!strncmp(reply,"+CONTINUE",9)) {
        /* Partial resync was accepted, set the replication state accordingly */
        serverLog(LL_NOTICE,
            "Successful partial resynchronization with master.");
        sdsfree(reply);
        replicationResurrectCachedMaster(fd);
        return PSYNC_CONTINUE;
    }

    /* If we reach this point we received either an error since the master does
     * not understand PSYNC, or an unexpected reply from the master.
     * Return PSYNC_NOT_SUPPORTED to the caller in both cases. */

    if (strncmp(reply,"-ERR",4)) {
        /* If it's not an error, log the unexpected event. */
        serverLog(LL_WARNING,
            "Unexpected reply to PSYNC from master: %s", reply);
    } else {
        serverLog(LL_NOTICE,
            "Master does not support PSYNC or is in "
            "error state (reply: %s)", reply);
    }
    sdsfree(reply);
    replicationDiscardCachedMaster();
    return PSYNC_NOT_SUPPORTED;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x28, %rsp
movl %edi, %ebp
testl %esi, %esi
je 0x3eb7b
pushq $0x1
popq %r14
movl %r14d, %edi
movl %ebp, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x3e8bf
movq %rax, %rbx
movq %rax, %rdi
callq 0x3cf20
testq %rax, %rax
je 0x3ebdb
leaq 0xbad97(%rip), %r15     # 0xf98a8
movq 0x40(%r15), %rdi
pushq $0x1
popq %rdx
movl %ebp, %esi
callq 0x28ba8
leaq 0x74b40(%rip), %rsi     # 0xb3666
pushq $0xb
popq %rdx
movq %rbx, %rdi
callq 0x26350
testl %eax, %eax
je 0x3ebe8
leaq 0x74b81(%rip), %rsi     # 0xb36c1
pushq $0x9
popq %rdx
movq %rbx, %rdi
callq 0x26350
testl %eax, %eax
je 0x3ec9d
leaq 0x74ba3(%rip), %rsi     # 0xb36fd
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0x26350
testl %eax, %eax
je 0x3ecc6
leaq 0x74b8e(%rip), %rsi     # 0xb3702
pushq $0x3
jmp 0x3eccf
leaq 0xbad26(%rip), %r15     # 0xf98a8
orq $-0x1, 0x8d8(%r15)
movq 0x840(%r15), %rbx
testq %rbx, %rbx
je 0x3ec1d
movq 0xc8(%rbx), %rcx
addq $0xe8, %rbx
incq %rcx
leaq 0x7b23e(%rip), %rdx     # 0xb9df0
movq %rsp, %r14
pushq $0x20
popq %rsi
movq %r14, %rdi
xorl %eax, %eax
callq 0x26120
leaq 0x74a06(%rip), %rsi     # 0xb35cf
pushq $0x2
popq %rdi
movq %rbx, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x2b699
jmp 0x3ec40
movq %rbx, %rdi
callq 0x311c8
jmp 0x3eda3
pushq $0x20
popq %rsi
movq %rbx, %rdi
callq 0x267f0
testq %rax, %rax
je 0x3ecee
movq %rax, %r14
incq %r14
pushq $0x20
popq %rsi
movq %r14, %rdi
callq 0x267f0
leaq 0x1(%rax), %rdi
testq %rax, %rax
cmoveq %rax, %rdi
jmp 0x3ecf3
leaq 0x749df(%rip), %rsi     # 0xb3603
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x2b699
movb $0x0, 0x2(%rsp)
movw $0x312d, (%rsp)         # imm = 0x312D
leaq 0x83136(%rip), %rbx     # 0xc1d76
leaq 0x749f6(%rip), %rdx     # 0xb363d
xorl %r14d, %r14d
pushq $0x2
popq %rdi
movq %rsp, %r8
movl %ebp, %esi
movq %rbx, %rcx
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x3e8bf
testq %rax, %rax
je 0x3ec96
movq %rax, %rbx
leaq 0x749d5(%rip), %rsi     # 0xb3643
pushq $0x3
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2b699
movq %rbx, %rdi
callq 0x311c8
movq 0x40(%r15), %rdi
pushq $0x1
popq %rdx
movl %ebp, %esi
callq 0x28ba8
jmp 0x3eda3
pushq $0x1
jmp 0x3eda1
leaq 0x74a27(%rip), %rsi     # 0xb36cb
pushq $0x2
popq %r14
movl %r14d, %edi
xorl %eax, %eax
callq 0x2b699
movq %rbx, %rdi
callq 0x311c8
movl %ebp, %edi
callq 0x3edf1
jmp 0x3eda3
leaq 0x74a5f(%rip), %rsi     # 0xb372c
pushq $0x2
popq %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2b699
movq %rbx, %rdi
callq 0x311c8
callq 0x3edb1
pushq $0x4
jmp 0x3eda1
xorl %r14d, %r14d
xorl %edi, %edi
testq %r14, %r14
je 0x3ed66
testq %rdi, %rdi
je 0x3ed66
movq %r14, %rax
subq %rdi, %rax
cmpq $-0x29, %rax
jne 0x3ed66
movq 0x20(%r14), %rax
movq %rax, 0x8c8(%r15)
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups %xmm1, 0x8b8(%r15)
movups %xmm0, 0x8a8(%r15)
leaq 0x8a8(%r15), %r14
movb $0x0, 0x8d0(%r15)
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x26af0
movq %rax, 0x8d8(%r15)
leaq 0x7494c(%rip), %rsi     # 0xb36a0
pushq $0x2
popq %rdi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2b699
jmp 0x3ed92
leaq 0x74905(%rip), %rsi     # 0xb3672
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
xorps %xmm0, %xmm0
movups %xmm0, 0x8c1(%r15)
movups %xmm0, 0x8b8(%r15)
movups %xmm0, 0x8a8(%r15)
callq 0x3edb1
movq %rbx, %rdi
callq 0x311c8
pushq $0x3
popq %r14
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq