// File: /workspace/github_c_makefile_star2_O3/gplessis[P]dotdeb-redis/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 $0x28, %rsp
testb $0x50, (%rdi)
je 0x7b976
movq %rsi, %r15
movq %rdi, %r14
leaq 0x85af5(%rip), %rdi     # 0x1012e0
xorl %esi, %esi
callq 0x2e68c
movq %rax, %rbx
movq 0x80(%r14), %rdi
movl 0x28(%rdi), %eax
movq %rax, 0x18(%rsp)
movl 0x48(%rdi), %eax
movq %rax, 0x10(%rsp)
callq 0x2efbb
movq %rax, %r12
movq %rax, %rdi
callq 0x2f015
testq %rax, %rax
je 0x7b84e
movq 0x8(%rax), %rax
movq 0xd8(%rax), %rsi
testq %rsi, %rsi
je 0x7b849
movq 0xe0(%rax), %rax
cmpq 0x9aec9(%rip), %rax     # 0x116708
jne 0x7b849
movq %rbx, %rdi
callq 0x7b75c
movq %r12, %rdi
jmp 0x7b817
movq %r12, %rdi
callq 0x2f0dd
movq %rbx, %rdi
callq 0x2efbb
movq %rax, %r13
movq %rax, %rdi
callq 0x2f015
testq %rax, %rax
movq %r14, 0x8(%rsp)
je 0x7b8ac
xorl %ebp, %ebp
xorl %r12d, %r12d
movq 0x8(%rax), %rcx
cmpq %rbp, %rcx
jbe 0x7b887
movq (%rax), %r12
movq %rcx, %rbp
movq %r13, %rdi
callq 0x2f015
testq %rax, %rax
jne 0x7b878
movq %r15, %r14
movq %r13, %rdi
callq 0x2f0dd
testq %r12, %r12
je 0x7b8c5
xorl %r15d, %r15d
movq %r12, %r13
jmp 0x7b8d2
movq %r15, %r14
movq %r13, %rdi
callq 0x2f0dd
movb $0x1, %r15b
leaq 0x9ae17(%rip), %r12     # 0x1166d8
xorl %ebp, %ebp
jmp 0x7b8cf
movb $0x1, %r15b
leaq 0x9ae09(%rip), %r12     # 0x1166d8
xorl %r13d, %r13d
leaq 0x20(%rsp), %rcx
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x7ac49
movq %rax, %r12
testq %rax, %rax
je 0x7b918
cmpq %r14, 0x20(%rsp)
jne 0x7b918
movq %rbx, %rdi
movq %r12, %rsi
callq 0x7b75c
cltq
cmpq %rax, %rbp
cmovbeq %rax, %rbp
setae %al
cmovbq %r12, %r13
testb %r15b, %al
je 0x7b91d
jmp 0x7b944
testb %r15b, %r15b
jne 0x7b944
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
addl %ecx, %eax
incl %eax
shrl %eax
incl %eax
cmpq %rax, %rbp
jb 0x7b944
movq 0x8(%rsp), %rax
movl 0x90(%rax), %eax
cmpq %rax, %rbp
jae 0x7b969
xorl %r14d, %r14d
movq %r12, %rdi
callq 0x36330
movq %rbx, %rdi
callq 0x2eef3
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rdi
callq 0x362b0
movq %rax, %r14
jmp 0x7b947
leaq 0x51b62(%rip), %rdi     # 0xcd4df
leaq 0x508cc(%rip), %rsi     # 0xcc250
movl $0xe5a, %edx            # imm = 0xE5A
callq 0x61908
movl $0x1, %edi
callq 0x263c0