// File: /workspace/github_c_makefile_star2_O3/c-zhou[P]oatk/syncmer.c
// Function: ha_analyze_count
static int ha_analyze_count(int n_cnt, int start_cnt, const int64_t *cnt, int *peak_het, int verbose)
{
    // TODO code review
    const int hist_max = 100;
    int i, start, low_i, max_i, max2_i, max3_i;
    int64_t max, max2, max3, min;

    // determine the start point
    assert(n_cnt > start_cnt);
    *peak_het = -1;
    start = cnt[1] > 0? 1 : 2;

    // find the low point from the left
    low_i = start > start_cnt? start : start_cnt;
    for (i = low_i + 1; i < n_cnt; ++i)
        if (cnt[i] > cnt[i-1])
            break;
    low_i = i - 1;
    if (verbose > 0) fprintf(stderr, "[M::%s] lowest: count[%d] = %ld\n", __func__, low_i, (long)cnt[low_i]);
    if (low_i == n_cnt - 1) return -1; // low coverage

    // find the highest peak
    max_i = low_i + 1, max = cnt[max_i];
    for (i = low_i + 1; i < n_cnt; ++i)
        if (cnt[i] > max)
            max = cnt[i], max_i = i;
    if (verbose > 0) fprintf(stderr, "[M::%s] highest: count[%d] = %ld\n", __func__, max_i, (long)cnt[max_i]);

    // print histogram
    for (i = start; i < n_cnt; ++i) {        
        int x, exceed = 0;
        x = (int)((double)hist_max * cnt[i] / cnt[max_i] + .499);
        if (x > hist_max) exceed = 1, x = hist_max; // may happen if cnt[2] is higher
        if (i > max_i && x == 0) break;
        if (verbose > 0) ha_hist_line(i, x, exceed, cnt[i]);
    }
    {
        int x, exceed = 0;
        int64_t rest = 0;
        for (; i < n_cnt; ++i) rest += cnt[i];
        x = (int)((double)hist_max * rest / cnt[max_i] + .499);
        if (x > hist_max) exceed = 1, x = hist_max;
        if(verbose > 0) ha_hist_line(-1, x, exceed, rest);
    }

    // look for smaller peak on the low end
    max2 = -1; max2_i = -1;
    for (i = max_i - 1; i > low_i; --i)
        if (cnt[i] >= cnt[i-1] && cnt[i] >= cnt[i+1]) 
            if (cnt[i] > max2)
                max2 = cnt[i], max2_i = i;
    if (max2_i > low_i && max2_i < max_i) {
        for (i = max2_i + 1, min = max; i < max_i; ++i)
            if (cnt[i] < min)
                min = cnt[i];
        if (max2 < max * 0.05 || min > max2 * 0.95)
            max2 = -1, max2_i = -1;
    }
    if (verbose > 0) {
        if (max2 > 0)
            fprintf(stderr, "[M::%s] left: count[%d] = %ld\n", __func__, max2_i, (long)cnt[max2_i]);
        else
            fprintf(stderr, "[M::%s] left: none\n", __func__);
    }
    // look for smaller peak on the high end
    max3 = -1; max3_i = -1;
    for (i = max_i + 1; i < n_cnt - 1; ++i)
        if (cnt[i] >= cnt[i-1] && cnt[i] >= cnt[i+1])
            if (cnt[i] > max3)
                max3 = cnt[i], max3_i = i;
    if (max3_i > max_i) {
        for (i = max_i + 1, min = max; i < max3_i; ++i)
            if (cnt[i] < min)
                min = cnt[i];
        if (max3 < max * 0.05 || min > max3 * 0.95 || max3_i > max_i * 2.5)
            max3 = -1, max3_i = -1;
    }
    if (verbose > 0) {
        if (max3 > 0)
            fprintf(stderr, "[M::%s] right: count[%d] = %ld\n", __func__, max3_i, (long)cnt[max3_i]);
        else
            fprintf(stderr, "[M::%s] right: none\n", __func__);
    }
    if (max3_i > 0) {
        *peak_het = max_i;
        return max3_i;
    } else {
        if (max2_i > 0) *peak_het = max2_i;
        return max_i;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edx, %r9d
movq %rdi, %r15
movl $0xffffffff, (%rsi)     # imm = 0xFFFFFFFF
movq 0x8(%rdi), %rbx
movl $0x6, %r14d
movq (%r15,%r14,8), %rax
cmpq -0x8(%r15,%r14,8), %rax
jg 0xe8f8
incq %r14
cmpq $0x3e9, %r14            # imm = 0x3E9
jne 0xe8db
movl $0x3e9, %r14d           # imm = 0x3E9
leal -0x1(%r14), %ebp
testl %r9d, %r9d
jg 0xec34
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl $0x3e8, %ebp            # imm = 0x3E8
je 0xec22
movq %rsi, 0x10(%rsp)
movl %r14d, %eax
movq (%r15,%rax,8), %rbp
movl %r14d, %r12d
cmpl $0x3e8, %r14d           # imm = 0x3E8
ja 0xe949
movq (%r15,%rax,8), %rcx
cmpq %rbp, %rcx
cmovgl %eax, %r12d
cmovgq %rcx, %rbp
incq %rax
cmpq $0x3e9, %rax            # imm = 0x3E9
jne 0xe92f
movslq %r12d, %r10
testl %r9d, %r9d
movl %r9d, 0x4(%rsp)
jg 0xec6b
xorl %r13d, %r13d
testq %rbx, %rbx
setle %r13b
incq %r13
movsd 0x11651(%rip), %xmm2    # 0x1ffc0
movsd 0x11651(%rip), %xmm3    # 0x1ffc8
xorl %ebx, %ebx
movl $0x64, %edi
movq %r10, 0x8(%rsp)
movq (%r15,%r13,8), %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd %xmm2, %xmm0
xorps %xmm1, %xmm1
cvtsi2sdq (%r15,%r10,8), %xmm1
divsd %xmm1, %xmm0
addsd %xmm3, %xmm0
cvttsd2si %xmm0, %esi
cmpq %r10, %r13
setg %dl
testl %esi, %esi
sete %al
andb %dl, %al
testl %r9d, %r9d
jle 0xe9be
testb %al, %al
je 0xe9d0
testb %al, %al
jne 0xea04
incq %r13
cmpq $0x3e9, %r13            # imm = 0x3E9
jne 0xe983
jmp 0xea1f
xorl %edx, %edx
cmpl $0x64, %esi
cmovgel %edi, %esi
setg %dl
movl %r13d, %edi
callq 0x28ff
movl $0x64, %edi
movsd 0x115d8(%rip), %xmm3    # 0x1ffc8
movsd 0x115c8(%rip), %xmm2    # 0x1ffc0
movq 0x8(%rsp), %r10
movl 0x4(%rsp), %r9d
jmp 0xe9c2
xorl %ebx, %ebx
cmpq $0x3e8, %r13            # imm = 0x3E8
ja 0xea1f
addq (%r15,%r13,8), %rbx
incq %r13
cmpq $0x3e9, %r13            # imm = 0x3E9
jne 0xea0f
testl %r9d, %r9d
jg 0xeca1
cmpl %r14d, %r12d
jle 0xea71
movslq %r14d, %rcx
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq $-0x1, %rax
movq %r10, %rdx
movq -0x8(%r15,%rdx,8), %rsi
cmpq -0x10(%r15,%rdx,8), %rsi
jl 0xea67
cmpq (%r15,%rdx,8), %rsi
setge %dil
cmpq %rax, %rsi
setg %r8b
testb %r8b, %dil
je 0xea67
leal -0x1(%rdx), %r13d
movq %rsi, %rax
decq %rdx
cmpq %rcx, %rdx
jg 0xea40
jmp 0xea7e
movq $-0x1, %rax
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpl %r14d, %r13d
jl 0xeafc
movl %r12d, %ecx
subl %r13d, %ecx
jle 0xeafc
leal 0x1(%r13), %esi
movq %rbp, %rdx
cmpl %r12d, %esi
jge 0xeab8
movslq %r13d, %rdx
leaq (%r15,%rdx,8), %rsi
movl $0x1, %edi
movq %rbp, %rdx
movq (%rsi,%rdi,8), %r8
cmpq %rdx, %r8
cmovlq %r8, %rdx
incq %rdi
cmpl %edi, %ecx
jne 0xeaa6
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rbp, %xmm1
mulsd 0x11500(%rip), %xmm1    # 0x1ffd0
ucomisd %xmm0, %xmm1
ja 0xeaec
xorps %xmm1, %xmm1
cvtsi2sd %rdx, %xmm1
mulsd 0x114f2(%rip), %xmm0    # 0x1ffd8
ucomisd %xmm0, %xmm1
jbe 0xeafc
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
testl %r9d, %r9d
jle 0xeb05
jmp 0xed2d
testl %r9d, %r9d
jg 0xecfa
cmpl $0x3e6, %r12d           # imm = 0x3E6
jg 0xeb57
leaq 0x2(%r10), %rcx
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq $-0x1, %rax
movq -0x8(%r15,%rcx,8), %rdx
cmpq -0x10(%r15,%rcx,8), %rdx
jl 0xeb44
cmpq (%r15,%rcx,8), %rdx
setge %sil
cmpq %rax, %rdx
setg %dil
testb %dil, %sil
je 0xeb44
leal -0x1(%rcx), %ebx
movq %rdx, %rax
movq %rcx, %rdx
incq %rdx
cmpl $0x3e8, %ecx            # imm = 0x3E8
movq %rdx, %rcx
jne 0xeb1e
jmp 0xeb63
movq $-0x1, %rax
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl %r12d, %ebx
jle 0xec00
leal 0x1(%r12), %edx
movq %rbp, %rcx
cmpl %ebx, %edx
jge 0xeb9b
leaq (%r15,%r10,8), %rdx
movl %ebx, %esi
subl %r10d, %esi
movl $0x1, %edi
movq %rbp, %rcx
movq (%rdx,%rdi,8), %r8
cmpq %rcx, %r8
cmovlq %r8, %rcx
incq %rdi
cmpl %edi, %esi
jne 0xeb89
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rbp, %xmm1
mulsd 0x1141d(%rip), %xmm1    # 0x1ffd0
ucomisd %xmm0, %xmm1
ja 0xebec
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
mulsd 0x1140f(%rip), %xmm0    # 0x1ffd8
ucomisd %xmm0, %xmm1
ja 0xebec
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
mulsd 0x113fa(%rip), %xmm1    # 0x1ffe0
ucomisd %xmm1, %xmm0
jbe 0xec00
testl %r9d, %r9d
movq 0x10(%rsp), %rax
jle 0xec1a
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0xed8e
testl %r9d, %r9d
jg 0xed5c
testl %ebx, %ebx
movq 0x10(%rsp), %rax
jle 0xec1a
movl %r12d, %r13d
movl %ebx, %r12d
jmp 0xec1f
testl %r13d, %r13d
jle 0xec22
movl %r13d, (%rax)
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x173b5(%rip), %rax     # 0x25ff0
movq (%rax), %rdi
movq %rsi, %r12
movslq %ebp, %rcx
movq (%r15,%rcx,8), %r8
leaq 0x117b4(%rip), %rsi     # 0x20403
leaq 0x117ce(%rip), %rdx     # 0x20424
xorl %eax, %eax
movl %r9d, %r13d
callq 0x2280
movl %r13d, %r9d
movq %r12, %rsi
jmp 0xe905
movq 0x1737e(%rip), %rax     # 0x25ff0
movq (%rax), %rdi
movq (%r15,%r10,8), %r8
leaq 0x117b5(%rip), %rsi     # 0x20435
leaq 0x1179d(%rip), %rdx     # 0x20424
movl %r10d, %ecx
xorl %eax, %eax
movq %r10, %r13
callq 0x2280
movq %r13, %r10
movl 0x4(%rsp), %r9d
jmp 0xe95a
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
mulsd 0x1130f(%rip), %xmm0    # 0x1ffc0
xorps %xmm1, %xmm1
cvtsi2sdq (%r15,%r10,8), %xmm1
divsd %xmm1, %xmm0
addsd 0x11302(%rip), %xmm0    # 0x1ffc8
cvttsd2si %xmm0, %eax
xorl %edx, %edx
cmpl $0x65, %eax
setge %dl
cmpl $0x64, %eax
movl $0x64, %esi
cmovll %eax, %esi
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %rbx, %rcx
movl %r9d, %ebx
callq 0x28ff
movq 0x8(%rsp), %r10
movl %ebx, %r9d
jmp 0xea28
testq %rax, %rax
jle 0xed2d
movq 0x172ea(%rip), %rax     # 0x25ff0
movq (%rax), %rdi
movslq %r13d, %rax
movq (%r15,%rax,8), %r8
leaq 0x11740(%rip), %rsi     # 0x20457
leaq 0x11706(%rip), %rdx     # 0x20424
movl %r13d, %ecx
xorl %eax, %eax
movl %r9d, %ebx
callq 0x2280
jmp 0xed4f
movq 0x172bc(%rip), %rax     # 0x25ff0
movq (%rax), %rdi
leaq 0x11738(%rip), %rsi     # 0x20476
leaq 0x116df(%rip), %rdx     # 0x20424
xorl %eax, %eax
movl %r9d, %ebx
callq 0x2280
movq 0x8(%rsp), %r10
movl %ebx, %r9d
jmp 0xeb05
testq %rax, %rax
jle 0xed8e
movq 0x17288(%rip), %rax     # 0x25ff0
movq (%rax), %rdi
movslq %ebx, %rax
movq (%r15,%rax,8), %r8
leaq 0x11711(%rip), %rsi     # 0x2048a
leaq 0x116a4(%rip), %rdx     # 0x20424
movl %ebx, %ecx
xorl %eax, %eax
callq 0x2280
jmp 0xec09
movq 0x1725b(%rip), %rax     # 0x25ff0
movq (%rax), %rdi
leaq 0x1170b(%rip), %rsi     # 0x204aa
leaq 0x1167e(%rip), %rdx     # 0x20424
xorl %eax, %eax
callq 0x2280
jmp 0xec09