// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
leaq 0xd4758(%rip), %rax     # 0x1138f8
cmpq $0x0, 0x600(%rax)
jne 0x3f1ba
movq -0x8(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x3f1ba
jmp 0x3f4a6
movq -0x8(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x3f1d6
leaq 0xd472c(%rip), %rax     # 0x1138f8
cmpq $0x0, 0x600(%rax)
je 0x3f1d8
jmp 0x3f1fa
leaq 0xa26a6(%rip), %rdi     # 0xe1885
leaq 0xa2672(%rip), %rsi     # 0xe1858
movl $0xd5, %edx
callq 0x63ca0
movl $0x1, %edi
callq 0x1f3b0
leaq 0xd46f7(%rip), %rax     # 0x1138f8
movl 0x5ec(%rax), %eax
cmpl -0xc(%rbp), %eax
je 0x3f2e1
cmpl $0x0, -0xc(%rbp)
jl 0x3f235
cmpl $0xa, -0xc(%rbp)
jge 0x3f235
movslq -0xc(%rbp), %rcx
leaq 0xd4f29(%rip), %rax     # 0x114150
movq 0x160(%rax,%rcx,8), %rax
movq %rax, -0x68(%rbp)
jmp 0x3f275
leaq -0x60(%rbp), %rdi
movslq -0xc(%rbp), %rdx
movl $0x15, %esi
callq 0x39660
movl %eax, -0x6c(%rbp)
callq 0x2c0c0
movq %rax, %rdi
movl -0x6c(%rbp), %edx
leaq -0x60(%rbp), %rcx
leaq 0x9fa65(%rip), %rsi     # 0xdecc5
movb $0x0, %al
callq 0x2cad0
movq %rax, %rsi
xorl %edi, %edi
callq 0x3a0d0
movq %rax, -0x68(%rbp)
leaq 0xd467c(%rip), %rax     # 0x1138f8
cmpq $0x0, 0x600(%rax)
je 0x3f28f
movq -0x68(%rbp), %rdi
callq 0x3f100
movq -0x8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x202b0
leaq -0x38(%rbp), %rdi
callq 0x20310
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x3f2ca
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x35a70
jmp 0x3f29c
cmpl $0x0, -0xc(%rbp)
jl 0x3f2d6
cmpl $0xa, -0xc(%rbp)
jl 0x3f2df
movq -0x68(%rbp), %rdi
callq 0x3aaa0
jmp 0x3f2e1
movl -0xc(%rbp), %ecx
leaq 0xd460d(%rip), %rax     # 0x1138f8
movl %ecx, 0x5ec(%rax)
leaq 0xd4600(%rip), %rax     # 0x1138f8
cmpq $0x0, 0x600(%rax)
je 0x3f421
movb $0x2a, -0x90(%rbp)
leaq -0x90(%rbp), %rdi
addq $0x1, %rdi
movslq -0x1c(%rbp), %rdx
movl $0x17, %esi
callq 0x39660
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
addl $0x1, %eax
cltq
movb $0xd, -0x90(%rbp,%rax)
movl -0x40(%rbp), %eax
addl $0x2, %eax
cltq
movb $0xa, -0x90(%rbp,%rax)
leaq -0x90(%rbp), %rdi
movl -0x40(%rbp), %eax
addl $0x3, %eax
movslq %eax, %rsi
callq 0x3ef60
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x3f41f
movq -0x18(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x3b2c0
movq %rax, -0x98(%rbp)
movb $0x24, -0x90(%rbp)
leaq -0x90(%rbp), %rdi
addq $0x1, %rdi
movq -0x98(%rbp), %rdx
movl $0x17, %esi
callq 0x39660
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
addl $0x1, %eax
cltq
movb $0xd, -0x90(%rbp,%rax)
movl -0x40(%rbp), %eax
addl $0x2, %eax
cltq
movb $0xa, -0x90(%rbp,%rax)
leaq -0x90(%rbp), %rdi
movl -0x40(%rbp), %eax
addl $0x3, %eax
movslq %eax, %rsi
callq 0x3ef60
movq -0x18(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x3f100
leaq -0x90(%rbp), %rdi
movslq -0x40(%rbp), %rax
addq %rax, %rdi
addq $0x1, %rdi
movl $0x2, %esi
callq 0x3ef60
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x3f365
jmp 0x3f421
movq -0x8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x202b0
leaq -0x38(%rbp), %rdi
callq 0x20310
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x3f4a6
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
cmpl $0x6, 0x98(%rax)
jne 0x3f462
jmp 0x3f42e
movq -0xa0(%rbp), %rdi
movslq -0x1c(%rbp), %rsi
callq 0x364f0
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x3f4a4
movq -0xa0(%rbp), %rdi
movq -0x18(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x36620
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x3f479
jmp 0x3f42e
addq $0xa0, %rsp
popq %rbp
retq
nop