// File: /workspace/github_cpp_makefile_star2_O1/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 0x11941f
movq %r8, %r13
movq %rcx, %r15
movl %edi, %ebx
movl $0xffffffff, (%r8)      # imm = 0xFFFFFFFF
xorl %ebp, %ebp
cmpq $0x0, 0x8(%rcx)
setle %bpl
incl %ebp
cmpl %esi, %ebp
cmovgl %ebp, %esi
movl %edi, %r12d
movslq %edi, %rax
movl %esi, %r14d
incl %r14d
cmpl %r14d, %edi
cmovgl %edi, %r14d
leal -0x1(%r14), %ecx
movl %edx, 0x14(%rsp)
leaq 0x1(%rsi), %r8
cmpq %rax, %r8
jge 0x119007
movq 0x8(%r15,%rsi,8), %rdi
cmpq (%r15,%rsi,8), %rdi
movq %r8, %rsi
jle 0x118fe9
leal -0x1(%r8), %ecx
movl %r8d, %r14d
movl %ecx, 0x4(%rsp)
movq 0x170fce(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
movl %ecx, %eax
movq (%r15,%rax,8), %r8
leaq 0x115436(%rip), %rsi    # 0x22e458
leaq 0x1154bf(%rip), %rdx    # 0x22e4e8
xorl %eax, %eax
callq 0x22b0
leal -0x1(%rbx), %edx
movq %rbx, %rcx
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl %edx, 0x4(%rsp)
jne 0x119052
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, 0x20(%rsp)
movl %r14d, %eax
movq (%r15,%rax,8), %r13
cmpl %ecx, %r14d
movq %rcx, %rbx
movl %edx, 0x10(%rsp)
jge 0x119081
movq (%r15,%rax,8), %rcx
cmpq %r13, %rcx
cmovgl %eax, %r14d
cmovgq %rcx, %r13
incq %rax
cmpq %rax, %r12
jne 0x11906a
movq 0x170f58(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
movslq %r14d, %rcx
movq (%r15,%rcx,8), %r8
leaq 0x1153f1(%rip), %rsi    # 0x22e48a
leaq 0x115448(%rip), %rdx    # 0x22e4e8
movq %rcx, 0x8(%rsp)
xorl %eax, %eax
callq 0x22b0
cmpl %ebx, %ebp
jge 0x119119
movq %rbx, 0x18(%rsp)
movl %ebp, %ebp
movl $0x64, %ebx
movq (%r15,%rbp,8), %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x1143d0(%rip), %xmm0   # 0x22d4a0
movq 0x8(%rsp), %rax
xorps %xmm1, %xmm1
cvtsi2sdq (%r15,%rax,8), %xmm1
divsd %xmm1, %xmm0
addsd 0x108f26(%rip), %xmm0   # 0x222010
cvttsd2si %xmm0, %esi
cmpq %rax, %rbp
jle 0x1190f7
testl %esi, %esi
je 0x119114
xorl %edx, %edx
cmpl $0x64, %esi
cmovgel %ebx, %esi
setg %dl
movl %ebp, %edi
callq 0x152fa
incq %rbp
cmpq %rbp, %r12
jne 0x1190bc
movl %r12d, %ebp
movq 0x18(%rsp), %rbx
cmpl %ebx, %ebp
jge 0x119134
movl %ebp, %eax
leaq (%r15,%rax,8), %rax
xorl %ecx, %ecx
addq (%rax), %rcx
addq $0x8, %rax
incl %ebp
cmpl %ebx, %ebp
jl 0x119125
jmp 0x119136
xorl %ecx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x11435a(%rip), %xmm0   # 0x22d4a0
movq 0x8(%rsp), %r12
xorps %xmm1, %xmm1
cvtsi2sdq (%r15,%r12,8), %xmm1
divsd %xmm1, %xmm0
addsd 0x108eb0(%rip), %xmm0   # 0x222010
cvttsd2si %xmm0, %eax
xorl %edx, %edx
cmpl $0x65, %eax
setge %dl
cmpl $0x64, %eax
movl $0x64, %esi
cmovll %eax, %esi
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x152fa
leal -0x1(%r12), %ecx
movq $-0x1, %rax
movl 0x4(%rsp), %r10d
cmpl %r10d, %ecx
jle 0x1191dc
movslq %ecx, %rcx
movslq %r10d, %rdx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %r14d, %esi
movq (%r15,%rcx,8), %rdi
movslq %esi, %r8
cmpq -0x10(%r15,%r8,8), %rdi
jl 0x1191d2
cmpq (%r15,%r8,8), %rdi
setge %r8b
cmpq %rax, %rdi
setg %r9b
testb %r9b, %r8b
je 0x1191d2
leal -0x1(%rsi), %ebp
movq %rdi, %rax
decq %rcx
decl %esi
cmpq %rdx, %rcx
jg 0x1191aa
cmpl %r10d, %ebp
movq 0x170dfa(%rip), %r12    # 0x289fe0
jle 0x11925b
movl %r14d, %ecx
subl %ebp, %ecx
jle 0x11925b
leal 0x1(%rbp), %esi
movq %r13, %rdx
cmpl %r14d, %esi
jge 0x11921b
movslq %ebp, %rdx
leaq (%r15,%rdx,8), %rsi
movl $0x1, %edi
movq %r13, %rdx
movq (%rsi,%rdi,8), %r8
cmpq %rdx, %r8
cmovlq %r8, %rdx
incq %rdi
cmpl %edi, %ecx
jne 0x119209
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r13, %xmm1
mulsd 0x10c295(%rip), %xmm1   # 0x2254c8
ucomisd %xmm0, %xmm1
ja 0x11924f
xorps %xmm1, %xmm1
cvtsi2sd %rdx, %xmm1
mulsd 0x10c24f(%rip), %xmm0   # 0x225498
ucomisd %xmm0, %xmm1
jbe 0x11925b
movq $-0x1, %rax
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq (%r12), %rdi
testq %rax, %rax
jle 0x119282
movslq %ebp, %rcx
movq (%r15,%rcx,8), %r8
leaq 0x115287(%rip), %rsi    # 0x22e4f9
leaq 0x11526f(%rip), %rdx    # 0x22e4e8
xorl %eax, %eax
callq 0x22b0
jmp 0x119297
leaq 0x11528f(%rip), %rsi    # 0x22e518
leaq 0x115258(%rip), %rdx    # 0x22e4e8
xorl %eax, %eax
callq 0x22b0
leal 0x1(%r14), %ecx
cmpl 0x10(%rsp), %ecx
jge 0x1192fe
movslq %ecx, %rax
leaq (%r15,%rax,8), %rdx
movl $0x2, %esi
subl %ebx, %esi
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq $-0x1, %rax
movl %ecx, %edi
movq (%rdx), %r8
cmpq -0x8(%rdx), %r8
jl 0x1192e9
cmpq 0x8(%rdx), %r8
setge %r10b
cmpq %rax, %r8
setg %r11b
movl %edi, %r9d
testb %r11b, %r10b
jne 0x1192e3
movl %ebx, %r9d
movq %rax, %r8
movl %r9d, %ebx
movq %r8, %rax
addq $0x8, %rdx
leal (%rsi,%rdi), %r8d
incl %r8d
incl %edi
cmpl $0x1, %r8d
jne 0x1192bd
jmp 0x11930a
movq $-0x1, %rax
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %edx
subl %r14d, %edx
jle 0x11939c
movq %r13, %rsi
cmpl %ebx, %ecx
jge 0x11933f
movq 0x8(%rsp), %rcx
leaq (%r15,%rcx,8), %rcx
movl $0x1, %edi
movq %r13, %rsi
movq (%rcx,%rdi,8), %r8
cmpq %rsi, %r8
cmovlq %r8, %rsi
incq %rdi
cmpl %edi, %edx
jne 0x11932d
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r13, %xmm1
mulsd 0x10c171(%rip), %xmm1   # 0x2254c8
ucomisd %xmm0, %xmm1
ja 0x119390
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
mulsd 0x10c12b(%rip), %xmm0   # 0x225498
ucomisd %xmm0, %xmm1
ja 0x119390
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r14d, %xmm1
mulsd 0x110806(%rip), %xmm1   # 0x229b90
ucomisd %xmm1, %xmm0
jbe 0x11939c
movq $-0x1, %rax
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq (%r12), %rdi
testq %rax, %rax
movq 0x20(%rsp), %r12
jle 0x1193c8
movslq %ebx, %rcx
movq (%r15,%rcx,8), %r8
leaq 0x115174(%rip), %rsi    # 0x22e52c
leaq 0x115129(%rip), %rdx    # 0x22e4e8
xorl %eax, %eax
callq 0x22b0
jmp 0x1193dd
leaq 0x11517d(%rip), %rsi    # 0x22e54c
leaq 0x115112(%rip), %rdx    # 0x22e4e8
xorl %eax, %eax
callq 0x22b0
movl 0x14(%rsp), %edi
testl %edi, %edi
jle 0x119402
movl %r14d, %esi
movl %ebp, %edx
movl %ebx, %ecx
movq %r12, %r8
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x118ee2
testl %ebx, %ebx
jle 0x11940f
movl %r14d, (%r12)
jmp 0x119041
testl %ebp, %ebp
jle 0x119417
movl %ebp, (%r12)
movl %r14d, %ebx
jmp 0x119041
leaq 0x114fe6(%rip), %rdi    # 0x22e40c
leaq 0x114ff1(%rip), %rsi    # 0x22e41e
leaq 0x115078(%rip), %rcx    # 0x22e4ac
movl $0x51, %edx
callq 0x2110
nop