// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/src/replication.c
// Function: readSyncBulkPayload
void readSyncBulkPayload(aeEventLoop *el, int fd, void *privdata, int mask) {
    char buf[4096];
    ssize_t nread, readlen;
    off_t left;
    UNUSED(el);
    UNUSED(privdata);
    UNUSED(mask);

    /* Static vars used to hold the EOF mark, and the last bytes received
     * form the server: when they match, we reached the end of the transfer. */
    static char eofmark[CONFIG_RUN_ID_SIZE];
    static char lastbytes[CONFIG_RUN_ID_SIZE];
    static int usemark = 0;

    /* If repl_transfer_size == -1 we still have to read the bulk length
     * from the master reply. */
    if (server.repl_transfer_size == -1) {
        if (syncReadLine(fd,buf,1024,server.repl_syncio_timeout*1000) == -1) {
            serverLog(LL_WARNING,
                "I/O error reading bulk count from MASTER: %s",
                strerror(errno));
            goto error;
        }

        if (buf[0] == '-') {
            serverLog(LL_WARNING,
                "MASTER aborted replication with an error: %s",
                buf+1);
            goto error;
        } else if (buf[0] == '\0') {
            /* At this stage just a newline works as a PING in order to take
             * the connection live. So we refresh our last interaction
             * timestamp. */
            server.repl_transfer_lastio = server.unixtime;
            return;
        } else if (buf[0] != '$') {
            serverLog(LL_WARNING,"Bad protocol from MASTER, the first byte is not '$' (we received '%s'), are you sure the host and port are right?", buf);
            goto error;
        }

        /* There are two possible forms for the bulk payload. One is the
         * usual $<count> bulk format. The other is used for diskless transfers
         * when the master does not know beforehand the size of the file to
         * transfer. In the latter case, the following format is used:
         *
         * $EOF:<40 bytes delimiter>
         *
         * At the end of the file the announced delimiter is transmitted. The
         * delimiter is long and random enough that the probability of a
         * collision with the actual file content can be ignored. */
        if (strncmp(buf+1,"EOF:",4) == 0 && strlen(buf+5) >= CONFIG_RUN_ID_SIZE) {
            usemark = 1;
            memcpy(eofmark,buf+5,CONFIG_RUN_ID_SIZE);
            memset(lastbytes,0,CONFIG_RUN_ID_SIZE);
            /* Set any repl_transfer_size to avoid entering this code path
             * at the next call. */
            server.repl_transfer_size = 0;
            serverLog(LL_NOTICE,
                "MASTER <-> SLAVE sync: receiving streamed RDB from master");
        } else {
            usemark = 0;
            server.repl_transfer_size = strtol(buf+1,NULL,10);
            serverLog(LL_NOTICE,
                "MASTER <-> SLAVE sync: receiving %lld bytes from master",
                (long long) server.repl_transfer_size);
        }
        return;
    }

    /* Read bulk data */
    if (usemark) {
        readlen = sizeof(buf);
    } else {
        left = server.repl_transfer_size - server.repl_transfer_read;
        readlen = (left < (signed)sizeof(buf)) ? left : (signed)sizeof(buf);
    }

    nread = read(fd,buf,readlen);
    if (nread <= 0) {
        serverLog(LL_WARNING,"I/O error trying to sync with MASTER: %s",
            (nread == -1) ? strerror(errno) : "connection lost");
        cancelReplicationHandshake();
        return;
    }
    server.stat_net_input_bytes += nread;

    /* When a mark is used, we want to detect EOF asap in order to avoid
     * writing the EOF mark into the file... */
    int eof_reached = 0;

    if (usemark) {
        /* Update the last bytes array, and check if it matches our delimiter.*/
        if (nread >= CONFIG_RUN_ID_SIZE) {
            memcpy(lastbytes,buf+nread-CONFIG_RUN_ID_SIZE,CONFIG_RUN_ID_SIZE);
        } else {
            int rem = CONFIG_RUN_ID_SIZE-nread;
            memmove(lastbytes,lastbytes+nread,rem);
            memcpy(lastbytes+rem,buf,nread);
        }
        if (memcmp(lastbytes,eofmark,CONFIG_RUN_ID_SIZE) == 0) eof_reached = 1;
    }

    server.repl_transfer_lastio = server.unixtime;
    if (write(server.repl_transfer_fd,buf,nread) != nread) {
        serverLog(LL_WARNING,"Write error or short write writing to the DB dump file needed for MASTER <-> SLAVE synchronization: %s", strerror(errno));
        goto error;
    }
    server.repl_transfer_read += nread;

    /* Delete the last 40 bytes from the file if we reached EOF. */
    if (usemark && eof_reached) {
        if (ftruncate(server.repl_transfer_fd,
            server.repl_transfer_read - CONFIG_RUN_ID_SIZE) == -1)
        {
            serverLog(LL_WARNING,"Error truncating the RDB file received from the master for SYNC: %s", strerror(errno));
            goto error;
        }
    }

    /* Sync data on disk from time to time, otherwise at the end of the transfer
     * we may suffer a big delay as the memory buffers are copied into the
     * actual disk. */
    if (server.repl_transfer_read >=
        server.repl_transfer_last_fsync_off + REPL_MAX_WRITTEN_BEFORE_FSYNC)
    {
        off_t sync_size = server.repl_transfer_read -
                          server.repl_transfer_last_fsync_off;
        rdb_fsync_range(server.repl_transfer_fd,
            server.repl_transfer_last_fsync_off, sync_size);
        server.repl_transfer_last_fsync_off += sync_size;
    }

    /* Check if the transfer is now complete */
    if (!usemark) {
        if (server.repl_transfer_read == server.repl_transfer_size)
            eof_reached = 1;
    }

    if (eof_reached) {
        if (rename(server.repl_transfer_tmpfile,server.rdb_filename) == -1) {
            serverLog(LL_WARNING,"Failed trying to rename the temp DB into dump.rdb in MASTER <-> SLAVE synchronization: %s", strerror(errno));
            cancelReplicationHandshake();
            return;
        }
        serverLog(LL_NOTICE, "MASTER <-> SLAVE sync: Flushing old data");
        signalFlushedDb(-1);
        emptyDb(replicationEmptyDbCallback);
        /* Before loading the DB into memory we need to delete the readable
         * handler, otherwise it will get called recursively since
         * rdbLoad() will call the event loop to process events from time to
         * time for non blocking loading. */
        aeDeleteFileEvent(server.el,server.repl_transfer_s,AE_READABLE);
        serverLog(LL_NOTICE, "MASTER <-> SLAVE sync: Loading DB in memory");
        if (rdbLoad(server.rdb_filename) != C_OK) {
            serverLog(LL_WARNING,"Failed trying to load the MASTER synchronization DB from disk");
            cancelReplicationHandshake();
            return;
        }
        /* Final setup of the connected slave <- master link */
        zfree(server.repl_transfer_tmpfile);
        close(server.repl_transfer_fd);
        replicationCreateMasterClient(server.repl_transfer_s);
        serverLog(LL_NOTICE, "MASTER <-> SLAVE sync: Finished with success");
        /* Restart the AOF subsystem now that we finished the sync. This
         * will trigger an AOF rewrite, and when done will start appending
         * to the new file. */
        if (server.aof_state != AOF_OFF) {
            int retry = 10;

            stopAppendOnly();
            while (retry-- && startAppendOnly() == C_ERR) {
                serverLog(LL_WARNING,"Failed enabling the AOF after successful master synchronization! Trying it again in one second.");
                sleep(1);
            }
            if (!retry) {
                serverLog(LL_WARNING,"FATAL: this slave instance finished the synchronization with its master, but the AOF can't be turned on. Exiting now.");
                exit(1);
            }
        }
    }

    return;

error:
    cancelReplicationHandshake();
    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x1000, %rsp           # imm = 0x1000
movl %esi, %edi
leaq 0xbde40(%rip), %r12     # 0x106858
movq 0x840(%r12), %rax
cmpq $-0x1, %rax
je 0x48ab3
movl $0x1000, %edx           # imm = 0x1000
cmpb $0x0, 0xd2782(%rip)     # 0x11b1b8
jne 0x48a4f
subq 0x848(%r12), %rax
movl $0x1000, %ecx           # imm = 0x1000
cmpq %rcx, %rax
cmovgeq %rcx, %rax
movq %rax, %rdx
movq %rsp, %rsi
callq 0x26350
movq %rax, %rbx
testq %rax, %rax
jle 0x48b04
addq %rbx, 0x410(%r12)
xorl %r14d, %r14d
cmpb $0x1, 0xd2743(%rip)     # 0x11b1b8
movl $0x0, %ebp
jne 0x48b86
cmpq $0x28, %rbx
jl 0x48b32
movq -0x8(%rsp,%rbx), %rax
movq %rax, 0xd271a(%rip)     # 0x11b1b0
movups -0x28(%rsp,%rbx), %xmm0
movups -0x18(%rsp,%rbx), %xmm1
movaps %xmm1, 0xd26f9(%rip)    # 0x11b1a0
movaps %xmm0, 0xd26e2(%rip)    # 0x11b190
jmp 0x48b62
movslq 0x838(%r12), %rax
imulq $0x3e8, %rax, %rcx      # imm = 0x3E8
movq %rsp, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x68c35
cmpq $-0x1, %rax
je 0x48b1a
movzbl (%rsp), %eax
testl %eax, %eax
je 0x48dc6
cmpl $0x24, %eax
je 0x48d43
cmpl $0x2d, %eax
jne 0x48dd8
leaq 0x1(%rsp), %rdx
leaq 0x7bed7(%rip), %rsi     # 0xc49d6
jmp 0x48de2
cmpq $-0x1, %rbx
je 0x48d0d
leaq 0x7bffb(%rip), %rdx     # 0xc4b10
jmp 0x48d1c
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x7be7c(%rip), %rsi     # 0xc49a9
jmp 0x48d00
movl $0x28, %r14d
subl %ebx, %r14d
leaq 0xd264e(%rip), %r15     # 0x11b190
leaq (%r15,%rbx), %rsi
movq %r15, %rdi
movq %r14, %rdx
callq 0x267f0
addq %r14, %r15
movq %rsp, %rsi
movq %r15, %rdi
movq %rbx, %rdx
callq 0x26c60
leaq 0xd2627(%rip), %rdi     # 0x11b190
leaq 0xd25f0(%rip), %rsi     # 0x11b160
movl $0x28, %edx
callq 0x26700
xorl %r14d, %r14d
testl %eax, %eax
sete %r14b
movl %r14d, %ebp
movq 0x968(%r12), %rax
movq %rax, 0x868(%r12)
movl 0x85c(%r12), %edi
movq %rsp, %rsi
movq %rbx, %rdx
callq 0x26bc0
cmpq %rbx, %rax
jne 0x48ced
movq 0x848(%r12), %rsi
addq %rbx, %rsi
movq %rsi, 0x848(%r12)
andb 0xd25ec(%rip), %r14b    # 0x11b1b8
cmpb $0x1, %r14b
jne 0x48bec
movl 0x85c(%r12), %edi
addq $-0x28, %rsi
callq 0x26740
cmpl $-0x1, %eax
je 0x48e5b
movq 0x848(%r12), %rbx
movq 0x850(%r12), %rsi
leaq 0x800000(%rsi), %rax
cmpq %rax, %rbx
jl 0x48c28
subq %rsi, %rbx
movl 0x85c(%r12), %edi
movq %rbx, %rdx
movl $0x3, %ecx
callq 0x26cc0
addq %rbx, 0x850(%r12)
movq 0x848(%r12), %rax
cmpq 0x840(%r12), %rax
movl $0x1, %eax
cmovnel %ebp, %eax
cmpb $0x0, 0xd2571(%rip)     # 0x11b1b8
cmovnel %ebp, %eax
testl %eax, %eax
je 0x48df3
movq 0x738(%r12), %rsi
movq 0x860(%r12), %rdi
callq 0x26cb0
cmpl $-0x1, %eax
je 0x48e3b
leaq 0x7bfae(%rip), %rsi     # 0xc4c25
movl $0x2, %edi
xorl %eax, %eax
callq 0x32ea6
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x45574
leaq -0x31a(%rip), %rdi      # 0x4897a
callq 0x45479
movq 0x40(%r12), %rdi
movl 0x858(%r12), %esi
movl $0x1, %edx
callq 0x2ffb6
leaq 0x7bf97(%rip), %rsi     # 0xc4c4e
movl $0x2, %edi
xorl %eax, %eax
callq 0x32ea6
movq 0x738(%r12), %rdi
callq 0x4da84
testl %eax, %eax
je 0x48e73
leaq 0x7bf9b(%rip), %rsi     # 0xc4c7a
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
jmp 0x48d2f
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x7be20(%rip), %rsi     # 0xc4b20
movl $0x3, %edi
movq %rax, %rdx
jmp 0x48de7
callq 0x26890
movl (%rax), %edi
callq 0x26690
movq %rax, %rdx
leaq 0x7bdc4(%rip), %rsi     # 0xc4ae7
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
addq $0x1000, %rsp           # imm = 0x1000
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x48f1f
cmpl $0x3a464f45, 0x1(%rsp)  # imm = 0x3A464F45
jne 0x48e03
leaq 0x5(%rsp), %rbx
movq %rbx, %rdi
callq 0x26510
cmpq $0x28, %rax
jb 0x48e03
movb $0x1, 0xd2449(%rip)     # 0x11b1b8
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movaps %xmm0, 0xd23e3(%rip)    # 0x11b160
movaps %xmm1, 0xd23ec(%rip)    # 0x11b170
movq 0x20(%rbx), %rax
movq %rax, 0xd23f1(%rip)     # 0x11b180
xorps %xmm0, %xmm0
movaps %xmm0, 0xd23f7(%rip)    # 0x11b190
movaps %xmm0, 0xd2400(%rip)    # 0x11b1a0
xorl %eax, %eax
movq %rax, 0xd2407(%rip)     # 0x11b1b0
movq %rax, 0x840(%r12)
leaq 0x7bcbd(%rip), %rsi     # 0xc4a75
movl $0x2, %edi
xorl %eax, %eax
callq 0x32ea6
jmp 0x48df3
movq 0x968(%r12), %rax
movq %rax, 0x868(%r12)
jmp 0x48df3
leaq 0x7bc24(%rip), %rsi     # 0xc4a03
movq %rsp, %rdx
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
callq 0x48f1f
addq $0x1000, %rsp           # imm = 0x1000
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1(%rsp), %rdi
movb $0x0, 0xd23a9(%rip)     # 0x11b1b8
xorl %esi, %esi
movl $0xa, %edx
callq 0x26730
movq %rax, 0x840(%r12)
leaq 0x7bc85(%rip), %rsi     # 0xc4aaf
movl $0x2, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x32ea6
jmp 0x48df3
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x7bd7d(%rip), %rsi     # 0xc4bcb
movl $0x3, %edi
movq %rax, %rdx
jmp 0x48d28
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x7bd19(%rip), %rsi     # 0xc4b87
jmp 0x48d00
movq 0x860(%r12), %rdi
callq 0x3b3df
movl 0x85c(%r12), %edi
callq 0x26180
movl 0x858(%r12), %edi
callq 0x4897f
leaq 0x7be17(%rip), %rsi     # 0xc4cb8
movl $0x2, %edi
xorl %eax, %eax
callq 0x32ea6
cmpl $0x0, 0x658(%r12)
je 0x48df3
callq 0x60c4a
movl $0x9, %ebp
leaq 0x7be18(%rip), %rbx     # 0xc4ce5
callq 0x6139a
cmpl $-0x1, %eax
jne 0x48efa
movl $0x3, %edi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x32ea6
movl $0x1, %edi
callq 0x26600
addl $-0x1, %ebp
jb 0x48ecd
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %ebp, %ebp
jne 0x48df3
leaq 0x7be3c(%rip), %rsi     # 0xc4d45
movl $0x3, %edi
xorl %eax, %eax
callq 0x32ea6
movl $0x1, %edi
callq 0x262d0