// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/sentinel.c
// Function: sentinelEvent
void sentinelEvent(int level, char *type, sentinelRedisInstance *ri,
                   const char *fmt, ...) {
    va_list ap;
    // 日志字符串
    char msg[REDIS_MAX_LOGMSG_LEN];
    robj *channel, *payload;

    /* Handle %@ */
    // 处理 %@
    if (fmt[0] == '%' && fmt[1] == '@') {

        // 如果 ri 实例是主服务器，那么 master 就是 NULL 
        // 否则 ri 就是一个从服务器或者 sentinel ，而 master 就是该实例的主服务器
        //
        // sentinelRedisInstance *master = NULL;
        // if (~(ri->flags & SRI_MASTER))
        //     master = ri->master;
        sentinelRedisInstance *master = (ri->flags & SRI_MASTER) ?
                                         NULL : ri->master;

        if (master) {
            
            // ri 不是主服务器

            snprintf(msg, sizeof(msg), "%s %s %s %d @ %s %s %d",
                // 打印 ri 的类型
                sentinelRedisInstanceTypeStr(ri),
                // 打印 ri 的名字、IP 和端口号
                ri->name, ri->addr->ip, ri->addr->port,
                // 打印 ri 的主服务器的名字、 IP 和端口号
                master->name, master->addr->ip, master->addr->port);
        } else {

            // ri 是主服务器

            snprintf(msg, sizeof(msg), "%s %s %s %d",
                // 打印 ri 的类型
                sentinelRedisInstanceTypeStr(ri),
                // 打印 ri 的名字、IP 和端口号
                ri->name, ri->addr->ip, ri->addr->port);
        }

        // 跳过已处理的 "%@" 字符
        fmt += 2;

    } else {
        msg[0] = '\0';
    }

    /* Use vsprintf for the rest of the formatting if any. */
    // 打印之后的内容，格式和平常的 printf 一样
    if (fmt[0] != '\0') {
        va_start(ap, fmt);
        vsnprintf(msg+strlen(msg), sizeof(msg)-strlen(msg), fmt, ap);
        va_end(ap);
    }

    /* Log the message if the log level allows it to be logged. */
    // 如果日志的级别足够高的话，那么记录到日志中
    if (level >= server.verbosity)
        redisLog(level,"%s %s",type,msg);

    /* Publish the message via Pub/Sub if it's not a debugging one. */
    // 如果日志不是 DEBUG 日志，那么将它发送到频道中
    if (level != REDIS_DEBUG) {
        // 频道
        channel = createStringObject(type,strlen(type));
        // 内容
        payload = createStringObject(msg,strlen(msg));
        // 发送信息
        pubsubPublishMessage(channel,payload);
        decrRefCount(channel);
        decrRefCount(payload);
    }

    /* Call the notification script if applicable. */
    // 如果有需要的话，调用提醒脚本
    if (level == REDIS_WARNING && ri != NULL) {
        sentinelRedisInstance *master = (ri->flags & SRI_MASTER) ?
                                         ri : ri->master;
        if (master->notification_script) {
            sentinelScheduleScriptExecution(master->notification_script,
                type,msg,NULL);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4d8, %rsp            # imm = 0x4D8
movq %rcx, %r15
movq %rdx, %r14
movq %rsi, %rbx
movl %edi, %ebp
movq %r8, 0x40(%rsp)
movq %r9, 0x48(%rsp)
testb %al, %al
je 0x567f0
movaps %xmm0, 0x50(%rsp)
movaps %xmm1, 0x60(%rsp)
movaps %xmm2, 0x70(%rsp)
movaps %xmm3, 0x80(%rsp)
movaps %xmm4, 0x90(%rsp)
movaps %xmm5, 0xa0(%rsp)
movaps %xmm6, 0xb0(%rsp)
movaps %xmm7, 0xc0(%rsp)
movb (%rcx), %al
cmpb $0x25, %al
jne 0x5685f
cmpb $0x40, 0x1(%r15)
jne 0x5685f
testb $0x1, (%r14)
jne 0x56869
movq 0xf8(%r14), %r12
testq %r12, %r12
je 0x56869
movq %r14, %rdi
callq 0x569d5
movq 0x8(%r14), %r8
movq 0x20(%r14), %rcx
movq (%rcx), %r9
movl 0x8(%rcx), %r10d
movq 0x20(%r12), %r11
movl 0x8(%r11), %r13d
leaq 0x3e5ed(%rip), %rdx     # 0x94e23
leaq 0xd0(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rcx
xorl %eax, %eax
pushq %r13
pushq (%r11)
pushq 0x8(%r12)
pushq %r10
callq 0x1f120
addq $0x20, %rsp
jmp 0x568a6
movb $0x0, 0xd0(%rsp)
jmp 0x568ae
movq %r14, %rdi
callq 0x569d5
movq 0x8(%r14), %r8
movq 0x20(%r14), %rcx
movq (%rcx), %r9
movl 0x8(%rcx), %r10d
subq $0x8, %rsp
leaq 0x3e5af(%rip), %rdx     # 0x94e3a
leaq 0xd8(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rcx
xorl %eax, %eax
pushq %r10
callq 0x1f120
popq %rcx
popq %rdx
movb 0x2(%r15), %al
addq $0x2, %r15
testb %al, %al
je 0x56905
leaq 0x20(%rsp), %rax
movq %rsp, %r12
movq %rax, 0x10(%r12)
leaq 0x510(%rsp), %rax
movq %rax, 0x8(%r12)
movabsq $0x3000000020, %rax     # imm = 0x3000000020
movq %rax, (%r12)
leaq 0xd0(%rsp), %rdi
callq 0x1f4f0
leaq (%rsp,%rax), %rdi
addq $0xd0, %rdi
movl $0x400, %esi            # imm = 0x400
subq %rax, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x1f7b0
leaq 0x7069c(%rip), %rax     # 0xc6fa8
cmpl %ebp, 0x464(%rax)
jg 0x5692f
leaq 0x3f2c1(%rip), %rsi     # 0x95bdc
leaq 0xd0(%rsp), %rcx
movl %ebp, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22613
testl %ebp, %ebp
je 0x569c3
movq %rbx, %rdi
callq 0x1f4f0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2ea81
movq %rax, %r15
leaq 0xd0(%rsp), %r12
movq %r12, %rdi
callq 0x1f4f0
movq %r12, %rdi
movq %rax, %rsi
callq 0x2ea81
movq %rax, %r12
movq %r15, %rdi
movq %rax, %rsi
callq 0x47f05
movq %r15, %rdi
callq 0x2ee1f
movq %r12, %rdi
callq 0x2ee1f
cmpl $0x3, %ebp
setne %al
testq %r14, %r14
sete %cl
orb %al, %cl
jne 0x569c3
testb $0x1, (%r14)
jne 0x569a3
movq 0xf8(%r14), %r14
movq 0x160(%r14), %rdi
testq %rdi, %rdi
je 0x569c3
leaq 0xd0(%rsp), %rdx
movq %rbx, %rsi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x56a04
addq $0x4d8, %rsp            # imm = 0x4D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq