// File: /workspace/github_c_makefile_star2_O0/nilyang[P]redis-tdd-annotation/src/sentinel.c
// Function: sentinelGetLeader
char *sentinelGetLeader(sentinelRedisInstance *master, uint64_t epoch) {
    dict *counters;
    dictIterator *di;
    dictEntry *de;
    unsigned int voters = 0, voters_quorum;
    char *myvote;
    char *winner = NULL;
    uint64_t leader_epoch;
    uint64_t max_votes = 0;

    redisAssert(master->flags & (SRI_O_DOWN|SRI_FAILOVER_IN_PROGRESS));

    // 统计器
    counters = dictCreate(&leaderVotesDictType,NULL);

    /* Count other sentinels votes */
    // 统计其他 sentinel 的主观 leader 投票
    di = dictGetIterator(master->sentinels);
    while((de = dictNext(di)) != NULL) {
        sentinelRedisInstance *ri = dictGetVal(de);

        // 为目标 Sentinel 选出的领头 Sentinel 增加一票
        if (ri->leader != NULL && ri->leader_epoch == sentinel.current_epoch)
            sentinelLeaderIncr(counters,ri->leader);

        // 统计投票数量
        voters++;
    }
    dictReleaseIterator(di);

    /* Check what's the winner. For the winner to win, it needs two conditions:
     *
     * 选出领头 leader ，它必须满足以下两个条件：
     *
     * 1) Absolute majority between voters (50% + 1).
     *    有多于一般的 Sentinel 支持
     * 2) And anyway at least master->quorum votes. 
     *    投票数至少要有 master->quorum 那么多
     */
    di = dictGetIterator(counters);
    while((de = dictNext(di)) != NULL) {

        // 取出票数
        uint64_t votes = dictGetUnsignedIntegerVal(de);

        // 选出票数最大的人
        if (votes > max_votes) {
            max_votes = votes;
            winner = dictGetKey(de);
        }
    }
    dictReleaseIterator(di);

    /* Count this Sentinel vote:
     * if this Sentinel did not voted yet, either vote for the most
     * common voted sentinel, or for itself if no vote exists at all. */
    // 本 Sentinel 进行投票
    // 如果 Sentinel 之前还没有进行投票，那么有两种选择：
    // 1）如果选出了 winner （最多票数支持的 Sentinel ），那么这个 Sentinel 也投 winner 一票
    // 2）如果没有选出 winner ，那么 Sentinel 投自己一票
    if (winner)
        myvote = sentinelVoteLeader(master,epoch,winner,&leader_epoch);
    else
        myvote = sentinelVoteLeader(master,epoch,server.runid,&leader_epoch);

    // 领头 Sentinel 已选出，并且领头的纪元和给定的纪元一样
    if (myvote && leader_epoch == epoch) {

        // 为领头 Sentinel 增加一票（这一票来自本 Sentinel ）
        uint64_t votes = sentinelLeaderIncr(counters,myvote);

        // 如果投票之后的票数比最大票数要大，那么更换领头 Sentinel
        if (votes > max_votes) {
            max_votes = votes;
            winner = myvote;
        }
    }
    voters++; /* Anyway, count me as one of the voters. */

    // 如果支持领头的投票数量不超过半数
    // 并且支持票数不超过 master 配置指定的投票数量
    // 那么这次领头选举无效
    voters_quorum = voters/2+1;
    if (winner && (max_votes < voters_quorum || max_votes < master->quorum))
        winner = NULL;

    // 返回领头 Sentinel ，或者 NULL
    winner = winner ? sdsnew(winner) : NULL;
    sdsfree(myvote);
    dictRelease(counters);
    return winner;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x2c(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
andl $0xa0, %eax
cmpl $0x0, %eax
je 0x7ff49
jmp 0x7ff6b
leaq 0x69dc7(%rip), %rdi     # 0xe9d17
leaq 0x68e91(%rip), %rsi     # 0xe8de8
movl $0xfdf, %edx            # imm = 0xFDF
callq 0x63ca0
movl $0x1, %edi
callq 0x1f3b0
leaq 0x92d0e(%rip), %rdi     # 0x112c80
xorl %eax, %eax
movl %eax, %esi
callq 0x231a0
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0xc0(%rax), %rdi
callq 0x241d0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x24270
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x7fff3
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq $0x0, 0x118(%rax)
je 0x7ffe8
movq -0x58(%rbp), %rax
movq 0x120(%rax), %rax
cmpq 0xa8266(%rip), %rax     # 0x128238
jne 0x7ffe8
movq -0x18(%rbp), %rdi
movq -0x58(%rbp), %rax
movq 0x118(%rax), %rsi
callq 0x7fe60
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x7ff93
movq -0x20(%rbp), %rdi
callq 0x243d0
movq -0x18(%rbp), %rdi
callq 0x241d0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x24270
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x80047
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x50(%rbp), %rax
jbe 0x80045
movq -0x60(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x80009
movq -0x20(%rbp), %rdi
callq 0x243d0
cmpq $0x0, -0x40(%rbp)
je 0x80072
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x40(%rbp), %rdx
leaq -0x48(%rbp), %rcx
callq 0x7ef30
movq %rax, -0x38(%rbp)
jmp 0x80092
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq 0x93877(%rip), %rdx     # 0x1138f8
addq $0x58, %rdx
leaq -0x48(%rbp), %rcx
callq 0x7ef30
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x800d2
movq -0x48(%rbp), %rax
cmpq -0x10(%rbp), %rax
jne 0x800d2
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x7fe60
cltq
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x50(%rbp), %rax
jbe 0x800d0
movq -0x68(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x800d2
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
shrl %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x80115
movq -0x50(%rbp), %rax
movl -0x30(%rbp), %ecx
cmpq %rcx, %rax
jb 0x8010d
movq -0x50(%rbp), %rax
movq -0x8(%rbp), %rcx
movslq 0xd0(%rcx), %rcx
cmpq %rcx, %rax
jae 0x80115
movq $0x0, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x8012b
movq -0x40(%rbp), %rdi
callq 0x2c0e0
movq %rax, -0x70(%rbp)
jmp 0x80133
xorl %eax, %eax
movq %rax, -0x70(%rbp)
jmp 0x80133
movq -0x70(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
callq 0x2c160
movq -0x18(%rbp), %rdi
callq 0x24000
movq -0x40(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)