// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq %rcx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movl $0x64, -0x24(%rbp)
movl -0x8(%rbp), %eax
cmpl -0xc(%rbp), %eax
jle 0x1925ed
jmp 0x19260c
leaq 0x1f8be8(%rip), %rdi    # 0x38b1dc
leaq 0x1f8bf3(%rip), %rsi    # 0x38b1ee
movl $0x51, %edx
leaq 0x1f8c75(%rip), %rcx    # 0x38b27c
callq 0x2130
movq -0x20(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
movl $0x2, %eax
movl $0x1, %ecx
cmpq $0x0, %rdx
cmovgl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jle 0x192642
movl -0x2c(%rbp), %eax
movl %eax, -0x7c(%rbp)
jmp 0x192648
movl -0xc(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x19268d
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x18(%rbp), %rcx
movl -0x28(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
cmpq (%rcx,%rdx,8), %rax
jle 0x192680
jmp 0x19268d
jmp 0x192682
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x192657
movl -0x28(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x30(%rbp)
movq 0x231943(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movl -0x30(%rbp), %ecx
movq -0x18(%rbp), %rax
movslq -0x30(%rbp), %rdx
movq (%rax,%rdx,8), %r8
leaq 0x1f8b72(%rip), %rsi    # 0x38b228
leaq 0x1f8bfb(%rip), %rdx    # 0x38b2b8
movb $0x0, %al
callq 0x22a0
movl -0x30(%rbp), %eax
movl -0x8(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x1926dd
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x192c37
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x48(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x19273c
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq -0x48(%rbp), %rax
jle 0x19272f
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x48(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x34(%rbp)
jmp 0x192731
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1926ff
movq 0x23189d(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movl -0x34(%rbp), %ecx
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rdx
movq (%rax,%rdx,8), %r8
leaq 0x1f8afe(%rip), %rsi    # 0x38b25a
leaq 0x1f8b55(%rip), %rdx    # 0x38b2b8
movb $0x0, %al
callq 0x22a0
movl -0x2c(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x19280e
movl $0x0, -0x68(%rbp)
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
cvtsi2sdq (%rax,%rcx,8), %xmm1
movsd 0x1f7b07(%rip), %xmm0   # 0x38a2a0
mulsd %xmm1, %xmm0
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
cvtsi2sdq (%rax,%rcx,8), %xmm1
divsd %xmm1, %xmm0
movsd 0x1ec889(%rip), %xmm1   # 0x37f040
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x64(%rbp)
cmpl $0x64, -0x64(%rbp)
jle 0x1927d6
movl $0x1, -0x68(%rbp)
movl $0x64, -0x64(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x34(%rbp), %eax
jle 0x1927e6
cmpl $0x0, -0x64(%rbp)
jne 0x1927e6
jmp 0x19280e
movl -0x28(%rbp), %edi
movl -0x64(%rbp), %esi
movl -0x68(%rbp), %edx
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
callq 0x192340
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x192770
movl $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x192844
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x19281d
cvtsi2sdq -0x78(%rbp), %xmm1
movsd 0x1f7a4e(%rip), %xmm0   # 0x38a2a0
mulsd %xmm1, %xmm0
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
cvtsi2sdq (%rax,%rcx,8), %xmm1
divsd %xmm1, %xmm0
movsd 0x1ec7d0(%rip), %xmm1   # 0x37f040
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x6c(%rbp)
cmpl $0x64, -0x6c(%rbp)
jle 0x19288f
movl $0x1, -0x70(%rbp)
movl $0x64, -0x6c(%rbp)
movl -0x6c(%rbp), %esi
movl -0x70(%rbp), %edx
movq -0x78(%rbp), %rcx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x192340
movq $-0x1, -0x50(%rbp)
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
movl -0x34(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x30(%rbp), %eax
jle 0x192938
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x18(%rbp), %rcx
movl -0x28(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
cmpq (%rcx,%rdx,8), %rax
jl 0x19292b
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x18(%rbp), %rcx
movl -0x28(%rbp), %edx
addl $0x1, %edx
movslq %edx, %rdx
cmpq (%rcx,%rdx,8), %rax
jl 0x19292b
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq -0x50(%rbp), %rax
jle 0x192929
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x19292b
jmp 0x19292d
movl -0x28(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1928bb
movl -0x38(%rbp), %eax
cmpl -0x30(%rbp), %eax
jle 0x1929e5
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x1929e5
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x60(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x192998
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq -0x60(%rbp), %rax
jge 0x19298b
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
jmp 0x19298d
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x192961
cvtsi2sdq -0x50(%rbp), %xmm1
cvtsi2sdq -0x48(%rbp), %xmm0
movsd 0x1ef894(%rip), %xmm2   # 0x382240
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
ja 0x1929d4
cvtsi2sdq -0x60(%rbp), %xmm0
cvtsi2sdq -0x50(%rbp), %xmm1
movsd 0x1ef856(%rip), %xmm2   # 0x382220
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1929e3
movq $-0x1, -0x50(%rbp)
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
jmp 0x1929e5
cmpq $0x0, -0x50(%rbp)
jle 0x192a1c
movq 0x2315ed(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movl -0x38(%rbp), %ecx
movq -0x18(%rbp), %rax
movslq -0x38(%rbp), %rdx
movq (%rax,%rdx,8), %r8
leaq 0x1f88bd(%rip), %rsi    # 0x38b2c9
leaq 0x1f88a5(%rip), %rdx    # 0x38b2b8
movb $0x0, %al
callq 0x22a0
jmp 0x192a3b
movq 0x2315bd(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x1f88bb(%rip), %rsi    # 0x38b2e8
leaq 0x1f8884(%rip), %rdx    # 0x38b2b8
movb $0x0, %al
callq 0x22a0
movq $-0x1, -0x58(%rbp)
movl $0xffffffff, -0x3c(%rbp) # imm = 0xFFFFFFFF
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
movl -0x8(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x192ad8
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x18(%rbp), %rcx
movl -0x28(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
cmpq (%rcx,%rdx,8), %rax
jl 0x192ac8
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0x18(%rbp), %rcx
movl -0x28(%rbp), %edx
addl $0x1, %edx
movslq %edx, %rdx
cmpq (%rcx,%rdx,8), %rax
jl 0x192ac8
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq -0x58(%rbp), %rax
jle 0x192ac6
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x58(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x192ac8
jmp 0x192aca
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x192a53
movl -0x3c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jle 0x192b95
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x60(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x192b2c
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq -0x60(%rbp), %rax
jge 0x192b1f
movq -0x18(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
jmp 0x192b21
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x192af5
cvtsi2sdq -0x58(%rbp), %xmm1
cvtsi2sdq -0x48(%rbp), %xmm0
movsd 0x1ef700(%rip), %xmm2   # 0x382240
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
ja 0x192b84
cvtsi2sdq -0x60(%rbp), %xmm0
cvtsi2sdq -0x58(%rbp), %xmm1
movsd 0x1ef6c2(%rip), %xmm2   # 0x382220
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
ja 0x192b84
cvtsi2sdl -0x3c(%rbp), %xmm0
cvtsi2sdl -0x34(%rbp), %xmm1
movsd 0x1f3cf6(%rip), %xmm2   # 0x386870
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x192b93
movq $-0x1, -0x58(%rbp)
movl $0xffffffff, -0x3c(%rbp) # imm = 0xFFFFFFFF
jmp 0x192b95
cmpq $0x0, -0x58(%rbp)
jle 0x192bcc
movq 0x23143d(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movl -0x3c(%rbp), %ecx
movq -0x18(%rbp), %rax
movslq -0x3c(%rbp), %rdx
movq (%rax,%rdx,8), %r8
leaq 0x1f8740(%rip), %rsi    # 0x38b2fc
leaq 0x1f86f5(%rip), %rdx    # 0x38b2b8
movb $0x0, %al
callq 0x22a0
jmp 0x192beb
movq 0x23140d(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x1f873f(%rip), %rsi    # 0x38b31c
leaq 0x1f86d4(%rip), %rdx    # 0x38b2b8
movb $0x0, %al
callq 0x22a0
cmpl $0x0, -0x10(%rbp)
jle 0x192c0b
movl -0x10(%rbp), %edi
movl -0x34(%rbp), %esi
movl -0x38(%rbp), %edx
movl -0x3c(%rbp), %ecx
movq -0x20(%rbp), %r8
callq 0x192410
movl %eax, -0x4(%rbp)
jmp 0x192c37
cmpl $0x0, -0x3c(%rbp)
jle 0x192c22
movl -0x34(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x192c37
cmpl $0x0, -0x38(%rbp)
jle 0x192c31
movl -0x38(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)