// File: /workspace/github_cpp_makefile_star2_O3/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 0x3f792
cmpl $0x2, %esi
jl 0x3f63c
movl %esi, %eax
movq %rax, %r8
shlq $0x4, %r8
movl -0x8(%rdi,%r8), %r9d
subl 0x8(%rdi), %r9d
jbe 0x3f792
addq %rdi, %r8
movl -0xc(%r8), %r8d
subl 0x4(%rdi), %r8d
je 0x3f792
movl %r9d, %r10d
subl %r8d, %r10d
cmovll %r9d, %r8d
testl %r8d, %r8d
je 0x3f792
movl %r10d, %r9d
negl %r9d
cmovsl %r10d, %r9d
cvtsi2sd %r9d, %xmm1
cvtsi2sd %r8d, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
ja 0x3f792
shlq $0x4, %rax
movl $0x10, %r8d
movl -0x8(%rdi,%r8), %r9d
cmpl 0x8(%rdi,%r8), %r9d
jae 0x3f792
movl -0xc(%rdi,%r8), %r9d
cmpl 0x4(%rdi,%r8), %r9d
je 0x3f792
addq $0x10, %r8
cmpq %r8, %rax
jne 0x3f613
movl 0xc(%rdi), %eax
movzbl %al, %r10d
movl %eax, %r9d
shrl $0x8, %r9d
movl $0x1, %r8d
cmpl %r9d, %r10d
jb 0x3f65f
movzbl %al, %eax
divb %r9b
movzbl %al, %r8d
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq (%rdx), %r9
movl %r8d, (%r9)
movq 0x8(%rdx), %r10
movq $-0x1, (%r10)
cmpl $0x2, %esi
jl 0x3f786
movsd 0x1f1c85(%rip), %xmm1   # 0x231310
divsd %xmm0, %xmm1
movl %esi, %edx
leaq 0x1c(%rdi), %r11
leaq -0x1(%rdx), %rbx
xorl %r14d, %r14d
xorl %ebp, %ebp
movl -0x4(%r11), %r15d
subl -0x14(%r11), %r15d
movl -0x8(%r11), %eax
subl -0x18(%r11), %eax
movl %r15d, %r12d
subl %eax, %r12d
cmovll %r15d, %eax
movl %r12d, %r15d
negl %r15d
cmovsl %r12d, %r15d
testl %eax, %eax
je 0x3f788
xorps %xmm2, %xmm2
cvtsi2sd %r15d, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %eax, %xmm3
cmpl $0x20, %r15d
jb 0x3f6f1
movapd %xmm3, %xmm4
mulsd %xmm0, %xmm4
ucomisd %xmm4, %xmm2
ja 0x3f788
movl (%r11), %r12d
movzbl %r12b, %r13d
cmpl %r13d, %eax
cmovll %eax, %r13d
shrl $0x8, %r12d
movl $0x1, %eax
cmpl %r12d, %r13d
jl 0x3f717
movzbl %r13b, %eax
divb %r12b
movzbl %al, %eax
divsd %xmm3, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %eax, %xmm3
mulsd %xmm1, %xmm2
mulsd %xmm2, %xmm3
cvttsd2si %xmm3, %r12d
addl %r15d, %ebp
addl %eax, %r8d
subl %r12d, %r8d
movl %r8d, 0x4(%r9,%r14,4)
movq %r14, 0x8(%r10,%r14,8)
incq %r14
addq $0x10, %r11
cmpq %r14, %rbx
jne 0x3f69e
cmpl $0x2, %esi
jl 0x3f786
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
shlq $0x4, %rdx
movl -0x8(%rdi,%rdx), %eax
subl 0x8(%rdi), %eax
movl -0xc(%rdi,%rdx), %edx
subl 0x4(%rdi), %edx
cmpl %edx, %eax
cmovll %eax, %edx
xorps %xmm2, %xmm2
cvtsi2sd %edx, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
ja 0x3f788
movl %esi, %ecx
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
movl %ecx, %eax
retq