// File: /workspace/github_c_makefile_star2_O2/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 0xb6b30(%rip), %r15     # 0xf98a8
cmpl $-0x1, 0x69c(%r15)
pushq $-0x1
popq %r12
jne 0x43115
cmpl $-0x1, 0x730(%r15)
jne 0x43115
leaq 0x8(%rsp), %rdi
callq 0x265e0
cmpl $-0x1, %eax
je 0x43115
movq 0x8(%rsp), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x788(%r15)
movq 0x1e0(%r15), %rax
movq 0x28(%rax), %rdi
shlq $0x2, %rdi
callq 0x327b9
movq %rax, %r14
movq 0x1e0(%r15), %rax
movq 0x28(%rax), %rdi
shlq $0x3, %rdi
callq 0x327b9
movq %rax, %rbx
movq 0x1e0(%r15), %rdi
leaq 0x10(%rsp), %r12
movq %r12, %rsi
callq 0x2702c
xorl %r15d, %r15d
movq %r12, %rdi
callq 0x27046
movl %r15d, %ebp
testq %rax, %rax
je 0x42e72
movq 0x10(%rax), %r13
cmpl $0x6, 0xa0(%r13)
jne 0x42e04
movq (%r13), %rax
movq %rax, (%rbx,%rbp,8)
movl 0x8(%r13), %eax
incq %r15
movl %eax, (%r14,%rbp,4)
callq 0x3d4e3
movq %r13, %rdi
movq %rax, %rsi
callq 0x3d4fe
movl 0x8(%r13), %esi
xorl %edi, %edi
callq 0x292a4
movl 0x8(%r13), %esi
leaq 0xb6a4d(%rip), %rax     # 0xf98a8
movslq 0x834(%rax), %rax
imulq $0x3e8, %rax, %rdx      # imm = 0x3E8
xorl %edi, %edi
callq 0x294a4
jmp 0x42e04
callq 0x2b88b
movq %rax, %r12
callq 0x26ab0
testl %eax, %eax
je 0x42f60
movl %eax, %r13d
cmpl $-0x1, %eax
je 0x4301d
callq 0x2b88b
subq %r12, %rax
leaq 0xb6a06(%rip), %r15     # 0xf98a8
movq %rax, 0x3c8(%r15)
callq 0x32960
movq %rax, %xmm0
punpckldq 0x71165(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x7116d(%rip), %xmm0    # 0xb4030
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x71169(%rip), %xmm1    # 0xb4040
movq 0x3c8(%r15), %rax
cvtsi2sd %rax, %xmm0
divsd %xmm0, %xmm1
mulsd 0x6b541(%rip), %xmm1    # 0xae430
movsd %xmm1, 0x3d0(%r15)
movq 0xa18(%r15), %rcx
testq %rcx, %rcx
je 0x42f22
movl $0x3e8, %esi            # imm = 0x3E8
cqto
idivq %rsi
cmpq %rcx, %rax
jl 0x42f22
leaq 0x71394(%rip), %rdi     # 0xb42ae
movq %rax, %rsi
callq 0x71e85
leaq 0x717a3(%rip), %rsi     # 0xb46cc
pushq $0x2
popq %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0x2b699
xorl %edi, %edi
callq 0x26cd0
movq %rax, 0x770(%r15)
movl %r13d, 0x730(%r15)
movl $0x2, 0x77c(%r15)
callq 0x2bb7f
jmp 0x4309e
leaq 0x20(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
movl %r15d, %edx
callq 0x66ad4
movq %r14, %rdi
callq 0x328f9
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x2edf6
leaq 0x7172c(%rip), %rdi     # 0xb46b8
callq 0x308b2
movq %r13, %rdi
xorl %esi, %esi
callq 0x418ad
pushq $0x1
popq %r13
testl %eax, %eax
jne 0x430fb
leaq 0x20(%rsp), %rdi
callq *0x18(%rdi)
testl %eax, %eax
je 0x430fb
callq 0x32b90
testq %rax, %rax
je 0x42fd9
shrq $0x14, %rax
leaq 0x712b6(%rip), %rsi     # 0xb4282
pushq $0x2
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2b699
leal 0x1(,%r15,2), %r13d
leaq (,%r13,8), %rdi
callq 0x327b9
movq %rax, %r14
movq %rbp, (%rax)
xorl %eax, %eax
subq $0x1, %r15
jb 0x430be
movq (%rbx,%rax,2), %rcx
movq %rcx, 0x8(%r14,%rax,4)
movq 0x68(%rsp), %rcx
movslq (%rcx,%rax), %rcx
movq %rcx, 0x10(%r14,%rax,4)
addq $0x4, %rax
jmp 0x42ff6
callq 0x26880
movl (%rax), %edi
callq 0x26680
leaq 0x71283(%rip), %rsi     # 0xb42b3
pushq $0x3
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x2b699
leaq 0xb6864(%rip), %rax     # 0xf98a8
movq 0x1e0(%rax), %rdi
leaq 0x10(%rsp), %r12
movq %r12, %rsi
callq 0x2702c
movq %r12, %rdi
callq 0x27046
testq %rax, %rax
je 0x4308c
movq 0x10(%rax), %rax
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x43058
movq (%rax), %rdx
leaq 0x1(%rcx), %rsi
cmpq (%rbx,%rcx,8), %rdx
movq %rsi, %rcx
jne 0x4306b
movl $0x6, 0xa0(%rax)
jmp 0x43058
movl 0x8(%rsp), %edi
callq 0x26180
movl 0xc(%rsp), %edi
callq 0x26180
xorl %r12d, %r12d
cmpl $-0x1, %r13d
sete %r12b
movq %rbx, %rdi
callq 0x328f9
movq %r14, %rdi
callq 0x328f9
negl %r12d
jmp 0x43115
cmpq $0x0, (%r14)
je 0x430ef
leaq 0xb67dd(%rip), %rax     # 0xf98a8
movl 0x788(%rax), %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 0x430f3
pushq $0x1
popq %r13
movq %r14, %rdi
callq 0x328f9
movq %rbx, %rdi
callq 0x328f9
leaq 0x20(%rsp), %rdi
callq 0x66b5c
movl %r13d, %edi
callq 0x2b8c0
movl %r12d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq