// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/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 */
        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);
        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 {
            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 0xb84c5(%rip), %rbp     # 0x106858
cmpl $-0x1, 0x68c(%rbp)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jne 0x4e762
cmpl $-0x1, 0x720(%rbp)
jne 0x4e762
leaq 0x8(%rsp), %rdi
callq 0x265f0
cmpl $-0x1, %eax
je 0x4e762
movq 0x8(%rsp), %rax
rolq $0x20, %rax
movq %rax, 0x778(%rbp)
movq 0x1e0(%rbp), %rax
movq 0x28(%rax), %rdi
shlq $0x2, %rdi
callq 0x3b29d
movq %rax, %r14
movq 0x1e0(%rbp), %rax
movq 0x28(%rax), %rdi
shlq $0x3, %rdi
callq 0x3b29d
movq %rax, %rbx
movq 0x1e0(%rbp), %rdi
leaq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x2e582
movq %r15, %rdi
callq 0x2e59f
xorl %r15d, %r15d
testq %rax, %rax
je 0x4e493
leaq 0x10(%rsp), %r12
movq 0x10(%rax), %r13
cmpl $0x6, 0xa0(%r13)
jne 0x4e486
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 0x47989
movq %r13, %rdi
movq %rax, %rsi
callq 0x479a4
movl 0x8(%r13), %esi
xorl %edi, %edi
callq 0x30783
movl 0x8(%r13), %esi
movslq 0x824(%rbp), %rax
imulq $0x3e8, %rax, %rdx      # imm = 0x3E8
xorl %edi, %edi
callq 0x309c8
movq %r12, %rdi
callq 0x2e59f
testq %rax, %rax
jne 0x4e42d
callq 0x330a5
movq %rax, %r12
callq 0x26ac0
testl %eax, %eax
je 0x4e58f
movl %eax, %r13d
callq 0x330a5
subq %r12, %rax
movq %rax, 0x3b8(%rbp)
callq 0x3b446
movq %rax, %xmm0
punpckldq 0x77414(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x7741c(%rip), %xmm0    # 0xc58f0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x77418(%rip), %xmm1    # 0xc5900
movq 0x3b8(%rbp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
divsd %xmm0, %xmm1
mulsd 0x70fed(%rip), %xmm1    # 0xbf4f0
movsd %xmm1, 0x3c0(%rbp)
movq 0xa08(%rbp), %rcx
testq %rcx, %rcx
je 0x4e546
movabsq $0x20c49ba5e353f7cf, %rdx # imm = 0x20C49BA5E353F7CF
imulq %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
cmpq %rcx, %rdx
jl 0x4e546
leaq 0x77630(%rip), %rdi     # 0xc5b6e
movq %rdx, %rsi
callq 0x825dd
cmpl $-0x1, %r13d
je 0x4e6b2
leaq 0x77a35(%rip), %rsi     # 0xc5f8c
movl $0x2, %edi
movl %r13d, %edx
xorl %eax, %eax
callq 0x32ea6
xorl %edi, %edi
callq 0x26ce0
movq %rax, 0x760(%rbp)
movl %r13d, 0x720(%rbp)
movl $0x2, 0x76c(%rbp)
callq 0x3351d
jmp 0x4e744
leaq 0x20(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
movl %r15d, %edx
callq 0x76193
movq %r14, %rdi
callq 0x3b3df
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x36c98
leaq 0x779bd(%rip), %rdi     # 0xc5f78
callq 0x38932
movq %r13, %rdi
xorl %esi, %esi
callq 0x4c96a
movl $0x1, %r13d
testl %eax, %eax
jne 0x4e693
leaq 0x20(%rsp), %rdi
callq *0x18(%rdi)
testl %eax, %eax
je 0x4e693
callq 0x3b67c
testq %rax, %rax
je 0x4e60c
shrq $0x14, %rax
leaq 0x77545(%rip), %rsi     # 0xc5b42
movl $0x2, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x32ea6
leal 0x1(,%r15,2), %eax
movslq %eax, %r13
leaq (,%r13,8), %rdi
callq 0x3b29d
movq %rax, %r14
movslq %r15d, %rax
movq %rax, (%r14)
testl %r15d, %r15d
jle 0x4e65b
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 0x4e63b
cmpq $0x0, (%r14)
je 0x4e685
movl 0x778(%rbp), %edi
shlq $0x23, %r13
sarq $0x20, %r13
movq %r14, %rsi
movq %r13, %rdx
callq 0x26bc0
movq %r13, %rcx
xorl %r13d, %r13d
cmpq %rcx, %rax
je 0x4e68b
movl $0x1, %r13d
movq %r14, %rdi
callq 0x3b3df
movq %rbx, %rdi
callq 0x3b3df
leaq 0x20(%rsp), %rdi
callq 0x7621d
movl %r13d, %edi
callq 0x33106
jmp 0x4e762
callq 0x26890
movl (%rax), %edi
callq 0x26690
leaq 0x774ae(%rip), %rsi     # 0xc5b73
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x32ea6
movq 0x1e0(%rbp), %rdi
leaq 0x10(%rsp), %r12
movq %r12, %rsi
callq 0x2e582
movq %r12, %rdi
callq 0x2e59f
testq %rax, %rax
je 0x4e732
movl %r15d, %ebp
leaq 0x10(%rsp), %r12
testl %r15d, %r15d
jle 0x4e725
movq 0x10(%rax), %rax
movq (%rax), %rcx
xorl %edx, %edx
cmpq (%rbx,%rdx,8), %rcx
je 0x4e71b
incq %rdx
cmpq %rdx, %rbp
jne 0x4e70b
jmp 0x4e725
movl $0x6, 0xa0(%rax)
movq %r12, %rdi
callq 0x2e59f
testq %rax, %rax
jne 0x4e6fd
movl 0x8(%rsp), %edi
callq 0x26180
movl 0xc(%rsp), %edi
callq 0x26180
xorl %r12d, %r12d
cmpl $-0x1, %r13d
sete %r12b
movq %rbx, %rdi
callq 0x3b3df
movq %r14, %rdi
callq 0x3b3df
negl %r12d
movl %r12d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq