// File: /workspace/github_cpp_makefile_star2_O1/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 0x4003f
movq %rdx, %r8
cmpl $0x2, %esi
jl 0x3feb2
movl %esi, %eax
shlq $0x4, %rax
movl -0x8(%rdi,%rax), %edx
subl 0x8(%rdi), %edx
jbe 0x4003f
addq %rdi, %rax
movl -0xc(%rax), %eax
subl 0x4(%rdi), %eax
je 0x4003f
movl %edx, %r9d
subl %eax, %r9d
cmovll %edx, %eax
testl %eax, %eax
je 0x4003f
movl %r9d, %edx
negl %edx
cmovsl %r9d, %edx
cvtsi2sd %edx, %xmm1
cvtsi2sd %eax, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
ja 0x4003f
cmpl $0x2, %esi
jl 0x3feeb
movl %esi, %eax
shlq $0x4, %rax
movl $0x10, %edx
movl -0x8(%rdi,%rdx), %r9d
cmpl 0x8(%rdi,%rdx), %r9d
jae 0x4003f
movl -0xc(%rdi,%rdx), %r9d
cmpl 0x4(%rdi,%rdx), %r9d
je 0x4003f
addq $0x10, %rdx
cmpq %rdx, %rax
jne 0x3fec2
movl 0xc(%rdi), %r9d
movzbl %r9b, %edx
shrl $0x8, %r9d
movl $0x1, %eax
cmpl %r9d, %edx
jb 0x3ff08
movl %edx, %eax
xorl %edx, %edx
divl %r9d
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq (%r8), %r9
movl %eax, (%r9)
movq 0x8(%r8), %r8
movq $-0x1, (%r8)
xorpd %xmm1, %xmm1
cmpl $0x2, %esi
setge %al
jl 0x40002
movsd 0x1e5337(%rip), %xmm1   # 0x225270
divsd %xmm0, %xmm1
movl %esi, %r11d
leaq 0x1c(%rdi), %rbx
leaq -0x1(%r11), %r14
xorl %r15d, %r15d
xorl %r10d, %r10d
movl -0x4(%rbx), %edx
subl -0x14(%rbx), %edx
movl -0x8(%rbx), %ebp
subl -0x18(%rbx), %ebp
movl %edx, %r12d
subl %ebp, %r12d
cmovll %edx, %ebp
movl %r12d, %edx
negl %edx
cmovsl %r12d, %edx
testl %ebp, %ebp
je 0x3fffa
xorps %xmm2, %xmm2
cvtsi2sd %edx, %xmm2
addl %edx, %r10d
xorps %xmm3, %xmm3
cvtsi2sd %ebp, %xmm3
cmpl $0x20, %edx
jb 0x3ff98
movapd %xmm3, %xmm4
mulsd %xmm0, %xmm4
ucomisd %xmm4, %xmm2
ja 0x3fffa
movl (%rbx), %r12d
movzbl %r12b, %edx
cmpl %edx, %ebp
cmovll %ebp, %edx
shrl $0x8, %r12d
movl $0x1, %eax
cmpl %r12d, %edx
jl 0x3ffb9
movl %edx, %eax
xorl %edx, %edx
divl %r12d
divsd %xmm3, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %eax, %xmm3
mulsd %xmm1, %xmm2
mulsd %xmm2, %xmm3
cvttsd2si %xmm3, %edx
addl (%r9,%r15,4), %eax
subl %edx, %eax
movl %eax, 0x4(%r9,%r15,4)
movq %r15, 0x8(%r8,%r15,8)
leaq 0x2(%r15), %rax
cmpq %r11, %rax
setb %al
incq %r15
addq $0x10, %rbx
cmpq %r15, %r14
jne 0x3ff4e
xorps %xmm1, %xmm1
cvtsi2sd %r10d, %xmm1
testb $0x1, %al
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jne 0x4003f
cmpl $0x2, %esi
jl 0x4003d
movl %esi, %eax
shlq $0x4, %rax
movl -0x8(%rdi,%rax), %edx
subl 0x8(%rdi), %edx
movl -0xc(%rdi,%rax), %eax
subl 0x4(%rdi), %eax
cmpl %eax, %edx
cmovll %edx, %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
ja 0x4003f
movl %esi, %ecx
movl %ecx, %eax
retq