// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/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;

    serverAssert(master->flags & (SRI_O_DOWN|SRI_FAILOVER_IN_PROGRESS));
    counters = dictCreate(&leaderVotesDictType,NULL);

    voters = dictSize(master->sentinels)+1; /* All the other sentinels and me. */

    /* Count other sentinels votes */
    di = dictGetIterator(master->sentinels);
    while((de = dictNext(di)) != NULL) {
        sentinelRedisInstance *ri = dictGetVal(de);
        if (ri->leader != NULL && ri->leader_epoch == sentinel.current_epoch)
            sentinelLeaderIncr(counters,ri->leader);
    }
    dictReleaseIterator(di);

    /* Check what's the winner. For the winner to win, it needs two conditions:
     * 1) Absolute majority between voters (50% + 1).
     * 2) And anyway at least master->quorum votes. */
    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. */
    if (winner)
        myvote = sentinelVoteLeader(master,epoch,winner,&leader_epoch);
    else
        myvote = sentinelVoteLeader(master,epoch,sentinel.myid,&leader_epoch);

    if (myvote && leader_epoch == epoch) {
        uint64_t votes = sentinelLeaderIncr(counters,myvote);

        if (votes > max_votes) {
            max_votes = votes;
            winner = myvote;
        }
    }

    voters_quorum = voters/2+1;
    if (winner && (max_votes < voters_quorum || max_votes < master->quorum))
        winner = NULL;

    winner = winner ? sdsnew(winner) : NULL;
    sdsfree(myvote);
    dictRelease(counters);
    return winner;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
testb $0x50, (%rdi)
je 0x7f423
movq %rsi, %r15
movq %rdi, %r14
leaq 0x86f75(%rip), %rdi     # 0x106240
xorl %esi, %esi
callq 0x31774
movq %rax, %rbx
movq 0x80(%r14), %rdi
movl 0x28(%rdi), %eax
movq %rax, 0x8(%rsp)
movl 0x48(%rdi), %eax
movq %rax, (%rsp)
callq 0x320bb
movq %rax, %r12
movq %rax, %rdi
callq 0x32116
testq %rax, %rax
je 0x7f32d
movq 0x8(%rax), %rax
movq 0xd8(%rax), %rsi
testq %rsi, %rsi
je 0x7f328
movq 0xe0(%rax), %rax
cmpq 0x9c38a(%rip), %rax     # 0x11b6a8
jne 0x7f328
movq %rbx, %rdi
callq 0x7f23c
movq %r12, %rdi
jmp 0x7f2f6
movq %r12, %rdi
callq 0x321d3
movq %rbx, %rdi
callq 0x320bb
movq %rax, %r13
movq %rax, %rdi
callq 0x32116
testq %rax, %rax
je 0x7f370
xorl %ebp, %ebp
xorl %r12d, %r12d
movq 0x8(%rax), %rcx
cmpq %rbp, %rcx
jbe 0x7f361
movq (%rax), %r12
movq %rcx, %rbp
movq %r13, %rdi
callq 0x32116
testq %rax, %rax
jne 0x7f352
jmp 0x7f375
xorl %r12d, %r12d
xorl %ebp, %ebp
movq %r13, %rdi
callq 0x321d3
testq %r12, %r12
leaq 0x9c2f1(%rip), %rdx     # 0x11b678
cmovneq %r12, %rdx
leaq 0x10(%rsp), %rcx
movq %r14, %rdi
movq %r15, %rsi
callq 0x7e6e8
movq %rax, %r13
testq %rax, %rax
je 0x7f3c2
cmpq %r15, 0x10(%rsp)
jne 0x7f3c2
movq %rbx, %rdi
movq %r13, %rsi
callq 0x7f23c
cltq
cmpq %rax, %rbp
cmovbq %r13, %r12
cmovbeq %rax, %rbp
testq %r12, %r12
je 0x7f3e9
movq 0x8(%rsp), %rax
movq (%rsp), %rcx
addl %ecx, %eax
incl %eax
shrl %eax
incl %eax
cmpq %rax, %rbp
jb 0x7f3e9
movl 0x90(%r14), %eax
cmpq %rax, %rbp
jae 0x7f3ec
xorl %r12d, %r12d
testq %r12, %r12
je 0x7f3fe
movq %r12, %rdi
callq 0x39245
movq %rax, %r14
jmp 0x7f401
xorl %r14d, %r14d
movq %r13, %rdi
callq 0x392b6
movq %rbx, %rdi
callq 0x31ff3
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x50fb5(%rip), %rdi     # 0xd03df
leaq 0x4fd1f(%rip), %rsi     # 0xcf150
movl $0xe5a, %edx            # imm = 0xE5A
callq 0x654e1
movl $0x1, %edi
callq 0x263d0