// File: /workspace/github_c_makefile_star2_O3/gplessis[P]dotdeb-redis/src/rdb.c
// Function: rdbSaveToSlavesSockets
int rdbSaveToSlavesSockets(void) {
    int *fds;
    uint64_t *clientids;
    int numfds;
    listNode *ln;
    listIter li;
    pid_t childpid;
    long long start;
    int pipefds[2];

    if (server.aof_child_pid != -1 || server.rdb_child_pid != -1) return C_ERR;

    /* Before to fork, create a pipe that will be used in order to
     * send back to the parent the IDs of the slaves that successfully
     * received all the writes. */
    if (pipe(pipefds) == -1) return C_ERR;
    server.rdb_pipe_read_result_from_child = pipefds[0];
    server.rdb_pipe_write_result_to_parent = pipefds[1];

    /* Collect the file descriptors of the slaves we want to transfer
     * the RDB to, which are i WAIT_BGSAVE_START state. */
    fds = zmalloc(sizeof(int)*listLength(server.slaves));
    /* We also allocate an array of corresponding client IDs. This will
     * be useful for the child process in order to build the report
     * (sent via unix pipe) that will be sent to the parent. */
    clientids = zmalloc(sizeof(uint64_t)*listLength(server.slaves));
    numfds = 0;

    listRewind(server.slaves,&li);
    while((ln = listNext(&li))) {
        client *slave = ln->value;

        if (slave->replstate == SLAVE_STATE_WAIT_BGSAVE_START) {
            clientids[numfds] = slave->id;
            fds[numfds++] = slave->fd;
            replicationSetupSlaveForFullResync(slave,getPsyncInitialOffset());
            /* Put the socket in blocking mode to simplify RDB transfer.
             * We'll restore it when the children returns (since duped socket
             * will share the O_NONBLOCK attribute with the parent). */
            anetBlock(NULL,slave->fd);
            anetSendTimeout(NULL,slave->fd,server.repl_timeout*1000);
        }
    }

    /* Create the child process. */
    start = ustime();
    if ((childpid = fork()) == 0) {
        /* Child */
        int retval;
        rio slave_sockets;

        rioInitWithFdset(&slave_sockets,fds,numfds);
        zfree(fds);

        closeListeningSockets(0);
        redisSetProcTitle("redis-rdb-to-slaves");

        retval = rdbSaveRioWithEOFMark(&slave_sockets,NULL);
        if (retval == C_OK && rioFlush(&slave_sockets) == 0)
            retval = C_ERR;

        if (retval == C_OK) {
            size_t private_dirty = zmalloc_get_private_dirty();

            if (private_dirty) {
                serverLog(LL_NOTICE,
                    "RDB: %zu MB of memory used by copy-on-write",
                    private_dirty/(1024*1024));
            }

            /* If we are returning OK, at least one slave was served
             * with the RDB file as expected, so we need to send a report
             * to the parent via the pipe. The format of the message is:
             *
             * <len> <slave[0].id> <slave[0].error> ...
             *
             * len, slave IDs, and slave errors, are all uint64_t integers,
             * so basically the reply is composed of 64 bits for the len field
             * plus 2 additional 64 bit integers for each entry, for a total
             * of 'len' entries.
             *
             * The 'id' represents the slave's client ID, so that the master
             * can match the report with a specific slave, and 'error' is
             * set to 0 if the replication process terminated with a success
             * or the error code if an error occurred. */
            void *msg = zmalloc(sizeof(uint64_t)*(1+2*numfds));
            uint64_t *len = msg;
            uint64_t *ids = len+1;
            int j, msglen;

            *len = numfds;
            for (j = 0; j < numfds; j++) {
                *ids++ = clientids[j];
                *ids++ = slave_sockets.io.fdset.state[j];
            }

            /* Write the message to the parent. If we have no good slaves or
             * we are unable to transfer the message to the parent, we exit
             * with an error so that the parent will abort the replication
             * process with all the childre that were waiting. */
            msglen = sizeof(uint64_t)*(1+2*numfds);
            if (*len == 0 ||
                write(server.rdb_pipe_write_result_to_parent,msg,msglen)
                != msglen)
            {
                retval = C_ERR;
            }
            zfree(msg);
        }
        zfree(clientids);
        rioFreeFdset(&slave_sockets);
        exitFromChild((retval == C_OK) ? 0 : 1);
    } else {
        /* Parent */
        if (childpid == -1) {
            serverLog(LL_WARNING,"Can't save in background: fork: %s",
                strerror(errno));

            /* Undo the state change. The caller will perform cleanup on
             * all the slaves in BGSAVE_START state, but an early call to
             * replicationSetupSlaveForFullResync() turned it into BGSAVE_END */
            listRewind(server.slaves,&li);
            while((ln = listNext(&li))) {
                client *slave = ln->value;
                int j;

                for (j = 0; j < numfds; j++) {
                    if (slave->id == clientids[j]) {
                        slave->replstate = SLAVE_STATE_WAIT_BGSAVE_START;
                        break;
                    }
                }
            }
            close(pipefds[0]);
            close(pipefds[1]);
        } else {
            server.stat_fork_time = ustime()-start;
            server.stat_fork_rate = (double) zmalloc_used_memory() * 1000000 / server.stat_fork_time / (1024*1024*1024); /* GB per second. */
            latencyAddSampleIfNeeded("fork",server.stat_fork_time/1000);

            serverLog(LL_NOTICE,"Background RDB transfer started by pid %d",
                childpid);
            server.rdb_save_time_start = time(NULL);
            server.rdb_child_pid = childpid;
            server.rdb_child_type = RDB_CHILD_TYPE_SOCKET;
            updateDictResizePolicy();
        }
        zfree(clientids);
        zfree(fds);
        return (childpid == -1) ? C_ERR : C_OK;
    }
    return C_OK; /* Unreached. */
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
leaq 0xb6c03(%rip), %rbp     # 0x1018a8
cmpl $-0x1, 0x69c(%rbp)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jne 0x4b078
cmpl $-0x1, 0x730(%rbp)
jne 0x4b078
leaq 0x8(%rsp), %rdi
callq 0x265e0
cmpl $-0x1, %eax
je 0x4b078
movq 0x8(%rsp), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x788(%rbp)
movq 0x1e0(%rbp), %rax
movq 0x28(%rax), %rdi
shlq $0x2, %rdi
callq 0x382d1
movq %rax, %r14
movq 0x1e0(%rbp), %rax
movq 0x28(%rax), %rdi
shlq $0x3, %rdi
callq 0x382d1
movq %rax, %rbx
movq 0x1e0(%rbp), %rdi
leaq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x2b3b7
movq %r15, %rdi
callq 0x2b3d4
xorl %r15d, %r15d
testq %rax, %rax
je 0x4ada8
leaq 0x10(%rsp), %r12
movq 0x10(%rax), %r13
cmpl $0x6, 0xa0(%r13)
jne 0x4ad9b
movq (%r13), %rax
movslq %r15d, %rcx
movq %rax, (%rbx,%rcx,8)
movl 0x8(%r13), %eax
leal 0x1(%rcx), %r15d
movl %eax, (%r14,%rcx,4)
callq 0x4477d
movq %r13, %rdi
movq %rax, %rsi
callq 0x44798
movl 0x8(%r13), %esi
xorl %edi, %edi
callq 0x2d6ab
movl 0x8(%r13), %esi
movslq 0x834(%rbp), %rax
imulq $0x3e8, %rax, %rdx      # imm = 0x3E8
xorl %edi, %edi
callq 0x2d8f0
movq %r12, %rdi
callq 0x2b3d4
testq %rax, %rax
jne 0x4ad42
callq 0x2ffab
movq %rax, %r12
callq 0x26ab0
testl %eax, %eax
je 0x4aea3
movl %eax, %r13d
cmpl $-0x1, %eax
je 0x4afc8
callq 0x2ffab
subq %r12, %rax
movq %rax, 0x3c8(%rbp)
callq 0x3847a
movq %rax, %xmm0
punpckldq 0x77b76(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x77b7e(%rip), %xmm0    # 0xc2970
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x77b7a(%rip), %xmm1    # 0xc2980
movq 0x3c8(%rbp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
divsd %xmm0, %xmm1
mulsd 0x7171f(%rip), %xmm1    # 0xbc540
movsd %xmm1, 0x3d0(%rbp)
movq 0xa18(%rbp), %rcx
testq %rcx, %rcx
je 0x4ae64
movabsq $0x20c49ba5e353f7cf, %rdx # imm = 0x20C49BA5E353F7CF
imulq %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
cmpq %rcx, %rdx
jl 0x4ae64
leaq 0x77d92(%rip), %rdi     # 0xc2bee
movq %rdx, %rsi
callq 0x7eb75
leaq 0x781a1(%rip), %rsi     # 0xc300c
movl $0x2, %edi
movl %r13d, %edx
xorl %eax, %eax
callq 0x2fdad
xorl %edi, %edi
callq 0x26cd0
movq %rax, 0x770(%rbp)
movl %r13d, 0x730(%rbp)
movl $0x2, 0x77c(%rbp)
callq 0x30423
jmp 0x4b05a
leaq 0x20(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
movl %r15d, %edx
callq 0x726e3
movq %r14, %rdi
callq 0x38413
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x33c2a
leaq 0x78129(%rip), %rdi     # 0xc2ff8
callq 0x3598c
movq %r13, %rdi
xorl %esi, %esi
callq 0x493db
movl $0x1, %r13d
testl %eax, %eax
jne 0x4afa9
leaq 0x20(%rsp), %rdi
callq *0x18(%rdi)
testl %eax, %eax
je 0x4afa9
callq 0x386a8
testq %rax, %rax
je 0x4af20
shrq $0x14, %rax
leaq 0x77cb1(%rip), %rsi     # 0xc2bc2
movl $0x2, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2fdad
leal 0x1(,%r15,2), %eax
movslq %eax, %r13
leaq (,%r13,8), %rdi
callq 0x382d1
movq %rax, %r14
movslq %r15d, %rax
movq %rax, (%r14)
testl %r15d, %r15d
jle 0x4af72
movl %r15d, %eax
shlq $0x2, %rax
xorl %ecx, %ecx
movq (%rbx,%rcx,2), %rdx
movq %rdx, 0x8(%r14,%rcx,4)
movq 0x68(%rsp), %rdx
movslq (%rdx,%rcx), %rdx
movq %rdx, 0x10(%r14,%rcx,4)
addq $0x4, %rcx
cmpq %rcx, %rax
jne 0x4af4f
movq (%r14), %rax
testq %rax, %rax
je 0x4af9b
movl 0x788(%rbp), %edi
shlq $0x23, %r13
sarq $0x20, %r13
movq %r14, %rsi
movq %r13, %rdx
callq 0x26bb0
movq %r13, %rcx
xorl %r13d, %r13d
cmpq %rcx, %rax
je 0x4afa1
movl $0x1, %r13d
movq %r14, %rdi
callq 0x38413
movq %rbx, %rdi
callq 0x38413
leaq 0x20(%rsp), %rdi
callq 0x7276d
movl %r13d, %edi
callq 0x3000c
jmp 0x4b078
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x77c18(%rip), %rsi     # 0xc2bf3
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2fdad
movq 0x1e0(%rbp), %rdi
leaq 0x10(%rsp), %r12
movq %r12, %rsi
callq 0x2b3b7
movq %r12, %rdi
callq 0x2b3d4
testq %rax, %rax
je 0x4b048
movl %r15d, %ebp
leaq 0x10(%rsp), %r12
testl %r15d, %r15d
jle 0x4b03b
movq 0x10(%rax), %rax
movq (%rax), %rcx
xorl %edx, %edx
cmpq (%rbx,%rdx,8), %rcx
je 0x4b031
incq %rdx
cmpq %rdx, %rbp
jne 0x4b021
jmp 0x4b03b
movl $0x6, 0xa0(%rax)
movq %r12, %rdi
callq 0x2b3d4
testq %rax, %rax
jne 0x4b013
movl 0x8(%rsp), %edi
callq 0x26180
movl 0xc(%rsp), %edi
callq 0x26180
xorl %r12d, %r12d
cmpl $-0x1, %r13d
sete %r12b
movq %rbx, %rdi
callq 0x38413
movq %r14, %rdi
callq 0x38413
negl %r12d
movl %r12d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq