// File: /workspace/github_c_makefile_star2_O2/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 $0x18, %rsp
movq %rdi, 0x8(%rsp)
testb $0x50, (%rdi)
je 0x6ef94
movq %rsi, %r14
leaq 0x8a490(%rip), %rdi     # 0xf92e0
xorl %esi, %esi
callq 0x2a155
movq %rax, %rbx
movq 0x8(%rsp), %rax
movq 0x80(%rax), %rdi
movl 0x48(%rdi), %ebp
addl 0x28(%rdi), %ebp
callq 0x2a99f
movq %rax, %r12
movq %r12, %rdi
callq 0x2a9d3
testq %rax, %rax
je 0x6eeab
movq 0x8(%rax), %rax
movq 0xd8(%rax), %rsi
testq %rsi, %rsi
je 0x6ee74
movq 0xe0(%rax), %rax
cmpq 0x9f869(%rip), %rax     # 0x10e708
jne 0x6ee74
movq %rbx, %rdi
callq 0x6edc3
jmp 0x6ee74
movq %r12, %rdi
callq 0x2aa94
movq %rbx, %rdi
callq 0x2a99f
movq %rax, %r13
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %r13, %rdi
callq 0x2a9d3
testq %rax, %rax
je 0x6eee2
movq 0x8(%rax), %rcx
cmpq %r15, %rcx
jbe 0x6eec4
movq (%rax), %r12
movq %rcx, %r15
jmp 0x6eec4
movq %r13, %rdi
callq 0x2aa94
testq %r12, %r12
leaq 0x9f7e4(%rip), %rdx     # 0x10e6d8
cmovneq %r12, %rdx
leaq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x6e359
movq %rax, %r13
testq %rax, %rax
je 0x6ef42
cmpq %r14, 0x10(%rsp)
jne 0x6ef42
testq %r12, %r12
setne %r14b
movq %rbx, %rdi
movq %r13, %rsi
callq 0x6edc3
cltq
cmpq %rax, %r15
cmovbeq %rax, %r15
setb %al
cmovbq %r13, %r12
orb %r14b, %al
jne 0x6ef47
jmp 0x6ef62
testq %r12, %r12
je 0x6ef62
incl %ebp
shrl %ebp
incl %ebp
cmpq %rbp, %r15
jb 0x6ef62
movq 0x8(%rsp), %rax
movl 0x90(%rax), %eax
cmpq %rax, %r15
jae 0x6ef87
xorl %r14d, %r14d
movq %r13, %rdi
callq 0x311c8
movq %rbx, %rdi
callq 0x2a8da
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x3115a
movq %rax, %r14
jmp 0x6ef65
leaq 0x4f190(%rip), %rdi     # 0xbe12b
leaq 0x4defa(%rip), %rsi     # 0xbce9c
movl $0xe5a, %edx            # imm = 0xE5A
callq 0x57ac8
pushq $0x1
popq %rdi
callq 0x263c0