// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rcx, -0x38(%rsp)
movq %rdx, -0x40(%rsp)
testq %rsi, %rsi
jle 0x42874
movq 0x70(%rsp), %rax
movq 0x68(%rsp), %rcx
movq 0x60(%rsp), %rdx
movq 0x58(%rsp), %r10
movq 0x50(%rsp), %r11
movq 0x48(%rsp), %rbx
movq 0x40(%rsp), %r14
xorl %r15d, %r15d
movq %r15, (%rbx)
movq $-0x80000000, %rbx      # imm = 0x80000000
movq %rbx, (%r10)
movq %rbx, (%r11)
movq $0x7fffffff, (%rdx)     # imm = 0x7FFFFFFF
movq %r15, (%rcx)
movq %rsi, (%rax)
movq -0x40(%rsp), %rax
decq %rax
movq %rax, -0x18(%rsp)
leaq 0x1c(%rdi), %rax
movq %rax, -0x10(%rsp)
xorl %r11d, %r11d
movl $0x1, %r12d
movl $0x1, %eax
movq %r9, -0x60(%rsp)
movq %rsi, -0x50(%rsp)
movq %rdi, -0x58(%rsp)
cmpq %rsi, %r12
je 0x42491
movq %r12, %rcx
shlq $0x4, %rcx
movq %r11, %rdx
shlq $0x4, %rdx
movl (%rdi,%rdx), %edx
xorl (%rdi,%rcx), %edx
js 0x42491
leaq (%rdi,%rcx), %rdx
movl 0x8(%rdx), %r10d
cmpl -0x8(%rcx,%rdi), %r10d
jbe 0x42475
addq %rdi, %rcx
addq $-0x10, %rcx
movl 0x4(%rdx), %edx
cmpl 0x4(%rcx), %edx
ja 0x42477
xorl %eax, %eax
movq $0x0, -0x8(%r9,%r12,8)
movl $0x0, -0x4(%r14,%r12,4)
movq %rax, %r15
jmp 0x42860
movq $0x0, -0x8(%r9,%r12,8)
movl $0x0, -0x4(%r14,%r12,4)
movl $0x1, %r15d
testq %rax, %rax
je 0x42690
movq $-0x1, (%r8,%r11,8)
movq %r11, %rcx
shlq $0x4, %rcx
movzbl 0xc(%rdi,%rcx), %r14d
movq 0x38(%rsp), %rbx
movl %r14d, (%rbx,%r11,4)
leaq 0x1(%r11), %r9
cmpq %r12, %r9
movq %r11, -0x48(%rsp)
movq %rcx, -0x8(%rsp)
jge 0x42698
movq -0x10(%rsp), %rax
leaq (%rax,%rcx), %r10
movq %r11, %r13
xorl %ebp, %ebp
xorl %r15d, %r15d
movq %r12, -0x30(%rsp)
movl -0x14(%r10), %ecx
movl -0x4(%r10), %eax
movq %rax, %rdi
subq %rcx, %rdi
jle 0x426b1
movl -0x18(%r10), %edx
movl -0x8(%r10), %r11d
movq %r11, %rsi
subq %rdx, %rsi
jle 0x426b1
movq %r13, -0x28(%rsp)
movq %rbp, -0x20(%rsp)
movq %rsi, %rbp
subq %rdi, %rbp
movq %rbp, %r13
negq %r13
cmovsq %rbp, %r13
cmpq $0x11, %r13
jb 0x4259b
incl %eax
incl %r11d
movq -0x40(%rsp), %rbx
movq %rbx, %rbp
subq %rax, %rbp
movq -0x38(%rsp), %r12
subq %r11, %r12
movq %rdx, %r11
subq %rcx, %r11
cmpl %edx, %ecx
movl $0x0, %ecx
cmovbeq %rcx, %r11
addq %r12, %rax
cmpq %r12, %rbp
cmovleq %rbx, %rax
movq 0x38(%rsp), %rbx
addq %r11, %rax
xorps %xmm3, %xmm3
cvtsi2sd %rax, %xmm3
mulsd %xmm2, %xmm3
cvttsd2si %xmm3, %eax
cltq
cmpq %rax, %r13
jg 0x426a2
cmpq %rdi, %rsi
movq %rdi, %r12
cmovlq %rsi, %r12
movl (%r10), %ebp
movzbl %bpl, %ecx
cmpq %rcx, %r12
movq %rcx, %rdx
cmovlq %r12, %rdx
shrl $0x8, %ebp
movl $0x1, %eax
cmpq %rbp, %rdx
jl 0x425cb
movq %rdx, %rax
xorl %edx, %edx
divq %rbp
cmpq %rdi, %rsi
jne 0x425e1
cmpq %rcx, %r12
jg 0x425e1
movq -0x30(%rsp), %r12
movq -0x20(%rsp), %rbp
jmp 0x42648
xorps %xmm3, %xmm3
cvtsi2sd %r13, %xmm3
movapd %xmm3, %xmm5
mulsd %xmm0, %xmm5
cvtsi2sd %rax, %xmm6
xorps %xmm4, %xmm4
cvtsi2sd %r12, %xmm4
divsd %xmm4, %xmm3
divsd %xmm2, %xmm3
mulsd %xmm6, %xmm3
xorl %edx, %edx
ucomisd %xmm5, %xmm3
seta %dl
xorl %esi, %esi
ucomisd %xmm3, %xmm5
seta %sil
cmpq $0x4, %r13
cmovbl %esi, %edx
testb %dl, %dl
movq -0x20(%rsp), %rbp
jne 0x42631
movapd %xmm5, %xmm3
mulsd %xmm1, %xmm4
addsd %xmm3, %xmm4
cvttsd2si %xmm4, %edx
movslq %edx, %rdx
subq %rdx, %rax
movq -0x30(%rsp), %r12
movslq -0x4(%rbx,%r9,4), %rdx
addq %rax, %rdx
cmpq %rcx, %rdx
jl 0x426ac
leaq -0x1(%r9), %rax
addq %r13, %r15
movslq %edx, %rcx
cmpq %r14, %rcx
cmovgq %rcx, %r14
movq -0x28(%rsp), %r13
cmovgeq %r9, %r13
movq %rax, (%r8,%r9,8)
cmpq %rcx, %rbp
cmovgeq %rcx, %rbp
movl %edx, (%rbx,%r9,4)
incq %r9
addq $0x10, %r10
cmpq %r12, %r9
jl 0x424fd
jmp 0x426b1
movq %r12, %r11
jmp 0x42860
xorl %r15d, %r15d
xorl %ebp, %ebp
movq %r11, %r13
jmp 0x426b1
movq -0x30(%rsp), %r12
movq -0x20(%rsp), %rbp
movq -0x28(%rsp), %r13
cmpq %r12, %r9
jl 0x4281d
leaq -0x1(%r12), %rax
cmpq %rax, %r13
jne 0x4281d
movq %r12, %rcx
subq -0x48(%rsp), %rcx
cmpq $0x2, %rcx
jl 0x4275a
cmpq $0x11, %r15
jb 0x4275a
movq -0x58(%rsp), %r9
movq -0x8(%rsp), %rsi
addq %r9, %rsi
movq %rax, %rcx
shlq $0x4, %rcx
movl 0x4(%rsi), %edx
movl 0x8(%rsi), %esi
movl 0x4(%r9,%rcx), %edi
movl 0x8(%r9,%rcx), %ecx
incl %ecx
incl %edi
movq -0x40(%rsp), %r11
movq %r11, %r10
subq %rcx, %r10
movq -0x38(%rsp), %r9
subq %rdi, %r9
movq %rdx, %rdi
subq %rsi, %rdi
cmpl %edx, %esi
movl $0x0, %edx
cmovbeq %rdx, %rdi
addq %r9, %rcx
cmpq %r9, %r10
cmovleq %r11, %rcx
addq %rdi, %rcx
xorps %xmm3, %xmm3
cvtsi2sd %rcx, %xmm3
mulsd %xmm2, %xmm3
cvttsd2si %xmm3, %ecx
movslq %ecx, %rcx
cmpq %rcx, %r15
movq $-0x80000000, %rcx      # imm = 0x80000000
cmovgq %rcx, %r13
movq -0x48(%rsp), %rbx
movl $0x1, %r15d
cmpq %rax, %r13
jne 0x4283c
movq 0x50(%rsp), %rax
movq (%rax), %rax
cmpq %rax, %r14
movq -0x58(%rsp), %rdi
jl 0x427f2
movq %r13, %rcx
shlq $0x4, %rcx
movl 0x4(%rdi,%rcx), %edx
movl 0x8(%rdi,%rcx), %esi
movq %rdx, %rcx
subq %rsi, %rcx
incq %rcx
movq %rsi, %r11
notq %r11
addq -0x40(%rsp), %r11
movq %rdx, %r10
notq %r10
addq -0x38(%rsp), %r10
movq %rsi, %r9
addq %r10, %r9
cmpq %r10, %r11
cmovleq -0x18(%rsp), %r9
cmpl %edx, %esi
movl $0x1, %edx
cmovbeq %rdx, %rcx
addq %r9, %rcx
cmpq %rax, %r14
jg 0x427da
movq 0x60(%rsp), %rax
cmpq (%rax), %rcx
jge 0x427f2
movq 0x50(%rsp), %rax
movq %r14, (%rax)
movq 0x58(%rsp), %rax
movq %r13, (%rax)
movq 0x60(%rsp), %rax
movq %rcx, (%rax)
movq 0x48(%rsp), %rax
cmpq (%rax), %rbp
jge 0x427ff
movq %rbp, (%rax)
movq 0x70(%rsp), %rax
cmpq %r12, (%rax)
movq -0x50(%rsp), %rsi
movq 0x40(%rsp), %r14
jle 0x42855
movq 0x68(%rsp), %rax
movq %r12, (%rax)
jmp 0x42858
movq %r12, %r11
movq -0x60(%rsp), %r9
movq -0x50(%rsp), %rsi
movq -0x58(%rsp), %rdi
movq 0x40(%rsp), %r14
movl $0x1, %r15d
jmp 0x42860
movq %r12, %r11
movq -0x60(%rsp), %r9
movq -0x50(%rsp), %rsi
movq -0x58(%rsp), %rdi
movq 0x40(%rsp), %r14
jmp 0x42860
movq %rbx, (%rax)
movq %r12, %r11
movq -0x60(%rsp), %r9
leaq 0x1(%r12), %rcx
movq %r15, %rax
cmpq %rsi, %r12
movq %rcx, %r12
jne 0x4243c
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq