// File: /workspace/github_cpp_makefile_star2_O2/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 0x1334f
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdx, %r9
movslq %esi, %r10
leaq 0x18(%rdi), %rdx
xorl %ebx, %ebx
incq %rbx
cmpq %r10, %rbx
jge 0x13218
movl -0x10(%rdx), %r8d
leaq 0x10(%rdx), %r11
cmpl (%rdx), %r8d
movq %r11, %rdx
jb 0x131fb
jmp 0x13345
movl %esi, -0x4(%rsp)
movsd 0x1d007c(%rip), %xmm1   # 0x1e32a0
movslq %ecx, %rdx
movl 0xc(%rdi), %r8d
movl $0x1, %eax
cmpq %r8, %rdx
jl 0x1323c
movl %ecx, %eax
xorl %edx, %edx
divl %r8d
movq (%r9), %rbx
movl %eax, (%rbx)
movq 0x8(%r9), %rsi
orq $-0x1, (%rsi)
movq 0x10(%r9), %r15
andl $0x0, (%r15)
movq 0x18(%r9), %r12
andl $0x0, (%r12)
divsd %xmm0, %xmm1
movq 0x20(%r9), %r9
movl $0x1, (%r9)
addq $0x1c, %rdi
xorl %ebp, %ebp
movabsq $0x1, %r13
xorl %r8d, %r8d
cmpq %r10, %r13
jge 0x13341
movl -0x8(%rdi), %edx
movl -0x4(%rdi), %eax
subl -0x18(%rdi), %edx
subl -0x14(%rdi), %eax
movl %edx, %r14d
subl %eax, %r14d
movl %r14d, %r11d
negl %r11d
cmovsl %r14d, %r11d
addl %r11d, %r8d
addl %eax, %ebp
cvtsi2sd %r8d, %xmm2
cvtsi2sd %ebp, %xmm3
movapd %xmm3, %xmm4
mulsd %xmm0, %xmm4
ucomisd %xmm4, %xmm2
ja 0x1333a
cmpl %eax, %edx
cmovll %edx, %eax
cmpl $0x20, %r11d
jb 0x132db
cvtsi2sd %r11d, %xmm4
cvtsi2sd %eax, %xmm5
mulsd %xmm0, %xmm5
ucomisd %xmm5, %xmm4
ja 0x1333a
cmpl %ecx, %eax
cmovgel %ecx, %eax
movslq %eax, %r14
movl (%rdi), %r11d
movl $0x1, %edx
cmpq %r11, %r14
jl 0x132f7
xorl %edx, %edx
divl %r11d
movl %eax, %edx
divsd %xmm3, %xmm2
cvtsi2sd %edx, %xmm3
mulsd %xmm2, %xmm3
mulsd %xmm1, %xmm3
cvttsd2si %xmm3, %eax
addl -0x4(%rbx,%r13,4), %edx
subl %eax, %edx
movl %edx, (%rbx,%r13,4)
leaq -0x1(%r13), %rax
movq %rax, (%rsi,%r13,8)
movl %r8d, (%r15,%r13,4)
movl %ebp, (%r12,%r13,4)
leaq 0x1(%r13), %rax
movl %eax, (%r9,%r13,4)
addq $0x10, %rdi
movq %rax, %r13
jmp 0x1327c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x13345
movl -0x4(%rsp), %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq