// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Hash_Table.cpp
// Function: lchain_check(k_mer_hit*, int, Chain_Data*, double)
int32_t lchain_check(k_mer_hit *a, int32_t n_a, Chain_Data *dp, double bw_thres)
{
	int32_t i, tot_g = 0, sc, dg, dq, dr, dd, span;
	double bw_pen;
	if (n_a == 0) return -1;
    if (n_a > 1) {
        if ((a[0].self_offset >= a[n_a-1].self_offset)||(a[0].offset == a[n_a-1].offset)) return -1;
        dq = (int32_t)a[n_a-1].self_offset - (int32_t)a[0].self_offset;
        dr = (int32_t)a[n_a-1].offset - (int32_t)a[0].offset;
        dd = ((dq>=dr)? (dq-dr): (dr-dq));//gap
        dg = ((dq>=dr)? (dr): (dq));///len
        if (dg == 0 || dd > (dg*bw_thres)) return -1;
    }

	for (i = 1; i < n_a; ++i) {///a[] is sorted by offset, instead of self_offset; but offset might be equal
        if(a[i-1].self_offset >= a[i].self_offset) break;
        if(a[i-1].offset == a[i].offset) break;
    }
	if (i < n_a) return -1;

	bw_pen = 1.0 / bw_thres;
    dp->score[0] = normal_w((a[0].cnt&(0xffu)), (a[0].cnt>>8)); dp->pre[0] = -1;
	for (i = 1; i < n_a; ++i) {
        dq = (int32_t)a[i].self_offset - (int32_t)a[i-1].self_offset;
		dr = (int32_t)a[i].offset - (int32_t)a[i-1].offset;
		dd = ((dq>=dr)? (dq-dr): (dr-dq));//gap
        dg = ((dq>=dr)? (dr): (dq));///len
        if(dg == 0) break;
		
		tot_g += dd;
		if (dd > THRESHOLD_MAX_SIZE && dd > (dg*bw_thres)) break;
        span = a[i].cnt&(0xffu);
		sc = dg < span? dg : span;
        sc = normal_w(sc, ((int32_t)(a[i].cnt>>8)));
		sc -= (int32_t)((((double)dd)/((double)dg))*bw_pen*((double)sc));///bw_pen is 20 for HiFi

		dp->score[i] = dp->score[i-1] + sc;
		dp->pre[i] = i - 1;
	}
	if (i < n_a) return -1;

    if(n_a > 1) {
        dq = (int32_t)a[n_a-1].self_offset - (int32_t)a[0].self_offset;
        dr = (int32_t)a[n_a-1].offset - (int32_t)a[0].offset;
        dg = ((dq>=dr)? (dr): (dq));///len
        dd = tot_g;///gap
        if (dd > (dg*bw_thres)) return -1;
    }
	return n_a;
}

// Assembly:
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testl %esi, %esi
je 0x1403b
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl %esi, %ebx
cmpl $0x2, %esi
jl 0x13ec2
movq %rbx, %rax
shlq $0x4, %rax
movl -0x8(%rdi,%rax), %r9d
subl 0x8(%rdi), %r9d
jbe 0x14031
addq %rdi, %rax
movl -0xc(%rax), %eax
subl 0x4(%rdi), %eax
je 0x14031
movl %r9d, %r8d
subl %eax, %r8d
cmovll %r9d, %eax
testl %eax, %eax
je 0x14031
movl %r8d, %r9d
negl %r9d
cmovsl %r8d, %r9d
cvtsi2sd %r9d, %xmm1
cvtsi2sd %eax, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
ja 0x14031
movslq %esi, %r9
leaq 0x18(%rdi), %r10
xorl %eax, %eax
incq %rax
cmpq %r9, %rax
jge 0x13ef6
movl -0x10(%r10), %r8d
cmpl (%r10), %r8d
jae 0x14031
movl -0x14(%r10), %r8d
leaq 0x10(%r10), %r11
cmpl -0x4(%r10), %r8d
movq %r11, %r10
jne 0x13ecb
jmp 0x14031
movq %rbx, -0x8(%rsp)
movsd 0x1cf39d(%rip), %xmm1   # 0x1e32a0
movl 0xc(%rdi), %eax
movzbl %al, %ebx
movl %eax, %r8d
shrl $0x8, %r8d
movl $0x1, %r10d
movl %r10d, %r11d
cmpl %r8d, %ebx
jb 0x13f28
movzbl %al, %eax
divb %r8b
movzbl %al, %r11d
movq (%rdx), %rbx
movl %r11d, (%rbx)
movq 0x8(%rdx), %r14
orq $-0x1, (%r14)
divsd %xmm0, %xmm1
leaq 0x1c(%rdi), %r15
xorl %edx, %edx
movabsq $0x1, %r12
cmpq %r9, %r12
jge 0x13ff7
movl -0x4(%r15), %r8d
subl -0x14(%r15), %r8d
movl -0x8(%r15), %eax
subl -0x18(%r15), %eax
movl %r8d, %r13d
subl %eax, %r13d
cmovll %r8d, %eax
movl %r13d, %ebp
negl %ebp
cmovsl %r13d, %ebp
testl %eax, %eax
je 0x14031
cvtsi2sd %ebp, %xmm2
cvtsi2sd %eax, %xmm3
cmpl $0x20, %ebp
jb 0x13f9d
movapd %xmm3, %xmm4
mulsd %xmm0, %xmm4
ucomisd %xmm4, %xmm2
ja 0x14031
movl (%r15), %r13d
movzbl %r13b, %r8d
cmpl %r8d, %eax
cmovll %eax, %r8d
shrl $0x8, %r13d
movl %r10d, %eax
cmpl %r13d, %r8d
jl 0x13fc1
movzbl %r8b, %eax
divb %r13b
movzbl %al, %eax
divsd %xmm3, %xmm2
cvtsi2sd %eax, %xmm3
mulsd %xmm1, %xmm2
mulsd %xmm2, %xmm3
cvttsd2si %xmm3, %r8d
addl %ebp, %edx
addl %eax, %r11d
subl %r8d, %r11d
movl %r11d, (%rbx,%r12,4)
leaq -0x1(%r12), %rax
movq %rax, (%r14,%r12,8)
addq $0x10, %r15
incq %r12
jmp 0x13f4a
cmpl $0x2, %esi
jl 0x1402f
movq -0x8(%rsp), %r8
shlq $0x4, %r8
movl -0xc(%rdi,%r8), %eax
movl -0x8(%rdi,%r8), %r8d
subl 0x8(%rdi), %r8d
subl 0x4(%rdi), %eax
cmpl %eax, %r8d
cmovll %r8d, %eax
cvtsi2sd %edx, %xmm1
cvtsi2sd %eax, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
ja 0x14031
movl %esi, %ecx
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
movl %ecx, %eax
retq