// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Hash_Table.cpp
// Function: ha_chain_check(k_mer_hit*, int, Chain_Data*, int, double)
int32_t ha_chain_check(k_mer_hit *a, int32_t n_a, Chain_Data *dp, int32_t min_sc, double bw_thres)
{
	int32_t i, tot_indel = 0, tot_len = 0;
	double bw_pen;
	if (n_a == 0) return -1;
	for (i = 1; i < n_a; ++i)
		if (a[i-1].self_offset >= a[i].self_offset)
			break;
	if (i < n_a) return -1;
	bw_pen = 1.0 / bw_thres;
	// dp->score[0] = a[0].good? min_sc : min_sc>>1;
    dp->score[0] = normal_w(min_sc, (int64_t)a[0].cnt);
	dp->pre[0] = -1, dp->indels[0] = 0, dp->self_length[0] = 0, dp->occ[0] = 1;
	for (i = 1; i < n_a; ++i) {
		int32_t score, dg;
		int32_t dx = (int32_t)a[i].offset - (int32_t)a[i-1].offset;
		int32_t dy = (int32_t)a[i].self_offset - (int32_t)a[i-1].self_offset;
		int32_t dd = dx > dy? dx - dy : dy - dx;
		double gap_rate;
		tot_indel += dd;
		tot_len += dy;
		if (tot_indel > tot_len * bw_thres) break;
		dg = dx < dy? dx : dy;
		if (dd > THRESHOLD_MAX_SIZE && dd > dg * bw_thres) break;
		score = dg < min_sc? dg : min_sc;
        score = normal_w(score, (int64_t)a[i].cnt);
		gap_rate = (double)tot_indel / tot_len;
		score -= (int)(gap_rate * score * bw_pen);
		dp->score[i] = dp->score[i-1] + score;
		dp->pre[i] = i - 1;
		dp->indels[i] = tot_indel;
		dp->self_length[i] = tot_len;
        dp->occ[i] = i + 1;
	}
	if (i < n_a) return -1;
	return n_a;
}

// Assembly:
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %esi, %esi
je 0x3e6e7
movq %rdx, %r9
cmpl $0x2, %esi
jl 0x3e57f
movl %esi, %r10d
shlq $0x4, %r10
movl $0x10, %edx
movl -0x8(%rdi,%rdx), %r8d
cmpl 0x8(%rdi,%rdx), %r8d
jae 0x3e6e7
addq $0x10, %rdx
cmpq %rdx, %r10
jne 0x3e566
movslq %ecx, %rdx
movl 0xc(%rdi), %r8d
movl $0x1, %eax
cmpq %r8, %rdx
jl 0x3e597
movl %ecx, %eax
xorl %edx, %edx
divl %r8d
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
xorl %r10d, %r10d
movq (%r9), %r11
movl %eax, (%r11)
movq 0x8(%r9), %rax
movq $-0x1, (%rax)
movq 0x10(%r9), %r14
movl %r10d, (%r14)
movq 0x18(%r9), %r15
movl %r10d, (%r15)
movq 0x20(%r9), %r9
movl $0x1, (%r9)
cmpl $0x2, %esi
jl 0x3e6d4
movq %rax, %rdx
movsd 0x1f2d2e(%rip), %xmm1   # 0x231310
divsd %xmm0, %xmm1
movl %esi, -0x4(%rsp)
movl %esi, %eax
addq $0x1c, %rdi
movl $0x1, %r12d
subq %rax, %r12
xorl %r13d, %r13d
xorl %ebp, %ebp
movq %rdx, %rsi
movl -0x8(%rdi), %edx
movl -0x4(%rdi), %eax
subl -0x18(%rdi), %edx
subl -0x14(%rdi), %eax
movl %edx, %ebx
subl %eax, %ebx
movl %ebx, %r8d
negl %r8d
cmovsl %ebx, %r8d
addl %r8d, %r10d
addl %eax, %ebp
xorps %xmm2, %xmm2
cvtsi2sd %r10d, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %ebp, %xmm3
movapd %xmm3, %xmm4
mulsd %xmm0, %xmm4
ucomisd %xmm4, %xmm2
ja 0x3e6d8
cmpl %eax, %edx
cmovll %edx, %eax
cmpl $0x20, %r8d
jb 0x3e665
xorps %xmm4, %xmm4
cvtsi2sd %r8d, %xmm4
xorps %xmm5, %xmm5
cvtsi2sd %eax, %xmm5
mulsd %xmm0, %xmm5
ucomisd %xmm5, %xmm4
ja 0x3e6d8
cmpl %ecx, %eax
cmovgel %ecx, %eax
movslq %eax, %rbx
movl (%rdi), %r8d
movl $0x1, %edx
cmpq %r8, %rbx
jl 0x3e681
xorl %edx, %edx
divl %r8d
movl %eax, %edx
divsd %xmm3, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %edx, %xmm3
mulsd %xmm2, %xmm3
mulsd %xmm1, %xmm3
cvttsd2si %xmm3, %eax
addl (%r11,%r13,4), %edx
subl %eax, %edx
movl %edx, 0x4(%r11,%r13,4)
movq %r13, 0x8(%rsi,%r13,8)
movl %r10d, 0x4(%r14,%r13,4)
movl %ebp, 0x4(%r15,%r13,4)
leal 0x2(%r13), %eax
movl %eax, 0x4(%r9,%r13,4)
incq %r13
addq $0x10, %rdi
movq %r12, %rax
addq %r13, %rax
jne 0x3e601
movl -0x4(%rsp), %eax
jmp 0x3e6dd
movl %esi, %eax
jmp 0x3e6dd
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq