// File: /workspace/github_c_makefile_star2_O3/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 0x45fc2
movl $0x1, %r14d
movl $0x1, %edi
movl %ebp, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x45d04
movq %rax, %rbx
movzbl -0x1(%rax), %eax
movl %eax, %ecx
andl $0x7, %ecx
cmpl $0x4, %ecx
ja 0x46143
leaq 0x7b203(%rip), %rdx     # 0xc11b4
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
shrl $0x3, %eax
jmp 0x460c6
leaq 0xbb8df(%rip), %r15     # 0x1018a8
movq $-0x1, 0x8d8(%r15)
movq 0x840(%r15), %rbx
testq %rbx, %rbx
je 0x46025
movq 0xc8(%rbx), %rcx
addq $0xe8, %rbx
incq %rcx
leaq 0x82e10(%rip), %rdx     # 0xc8e08
movq %rsp, %r14
movl $0x20, %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x26120
leaq 0x7be6e(%rip), %rsi     # 0xc1e7f
movl $0x2, %edi
movq %rbx, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x2fdad
jmp 0x4604a
leaq 0x7be87(%rip), %rsi     # 0xc1eb3
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
movb $0x0, 0x2(%rsp)
movw $0x312d, (%rsp)         # imm = 0x312D
leaq 0x8b26c(%rip), %rbx     # 0xd12b6
leaq 0x7be9c(%rip), %rdx     # 0xc1eed
xorl %r14d, %r14d
movq %rsp, %r8
movl $0x2, %edi
movl %ebp, %esi
movq %rbx, %rcx
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x45d04
testq %rax, %rax
je 0x460a6
movq %rax, %rbx
leaq 0x7be79(%rip), %rsi     # 0xc1ef3
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2fdad
movq %rbx, %rdi
callq 0x36330
movq 0x40(%r15), %rdi
movl %ebp, %esi
movl $0x1, %edx
callq 0x2cf0e
jmp 0x46289
movl $0x1, %r14d
jmp 0x46289
movq -0x11(%rbx), %rax
jmp 0x460c6
movzwl -0x5(%rbx), %eax
jmp 0x460c6
movl -0x9(%rbx), %eax
jmp 0x460c6
movzbl -0x3(%rbx), %eax
testq %rax, %rax
je 0x46143
leaq 0xbb7d6(%rip), %r15     # 0x1018a8
movq 0x40(%r15), %rdi
movl %ebp, %esi
movl $0x1, %edx
callq 0x2cf0e
leaq 0x7be2d(%rip), %rsi     # 0xc1f16
movl $0xb, %edx
movq %rbx, %rdi
callq 0x26350
testl %eax, %eax
je 0x46150
leaq 0x7be70(%rip), %rsi     # 0xc1f71
movl $0x9, %edx
movq %rbx, %rdi
callq 0x26350
testl %eax, %eax
je 0x46236
leaq 0x7be90(%rip), %rsi     # 0xc1fad
movl $0x4, %edx
movq %rbx, %rdi
callq 0x26350
testl %eax, %eax
je 0x46260
leaq 0x7be79(%rip), %rsi     # 0xc1fb2
movl $0x3, %edi
jmp 0x4626c
movq %rbx, %rdi
callq 0x36330
jmp 0x46289
movq %rbx, %rdi
movl $0x20, %esi
callq 0x267f0
testq %rax, %rax
je 0x461f3
movq %rax, %r14
incq %r14
movq %r14, %rdi
movl $0x20, %esi
callq 0x267f0
leaq 0x1(%rax), %rdi
testq %rax, %rax
cmovneq %rdi, %rax
je 0x461f3
movq %r14, %rcx
subq %rax, %rcx
cmpq $-0x29, %rcx
jne 0x461f3
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)
xorl %esi, %esi
movl $0xa, %edx
callq 0x26af0
movq %rax, 0x8d8(%r15)
leaq 0x7bd71(%rip), %rsi     # 0xc1f50
movl $0x2, %edi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x2fdad
jmp 0x46221
leaq 0x7bd28(%rip), %rsi     # 0xc1f22
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
xorps %xmm0, %xmm0
movups %xmm0, 0x8c1(%r15)
movups %xmm0, 0x8b8(%r15)
movups %xmm0, 0x8a8(%r15)
callq 0x46297
movq %rbx, %rdi
callq 0x36330
movl $0x3, %r14d
jmp 0x46289
leaq 0x7bd3e(%rip), %rsi     # 0xc1f7b
movl $0x2, %r14d
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
movq %rbx, %rdi
callq 0x36330
movl %ebp, %edi
callq 0x462dc
jmp 0x46289
leaq 0x7bd75(%rip), %rsi     # 0xc1fdc
movl $0x2, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2fdad
movq %rbx, %rdi
callq 0x36330
callq 0x46297
movl $0x4, %r14d
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq