// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
leaq 0x1211f6(%rip), %rax    # 0x1781d8
cmpl $-0x1, 0x69c(%rax)
jne 0x56ffb
leaq 0x1211e6(%rip), %rax    # 0x1781d8
cmpl $-0x1, 0x730(%rax)
je 0x57007
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x57596
leaq -0x50(%rbp), %rdi
callq 0x265f0
cmpl $-0x1, %eax
jne 0x57021
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x57596
movl -0x50(%rbp), %ecx
leaq 0x1211ad(%rip), %rax    # 0x1781d8
movl %ecx, 0x78c(%rax)
movl -0x4c(%rbp), %ecx
leaq 0x12119d(%rip), %rax    # 0x1781d8
movl %ecx, 0x788(%rax)
leaq 0x121190(%rip), %rax    # 0x1781d8
movq 0x1e0(%rax), %rax
movq 0x28(%rax), %rdi
shlq $0x2, %rdi
callq 0x3acd0
movq %rax, -0x10(%rbp)
leaq 0x121171(%rip), %rax    # 0x1781d8
movq 0x1e0(%rax), %rax
movq 0x28(%rax), %rdi
shlq $0x3, %rdi
callq 0x3acd0
movq %rax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
leaq 0x12114b(%rip), %rax    # 0x1781d8
movq 0x1e0(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x27320
leaq -0x38(%rbp), %rdi
callq 0x27380
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x57158
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpl $0x6, 0xa0(%rax)
jne 0x57153
movq -0x58(%rbp), %rax
movq (%rax), %rdx
movq -0x18(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
movl 0x8(%rax), %edx
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x1c(%rbp)
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movq -0x58(%rbp), %rax
movq %rax, -0x108(%rbp)
callq 0x4e4c0
movq -0x108(%rbp), %rdi
movq %rax, %rsi
callq 0x4e500
movq -0x58(%rbp), %rax
movl 0x8(%rax), %esi
xorl %eax, %eax
movl %eax, %edi
callq 0x2b890
movq -0x58(%rbp), %rax
movl 0x8(%rax), %esi
leaq 0x12109b(%rip), %rax    # 0x1781d8
imull $0x3e8, 0x834(%rax), %eax # imm = 0x3E8
movslq %eax, %rdx
xorl %eax, %eax
movl %eax, %edi
callq 0x2bc40
jmp 0x5709d
callq 0x2ff30
movq %rax, -0x48(%rbp)
callq 0x26ab0
movl %eax, -0x3c(%rbp)
cmpl $0x0, %eax
jne 0x5735c
movq -0x10(%rbp), %rsi
movl -0x1c(%rbp), %edx
leaq -0xc8(%rbp), %rdi
callq 0x8e670
movq -0x10(%rbp), %rdi
callq 0x3afa0
xorl %edi, %edi
callq 0x34f40
leaq 0xe65ef(%rip), %rdi     # 0x13d78b
callq 0x37840
leaq -0xc8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x54f60
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jne 0x571d2
leaq -0xc8(%rbp), %rdi
callq 0x575b0
cmpl $0x0, %eax
jne 0x571d2
movl $0xffffffff, -0x5c(%rbp) # imm = 0xFFFFFFFF
cmpl $0x0, -0x5c(%rbp)
jne 0x5732d
callq 0x3b4a0
movq %rax, -0xd0(%rbp)
cmpq $0x0, -0xd0(%rbp)
je 0x57210
movq -0xd0(%rbp), %rdx
shrq $0x14, %rdx
movl $0x2, %edi
leaq 0xe614c(%rip), %rsi     # 0x13d355
movb $0x0, %al
callq 0x2fc60
movl -0x1c(%rbp), %eax
shll %eax
addl $0x1, %eax
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x3acd0
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xe8(%rbp)
movslq -0x1c(%rbp), %rcx
movq -0xe0(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0xec(%rbp)
movl -0xec(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x572cd
movq -0x18(%rbp), %rax
movslq -0xec(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq -0xe8(%rbp), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, -0xe8(%rbp)
movq %rcx, (%rax)
movq -0x80(%rbp), %rax
movslq -0xec(%rbp), %rcx
movslq (%rax,%rcx,4), %rcx
movq -0xe8(%rbp), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, -0xe8(%rbp)
movq %rcx, (%rax)
movl -0xec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xec(%rbp)
jmp 0x57263
movl -0x1c(%rbp), %eax
shll %eax
addl $0x1, %eax
cltq
shlq $0x3, %rax
movl %eax, -0xf0(%rbp)
movq -0xe0(%rbp), %rax
cmpq $0x0, (%rax)
je 0x5731a
leaq 0x120ee3(%rip), %rax    # 0x1781d8
movl 0x788(%rax), %edi
movq -0xd8(%rbp), %rsi
movslq -0xf0(%rbp), %rdx
callq 0x26bc0
movslq -0xf0(%rbp), %rcx
cmpq %rcx, %rax
je 0x57321
movl $0xffffffff, -0x5c(%rbp) # imm = 0xFFFFFFFF
movq -0xd8(%rbp), %rdi
callq 0x3afa0
movq -0x18(%rbp), %rdi
callq 0x3afa0
leaq -0xc8(%rbp), %rdi
callq 0x8e740
movl -0x5c(%rbp), %ecx
movl $0x1, %edi
xorl %eax, %eax
cmpl $0x0, %ecx
cmovel %eax, %edi
callq 0x2ff90
jmp 0x5758f
cmpl $-0x1, -0x3c(%rbp)
jne 0x5742e
callq 0x26880
movl (%rax), %edi
callq 0x26690
movq %rax, %rdx
movl $0x3, %edi
leaq 0xe6005(%rip), %rsi     # 0x13d386
movb $0x0, %al
callq 0x2fc60
leaq 0x120e49(%rip), %rax    # 0x1781d8
movq 0x1e0(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x27320
leaq -0x38(%rbp), %rdi
callq 0x27380
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x57419
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xf8(%rbp)
movl $0x0, -0xfc(%rbp)
movl -0xfc(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x57417
movq -0xf8(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movslq -0xfc(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
jne 0x57404
movq -0xf8(%rbp), %rax
movl $0x6, 0xa0(%rax)
jmp 0x57417
jmp 0x57406
movl -0xfc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfc(%rbp)
jmp 0x573cb
jmp 0x5739f
movl -0x50(%rbp), %edi
callq 0x26180
movl -0x4c(%rbp), %edi
callq 0x26180
jmp 0x57568
callq 0x2ff30
movq %rax, %rcx
movq -0x48(%rbp), %rax
subq %rax, %rcx
leaq 0x120d94(%rip), %rax    # 0x1781d8
movq %rcx, 0x3c8(%rax)
callq 0x3b090
movq %rax, %xmm0
movaps 0xe0044(%rip), %xmm1    # 0x1374a0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0xe0048(%rip), %xmm1    # 0x1374b0
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0xe5c69(%rip), %xmm1    # 0x13d0e8
mulsd %xmm1, %xmm0
leaq 0x120d4e(%rip), %rax    # 0x1781d8
cvtsi2sdq 0x3c8(%rax), %xmm1
divsd %xmm1, %xmm0
movsd 0xdffe1(%rip), %xmm1    # 0x137480
divsd %xmm1, %xmm0
leaq 0x120d2e(%rip), %rax    # 0x1781d8
movsd %xmm0, 0x3d0(%rax)
leaq 0x120d1f(%rip), %rax    # 0x1781d8
cmpq $0x0, 0xa18(%rax)
je 0x57512
leaq 0x120d0e(%rip), %rax    # 0x1781d8
movq 0x3c8(%rax), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
leaq 0x120cf6(%rip), %rcx    # 0x1781d8
cmpq 0xa18(%rcx), %rax
jl 0x57512
leaq 0x120ce6(%rip), %rax    # 0x1781d8
movq 0x3c8(%rax), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movq %rax, %rsi
leaq 0xe5e74(%rip), %rdi     # 0x13d381
callq 0xa1210
movl -0x3c(%rbp), %edx
movl $0x2, %edi
leaq 0xe627e(%rip), %rsi     # 0x13d79f
movb $0x0, %al
callq 0x2fc60
xorl %eax, %eax
movl %eax, %edi
callq 0x26ce0
movq %rax, %rcx
leaq 0x120c9d(%rip), %rax    # 0x1781d8
movq %rcx, 0x770(%rax)
movl -0x3c(%rbp), %ecx
leaq 0x120c8c(%rip), %rax    # 0x1781d8
movl %ecx, 0x730(%rax)
leaq 0x120c7f(%rip), %rax    # 0x1781d8
movl $0x2, 0x77c(%rax)
callq 0x305c0
movq -0x18(%rbp), %rdi
callq 0x3afa0
movq -0x10(%rbp), %rdi
callq 0x3afa0
movl -0x3c(%rbp), %edx
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $-0x1, %edx
cmovel %ecx, %eax
movl %eax, -0x4(%rbp)
jmp 0x57596
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw %cs:(%rax,%rax)