// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xe0, %rsp
movl %edi, -0x4(%rbp)
movl $0x0, -0x14(%rbp)
leaq 0xd329c(%rip), %rax     # 0x1138f8
movq 0x1c0(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x202b0
leaq -0x28(%rbp), %rdi
callq 0x20310
movq %rax, -0x10(%rbp)
cmpq $0x0, %rax
je 0x40817
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x6, 0x98(%rax)
jne 0x406b6
movl $0x1, -0x14(%rbp)
movq -0x30(%rbp), %rax
movl $0x7, 0x98(%rax)
jmp 0x40812
movq -0x30(%rbp), %rax
cmpl $0x7, 0x98(%rax)
jne 0x40810
cmpl $0x0, -0x4(%rbp)
je 0x406eb
movq -0x30(%rbp), %rdi
callq 0x36a80
movl $0x3, %edi
leaq 0xa1739(%rip), %rsi     # 0xe1e1b
movb $0x0, %al
callq 0x24fb0
jmp 0x4066c
leaq 0xd3206(%rip), %rax     # 0x1138f8
movq 0x588(%rax), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x1f640
movq -0x30(%rbp), %rcx
movl %eax, 0x9c(%rcx)
cmpl $-0x1, %eax
je 0x4072c
movq -0x30(%rbp), %rax
movl 0x9c(%rax), %edi
leaq -0xc0(%rbp), %rsi
callq 0x1f690
cmpl $-0x1, %eax
jne 0x4075c
movq -0x30(%rbp), %rdi
callq 0x36a80
callq 0x1f820
movl (%rax), %edi
callq 0x1f630
movq %rax, %rdx
movl $0x3, %edi
leaq 0xa16f7(%rip), %rsi     # 0xe1e47
movb $0x0, %al
callq 0x24fb0
jmp 0x4066c
movq -0x30(%rbp), %rax
movq $0x0, 0xa0(%rax)
movq -0x90(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0xa8(%rax)
movq -0x30(%rbp), %rax
movl $0x8, 0x98(%rax)
callq 0x2c0c0
movq %rax, %rdi
movq -0x30(%rbp), %rax
movq 0xa8(%rax), %rdx
leaq 0xa16d3(%rip), %rsi     # 0xe1e78
movb $0x0, %al
callq 0x2cad0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0xb0(%rax)
leaq 0xd3137(%rip), %rax     # 0x1138f8
movq 0x28(%rax), %rdi
movq -0x30(%rbp), %rax
movl (%rax), %esi
movl $0x2, %edx
callq 0x20c60
leaq 0xd311c(%rip), %rax     # 0x1138f8
movq 0x28(%rax), %rdi
movq -0x30(%rbp), %rax
movl (%rax), %esi
movq -0x30(%rbp), %r8
movl $0x2, %edx
leaq -0x4d6(%rip), %rcx      # 0x40320
callq 0x20a80
cmpl $-0x1, %eax
jne 0x4080e
movq -0x30(%rbp), %rdi
callq 0x36a80
jmp 0x4066c
jmp 0x40810
jmp 0x40812
jmp 0x4066c
cmpl $0x0, -0x14(%rbp)
je 0x408b2
callq 0x40020
leaq 0xd30cb(%rip), %rax     # 0x1138f8
movq 0x588(%rax), %rdi
callq 0x44ef0
cmpl $0x0, %eax
je 0x408b0
leaq 0xd30b3(%rip), %rax     # 0x1138f8
movq 0x1c0(%rax), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x202b0
movl $0x3, %edi
leaq 0xa161c(%rip), %rsi     # 0xe1e80
movb $0x0, %al
callq 0x24fb0
leaq -0xd0(%rbp), %rdi
callq 0x20310
movq %rax, -0x10(%rbp)
cmpq $0x0, %rax
je 0x408ae
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
cmpl $0x6, 0x98(%rax)
jne 0x408ac
movq -0xd8(%rbp), %rdi
callq 0x36a80
jmp 0x4086b
jmp 0x408b0
jmp 0x408b2
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax,%rax)