// File: /workspace/github_c_makefile_star2_O2/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
leaq 0x9595a(%rip), %r13     # 0xc6fa8
cmpq $0x0, 0x600(%r13)
je 0x31689
movl %ecx, %r14d
movq %rdx, %rbx
movl %esi, %ebp
movq %rdi, (%rsp)
cmpl %esi, 0x5ec(%r13)
je 0x31742
cmpl $0x9, %ebp
ja 0x316a3
movl %ebp, %eax
leaq 0x96181(%rip), %rcx     # 0xc7800
movq 0x160(%rcx,%rax,8), %r15
jmp 0x316ec
cmpq $0x0, 0x28(%rdi)
jne 0x3184b
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movslq %ebp, %rdx
leaq 0x40(%rsp), %r15
pushq $0x15
popq %rsi
movq %r15, %rdi
callq 0x2e511
movl %eax, %r12d
callq 0x271bf
leaq 0x59598(%rip), %rsi     # 0x8ac5d
movq %rax, %rdi
movl %r12d, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2763d
xorl %edi, %edi
movq %rax, %rsi
callq 0x2e9b4
movq %rax, %r15
cmpq $0x0, 0x600(%r13)
je 0x316f4
movq %r15, %rdi
callq 0x315fd
leaq 0x30(%rsp), %r12
movq (%rsp), %rdi
movq %r12, %rsi
callq 0x1ffac
movq %r12, %rdi
callq 0x1ffc6
testq %rax, %rax
je 0x31720
movq 0x10(%rax), %rdi
movq %r15, %rsi
callq 0x2c10d
jmp 0x31705
cmpl $0xa, %ebp
jb 0x3172d
movq %r15, %rdi
callq 0x2ee1f
movl %ebp, 0x5ec(%r13)
cmpq $0x0, 0x600(%r13)
je 0x317df
leaq 0x11(%rsp), %r13
movb $0x2a, -0x1(%r13)
movq %r14, %r12
movslq %r12d, %rdx
pushq $0x17
popq %rsi
movq %r13, %rdi
movq %rdx, 0x8(%rsp)
callq 0x2e511
cltq
leaq 0x10(%rsp), %r15
movw $0xa0d, 0x1(%rax,%r15)  # imm = 0xA0D
addl $0x3, %eax
movslq %eax, %rsi
movq %r15, %rdi
callq 0x31533
xorl %r14d, %r14d
testl %r12d, %r12d
cmovlel %r14d, %r12d
cmpq %r14, %r12
je 0x317f2
movq (%rbx,%r14,8), %rdi
callq 0x2f227
movb $0x24, 0x10(%rsp)
movq %r13, %rdi
pushq $0x17
popq %rsi
movq %rax, %rdx
callq 0x2e511
movslq %eax, %rbp
movw $0xa0d, 0x1(%rbp,%r15)  # imm = 0xA0D
leal 0x3(%rbp), %eax
movslq %eax, %rsi
movq %r15, %rdi
callq 0x31533
movq (%rbx,%r14,8), %rdi
callq 0x315fd
addq %r13, %rbp
movq %rbp, %rdi
pushq $0x2
popq %rsi
callq 0x31533
incq %r14
jmp 0x31789
movslq %r14d, %rcx
xorl %eax, %eax
movq %rcx, 0x8(%rsp)
testl %ecx, %ecx
cmovgl %r14d, %eax
movl %eax, %r12d
leaq 0x30(%rsp), %r15
movq (%rsp), %rdi
movq %r15, %rsi
callq 0x1ffac
movl %r12d, %r12d
movq %r15, %rdi
callq 0x1ffc6
testq %rax, %rax
je 0x31694
movq 0x10(%rax), %r14
cmpl $0x6, 0x98(%r14)
je 0x31806
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0x2c7c7
xorl %r13d, %r13d
cmpq %r13, %r12
je 0x31806
movq (%rbx,%r13,8), %rsi
movq %r14, %rdi
callq 0x2c84a
incq %r13
jmp 0x31835
leaq 0x5bf2b(%rip), %rdi     # 0x8d77d
leaq 0x5bef7(%rip), %rsi     # 0x8d750
movl $0xd5, %edx
callq 0x4906b
pushq $0x1
popq %rdi
callq 0x1f3b0