// File: /workspace/github_c_makefile_star2_O0/nilyang[P]redis-tdd-annotation/src/replication.c
// Function: slaveTryPartialResynchronization
int slaveTryPartialResynchronization(int fd) {
    char *psync_runid;
    char psync_offset[32];
    sds 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 <master_run_id> <repl_offset>"
        psync_runid = server.cached_master->replrunid;
        snprintf(psync_offset,sizeof(psync_offset),"%lld", server.cached_master->reploff+1);
        redisLog(REDIS_NOTICE,"Trying a partial resynchronization (request %s:%s).", psync_runid, psync_offset);
    } else {
        // 缓存不存在
        // 发送 "PSYNC ? -1" ，要求完整重同步
        redisLog(REDIS_NOTICE,"Partial resynchronization not possible (no cached master)");
        psync_runid = "?";
        memcpy(psync_offset,"-1",3);
    }

    /* Issue the PSYNC command */
    // 向主服务器发送 PSYNC 命令
    reply = sendSynchronousCommand(fd,"PSYNC",psync_runid,psync_offset,NULL);

    // 接收到 FULLRESYNC ，进行 full-resync
    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. */
        // 分析并记录主服务器的 run id
        runid = strchr(reply,' ');
        if (runid) {
            runid++;
            offset = strchr(runid,' ');
            if (offset) offset++;
        }
        // 检查 run id 的合法性
        if (!runid || !offset || (offset-runid-1) != REDIS_RUN_ID_SIZE) {
            redisLog(REDIS_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. */
            // 主服务器支持 PSYNC ，但是却发来了异常的 run id
            // 只好将 run id 设为 0 ，让下次 PSYNC 时失败
            memset(server.repl_master_runid,0,REDIS_RUN_ID_SIZE+1);
        } else {
            // 保存 run id
            memcpy(server.repl_master_runid, runid, offset-runid-1);
            server.repl_master_runid[REDIS_RUN_ID_SIZE] = '\0';
            // 以及 initial offset
            server.repl_master_initial_offset = strtoll(offset,NULL,10);
            // 打印日志，这是一个 FULL resync
            redisLog(REDIS_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. */
        // 要开始完整重同步，缓存中的 master 已经没用了，清除它
        replicationDiscardCachedMaster();
        sdsfree(reply);
        
        // 返回状态
        return PSYNC_FULLRESYNC;
    }

    // 接收到 CONTINUE ，进行 partial resync
    if (!strncmp(reply,"+CONTINUE",9)) {
        /* Partial resync was accepted, set the replication state accordingly */
        redisLog(REDIS_NOTICE,
            "Successful partial resynchronization with master.");
        sdsfree(reply);
        // 将缓存中的 master 设为当前 master
        replicationResurrectCachedMaster(fd);

        // 返回状态
        return PSYNC_CONTINUE;
    }

    /* If we reach this point we receied 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. */
        redisLog(REDIS_WARNING,
            "Unexpected reply to PSYNC from master: %s", reply);
    } else {
        redisLog(REDIS_NOTICE,
            "Master does not support PSYNC or is in "
            "error state (reply: %s)", reply);
    }
    sdsfree(reply);
    replicationDiscardCachedMaster();

    // 主服务器不支持 PSYNC
    return PSYNC_NOT_SUPPORTED;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movl %edi, -0x8(%rbp)
leaq 0xd2636(%rip), %rax     # 0x1138f8
movq $-0x1, 0x6f0(%rax)
leaq 0xd2624(%rip), %rax     # 0x1138f8
cmpq $0x0, 0x668(%rax)
je 0x41343
leaq 0xd2613(%rip), %rax     # 0x1138f8
movq 0x668(%rax), %rax
addq $0xd0, %rax
movq %rax, -0x10(%rbp)
leaq -0x30(%rbp), %rdi
leaq 0xd25f7(%rip), %rax     # 0x1138f8
movq 0x668(%rax), %rax
movq 0xb8(%rax), %rcx
addq $0x1, %rcx
movl $0x20, %esi
leaq 0xa5bdd(%rip), %rdx     # 0xe6efc
movb $0x0, %al
callq 0x1f120
movq -0x10(%rbp), %rdx
leaq -0x30(%rbp), %rcx
movl $0x2, %edi
leaq 0xa0f22(%rip), %rsi     # 0xe225c
movb $0x0, %al
callq 0x24fb0
jmp 0x41375
movl $0x2, %edi
leaq 0xa0f41(%rip), %rsi     # 0xe2290
movb $0x0, %al
callq 0x24fb0
leaq 0xaab55(%rip), %rax     # 0xebeb2
movq %rax, -0x10(%rbp)
movw 0xa1832(%rip), %ax      # 0xe2b9a
movw %ax, -0x30(%rbp)
movb 0xa182a(%rip), %al      # 0xe2b9c
movb %al, -0x2e(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rdx
leaq -0x30(%rbp), %rcx
leaq 0xa0f43(%rip), %rsi     # 0xe22ca
xorl %eax, %eax
movl %eax, %r8d
movb $0x0, %al
callq 0x41030
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
leaq 0xa0f2e(%rip), %rsi     # 0xe22d0
movl $0xb, %edx
callq 0x1f340
cmpl $0x0, %eax
jne 0x41506
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x38(%rbp), %rdi
movl $0x20, %esi
callq 0x1f790
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x41411
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
movl $0x20, %esi
callq 0x1f790
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x4140f
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x41411
cmpq $0x0, -0x40(%rbp)
je 0x41434
cmpq $0x0, -0x48(%rbp)
je 0x41434
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
subq %rcx, %rax
subq $0x1, %rax
cmpq $0x28, %rax
je 0x41466
movl $0x3, %edi
leaq 0xa0e9c(%rip), %rsi     # 0xe22dc
movb $0x0, %al
callq 0x24fb0
leaq 0xd24aa(%rip), %rdi     # 0x1138f8
addq $0x6c0, %rdi            # imm = 0x6C0
xorl %esi, %esi
movl $0x29, %edx
callq 0x1f110
jmp 0x414ec
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x40(%rbp), %rax
subq %rax, %rdx
subq $0x1, %rdx
leaq 0xd2478(%rip), %rdi     # 0x1138f8
addq $0x6c0, %rdi            # imm = 0x6C0
callq 0x1fbf0
leaq 0xd2465(%rip), %rax     # 0x1138f8
movb $0x0, 0x6e8(%rax)
movq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x1fa80
movq %rax, %rcx
leaq 0xd2442(%rip), %rax     # 0x1138f8
movq %rcx, 0x6f0(%rax)
leaq 0xd2434(%rip), %rax     # 0x1138f8
movq 0x6f0(%rax), %rcx
movl $0x2, %edi
leaq 0xa0e33(%rip), %rsi     # 0xe230a
leaq 0xd241a(%rip), %rdx     # 0x1138f8
addq $0x6c0, %rdx            # imm = 0x6C0
movb $0x0, %al
callq 0x24fb0
callq 0x415c0
movq -0x38(%rbp), %rdi
callq 0x2c160
movl $0x1, -0x4(%rbp)
jmp 0x415ac
movq -0x38(%rbp), %rdi
leaq 0xa0e1a(%rip), %rsi     # 0xe232b
movl $0x9, %edx
callq 0x1f340
cmpl $0x0, %eax
jne 0x4154d
movl $0x2, %edi
leaq 0xa0e09(%rip), %rsi     # 0xe2335
movb $0x0, %al
callq 0x24fb0
movq -0x38(%rbp), %rdi
callq 0x2c160
movl -0x8(%rbp), %edi
callq 0x41630
movl $0x0, -0x4(%rbp)
jmp 0x415ac
movq -0x38(%rbp), %rdi
leaq 0xa0e0f(%rip), %rsi     # 0xe2367
movl $0x4, %edx
callq 0x1f340
cmpl $0x0, %eax
je 0x41580
movq -0x38(%rbp), %rdx
movl $0x3, %edi
leaq 0xa0df5(%rip), %rsi     # 0xe236c
movb $0x0, %al
callq 0x24fb0
jmp 0x41597
movq -0x38(%rbp), %rdx
movl $0x2, %edi
leaq 0xa0e06(%rip), %rsi     # 0xe2396
movb $0x0, %al
callq 0x24fb0
movq -0x38(%rbp), %rdi
callq 0x2c160
callq 0x415c0
movl $0x2, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)