// File: /workspace/github_cpp_makefile_star2_O2/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 0xdb4c8
movq %rcx, %r15
movq %r8, 0x20(%rsp)
orl $-0x1, (%r8)
xorl %r13d, %r13d
cmpq $0x0, 0x8(%rcx)
setle %r13b
incl %r13d
cmpl %esi, %r13d
cmovgl %r13d, %esi
movl %esi, %r12d
incl %r12d
cmpl %r12d, %edi
cmovgl %edi, %r12d
movq %rdi, (%rsp)
movslq %edi, %r14
movq %rsi, %rcx
shlq $0x20, %rcx
movabsq $0x100000000, %rax      # imm = 0x100000000
movl %edx, 0x14(%rsp)
movq %rsi, %rbp
movq %rcx, %rbx
incq %rsi
cmpq %r14, %rsi
jge 0xdb145
movq 0x8(%r15,%rbp,8), %rdi
leaq (%rbx,%rax), %rcx
cmpq (%r15,%rbp,8), %rdi
jle 0xdb125
movl %esi, %r12d
movq 0x177e94(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
movq (%r15,%rbp,8), %r8
leaq 0x1113ee(%rip), %rsi    # 0x1ec548
leaq 0x111477(%rip), %rdx    # 0x1ec5d8
movl %ebp, %ecx
xorl %eax, %eax
callq 0x22b0
movq (%rsp), %rax
decl %eax
cmpl %ebp, %eax
jne 0xdb188
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, 0x10(%rsp)
movl %r12d, %eax
movq (%r15,%rax,8), %rsi
cmpq %r14, %rax
jge 0xdb1ac
movq (%r15,%rax,8), %rcx
cmpq %rsi, %rcx
cmovgl %eax, %r12d
cmovgq %rcx, %rsi
incq %rax
jmp 0xdb193
movq %rsi, 0x18(%rsp)
movq 0x177e28(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
movq %r12, 0x8(%rsp)
movslq %r12d, %r12
movq (%r15,%r12,8), %r8
leaq 0x1113ac(%rip), %rsi    # 0x1ec57a
leaq 0x111403(%rip), %rdx    # 0x1ec5d8
movl %r12d, %ecx
xorl %eax, %eax
callq 0x22b0
movq (%rsp), %rax
cmpl %eax, %r13d
cmovgl %r13d, %eax
movq %rax, (%rsp)
movl %r13d, %r13d
cmpq %r14, %r13
jge 0xdb241
movq (%r15,%r13,8), %rcx
cvtsi2sd %rcx, %xmm0
mulsd 0x110389(%rip), %xmm0   # 0x1eb590
cvtsi2sdq (%r15,%r12,8), %xmm1
divsd %xmm1, %xmm0
addsd 0x104df7(%rip), %xmm0   # 0x1e0010
cvttsd2si %xmm0, %esi
cmpq %r12, %r13
jle 0xdb226
testl %esi, %esi
je 0xdb247
xorl %edx, %edx
cmpl $0x64, %esi
pushq $0x64
popq %rax
cmovgel %eax, %esi
setg %dl
movl %r13d, %edi
callq 0xdaf6f
incq %r13
jmp 0xdb1f1
movq (%rsp), %r13
jmp 0xdb247
movl %r13d, %eax
xorl %ecx, %ecx
cmpq %r14, %rax
jge 0xdb25a
addq (%r15,%rax,8), %rcx
incq %rax
jmp 0xdb24c
cvtsi2sd %rcx, %xmm0
mulsd 0x110329(%rip), %xmm0   # 0x1eb590
cvtsi2sdq (%r15,%r12,8), %xmm1
divsd %xmm1, %xmm0
addsd 0x104d97(%rip), %xmm0   # 0x1e0010
cvttsd2si %xmm0, %eax
xorl %edx, %edx
cmpl $0x65, %eax
setge %dl
cmpl $0x64, %eax
pushq $0x64
popq %rsi
cmovll %eax, %esi
pushq $-0x1
popq %r13
movl %r13d, %edi
callq 0xdaf6f
sarq $0x20, %rbx
pushq $-0x1
popq %rax
movq %r12, %rcx
movq 0x8(%rsp), %r8
movq 0x177d30(%rip), %r14    # 0x252fe0
movq 0x18(%rsp), %r9
movq %rcx, %rsi
leaq -0x1(%rsi), %rcx
cmpq %rbx, %rcx
jle 0xdb2ed
movq -0x8(%r15,%rsi,8), %rdx
cmpq -0x10(%r15,%rsi,8), %rdx
movq %rcx, %rsi
jl 0xdb2b8
cmpq 0x8(%r15,%rcx,8), %rdx
setge %sil
cmpq %rax, %rdx
setg %dil
testb %dil, %sil
cmovnel %ecx, %r13d
cmovneq %rdx, %rax
jmp 0xdb2b5
movslq %r13d, %rcx
cmpl %ebp, %r13d
jle 0xdb344
cmpl %r8d, %r13d
jge 0xdb344
leaq 0x1(%rcx), %rsi
movq %r9, %rdx
cmpq %r12, %rsi
jge 0xdb316
movq (%r15,%rsi,8), %rdi
cmpq %rdx, %rdi
cmovlq %rdi, %rdx
incq %rsi
jmp 0xdb301
cvtsi2sd %rax, %xmm0
cvtsi2sd %r9, %xmm1
mulsd 0x1081f0(%rip), %xmm1   # 0x1e3518
pushq $-0x1
popq %rbx
ucomisd %xmm0, %xmm1
ja 0xdb36d
cvtsi2sd %rdx, %xmm1
mulsd 0x1081aa(%rip), %xmm0   # 0x1e34e8
ucomisd %xmm0, %xmm1
ja 0xdb36d
testq %rax, %rax
jle 0xdb36a
movq (%r14), %rdi
movq (%r15,%rcx,8), %r8
leaq 0x111292(%rip), %rsi    # 0x1ec5e9
leaq 0x11127a(%rip), %rdx    # 0x1ec5d8
movl %r13d, %ecx
xorl %eax, %eax
callq 0x22b0
jmp 0xdb388
movl %r13d, %ebx
movq (%r14), %rdi
leaq 0x111291(%rip), %rsi    # 0x1ec608
leaq 0x11125a(%rip), %rdx    # 0x1ec5d8
xorl %eax, %eax
callq 0x22b0
movl %ebx, %r13d
movq 0x8(%rsp), %r10
leal 0x1(%r10), %eax
movslq %eax, %rcx
movslq 0x10(%rsp), %rdx
pushq $-0x1
popq %rax
pushq $-0x1
popq %rbp
movq 0x18(%rsp), %r11
cmpq %rdx, %rcx
jge 0xdb3da
movq (%r15,%rcx,8), %rsi
cmpq -0x8(%r15,%rcx,8), %rsi
jl 0xdb3d5
cmpq 0x8(%r15,%rcx,8), %rsi
setge %r8b
cmpq %rax, %rsi
setg %r9b
movl %ecx, %edi
testb %r9b, %r8b
jne 0xdb3d0
movl %ebp, %edi
movq %rax, %rsi
movl %edi, %ebp
movq %rsi, %rax
incq %rcx
jmp 0xdb3a4
movslq %ebp, %rcx
cmpl %r10d, %ebp
jle 0xdb442
incq %r12
movq %r11, %rdx
cmpq %rcx, %r12
jge 0xdb3fd
movq (%r15,%r12,8), %rsi
cmpq %rdx, %rsi
cmovlq %rsi, %rdx
incq %r12
jmp 0xdb3e8
cvtsi2sd %rax, %xmm0
cvtsi2sd %r11, %xmm1
mulsd 0x108109(%rip), %xmm1   # 0x1e3518
pushq $-0x1
popq %rbx
ucomisd %xmm0, %xmm1
ja 0xdb469
cvtsi2sd %rdx, %xmm1
mulsd 0x1080c3(%rip), %xmm0   # 0x1e34e8
ucomisd %xmm0, %xmm1
ja 0xdb469
cvtsi2sd %ebp, %xmm0
cvtsi2sd %r10d, %xmm1
mulsd 0x10c84c(%rip), %xmm1   # 0x1e7c88
ucomisd %xmm1, %xmm0
ja 0xdb469
testq %rax, %rax
jle 0xdb467
movq (%r14), %rdi
movq (%r15,%rcx,8), %r8
leaq 0x1111c7(%rip), %rsi    # 0x1ec61c
leaq 0x11117c(%rip), %rdx    # 0x1ec5d8
movl %ebp, %ecx
xorl %eax, %eax
callq 0x22b0
jmp 0xdb483
movl %ebp, %ebx
movq (%r14), %rdi
leaq 0x1111c9(%rip), %rsi    # 0x1ec63c
leaq 0x11115e(%rip), %rdx    # 0x1ec5d8
xorl %eax, %eax
callq 0x22b0
movl %ebx, %ebp
movl 0x14(%rsp), %edi
testl %edi, %edi
movq 0x20(%rsp), %r8
movq 0x8(%rsp), %rsi
jle 0xdb4ad
movl %r13d, %edx
movl %ebp, %ecx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xdb00e
testl %ebp, %ebp
jle 0xdb4b9
movl %esi, (%r8)
jmp 0xdb177
testl %r13d, %r13d
jle 0xdb4c1
movl %r13d, (%r8)
movl %esi, %ebp
jmp 0xdb177
leaq 0x11102d(%rip), %rdi    # 0x1ec4fc
leaq 0x111038(%rip), %rsi    # 0x1ec50e
leaq 0x1110bf(%rip), %rcx    # 0x1ec59c
pushq $0x51
popq %rdx
callq 0x2110
nopl (%rax)