// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/replication.c
// Function: updateSlavesWaitingBgsave
void updateSlavesWaitingBgsave(int bgsaveerr) {
    listNode *ln;
    int startbgsave = 0;
    listIter li;

    // 遍历所有 slave
    listRewind(server.slaves,&li);
    while((ln = listNext(&li))) {
        redisClient *slave = ln->value;

        if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) {
            // 之前的 RDB 文件不能被 slave 使用，
            // 开始新的 BGSAVE
            startbgsave = 1;
            slave->replstate = REDIS_REPL_WAIT_BGSAVE_END;
        } else if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) {

            // 执行到这里，说明有 slave 在等待 BGSAVE 完成

            struct redis_stat buf;

            // 但是 BGSAVE 执行错误
            if (bgsaveerr != REDIS_OK) {
                // 释放 slave
                freeClient(slave);
                redisLog(REDIS_WARNING,"SYNC failed. BGSAVE child returned an error");
                continue;
            }

            // 打开 RDB 文件
            if ((slave->repldbfd = open(server.rdb_filename,O_RDONLY)) == -1 ||
                redis_fstat(slave->repldbfd,&buf) == -1) {
                freeClient(slave);
                redisLog(REDIS_WARNING,"SYNC failed. Can't open/stat DB after BGSAVE: %s", strerror(errno));
                continue;
            }

            // 设置偏移量，各种值
            slave->repldboff = 0;
            slave->repldbsize = buf.st_size;
            // 更新状态
            slave->replstate = REDIS_REPL_SEND_BULK;

            slave->replpreamble = sdscatprintf(sdsempty(),"$%lld\r\n",
                (unsigned long long) slave->repldbsize);

            // 清空之前的写事件处理器
            aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE);
            // 将 sendBulkToSlave 安装为 slave 的写事件处理器
            // 它用于将 RDB 文件发送给 slave
            if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE, sendBulkToSlave, slave) == AE_ERR) {
                freeClient(slave);
                continue;
            }
        }
    }

    // 需要执行新的 BGSAVE
    if (startbgsave) {
        /* Since we are starting a new background save for one or more slaves,
         * we flush the Replication Script Cache to use EVAL to propagate every
         * new EVALSHA for the first time, since all the new slaves don't know
         * about previous scripts. */
        // 开始行的 BGSAVE ，并清空脚本缓存
        replicationScriptCacheFlush();
        if (rdbSaveBackground(server.rdb_filename) != REDIS_OK) {
            listIter li;

            listRewind(server.slaves,&li);
            redisLog(REDIS_WARNING,"SYNC failed. BGSAVE failed");
            while((ln = listNext(&li))) {
                redisClient *slave = ln->value;

                if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START)
                    freeClient(slave);
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %edi, %ebx
leaq 0x95e2e(%rip), %r15     # 0xc9fa8
movq 0x1c0(%r15), %rdi
leaq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0x205d2
movq %r14, %rdi
callq 0x205ef
testq %rax, %rax
je 0x34362
leaq 0x63b39(%rip), %rbp     # 0x97cdf
leaq 0x8(%rsp), %r12
xorl %r14d, %r14d
movq 0x10(%rax), %r13
movl 0x98(%r13), %eax
cmpl $0x7, %eax
je 0x341dd
cmpl $0x6, %eax
jne 0x342df
movl $0x7, 0x98(%r13)
movl $0x1, %r14d
jmp 0x342df
testl %ebx, %ebx
je 0x341fd
movq %r13, %rdi
callq 0x2e287
movl $0x3, %edi
movq %rbp, %rsi
xorl %eax, %eax
callq 0x23064
jmp 0x342df
movq 0x588(%r15), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x1f640
movl %eax, 0x9c(%r13)
cmpl $-0x1, %eax
je 0x342b5
movl %eax, %edi
leaq 0x18(%rsp), %rsi
callq 0x1f690
cmpl $-0x1, %eax
je 0x342b5
movq $0x0, 0xa0(%r13)
movq 0x48(%rsp), %rax
movq %rax, 0xa8(%r13)
movl $0x8, 0x98(%r13)
callq 0x281b2
movq 0xa8(%r13), %rdx
movq %rax, %rdi
leaq 0x63ad2(%rip), %rsi     # 0x97d3c
xorl %eax, %eax
callq 0x28760
movq %rax, 0xb0(%r13)
movq 0x28(%r15), %rdi
movl (%r13), %esi
movl $0x2, %edx
callq 0x20a9d
movq 0x28(%r15), %rdi
movl (%r13), %esi
movl $0x2, %edx
leaq -0x32c(%rip), %rcx      # 0x33f72
movq %r13, %r8
callq 0x209d7
cmpl $-0x1, %eax
jne 0x342df
movq %r13, %rdi
callq 0x2e287
jmp 0x342df
movq %r13, %rdi
callq 0x2e287
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
movl $0x3, %edi
leaq 0x63a36(%rip), %rsi     # 0x97d0b
movq %rax, %rdx
xorl %eax, %eax
callq 0x23064
movq %r12, %rdi
callq 0x205ef
testq %rax, %rax
jne 0x341ae
testl %r14d, %r14d
je 0x34362
callq 0x33d87
movq 0x588(%r15), %rdi
callq 0x37660
testl %eax, %eax
je 0x34362
movq 0x1c0(%r15), %rdi
leaq 0x18(%rsp), %rbx
movq %rbx, %rsi
callq 0x205d2
leaq 0x63a1f(%rip), %rsi     # 0x97d44
movl $0x3, %edi
xorl %eax, %eax
callq 0x23064
movq %rbx, %rdi
callq 0x205ef
testq %rax, %rax
je 0x34362
leaq 0x18(%rsp), %rbx
movq 0x10(%rax), %rdi
cmpl $0x6, 0x98(%rdi)
jne 0x34355
callq 0x2e287
movq %rbx, %rdi
callq 0x205ef
testq %rax, %rax
jne 0x34343
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq