// File: /workspace/github_c_makefile_star2_O2/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 0x94bc0(%rip), %r13     # 0xc6fa8
movq 0x1c0(%r13), %rdi
leaq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0x1ffac
movb $0x1, %al
movl %eax, 0x4(%rsp)
pushq $0x3
popq %rbp
leaq 0x5b907(%rip), %r15     # 0x8dd13
movq %r14, %rdi
callq 0x1ffc6
testq %rax, %rax
je 0x3255d
movq 0x10(%rax), %r12
movl 0x98(%r12), %eax
cmpl $0x7, %eax
je 0x32449
cmpl $0x6, %eax
jne 0x3240c
movl $0x7, 0x98(%r12)
movl $0x0, 0x4(%rsp)
jmp 0x3240c
testl %ebx, %ebx
je 0x32463
movq %r12, %rdi
callq 0x2cb56
movl %ebp, %edi
movq %r15, %rsi
xorl %eax, %eax
callq 0x22613
jmp 0x3240c
movq 0x588(%r13), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x1f640
movl %eax, 0x9c(%r12)
cmpl $-0x1, %eax
je 0x32531
movl %eax, %edi
leaq 0x18(%rsp), %rsi
callq 0x1f690
cmpl $-0x1, %eax
je 0x32531
andq $0x0, 0xa0(%r12)
movq 0x48(%rsp), %rax
movq %rax, 0xa8(%r12)
movl $0x8, 0x98(%r12)
callq 0x271bf
movq 0xa8(%r12), %rdx
movq %rax, %rdi
leaq 0x5b89e(%rip), %rsi     # 0x8dd70
xorl %eax, %eax
callq 0x2763d
movq %rax, 0xb0(%r12)
movq 0x28(%r13), %rdi
movl (%r12), %esi
pushq $0x2
popq %r15
movl %r15d, %edx
callq 0x203cd
movq %r13, %rbp
movq 0x28(%r13), %rdi
movl (%r12), %esi
movl %r15d, %edx
leaq -0x316(%rip), %rcx      # 0x321f4
movq %r12, %r8
callq 0x2030c
cmpl $-0x1, %eax
jne 0x3251f
movq %r12, %rdi
callq 0x2cb56
leaq 0x5b7ed(%rip), %r15     # 0x8dd13
movq %rbp, %r13
pushq $0x3
popq %rbp
jmp 0x3240c
movq %r12, %rdi
callq 0x2cb56
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
movl %ebp, %edi
leaq 0x5b7f1(%rip), %rsi     # 0x8dd3f
movq %rax, %rdx
xorl %eax, %eax
callq 0x22613
jmp 0x3240c
testb $0x1, 0x4(%rsp)
jne 0x325bf
callq 0x32011
movq 0x588(%r13), %rdi
callq 0x350c8
testl %eax, %eax
je 0x325bf
movq 0x1c0(%r13), %rdi
leaq 0x18(%rsp), %rbx
movq %rbx, %rsi
callq 0x1ffac
leaq 0x5b7e4(%rip), %rsi     # 0x8dd78
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
movq %rbx, %rdi
callq 0x1ffc6
testq %rax, %rax
je 0x325bf
movq 0x10(%rax), %rdi
cmpl $0x6, 0x98(%rdi)
jne 0x3259e
callq 0x2cb56
jmp 0x3259e
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq