// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Hash_Table.cpp
// Function: quick_ck_lchain(k_mer_hit*, long, long, long, double, double, double, long*, long*, int*, int*, long*, long*, long*, long*, long*, long*)
void quick_ck_lchain(k_mer_hit* a, int64_t a_n, int64_t xl, int64_t yl, double chn_pen_gap, double chn_pen_skip, double bw_rate, 
int64_t *p, int64_t *t, int32_t *f, int32_t *ii, int64_t *plus, int64_t *msc, int64_t *msc_i, int64_t *movl, int64_t *si, int64_t *ei)
{
    if(a_n <= 0) return;
    int64_t l, k, is_srt = 1, z; k_mer_hit *ai, *aj;
    int64_t dq, dr, dd, dg, q_span, sc, csc, ddt; 
    int64_t plus0, msc0, msc_i0, movl0; double lin_pen, a_pen;

    *plus = 0; *msc = *msc_i = INT32_MIN; *movl = INT32_MAX; *si = 0; *ei = a_n; 

    for (k = 1, l = 0; k <= a_n; k++) {
        if(k == a_n || a[k].strand != a[l].strand) {
            t[k-1] = 0; ii[k-1] = 0;
            // if(a_n && a[0].readID == 3125488) {
            //     fprintf(stderr, "[M::%s::] ii::[%ld,%ld)(%c), is_srt::%ld, chn_pen_gap::%f, chn_pen_skip::%f, bw_rate::%f\n", __func__, l, k, "+-"[a[l].strand], is_srt, chn_pen_gap, chn_pen_skip, bw_rate);
            // }
            if(is_srt) {
                plus0 = 0; msc0 = msc_i0 = INT32_MIN; movl0 = INT32_MAX; ddt = 0;


                p[l] = -1; f[l] = a[l].cnt&(0xffu);
                if(f[l] >= msc0) {msc0 = f[l]; msc_i0 = l;}///difference
                if(f[l] < plus0) plus0 = f[l];


                for (z = l + 1; z < k; z++) {
                    ///roughly same to comput_sc_ch(&a[z], &a[z-1])
                    ai = &a[z]; aj = &a[z-1];
                    dq = (int64_t)(ai->self_offset) - (int64_t)(aj->self_offset);
                    if(dq <= 0) break;
                    dr = (int64_t)(ai->offset) - (int64_t)(aj->offset);
                    if(dr <= 0) break;
                    dd = dr > dq? dr - dq : dq - dr;//gap
                    // if(a_n && a[0].readID == 3125488) {
                    //     fprintf(stderr, "%ld,", dd);
                    // }
                    if((dd > 16) && (dd > cal_bw(&(a[z]), &(a[z-1]), bw_rate, xl, yl))) break;
                    dg = dr < dq? dr : dq;//len
                    q_span = ai->cnt&(0xffu); 
                    sc = q_span < dg? q_span : dg;
                    sc = normal_w(sc, ((int32_t)(ai->cnt>>8)));
                    if (dd || (dg > q_span && dg > 0)) {
                        lin_pen = (chn_pen_gap*(double)dd);
                        a_pen = ((double)(sc))*((((double)dd)/((double)dg))/bw_rate);
                        ///for long gap
                        // if(lin_pen > a_pen) lin_pen = a_pen;
                        if(dd < 4) lin_pen = ((lin_pen > a_pen)?(a_pen):(lin_pen));
                        else lin_pen = ((lin_pen < a_pen)?(a_pen):(lin_pen));
                        lin_pen += (chn_pen_skip*(double)dg);
                        sc -= (int32_t)lin_pen;
                    }

                    sc += f[z-1]; csc = a[z].cnt&(0xffu); if(sc < csc) break;
                    p[z] = z - 1; f[z] = sc; ddt += dd; 

                    if(f[z] >= msc0) {msc0 = f[z]; msc_i0 = z;}///difference
                    if(f[z] < plus0) plus0 = f[z];
                }

                // if(a_n && a[0].readID == 3125488) {
                //     fprintf(stderr, "\n");
                //     fprintf(stderr, "[M::%s::] msc0::%ld, msc_i0::%ld, (%c)\n", __func__, msc0, msc_i0, "+-"[a[l].strand]);
                // }
                if((z >= k) && (msc_i0 == (k - 1))) {
                    if((k - l >= 2) && (ddt > 16) && (ddt > cal_bw(&(a[k-1]), &(a[l]), bw_rate, xl, yl))) msc_i0 = INT32_MIN;
                    if(msc_i0 == (k - 1)) {
                        if(msc0 >= (*msc)) {
                            movl0 = get_chainLen(a[msc_i0].self_offset, a[msc_i0].self_offset, xl, a[msc_i0].offset, a[msc_i0].offset, yl);
                            if(msc0 > (*msc) || movl0 < (*movl)) {
                                *msc = msc0; *msc_i = msc_i0; *movl = movl0;
                            }
                        }
                        if(plus0 < (*plus)) *plus = plus0;
                        if((*ei) > k) {
                            (*si) = k;
                        } else {
                            (*ei) = l;
                        }
                    }
                }
            }
            l = k; is_srt = 1;
        } else {
            if((a[k].self_offset <= a[k-1].self_offset) || (a[k].offset <= a[k-1].offset)) is_srt = 0;
            t[k-1] = 0; ii[k-1] = 0;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r8, 0x40(%rsp)
movsd %xmm2, 0x28(%rsp)
movsd %xmm1, 0x80(%rsp)
movsd %xmm0, 0x78(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
testq %rsi, %rsi
jle 0x1608b
movq %r9, %rbx
movq %rsi, %r15
movq %rdi, %r12
movq 0x108(%rsp), %rcx
movq 0x100(%rsp), %rdx
movq 0xf8(%rsp), %rsi
movq 0xf0(%rsp), %rdi
movq 0xe8(%rsp), %r8
movq 0xe0(%rsp), %rax
andq $0x0, (%rax)
movq 0xd8(%rsp), %r13
movq $-0x80000000, %rax      # imm = 0x80000000
movq %rax, (%rdi)
movq %rax, (%r8)
movq $0x7fffffff, (%rsi)     # imm = 0x7FFFFFFF
andq $0x0, (%rdx)
movq 0xd0(%rsp), %rdi
movq %r15, (%rcx)
leaq 0x1(%r15), %rbp
leaq 0x10(%r12), %rax
movq %rax, 0x48(%rsp)
pushq $0x1
popq %r9
xorl %r10d, %r10d
movq %r9, %rax
movq %rbx, 0x68(%rsp)
movq %r15, 0x60(%rsp)
movq %r12, 0x58(%rsp)
movq %rbp, 0x50(%rsp)
cmpq %rbp, %r9
je 0x1608b
cmpq %r15, %r9
je 0x15d1d
movq %r9, %rcx
shlq $0x4, %rcx
movq %r10, %rdx
shlq $0x4, %rdx
movl (%r12,%rdx), %edx
xorl (%r12,%rcx), %edx
js 0x15d1d
leaq (%r12,%rcx), %rdx
movl 0x8(%rdx), %esi
cmpl -0x8(%rcx,%r12), %esi
jbe 0x15d07
addq %r12, %rcx
addq $-0x10, %rcx
movl 0x4(%rdx), %edx
cmpl 0x4(%rcx), %edx
ja 0x15d09
xorl %eax, %eax
andq $0x0, -0x8(%rbx,%r9,8)
andl $0x0, -0x4(%r13,%r9,4)
movq %r10, %rcx
jmp 0x16080
andq $0x0, -0x8(%rbx,%r9,8)
andl $0x0, -0x4(%r13,%r9,4)
movq %r9, %rcx
testq %rax, %rax
pushq $0x1
popq %rax
je 0x16080
movq 0x40(%rsp), %rax
orq $-0x1, (%rax,%r10,8)
leaq -0x1(%r9), %rax
movq %rax, 0x8(%rsp)
movq %r10, %rbx
shlq $0x4, %rbx
leaq (%r12,%rbx), %rax
movq %rax, 0x70(%rsp)
movzbl 0xc(%r12,%rbx), %r11d
movl %r11d, (%rdi,%r10,4)
addq 0x48(%rsp), %rbx
movq %r10, %r8
xorl %r14d, %r14d
xorl %r12d, %r12d
movq %r10, 0x38(%rsp)
movq %r9, 0x20(%rsp)
leaq 0x1(%r8), %rax
movq %rax, (%rsp)
cmpq %r9, %rax
jge 0x15f05
movl -0x8(%rbx), %eax
movl 0x8(%rbx), %r13d
subq %rax, %r13
jle 0x15f05
movl -0xc(%rbx), %eax
movl 0x4(%rbx), %r15d
subq %rax, %r15
jle 0x15f05
movq %r13, %rax
subq %r15, %rax
negq %rax
movq %r13, %rbp
subq %r15, %rbp
cmovbq %rax, %rbp
cmpq $0x11, %rbp
jl 0x15e31
movq %r8, 0x88(%rsp)
leaq -0x10(%rbx), %rsi
movq %rbx, %rdi
movsd 0x28(%rsp), %xmm0
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movq %r14, 0x90(%rsp)
movq %r12, 0x30(%rsp)
movq %r11, %r12
movq %r10, %r14
callq 0x17e05
movq 0x88(%rsp), %r8
movq %r14, %r10
movq %r12, %r11
movq 0x30(%rsp), %r12
movq 0x90(%rsp), %r14
movq 0x20(%rsp), %r9
movq 0xd0(%rsp), %rdi
cltq
cmpq %rax, %rbp
jg 0x15f05
cmpq %r13, %r15
cmovbq %r15, %r13
movl 0xc(%rbx), %edx
movzbl %dl, %ecx
cmpq %rcx, %r13
movq %rcx, %rsi
cmovbq %r13, %rsi
shrl $0x8, %edx
pushq $0x1
popq %rax
cmpq %rdx, %rsi
jb 0x15e5c
movzbl %sil, %eax
divb %dl
movzbl %al, %eax
testq %rbp, %rbp
jne 0x15e66
cmpq %rcx, %r13
jbe 0x15ec5
cvtsi2sd %rbp, %xmm0
movapd %xmm0, %xmm2
mulsd 0x78(%rsp), %xmm2
cvtsi2sd %eax, %xmm3
cvtsi2sd %r13, %xmm1
divsd %xmm1, %xmm0
divsd 0x28(%rsp), %xmm0
mulsd %xmm3, %xmm0
xorl %edx, %edx
ucomisd %xmm2, %xmm0
seta %dl
xorl %esi, %esi
ucomisd %xmm0, %xmm2
seta %sil
cmpq $0x4, %rbp
cmovll %esi, %edx
testb %dl, %dl
jne 0x15eae
movapd %xmm2, %xmm0
mulsd 0x80(%rsp), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %edx
movslq %edx, %rdx
subq %rdx, %rax
movslq (%rdi,%r8,4), %rdx
addq %rax, %rdx
cmpq %rcx, %rdx
jl 0x15f05
movq 0x40(%rsp), %rax
movq %r8, 0x8(%rax,%r8,8)
addq %rbp, %r14
movslq %edx, %rax
cmpq %r11, %rax
cmovgq %rax, %r11
movl %edx, 0x4(%rdi,%r8,4)
movq (%rsp), %r8
cmovgeq %r8, %r10
cmpq %rax, %r12
cmovgeq %rax, %r12
addq $0x10, %rbx
jmp 0x15d7d
movq %r12, 0x30(%rsp)
pushq $0x1
popq %rax
movq %r9, %rcx
cmpq %r9, (%rsp)
movq 0x68(%rsp), %rbx
movq 0x60(%rsp), %r15
movq 0x58(%rsp), %r12
movq 0xd8(%rsp), %r13
movq 0x50(%rsp), %rbp
movq 0x38(%rsp), %rdx
jl 0x16080
movq %r9, %rcx
cmpq 0x8(%rsp), %r10
jne 0x16080
movq %r9, %rax
subq %rdx, %rax
cmpq $0x2, %rax
jl 0x15fbe
cmpq $0x11, %r14
jl 0x15fbe
movq 0x8(%rsp), %rdi
shlq $0x4, %rdi
addq %r12, %rdi
movq 0x70(%rsp), %rsi
movsd 0x28(%rsp), %xmm0
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movq %r11, (%rsp)
callq 0x17e05
movq (%rsp), %r11
movq 0x20(%rsp), %r9
pushq $0x1
popq %r8
movq 0xd0(%rsp), %rdi
cltq
cmpq %rax, %r14
setg %dl
cmpq $-0x80000000, 0x8(%rsp) # imm = 0x80000000
setne %sil
movq %r8, %rax
movq %r9, %rcx
testb %sil, %dl
jne 0x16080
movq 0xe8(%rsp), %rax
movq (%rax), %r14
cmpq %r14, %r11
jl 0x16043
movq 0x8(%rsp), %rax
shlq $0x4, %rax
movl 0x4(%r12,%rax), %ecx
movl 0x8(%r12,%rax), %edi
movq %rdi, %rsi
movq 0x10(%rsp), %rdx
movq %rcx, %r8
movq 0x18(%rsp), %r9
movq %r11, (%rsp)
callq 0x12ff0
movq (%rsp), %rdx
movq 0x20(%rsp), %r9
movq 0xd0(%rsp), %rdi
cmpq %r14, %rdx
jg 0x1601d
movq 0xf8(%rsp), %rcx
cmpq (%rcx), %rax
jge 0x16043
movq 0xe8(%rsp), %rcx
movq %rdx, (%rcx)
movq 0xf0(%rsp), %rcx
movq 0x8(%rsp), %rdx
movq %rdx, (%rcx)
movq 0xf8(%rsp), %rcx
movq %rax, (%rcx)
movq 0xe0(%rsp), %rax
movq 0x30(%rsp), %rcx
cmpq (%rax), %rcx
jge 0x16058
movq %rcx, (%rax)
movq 0x108(%rsp), %rax
cmpq %r9, (%rax)
movq 0x38(%rsp), %rcx
jle 0x16077
movq 0x100(%rsp), %rax
movq %r9, (%rax)
jmp 0x1607a
movq %rcx, (%rax)
pushq $0x1
popq %rax
movq %r9, %rcx
incq %r9
movq %rcx, %r10
jmp 0x15cc4
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq