// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/replication.c
// Function: replicationFeedSlaves
void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc) {
    listNode *ln;
    listIter li;
    int j, len;
    char llstr[REDIS_LONGSTR_SIZE];

    /* If there aren't slaves, and there is no backlog buffer to populate,
     * we can return ASAP. */
    // backlog 为空，且没有从服务器，直接返回
    if (server.repl_backlog == NULL && listLength(slaves) == 0) return;

    /* We can't have slaves attached and no backlog. */
    redisAssert(!(listLength(slaves) != 0 && server.repl_backlog == NULL));

    /* Send SELECT command to every slave if needed. */
    // 如果有需要的话，发送 SELECT 命令，指定数据库
    if (server.slaveseldb != dictid) {
        robj *selectcmd;

        /* For a few DBs we have pre-computed SELECT command. */
        if (dictid >= 0 && dictid < REDIS_SHARED_SELECT_CMDS) {
            selectcmd = shared.select[dictid];
        } else {
            int dictid_len;

            dictid_len = ll2string(llstr,sizeof(llstr),dictid);
            selectcmd = createObject(REDIS_STRING,
                sdscatprintf(sdsempty(),
                "*2\r\n$6\r\nSELECT\r\n$%d\r\n%s\r\n",
                dictid_len, llstr));
        }

        /* Add the SELECT command into the backlog. */
        // 将 SELECT 命令添加到 backlog
        if (server.repl_backlog) feedReplicationBacklogWithObject(selectcmd);

        /* Send it to slaves. */
        // 发送给所有从服务器
        listRewind(slaves,&li);
        while((ln = listNext(&li))) {
            redisClient *slave = ln->value;
            addReply(slave,selectcmd);
        }

        if (dictid < 0 || dictid >= REDIS_SHARED_SELECT_CMDS)
            decrRefCount(selectcmd);
    }

    server.slaveseldb = dictid;

    /* Write the command to the replication backlog if any. */
    // 将命令写入到backlog
    if (server.repl_backlog) {
        char aux[REDIS_LONGSTR_SIZE+3];

        /* Add the multi bulk reply length. */
        aux[0] = '*';
        len = ll2string(aux+1,sizeof(aux)-1,argc);
        aux[len+1] = '\r';
        aux[len+2] = '\n';
        feedReplicationBacklog(aux,len+3);

        for (j = 0; j < argc; j++) {
            long objlen = stringObjectLen(argv[j]);

            /* We need to feed the buffer with the object as a bulk reply
             * not just as a plain string, so create the $..CRLF payload len 
             * ad add the final CRLF */
            // 将参数从对象转换成协议格式
            aux[0] = '$';
            len = ll2string(aux+1,sizeof(aux)-1,objlen);
            aux[len+1] = '\r';
            aux[len+2] = '\n';
            feedReplicationBacklog(aux,len+3);
            feedReplicationBacklogWithObject(argv[j]);
            feedReplicationBacklog(aux+len+1,2);
        }
    }

    /* Write the command to every slave. */
    listRewind(slaves,&li);
    while((ln = listNext(&li))) {

        // 指向从服务器
        redisClient *slave = ln->value;

        /* Don't feed slaves that are still waiting for BGSAVE to start */
        // 不要给正在等待 BGSAVE 开始的从服务器发送命令
        if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) continue;

        /* Feed slaves that are waiting for the initial SYNC (so these commands
         * are queued in the output buffer until the initial SYNC completes),
         * or are already in sync with the master. */
        // 向已经接收完和正在接收 RDB 文件的从服务器发送命令
        // 如果从服务器正在接收主服务器发送的 RDB 文件，
        // 那么在初次 SYNC 完成之前，主服务器发送的内容会被放进一个缓冲区里面

        /* Add the multi bulk length. */
        addReplyMultiBulkLen(slave,argc);

        /* Finally any additional argument that was not stored inside the
         * static buffer if any (from j to argc). */
        for (j = 0; j < argc; j++)
            addReplyBulk(slave,argv[j]);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %ecx, %r13d
movq %rdx, %rbx
movl %esi, %r15d
movq %rdi, %r14
leaq 0x96c8e(%rip), %rbp     # 0xc9fa8
movq 0x600(%rbp), %rax
testq %rax, %rax
jne 0x33331
cmpq $0x0, 0x28(%r14)
je 0x33534
testq %rax, %rax
jne 0x33341
cmpq $0x0, 0x28(%r14)
jne 0x33543
cmpl %r15d, 0x5ec(%rbp)
je 0x3340c
cmpl $0x9, %r15d
movl %r13d, (%rsp)
ja 0x3336c
movl %r15d, %eax
leaq 0x9749e(%rip), %rcx     # 0xca800
movq 0x160(%rcx,%rax,8), %r12
jmp 0x333ad
movslq %r15d, %rdx
leaq 0x40(%rsp), %r12
movl $0x15, %esi
movq %r12, %rdi
callq 0x2fe12
movl %eax, %r13d
callq 0x281b2
leaq 0x618bd(%rip), %rsi     # 0x94c4d
movq %rax, %rdi
movl %r13d, %edx
movq %r12, %rcx
xorl %eax, %eax
callq 0x28760
xorl %edi, %edi
movq %rax, %rsi
callq 0x302c4
movq %rax, %r12
cmpq $0x0, 0x600(%rbp)
je 0x333bf
movq %r12, %rdi
callq 0x332bb
leaq 0x10(%rsp), %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0x205d2
movq %r13, %rdi
callq 0x205ef
testq %rax, %rax
je 0x333fa
leaq 0x10(%rsp), %r13
movq 0x10(%rax), %rdi
movq %r12, %rsi
callq 0x2d6f0
movq %r13, %rdi
callq 0x205ef
testq %rax, %rax
jne 0x333e1
cmpl $0xa, %r15d
movl (%rsp), %r13d
jb 0x3340c
movq %r12, %rdi
callq 0x30834
movl %r15d, 0x5ec(%rbp)
cmpq $0x0, 0x600(%rbp)
movslq %r13d, %rbp
movl %ebp, %r15d
je 0x334d0
movq %r14, (%rsp)
leaq 0x21(%rsp), %r12
movb $0x2a, -0x1(%r12)
movl $0x17, %esi
movq %r12, %rdi
movq %rbp, %rdx
callq 0x2fe12
cltq
leaq 0x20(%rsp), %r13
movw $0xa0d, 0x1(%rax,%r13)  # imm = 0xA0D
addl $0x3, %eax
movslq %eax, %rsi
movq %r13, %rdi
callq 0x331fa
movq %rbp, 0x8(%rsp)
testl %ebp, %ebp
jle 0x334c7
xorl %r14d, %r14d
movq (%rbx,%r14,8), %rdi
callq 0x30c6f
movb $0x24, 0x20(%rsp)
movl $0x17, %esi
movq %r12, %rdi
movq %rax, %rdx
callq 0x2fe12
movslq %eax, %rbp
movw $0xa0d, 0x1(%rbp,%r13)  # imm = 0xA0D
leal 0x3(%rbp), %eax
movslq %eax, %rsi
movq %r13, %rdi
callq 0x331fa
movq (%rbx,%r14,8), %rdi
callq 0x332bb
addq %r12, %rbp
movl $0x2, %esi
movq %rbp, %rdi
callq 0x331fa
incq %r14
cmpq %r14, %r15
jne 0x3346f
movq (%rsp), %r14
movq 0x8(%rsp), %rbp
leaq 0x10(%rsp), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x205d2
movq %r12, %rdi
callq 0x205ef
testq %rax, %rax
je 0x33534
leaq 0x10(%rsp), %r14
movq 0x10(%rax), %r12
cmpl $0x6, 0x98(%r12)
je 0x33527
movq %r12, %rdi
movq %rbp, %rsi
callq 0x2ded8
testl %ebp, %ebp
jle 0x33527
xorl %r13d, %r13d
movq (%rbx,%r13,8), %rsi
movq %r12, %rdi
callq 0x2df74
incq %r13
cmpq %r13, %r15
jne 0x33513
movq %r14, %rdi
callq 0x205ef
testq %rax, %rax
jne 0x334f2
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x641ff(%rip), %rdi     # 0x97749
leaq 0x641cb(%rip), %rsi     # 0x9771c
movl $0xd5, %edx
callq 0x4d037
movl $0x1, %edi
callq 0x1f3b0