// File: /workspace/github_cpp_makefile_star2_O1/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 0x3f282
movq %rdx, %r9
cmpl $0x2, %esi
jl 0x3f116
movl %esi, %r10d
shlq $0x4, %r10
movl $0x10, %edx
movl -0x8(%rdi,%rdx), %r8d
cmpl 0x8(%rdi,%rdx), %r8d
jae 0x3f282
addq $0x10, %rdx
cmpq %rdx, %r10
jne 0x3f0fd
movslq %ecx, %rdx
movl 0xc(%rdi), %r8d
movl $0x1, %eax
cmpq %r8, %rdx
jl 0x3f12f
movq %rdx, %rax
xorl %edx, %edx
divq %r8
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq (%r9), %r10
movl %eax, (%r10)
movq 0x8(%r9), %rax
movq $-0x1, (%rax)
movq 0x10(%r9), %rbx
xorl %r14d, %r14d
movl %r14d, (%rbx)
movq 0x18(%r9), %r15
movl %r14d, (%r15)
movq 0x20(%r9), %r9
movl $0x1, (%r9)
cmpl $0x2, %esi
jl 0x3f26f
movq %rax, %rdx
movsd 0x1e60f6(%rip), %xmm1   # 0x225270
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, %r11d
subl %eax, %r11d
movl %r11d, %r8d
negl %r8d
cmovsl %r11d, %r8d
addl %r8d, %r14d
addl %eax, %ebp
xorps %xmm2, %xmm2
cvtsi2sd %r14d, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %ebp, %xmm3
movapd %xmm3, %xmm4
mulsd %xmm0, %xmm4
ucomisd %xmm4, %xmm2
ja 0x3f273
cmpl %eax, %edx
cmovll %edx, %eax
cmpl $0x20, %r8d
jb 0x3f1ff
xorps %xmm4, %xmm4
cvtsi2sd %r8d, %xmm4
xorps %xmm5, %xmm5
cvtsi2sd %eax, %xmm5
mulsd %xmm0, %xmm5
ucomisd %xmm5, %xmm4
ja 0x3f273
cmpl %ecx, %eax
cmovgel %ecx, %eax
movslq %eax, %rdx
movl (%rdi), %r8d
movl $0x1, %eax
cmpq %r8, %rdx
jl 0x3f21c
movq %rdx, %rax
xorl %edx, %edx
divq %r8
divsd %xmm3, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %eax, %xmm3
mulsd %xmm2, %xmm3
mulsd %xmm1, %xmm3
cvttsd2si %xmm3, %edx
addl (%r10,%r13,4), %eax
subl %edx, %eax
movl %eax, 0x4(%r10,%r13,4)
movq %r13, 0x8(%rsi,%r13,8)
movl %r14d, 0x4(%rbx,%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 0x3f199
movl -0x4(%rsp), %eax
jmp 0x3f278
movl %esi, %eax
jmp 0x3f278
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq