// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/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 0xbb522(%rip), %r12     # 0xf98a8
movq 0x850(%r12), %rax
cmpq $-0x1, %rax
je 0x3e421
movl $0x1000, %edx           # imm = 0x1000
cmpb $0x0, 0xcfe74(%rip)     # 0x10e218
jne 0x3e3bd
subq 0x858(%r12), %rax
movl $0x1000, %ecx           # imm = 0x1000
cmpq %rcx, %rax
cmovgeq %rcx, %rax
movq %rax, %rdx
movq %rsp, %rsi
callq 0x26340
movq %rax, %rbx
testq %rax, %rax
jle 0x3e475
addq %rbx, 0x420(%r12)
xorl %r15d, %r15d
cmpb $0x1, 0xcfe35(%rip)     # 0x10e218
movl $0x0, %ebp
jne 0x3e4f3
cmpq $0x28, %rbx
jb 0x3e4a3
movq -0x8(%rsp,%rbx), %rax
movq %rax, 0xcfe0c(%rip)     # 0x10e210
movups -0x28(%rsp,%rbx), %xmm0
movups -0x18(%rsp,%rbx), %xmm1
movaps %xmm1, 0xcfdeb(%rip)    # 0x10e200
movaps %xmm0, 0xcfdd4(%rip)    # 0x10e1f0
jmp 0x3e4d1
movslq 0x848(%r12), %rax
imulq $0x3e8, %rax, %rcx      # imm = 0x3E8
movq %rsp, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x5ab2a
cmpq $-0x1, %rax
je 0x3e48b
movzbl (%rsp), %eax
testl %eax, %eax
je 0x3e73d
cmpl $0x24, %eax
je 0x3e6b9
cmpl $0x2d, %eax
jne 0x3e752
leaq 0x1(%rsp), %rdx
leaq 0x74d4d(%rip), %rsi     # 0xb31ba
pushq $0x3
popq %rdi
jmp 0x3e669
cmpq $-0x1, %rbx
je 0x3e685
leaq 0x74e6e(%rip), %rdx     # 0xb32f4
jmp 0x3e694
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x74cef(%rip), %rsi     # 0xb318d
jmp 0x3e663
pushq $0x28
popq %r14
subq %rbx, %r14
leaq 0xcfd3f(%rip), %r15     # 0x10e1f0
leaq (%r15,%rbx), %rsi
movq %r15, %rdi
movq %r14, %rdx
callq 0x267e0
addq %r15, %r14
movq %rsp, %rsi
movq %r14, %rdi
movq %rbx, %rdx
callq 0x26c50
leaq 0xcfd18(%rip), %rdi     # 0x10e1f0
leaq 0xcfce1(%rip), %rsi     # 0x10e1c0
pushq $0x28
popq %rdx
callq 0x266f0
xorl %r15d, %r15d
testl %eax, %eax
sete %r15b
movl %r15d, %ebp
movq 0x978(%r12), %rax
movq %rax, 0x878(%r12)
movl 0x86c(%r12), %edi
movq %rsp, %rsi
movq %rbx, %rdx
callq 0x26bb0
cmpq %rbx, %rax
jne 0x3e650
movq 0x858(%r12), %r14
addq %rbx, %r14
movq %r14, 0x858(%r12)
andb 0xcfcdf(%rip), %r15b    # 0x10e218
cmpb $0x1, %r15b
jne 0x3e564
movl 0x86c(%r12), %edi
addq $-0x28, %r14
movq %r14, %rsi
callq 0x26730
cmpl $-0x1, %eax
je 0x3e7b9
movq 0x858(%r12), %r14
movq 0x860(%r12), %rsi
leaq 0x800000(%rsi), %rax
cmpq %rax, %r14
jl 0x3e59e
subq %rsi, %r14
movl 0x86c(%r12), %edi
pushq $0x3
popq %rcx
movq %r14, %rdx
callq 0x26cb0
addq %r14, 0x860(%r12)
movq 0x858(%r12), %r14
cmpq 0x850(%r12), %r14
pushq $0x1
popq %rax
cmovnel %ebp, %eax
cmpb $0x0, 0xcfc65(%rip)     # 0x10e218
cmovnel %ebp, %eax
testl %eax, %eax
je 0x3e675
movq 0x748(%r12), %rsi
movq 0x870(%r12), %rdi
callq 0x26ca0
cmpl $-0x1, %eax
je 0x3e79b
leaq 0x74e26(%rip), %rsi     # 0xb3409
pushq $0x2
popq %rbx
movl %ebx, %edi
xorl %eax, %eax
callq 0x2b699
pushq $-0x1
popq %rdi
callq 0x3b3d8
leaq -0x316(%rip), %rdi      # 0x3e2e8
callq 0x3b308
movq 0x40(%r12), %rdi
movl 0x868(%r12), %esi
pushq $0x1
popq %rdx
callq 0x28ba8
leaq 0x74e13(%rip), %rsi     # 0xb3432
movl %ebx, %edi
xorl %eax, %eax
callq 0x2b699
movq 0x748(%r12), %rdi
callq 0x425c7
testl %eax, %eax
je 0x3e7d1
leaq 0x74e1a(%rip), %rsi     # 0xb345e
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
jmp 0x3e6a5
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x74ca1(%rip), %rsi     # 0xb3304
pushq $0x3
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2b699
callq 0x3e882
addq $0x1000, %rsp           # imm = 0x1000
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
callq 0x26880
movl (%rax), %edi
callq 0x26680
movq %rax, %rdx
leaq 0x74c30(%rip), %rsi     # 0xb32cb
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
addq $0x1000, %rsp           # imm = 0x1000
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x3e882
cmpl $0x3a464f45, 0x1(%rsp)  # imm = 0x3A464F45
jne 0x3e764
leaq 0x5(%rsp), %rbx
movq %rbx, %rdi
callq 0x26500
cmpq $0x28, %rax
jb 0x3e764
movb $0x1, 0xcfb33(%rip)     # 0x10e218
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movaps %xmm0, 0xcfacd(%rip)    # 0x10e1c0
movaps %xmm1, 0xcfad6(%rip)    # 0x10e1d0
movq 0x20(%rbx), %rax
movq %rax, 0xcfadb(%rip)     # 0x10e1e0
xorps %xmm0, %xmm0
movaps %xmm0, 0xcfae1(%rip)    # 0x10e1f0
andq $0x0, 0xcfaf9(%rip)     # 0x10e210
movaps %xmm0, 0xcfae2(%rip)    # 0x10e200
andq $0x0, 0x850(%r12)
leaq 0x74b2b(%rip), %rsi     # 0xb3259
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x2b699
jmp 0x3e675
movq 0x978(%r12), %rax
movq %rax, 0x878(%r12)
jmp 0x3e675
leaq 0x74a8e(%rip), %rsi     # 0xb31e7
pushq $0x3
popq %rdi
movq %rsp, %rdx
jmp 0x3e669
leaq 0x1(%rsp), %rdi
movb $0x0, 0xcfaa8(%rip)     # 0x10e218
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x26720
movq %rax, 0x850(%r12)
leaq 0x74b0a(%rip), %rsi     # 0xb3293
pushq $0x2
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2b699
jmp 0x3e675
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x74c01(%rip), %rsi     # 0xb33af
pushq $0x3
popq %rdi
movq %rax, %rdx
jmp 0x3e69e
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x74b9f(%rip), %rsi     # 0xb336b
jmp 0x3e663
movq 0x870(%r12), %rdi
callq 0x328f9
movl 0x86c(%r12), %edi
callq 0x26180
movl 0x868(%r12), %edi
callq 0x3e2ed
leaq 0x74c9d(%rip), %rsi     # 0xb349c
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x2b699
cmpl $0x0, 0x668(%r12)
je 0x3e675
callq 0x53ab6
pushq $0xa
popq %r12
leaq 0x74ca1(%rip), %rbx     # 0xb34c9
pushq $0x3
popq %rbp
pushq $0x1
popq %r14
movl %r12d, %r15d
subl $0x1, %r15d
jb 0x3e675
callq 0x540d1
cmpl $-0x1, %eax
jne 0x3e85f
movl %ebp, %edi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2b699
movl %r14d, %edi
callq 0x265f0
movl %r15d, %r12d
jmp 0x3e832
cmpl $0x1, %r12d
jne 0x3e675
leaq 0x74cb9(%rip), %rsi     # 0xb3529
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x2b699
pushq $0x1
popq %rdi
callq 0x262c0