// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1070, %rsp           # imm = 0x1070
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
leaq 0x128708(%rip), %rax    # 0x1781d8
cmpq $-0x1, 0x850(%rax)
jne 0x4fcaf
movl -0xc(%rbp), %edi
leaq -0x1020(%rbp), %rsi
leaq 0x1286e9(%rip), %rax    # 0x1781d8
imull $0x3e8, 0x848(%rax), %eax # imm = 0x3E8
movslq %eax, %rcx
movl $0x400, %edx            # imm = 0x400
callq 0x7bd40
cmpq $-0x1, %rax
jne 0x4fb33
callq 0x26880
movl (%rax), %edi
callq 0x26690
movq %rax, %rdx
movl $0x3, %edi
leaq 0xec7e2(%rip), %rsi     # 0x13c309
movb $0x0, %al
callq 0x2fc60
jmp 0x50229
movsbl -0x1020(%rbp), %eax
cmpl $0x2d, %eax
jne 0x4fb62
leaq -0x1020(%rbp), %rdx
addq $0x1, %rdx
movl $0x3, %edi
leaq 0xec7e0(%rip), %rsi     # 0x13c336
movb $0x0, %al
callq 0x2fc60
jmp 0x50229
movsbl -0x1020(%rbp), %eax
cmpl $0x0, %eax
jne 0x4fb8f
leaq 0x128663(%rip), %rax    # 0x1781d8
movq 0x978(%rax), %rcx
leaq 0x128655(%rip), %rax    # 0x1781d8
movq %rcx, 0x878(%rax)
jmp 0x5022e
movsbl -0x1020(%rbp), %eax
cmpl $0x24, %eax
je 0x4fbba
leaq -0x1020(%rbp), %rdx
movl $0x3, %edi
leaq 0xec7b5(%rip), %rsi     # 0x13c363
movb $0x0, %al
callq 0x2fc60
jmp 0x50229
jmp 0x4fbbc
jmp 0x4fbbe
leaq -0x1020(%rbp), %rdi
addq $0x1, %rdi
leaq 0xed695(%rip), %rsi     # 0x13d265
movl $0x4, %edx
callq 0x26350
cmpl $0x0, %eax
jne 0x4fc55
leaq -0x1020(%rbp), %rdi
addq $0x5, %rdi
callq 0x264f0
cmpq $0x28, %rax
jb 0x4fc55
movl $0x1, 0x13cf49(%rip)    # 0x18cb48
leaq -0x1020(%rbp), %rsi
addq $0x5, %rsi
leaq 0x13cedf(%rip), %rdi    # 0x18caf0
movl $0x28, %edx
callq 0x26c60
leaq 0x13cefe(%rip), %rdi    # 0x18cb20
xorl %esi, %esi
movl $0x28, %edx
callq 0x26110
leaq 0x1285a3(%rip), %rax    # 0x1781d8
movq $0x0, 0x850(%rax)
movl $0x2, %edi
leaq 0xec789(%rip), %rsi     # 0x13c3d5
movb $0x0, %al
callq 0x2fc60
jmp 0x4fcaa
movl $0x0, 0x13cee9(%rip)    # 0x18cb48
leaq -0x1020(%rbp), %rdi
addq $0x1, %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x26720
movq %rax, %rcx
leaq 0x128556(%rip), %rax    # 0x1781d8
movq %rcx, 0x850(%rax)
leaq 0x128548(%rip), %rax    # 0x1781d8
movq 0x850(%rax), %rdx
movl $0x2, %edi
leaq 0xec76c(%rip), %rsi     # 0x13c40f
movb $0x0, %al
callq 0x2fc60
jmp 0x5022e
cmpl $0x0, 0x13ce92(%rip)    # 0x18cb48
je 0x4fcc5
movq $0x1000, -0x1030(%rbp) # imm = 0x1000
jmp 0x4fd21
leaq 0x12850c(%rip), %rax    # 0x1781d8
movq 0x850(%rax), %rax
leaq 0x1284fe(%rip), %rcx    # 0x1781d8
subq 0x858(%rcx), %rax
movq %rax, -0x1038(%rbp)
cmpq $0x1000, -0x1038(%rbp) # imm = 0x1000
jge 0x4fd05
movq -0x1038(%rbp), %rax
movq %rax, -0x1058(%rbp)
jmp 0x4fd13
movl $0x1000, %eax           # imm = 0x1000
movq %rax, -0x1058(%rbp)
jmp 0x4fd13
movq -0x1058(%rbp), %rax
movq %rax, -0x1030(%rbp)
movl -0xc(%rbp), %edi
leaq -0x1020(%rbp), %rsi
movq -0x1030(%rbp), %rdx
callq 0x26340
movq %rax, -0x1028(%rbp)
cmpq $0x0, -0x1028(%rbp)
jg 0x4fd9b
cmpq $-0x1, -0x1028(%rbp)
jne 0x4fd67
callq 0x26880
movl (%rax), %edi
callq 0x26690
movq %rax, -0x1060(%rbp)
jmp 0x4fd77
leaq 0xec702(%rip), %rax     # 0x13c470
movq %rax, -0x1060(%rbp)
jmp 0x4fd77
movq -0x1060(%rbp), %rdx
movl $0x3, %edi
leaq 0xec6bd(%rip), %rsi     # 0x13c447
movb $0x0, %al
callq 0x2fc60
callq 0x50240
jmp 0x5022e
movq -0x1028(%rbp), %rcx
leaq 0x12842f(%rip), %rax    # 0x1781d8
addq 0x420(%rax), %rcx
leaq 0x128421(%rip), %rax    # 0x1781d8
movq %rcx, 0x420(%rax)
movl $0x0, -0x103c(%rbp)
cmpl $0x0, 0x13cd79(%rip)    # 0x18cb48
je 0x4fe84
cmpq $0x28, -0x1028(%rbp)
jl 0x4fe04
leaq -0x1020(%rbp), %rsi
addq -0x1028(%rbp), %rsi
addq $-0x28, %rsi
leaq 0x13cd28(%rip), %rdi    # 0x18cb20
movl $0x28, %edx
callq 0x26c60
jmp 0x4fe5b
movl $0x28, %eax
subq -0x1028(%rbp), %rax
movl %eax, -0x1040(%rbp)
leaq 0x13cd03(%rip), %rsi    # 0x18cb20
addq -0x1028(%rbp), %rsi
movslq -0x1040(%rbp), %rdx
leaq 0x13ccee(%rip), %rdi    # 0x18cb20
callq 0x267e0
movslq -0x1040(%rbp), %rax
leaq 0x13ccdb(%rip), %rdi    # 0x18cb20
addq %rax, %rdi
leaq -0x1020(%rbp), %rsi
movq -0x1028(%rbp), %rdx
callq 0x26c60
leaq 0x13ccbe(%rip), %rdi    # 0x18cb20
leaq 0x13cc87(%rip), %rsi    # 0x18caf0
movl $0x28, %edx
callq 0x269b0
cmpl $0x0, %eax
jne 0x4fe82
movl $0x1, -0x103c(%rbp)
jmp 0x4fe84
leaq 0x12834d(%rip), %rax    # 0x1781d8
movq 0x978(%rax), %rcx
leaq 0x12833f(%rip), %rax    # 0x1781d8
movq %rcx, 0x878(%rax)
leaq 0x128331(%rip), %rax    # 0x1781d8
movl 0x86c(%rax), %edi
leaq -0x1020(%rbp), %rsi
movq -0x1028(%rbp), %rdx
callq 0x26bc0
cmpq -0x1028(%rbp), %rax
je 0x4fef0
callq 0x26880
movl (%rax), %edi
callq 0x26690
movq %rax, %rdx
movl $0x3, %edi
leaq 0xec59c(%rip), %rsi     # 0x13c480
movb $0x0, %al
callq 0x2fc60
jmp 0x50229
movq -0x1028(%rbp), %rcx
leaq 0x1282da(%rip), %rax    # 0x1781d8
addq 0x858(%rax), %rcx
leaq 0x1282cc(%rip), %rax    # 0x1781d8
movq %rcx, 0x858(%rax)
cmpl $0x0, 0x13cc2e(%rip)    # 0x18cb48
je 0x4ff77
cmpl $0x0, -0x103c(%rbp)
je 0x4ff77
leaq 0x1282ac(%rip), %rax    # 0x1781d8
movl 0x86c(%rax), %edi
leaq 0x12829f(%rip), %rax    # 0x1781d8
movq 0x858(%rax), %rsi
subq $0x28, %rsi
callq 0x26730
cmpl $-0x1, %eax
jne 0x4ff75
callq 0x26880
movl (%rax), %edi
callq 0x26690
movq %rax, %rdx
movl $0x3, %edi
leaq 0xec57e(%rip), %rsi     # 0x13c4e7
movb $0x0, %al
callq 0x2fc60
jmp 0x50229
jmp 0x4ff77
leaq 0x12825a(%rip), %rax    # 0x1781d8
movq 0x858(%rax), %rax
leaq 0x12824c(%rip), %rcx    # 0x1781d8
movq 0x860(%rcx), %rcx
addq $0x800000, %rcx         # imm = 0x800000
cmpq %rcx, %rax
jl 0x50011
leaq 0x128232(%rip), %rax    # 0x1781d8
movq 0x858(%rax), %rax
leaq 0x128224(%rip), %rcx    # 0x1781d8
subq 0x860(%rcx), %rax
movq %rax, -0x1048(%rbp)
leaq 0x12820f(%rip), %rax    # 0x1781d8
movl 0x86c(%rax), %edi
leaq 0x128202(%rip), %rax    # 0x1781d8
movq 0x860(%rax), %rsi
movq -0x1048(%rbp), %rdx
movl $0x3, %ecx
callq 0x26cc0
movq -0x1048(%rbp), %rcx
leaq 0x1281dc(%rip), %rax    # 0x1781d8
addq 0x860(%rax), %rcx
leaq 0x1281ce(%rip), %rax    # 0x1781d8
movq %rcx, 0x860(%rax)
cmpl $0x0, 0x13cb30(%rip)    # 0x18cb48
jne 0x50044
leaq 0x1281b7(%rip), %rax    # 0x1781d8
movq 0x858(%rax), %rax
leaq 0x1281a9(%rip), %rcx    # 0x1781d8
cmpq 0x850(%rcx), %rax
jne 0x50042
movl $0x1, -0x103c(%rbp)
jmp 0x50044
cmpl $0x0, -0x103c(%rbp)
je 0x50227
leaq 0x128180(%rip), %rax    # 0x1781d8
movq 0x870(%rax), %rdi
leaq 0x128172(%rip), %rax    # 0x1781d8
movq 0x748(%rax), %rsi
callq 0x26cb0
cmpl $-0x1, %eax
jne 0x500a3
callq 0x26880
movl (%rax), %edi
callq 0x26690
movq %rax, %rdx
movl $0x3, %edi
leaq 0xec499(%rip), %rsi     # 0x13c52b
movb $0x0, %al
callq 0x2fc60
callq 0x50240
jmp 0x5022e
movl $0x2, %edi
leaq 0xec4d6(%rip), %rsi     # 0x13c585
movb $0x0, %al
callq 0x2fc60
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4af50
leaq -0x737(%rip), %rdi      # 0x4f990
callq 0x4adc0
leaq 0x128105(%rip), %rax    # 0x1781d8
movq 0x40(%rax), %rdi
leaq 0x1280fa(%rip), %rax    # 0x1781d8
movl 0x868(%rax), %esi
movl $0x1, %edx
callq 0x2aab0
movl $0x2, %edi
leaq 0xec4b4(%rip), %rsi     # 0x13c5ae
movb $0x0, %al
callq 0x2fc60
leaq 0x1280d0(%rip), %rax    # 0x1781d8
movq 0x748(%rax), %rdi
callq 0x56470
cmpl $0x0, %eax
je 0x50136
movl $0x3, %edi
leaq 0xec4b5(%rip), %rsi     # 0x13c5da
movb $0x0, %al
callq 0x2fc60
callq 0x50240
jmp 0x5022e
leaq 0x12809b(%rip), %rax    # 0x1781d8
movq 0x870(%rax), %rdi
callq 0x3afa0
leaq 0x128088(%rip), %rax    # 0x1781d8
movl 0x86c(%rax), %edi
callq 0x26180
leaq 0x128076(%rip), %rax    # 0x1781d8
movl 0x868(%rax), %edi
callq 0x4f9b0
movl $0x2, %edi
leaq 0xec49f(%rip), %rsi     # 0x13c618
movb $0x0, %al
callq 0x2fc60
leaq 0x128051(%rip), %rax    # 0x1781d8
cmpl $0x0, 0x668(%rax)
je 0x50225
movl $0xa, -0x104c(%rbp)
callq 0x70aa0
movl -0x104c(%rbp), %ecx
movl %ecx, %eax
addl $-0x1, %eax
movl %eax, -0x104c(%rbp)
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x1061(%rbp)
je 0x501d2
callq 0x713c0
cmpl $-0x1, %eax
sete %al
movb %al, -0x1061(%rbp)
movb -0x1061(%rbp), %al
testb $0x1, %al
jne 0x501de
jmp 0x501fd
movl $0x3, %edi
leaq 0xec45b(%rip), %rsi     # 0x13c645
movb $0x0, %al
callq 0x2fc60
movl $0x1, %edi
callq 0x26600
jmp 0x501a3
cmpl $0x0, -0x104c(%rbp)
jne 0x50223
movl $0x3, %edi
leaq 0xec493(%rip), %rsi     # 0x13c6a5
movb $0x0, %al
callq 0x2fc60
movl $0x1, %edi
callq 0x262d0
jmp 0x50225
jmp 0x50227
jmp 0x5022e
callq 0x50240
addq $0x1070, %rsp           # imm = 0x1070
popq %rbp
retq
nopw (%rax,%rax)