// File: /workspace/github_c_makefile_star2_O3/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 0xbc11c(%rip), %r12     # 0x1018a8
movq 0x850(%r12), %rax
cmpq $-0x1, %rax
je 0x45827
movl $0x1000, %edx           # imm = 0x1000
cmpb $0x0, 0xd0a6e(%rip)     # 0x116218
jne 0x457c3
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 0x45878
addq %rbx, 0x420(%r12)
xorl %r15d, %r15d
cmpb $0x1, 0xd0a2f(%rip)     # 0x116218
movl $0x0, %ebp
jne 0x458fa
cmpq $0x28, %rbx
jb 0x458a6
movq -0x8(%rsp,%rbx), %rax
movq %rax, 0xd0a06(%rip)     # 0x116210
movups -0x28(%rsp,%rbx), %xmm0
movups -0x18(%rsp,%rbx), %xmm1
movaps %xmm1, 0xd09e5(%rip)    # 0x116200
movaps %xmm0, 0xd09ce(%rip)    # 0x1161f0
jmp 0x458d6
movslq 0x848(%r12), %rax
imulq $0x3e8, %rax, %rcx      # imm = 0x3E8
movq %rsp, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x64fcb
cmpq $-0x1, %rax
je 0x4588e
movzbl (%rsp), %eax
testl %eax, %eax
je 0x45b3d
cmpl $0x24, %eax
je 0x45aba
cmpl $0x2d, %eax
jne 0x45b4f
leaq 0x1(%rsp), %rdx
leaq 0x7c1f7(%rip), %rsi     # 0xc1a6a
jmp 0x45b59
cmpq $-0x1, %rbx
je 0x45a84
leaq 0x7c31b(%rip), %rdx     # 0xc1ba4
jmp 0x45a93
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x7c19c(%rip), %rsi     # 0xc1a3d
jmp 0x45a77
movl $0x28, %r14d
subq %rbx, %r14
leaq 0xd093a(%rip), %r15     # 0x1161f0
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 0xd0913(%rip), %rdi     # 0x1161f0
leaq 0xd08dc(%rip), %rsi     # 0x1161c0
movl $0x28, %edx
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 0x45a64
movq 0x858(%r12), %r14
addq %rbx, %r14
movq %r14, 0x858(%r12)
andb 0xd08d8(%rip), %r15b    # 0x116218
cmpb $0x1, %r15b
jne 0x4596b
movl 0x86c(%r12), %edi
addq $-0x28, %r14
movq %r14, %rsi
callq 0x26730
cmpl $-0x1, %eax
je 0x45bd2
movq 0x858(%r12), %r14
movq 0x860(%r12), %rsi
leaq 0x800000(%rsi), %rax
cmpq %rax, %r14
jl 0x459a7
subq %rsi, %r14
movl 0x86c(%r12), %edi
movq %r14, %rdx
movl $0x3, %ecx
callq 0x26cb0
addq %r14, 0x860(%r12)
movq 0x858(%r12), %r14
cmpq 0x850(%r12), %r14
movl $0x1, %eax
cmovnel %ebp, %eax
cmpb $0x0, 0xd085a(%rip)     # 0x116218
cmovnel %ebp, %eax
testl %eax, %eax
je 0x45b6a
movq 0x748(%r12), %rsi
movq 0x870(%r12), %rdi
callq 0x26ca0
cmpl $-0x1, %eax
je 0x45bb2
leaq 0x7c2cb(%rip), %rsi     # 0xc1cb9
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x420e0
leaq -0x31d(%rip), %rdi      # 0x456ee
callq 0x41fe5
movq 0x40(%r12), %rdi
movl 0x868(%r12), %esi
movl $0x1, %edx
callq 0x2cf0e
leaq 0x7c2b4(%rip), %rsi     # 0xc1ce2
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
movq 0x748(%r12), %rdi
callq 0x4a3f2
testl %eax, %eax
je 0x45bea
leaq 0x7c2b8(%rip), %rsi     # 0xc1d0e
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
jmp 0x45aa6
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x7c13d(%rip), %rsi     # 0xc1bb4
movl $0x3, %edi
movq %rax, %rdx
jmp 0x45b5e
callq 0x26880
movl (%rax), %edi
callq 0x26680
movq %rax, %rdx
leaq 0x7c0e1(%rip), %rsi     # 0xc1b7b
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
addq $0x1000, %rsp           # imm = 0x1000
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x45c96
cmpl $0x3a464f45, 0x1(%rsp)  # imm = 0x3A464F45
jne 0x45b7a
leaq 0x5(%rsp), %rbx
movq %rbx, %rdi
callq 0x26500
cmpq $0x28, %rax
jb 0x45b7a
movb $0x1, 0xd0732(%rip)     # 0x116218
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movaps %xmm0, 0xd06cc(%rip)    # 0x1161c0
movaps %xmm1, 0xd06d5(%rip)    # 0x1161d0
movq 0x20(%rbx), %rax
movq %rax, 0xd06da(%rip)     # 0x1161e0
xorps %xmm0, %xmm0
movaps %xmm0, 0xd06e0(%rip)    # 0x1161f0
movaps %xmm0, 0xd06e9(%rip)    # 0x116200
xorl %eax, %eax
movq %rax, 0xd06f0(%rip)     # 0x116210
movq %rax, 0x850(%r12)
leaq 0x7bfda(%rip), %rsi     # 0xc1b09
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
jmp 0x45b6a
movq 0x978(%r12), %rax
movq %rax, 0x878(%r12)
jmp 0x45b6a
leaq 0x7bf41(%rip), %rsi     # 0xc1a97
movq %rsp, %rdx
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
callq 0x45c96
addq $0x1000, %rsp           # imm = 0x1000
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1(%rsp), %rdi
movb $0x0, 0xd0692(%rip)     # 0x116218
xorl %esi, %esi
movl $0xa, %edx
callq 0x26720
movq %rax, 0x850(%r12)
leaq 0x7bfa2(%rip), %rsi     # 0xc1b43
movl $0x2, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2fdad
jmp 0x45b6a
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x7c09a(%rip), %rsi     # 0xc1c5f
movl $0x3, %edi
movq %rax, %rdx
jmp 0x45a9f
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x7c036(%rip), %rsi     # 0xc1c1b
jmp 0x45a77
movq 0x870(%r12), %rdi
callq 0x38413
movl 0x86c(%r12), %edi
callq 0x26180
movl 0x868(%r12), %edi
callq 0x456f3
leaq 0x7c134(%rip), %rsi     # 0xc1d4c
movl $0x2, %edi
xorl %eax, %eax
callq 0x2fdad
cmpl $0x0, 0x668(%r12)
je 0x45b6a
callq 0x5d037
movl $0xa, %ebp
leaq 0x7c135(%rip), %rbx     # 0xc1d79
callq 0x5d786
cmpl $-0x1, %eax
jne 0x45c70
movl $0x3, %edi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2fdad
movl $0x1, %edi
callq 0x265f0
decl %ebp
jne 0x45c44
jmp 0x45b6a
cmpl $0x1, %ebp
jne 0x45b6a
leaq 0x7c159(%rip), %rsi     # 0xc1dd9
movl $0x3, %edi
xorl %eax, %eax
callq 0x2fdad
movl $0x1, %edi
callq 0x262c0