// File: 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:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movsd %xmm0, -0x28(%rbp)
movl $0x0, -0x30(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x2332d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x237a0
cmpl $0x1, -0x14(%rbp)
jle 0x2342c
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
movl -0x14(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
jae 0x23377
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
movl -0x14(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
jne 0x23383
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x237a0
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
subl 0x8(%rcx), %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
subl 0x4(%rcx), %eax
movl %eax, -0x40(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jl 0x233d8
movl -0x3c(%rbp), %eax
subl -0x40(%rbp), %eax
movl %eax, -0x54(%rbp)
jmp 0x233e1
movl -0x40(%rbp), %eax
subl -0x3c(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jl 0x233f7
movl -0x40(%rbp), %eax
movl %eax, -0x58(%rbp)
jmp 0x233fd
movl -0x3c(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
je 0x2341e
cvtsi2sdl -0x44(%rbp), %xmm0
cvtsi2sdl -0x38(%rbp), %xmm1
mulsd -0x28(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x2342a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x237a0
jmp 0x2342c
movl $0x1, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x234a2
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
jb 0x23468
jmp 0x234a2
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
jne 0x23495
jmp 0x234a2
jmp 0x23497
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x23433
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x234b6
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x237a0
movsd 0x35eb42(%rip), %xmm0   # 0x382000
divsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
andl $0xff, %eax
movq -0x10(%rbp), %rcx
movl 0xc(%rcx), %ecx
shrl $0x8, %ecx
cmpl %ecx, %eax
jb 0x23501
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
andl $0xff, %eax
movq -0x10(%rbp), %rcx
movl 0xc(%rcx), %ecx
shrl $0x8, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, -0x5c(%rbp)
jmp 0x2350b
movl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x2350b
movl -0x5c(%rbp), %ecx
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq $-0x1, (%rax)
movl $0x1, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x236f8
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
movl -0x2c(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
subl 0x8(%rcx), %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
movl -0x2c(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
subl 0x4(%rcx), %eax
movl %eax, -0x40(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jl 0x235a4
movl -0x3c(%rbp), %eax
subl -0x40(%rbp), %eax
movl %eax, -0x60(%rbp)
jmp 0x235ad
movl -0x40(%rbp), %eax
subl -0x3c(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jl 0x235c3
movl -0x40(%rbp), %eax
movl %eax, -0x64(%rbp)
jmp 0x235c9
movl -0x3c(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %eax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jne 0x235da
jmp 0x236f8
movl -0x44(%rbp), %eax
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
cmpl $0x1f, -0x44(%rbp)
jle 0x23603
cvtsi2sdl -0x44(%rbp), %xmm0
cvtsi2sdl -0x38(%rbp), %xmm1
mulsd -0x28(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x23603
jmp 0x236f8
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0xff, %eax
movl %eax, -0x48(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x48(%rbp), %eax
jge 0x2362d
movl -0x38(%rbp), %eax
movl %eax, -0x68(%rbp)
jmp 0x23633
movl -0x48(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x10(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
shrl $0x8, %ecx
cmpl %ecx, %eax
jl 0x23675
movl -0x34(%rbp), %eax
movq -0x10(%rbp), %rcx
movslq -0x2c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
shrl $0x8, %ecx
cltd
idivl %ecx
movl %eax, -0x6c(%rbp)
jmp 0x2367f
movl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x2367f
movl -0x6c(%rbp), %eax
movl %eax, -0x34(%rbp)
cvtsi2sdl -0x44(%rbp), %xmm0
cvtsi2sdl -0x38(%rbp), %xmm1
divsd %xmm1, %xmm0
mulsd -0x50(%rbp), %xmm0
cvtsi2sdl -0x34(%rbp), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movl -0x34(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x34(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
addl -0x34(%rbp), %edx
movq -0x20(%rbp), %rax
movq (%rax), %rax
movslq -0x2c(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x2c(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rdx
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x2c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x2352d
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x2370c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x237a0
cmpl $0x1, -0x14(%rbp)
jle 0x2379a
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
subl 0x8(%rcx), %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
subl 0x4(%rcx), %eax
movl %eax, -0x40(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jl 0x23768
movl -0x40(%rbp), %eax
movl %eax, -0x70(%rbp)
jmp 0x2376e
movl -0x3c(%rbp), %eax
movl %eax, -0x70(%rbp)
movl -0x70(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x30(%rbp), %eax
movl %eax, -0x44(%rbp)
cvtsi2sdl -0x44(%rbp), %xmm0
cvtsi2sdl -0x38(%rbp), %xmm1
mulsd -0x28(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x23798
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x237a0
jmp 0x2379a
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
popq %rbp
retq
nopw %cs:(%rax,%rax)