// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/hist.cpp
// Function: ha_analyze_count(int, int, int, long const*, int*)
int ha_analyze_count(int n_cnt, int start_cnt, int m_peak_hom, const int64_t *cnt, int *peak_het)
{
	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;
	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;
	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;
		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;
		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 (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 (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(m_peak_hom > 0) return adj_m_peak_hom(m_peak_hom, max_i, max2_i, max3_i, peak_het);
	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 $0x28, %rsp
cmpl %esi, %edi
jle 0x11f022
movq %r8, %rbx
movq %rcx, %r15
movl %edi, %r12d
movl $0xffffffff, (%r8)      # imm = 0xFFFFFFFF
xorl %ebp, %ebp
cmpq $0x0, 0x8(%rcx)
setle %bpl
incl %ebp
cmpl %esi, %ebp
cmovgl %ebp, %esi
movslq %edi, %rax
movl %esi, %r14d
incl %r14d
cmpl %r14d, %edi
cmovgl %edi, %r14d
leal -0x1(%r14), %r13d
movl %edx, 0x14(%rsp)
leaq 0x1(%rsi), %rcx
cmpq %rax, %rcx
jge 0x11ebc9
movq 0x8(%r15,%rsi,8), %rdi
cmpq (%r15,%rsi,8), %rdi
movq %rcx, %rsi
jle 0x11ebab
leal -0x1(%rcx), %r13d
movl %ecx, %r14d
movq 0x176410(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
movl %r13d, %eax
movq (%r15,%rax,8), %r8
leaq 0x11ba87(%rip), %rsi    # 0x23a668
leaq 0x11bb10(%rip), %rdx    # 0x23a6f8
movl %r13d, %ecx
xorl %eax, %eax
callq 0x22b0
leal -0x1(%r12), %ecx
movl %r13d, %eax
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x11ec16
movl %r13d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, 0xc(%rsp)
movq %rbx, 0x18(%rsp)
movl %r14d, %eax
movq (%r15,%rax,8), %rbx
cmpl %r12d, %r14d
movl %ecx, 0x10(%rsp)
jge 0x11ec49
movl %r12d, %ecx
movq (%r15,%rax,8), %rdx
cmpq %rbx, %rdx
cmovgl %eax, %r14d
cmovgq %rdx, %rbx
incq %rax
cmpq %rax, %rcx
jne 0x11ec32
movq 0x176390(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
movslq %r14d, %rcx
movq (%r15,%rcx,8), %r8
leaq 0x11ba39(%rip), %rsi    # 0x23a69a
leaq 0x11ba90(%rip), %rdx    # 0x23a6f8
movq %rcx, (%rsp)
xorl %eax, %eax
callq 0x22b0
cmpl %r12d, %ebp
jge 0x11ece2
movl %ebp, %ebp
movl %r12d, %eax
movq %rax, 0x20(%rsp)
xorl %r13d, %r13d
movq (%r15,%rbp,8), %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x11aa17(%rip), %xmm0   # 0x2396b0
movq (%rsp), %rax
xorps %xmm1, %xmm1
cvtsi2sdq (%r15,%rax,8), %xmm1
divsd %xmm1, %xmm0
addsd 0x10f35e(%rip), %xmm0   # 0x22e010
cvttsd2si %xmm0, %esi
cmpq %rax, %rbp
jle 0x11ecbf
testl %esi, %esi
je 0x11ece2
xorl %edx, %edx
cmpl $0x64, %esi
movl $0x64, %eax
cmovgel %eax, %esi
setg %dl
movl %ebp, %edi
callq 0x14aa0
incq %rbp
cmpq %rbp, 0x20(%rsp)
jne 0x11ec85
jmp 0x11ed03
cmpl %r12d, %ebp
jge 0x11ed00
movl %ebp, %eax
leaq (%r15,%rax,8), %rax
xorl %r13d, %r13d
addq (%rax), %r13
addq $0x8, %rax
incl %ebp
cmpl %r12d, %ebp
jl 0x11ecf0
jmp 0x11ed03
xorl %r13d, %r13d
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
mulsd 0x11a99d(%rip), %xmm0   # 0x2396b0
movq (%rsp), %rax
xorps %xmm1, %xmm1
cvtsi2sdq (%r15,%rax,8), %xmm1
divsd %xmm1, %xmm0
addsd 0x10f2e4(%rip), %xmm0   # 0x22e010
cvttsd2si %xmm0, %esi
cmpl $0x64, %esi
jle 0x11ed3f
movq %r13, %rdi
callq 0x14c88
jmp 0x11ed4e
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %edx, %edx
movq %r13, %rcx
callq 0x14aa0
leal -0x1(%r14), %eax
movl 0xc(%rsp), %r10d
cmpl %r10d, %eax
jle 0x11edb2
movslq %eax, %rcx
movslq %r10d, %rdx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq $-0x1, %rax
movl %r14d, %esi
movq (%r15,%rcx,8), %rdi
movslq %esi, %rsi
cmpq -0x10(%r15,%rsi,8), %rdi
jge 0x11ed83
movl %ecx, %esi
jmp 0x11eda8
cmpq (%r15,%rsi,8), %rdi
setge %sil
cmpq %rax, %rdi
setg %r9b
movl %ecx, %r8d
testb %r9b, %sil
jne 0x11eda0
movl %ebp, %r8d
movq %rax, %rdi
movl %ecx, %esi
movl %r8d, %ebp
movq %rdi, %rax
decq %rcx
cmpq %rdx, %rcx
jg 0x11ed71
jmp 0x11edbe
movq $-0x1, %rax
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl %r10d, %ebp
jle 0x11ee30
movl %r14d, %ecx
subl %ebp, %ecx
jle 0x11ee30
leal 0x1(%rbp), %esi
movq %rbx, %rdx
cmpl %r14d, %esi
jge 0x11edf6
movslq %ebp, %rdx
leaq (%r15,%rdx,8), %rsi
movl $0x1, %edi
movq %rbx, %rdx
movq (%rsi,%rdi,8), %r8
cmpq %rdx, %r8
cmovlq %r8, %rdx
incq %rdi
cmpl %edi, %ecx
jne 0x11ede4
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rbx, %xmm1
mulsd 0x1127ba(%rip), %xmm1   # 0x2315c8
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
ucomisd %xmm0, %xmm1
ja 0x11ee62
xorps %xmm1, %xmm1
cvtsi2sd %rdx, %xmm1
mulsd 0x11276e(%rip), %xmm0   # 0x231598
ucomisd %xmm0, %xmm1
ja 0x11ee62
testq %rax, %rax
jle 0x11ee5f
movq 0x1761a4(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
movslq %ebp, %rax
movq (%r15,%rax,8), %r8
leaq 0x11b8bc(%rip), %rsi    # 0x23a709
leaq 0x11b8a4(%rip), %rdx    # 0x23a6f8
movl %ebp, %ecx
xorl %eax, %eax
callq 0x22b0
jmp 0x11ee84
movl %ebp, %r13d
movq 0x176177(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
leaq 0x11b8b5(%rip), %rsi    # 0x23a728
leaq 0x11b87e(%rip), %rdx    # 0x23a6f8
xorl %eax, %eax
callq 0x22b0
movl %r13d, %ebp
leal 0x1(%r14), %ecx
cmpl 0x10(%rsp), %ecx
jge 0x11eef4
movslq %ecx, %rax
leaq (%r15,%rax,8), %rdx
movl $0x2, %esi
subl %r12d, %esi
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq $-0x1, %rax
movl %ecx, %edi
movq 0x17612d(%rip), %r12    # 0x294fe0
movq (%rdx), %r8
cmpq -0x8(%rdx), %r8
jl 0x11eedf
cmpq 0x8(%rdx), %r8
setge %r10b
cmpq %rax, %r8
setg %r11b
movl %edi, %r9d
testb %r11b, %r10b
jne 0x11eed9
movl %r13d, %r9d
movq %rax, %r8
movl %r9d, %r13d
movq %r8, %rax
addq $0x8, %rdx
leal (%rsi,%rdi), %r8d
incl %r8d
incl %edi
cmpl $0x1, %r8d
jne 0x11eeb3
jmp 0x11ef08
movq $-0x1, %rax
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq 0x1760d8(%rip), %r12    # 0x294fe0
movl %r13d, %edx
subl %r14d, %edx
jle 0x11ef95
movq %rbx, %rsi
cmpl %r13d, %ecx
jge 0x11ef3e
movq (%rsp), %rcx
leaq (%r15,%rcx,8), %rcx
movl $0x1, %edi
movq %rbx, %rsi
movq (%rcx,%rdi,8), %r8
cmpq %rsi, %r8
cmovlq %r8, %rsi
incq %rdi
cmpl %edi, %edx
jne 0x11ef2c
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rbx, %xmm1
mulsd 0x112672(%rip), %xmm1   # 0x2315c8
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
ucomisd %xmm0, %xmm1
ja 0x11efc2
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
mulsd 0x112627(%rip), %xmm0   # 0x231598
ucomisd %xmm0, %xmm1
ja 0x11efc2
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r14d, %xmm1
mulsd 0x116df9(%rip), %xmm1   # 0x235d88
ucomisd %xmm1, %xmm0
ja 0x11efc2
testq %rax, %rax
jle 0x11efbf
movq (%r12), %rdi
movslq %r13d, %rax
movq (%r15,%rax,8), %r8
leaq 0x11b790(%rip), %rsi    # 0x23a73c
leaq 0x11b745(%rip), %rdx    # 0x23a6f8
movl %r13d, %ecx
xorl %eax, %eax
callq 0x22b0
jmp 0x11efde
movl %r13d, %ebx
movq (%r12), %rdi
leaq 0x11b78f(%rip), %rsi    # 0x23a75c
leaq 0x11b724(%rip), %rdx    # 0x23a6f8
xorl %eax, %eax
callq 0x22b0
movl %ebx, %r13d
movl 0x14(%rsp), %edi
testl %edi, %edi
movq 0x18(%rsp), %r8
jle 0x11f006
movl %r14d, %esi
movl %ebp, %edx
movl %r13d, %ecx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x11eaa6
testl %r13d, %r13d
jle 0x11f013
movl %r14d, (%r8)
jmp 0x11ec04
testl %ebp, %ebp
jle 0x11f01a
movl %ebp, (%r8)
movl %r14d, %r13d
jmp 0x11ec04
leaq 0x11b5f3(%rip), %rdi    # 0x23a61c
leaq 0x11b5fe(%rip), %rsi    # 0x23a62e
leaq 0x11b685(%rip), %rcx    # 0x23a6bc
movl $0x51, %edx
callq 0x2110
nopl (%rax)