// File: /workspace/github_c_makefile_star2_O0/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
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 $0x50, %eax
cmpl $0x0, %eax
je 0x9b8c7
jmp 0x9b8e9
leaq 0xab91d(%rip), %rdi     # 0x1471eb
leaq 0xaa687(%rip), %rsi     # 0x145f5c
movl $0xe5a, %edx            # imm = 0xE5A
callq 0x76ce0
movl $0x1, %edi
callq 0x263c0
leaq 0xdba80(%rip), %rdi     # 0x177370
xorl %eax, %eax
movl %eax, %esi
callq 0x2d330
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x80(%rax), %rax
movq 0x28(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x80(%rcx), %rcx
addq 0x48(%rcx), %rax
addq $0x1, %rax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movq 0x80(%rax), %rdi
callq 0x2e400
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x2e4a0
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x9b98d
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq $0x0, 0xd8(%rax)
je 0x9b98b
movq -0x58(%rbp), %rax
movq 0xe0(%rax), %rax
cmpq 0xf16c3(%rip), %rax     # 0x18d038
jne 0x9b98b
movq -0x18(%rbp), %rdi
movq -0x58(%rbp), %rax
movq 0xd8(%rax), %rsi
callq 0x9b7e0
jmp 0x9b936
movq -0x20(%rbp), %rdi
callq 0x2e600
movq -0x18(%rbp), %rdi
callq 0x2e400
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x2e4a0
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
je 0x9b9e1
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x50(%rbp), %rax
jbe 0x9b9df
movq -0x60(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x9b9a3
movq -0x20(%rbp), %rdi
callq 0x2e600
cmpq $0x0, -0x40(%rbp)
je 0x9ba0c
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x40(%rbp), %rdx
leaq -0x48(%rbp), %rcx
callq 0x9a860
movq %rax, -0x38(%rbp)
jmp 0x9ba28
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq 0xf15ed(%rip), %rdx     # 0x18d008
leaq -0x48(%rbp), %rcx
callq 0x9a860
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x9ba68
movq -0x48(%rbp), %rax
cmpq -0x10(%rbp), %rax
jne 0x9ba68
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x9b7e0
cltq
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x50(%rbp), %rax
jbe 0x9ba66
movq -0x68(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0x9ba68
movl -0x2c(%rbp), %eax
shrl %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x9baa1
movq -0x50(%rbp), %rax
movl -0x30(%rbp), %ecx
cmpq %rcx, %rax
jb 0x9ba99
movq -0x50(%rbp), %rax
movq -0x8(%rbp), %rcx
movl 0x90(%rcx), %ecx
cmpq %rcx, %rax
jae 0x9baa1
movq $0x0, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x9bab7
movq -0x40(%rbp), %rdi
callq 0x38510
movq %rax, -0x70(%rbp)
jmp 0x9babf
xorl %eax, %eax
movq %rax, -0x70(%rbp)
jmp 0x9babf
movq -0x70(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
callq 0x38620
movq -0x18(%rbp), %rdi
callq 0x2e230
movq -0x40(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)