// File: /workspace/github_c_makefile_star2_O3/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 0x967fe(%rip), %r15     # 0xc9fa8
cmpq $0x0, 0x600(%r15)
je 0x337ed
movq %rdx, %rbx
movl %esi, %ebp
movq %rdi, (%rsp)
movslq %ecx, %rax
movq %rax, 0x8(%rsp)
movl %eax, %r14d
cmpl %esi, 0x5ec(%r15)
je 0x338cb
cmpl $0x9, %ebp
ja 0x3381a
movl %ebp, %eax
leaq 0x9701d(%rip), %rcx     # 0xca800
movq 0x160(%rcx,%rax,8), %r12
jmp 0x33865
cmpq $0x0, 0x28(%rdi)
je 0x339d2
leaq 0x63fba(%rip), %rdi     # 0x977b9
leaq 0x63f86(%rip), %rsi     # 0x9778c
movl $0xd5, %edx
callq 0x4ca32
movl $0x1, %edi
callq 0x1f3b0
movslq %ebp, %rdx
leaq 0x40(%rsp), %r12
movl $0x15, %esi
movq %r12, %rdi
callq 0x300c7
movl %eax, %r13d
callq 0x2845e
leaq 0x6145f(%rip), %rsi     # 0x94c9d
movq %rax, %rdi
movl %r13d, %edx
movq %r12, %rcx
xorl %eax, %eax
callq 0x28a5e
xorl %edi, %edi
movq %rax, %rsi
callq 0x3059c
movq %rax, %r12
cmpq $0x0, 0x600(%r15)
je 0x3386d
movq %r12, %rdi
callq 0x33757
leaq 0x10(%rsp), %r13
movq (%rsp), %rdi
movq %r13, %rsi
callq 0x20637
movq %r13, %rdi
callq 0x20654
testq %rax, %rax
je 0x338a9
leaq 0x10(%rsp), %r13
movq 0x10(%rax), %rdi
movq %r12, %rsi
callq 0x2d97c
movq %r13, %rdi
callq 0x20654
testq %rax, %rax
jne 0x33890
cmpl $0xa, %ebp
jb 0x338b6
movq %r12, %rdi
callq 0x30b0c
movl %ebp, 0x5ec(%r15)
cmpq $0x0, 0x600(%r15)
je 0x33968
leaq 0x21(%rsp), %r12
movb $0x2a, -0x1(%r12)
movl $0x17, %esi
movq %r12, %rdi
movq 0x8(%rsp), %r15
movq %r15, %rdx
callq 0x300c7
cltq
leaq 0x20(%rsp), %r13
movw $0xa0d, 0x1(%rax,%r13)  # imm = 0xA0D
addl $0x3, %eax
movslq %eax, %rsi
movq %r13, %rdi
callq 0x33682
testl %r15d, %r15d
jle 0x33968
xorl %r15d, %r15d
movq (%rbx,%r15,8), %rdi
callq 0x30f42
movb $0x24, 0x20(%rsp)
movl $0x17, %esi
movq %r12, %rdi
movq %rax, %rdx
callq 0x300c7
movslq %eax, %rbp
movw $0xa0d, 0x1(%rbp,%r13)  # imm = 0xA0D
leal 0x3(%rbp), %eax
movslq %eax, %rsi
movq %r13, %rdi
callq 0x33682
movq (%rbx,%r15,8), %rdi
callq 0x33757
addq %r12, %rbp
movl $0x2, %esi
movq %rbp, %rdi
callq 0x33682
incq %r15
cmpq %r15, %r14
jne 0x33910
leaq 0x10(%rsp), %r12
movq (%rsp), %rdi
movq %r12, %rsi
callq 0x20637
movq %r12, %rdi
callq 0x20654
testq %rax, %rax
movq 0x8(%rsp), %rbp
je 0x339d2
leaq 0x10(%rsp), %r15
movq 0x10(%rax), %r12
cmpl $0x6, 0x98(%r12)
je 0x339c5
movq %r12, %rdi
movq %rbp, %rsi
callq 0x2e168
testl %ebp, %ebp
jle 0x339c5
xorl %r13d, %r13d
movq (%rbx,%r13,8), %rsi
movq %r12, %rdi
callq 0x2e204
incq %r13
cmpq %r13, %r14
jne 0x339b1
movq %r15, %rdi
callq 0x20654
testq %rax, %rax
jne 0x33990
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq