// File: /workspace/github_cpp_makefile_star2_O3/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, -0x40(%rsp)
movq %rdx, -0x60(%rsp)
testq %rsi, %rsi
jle 0x42089
movq 0x70(%rsp), %rcx
movq 0x68(%rsp), %rdx
movq 0x60(%rsp), %r10
movq 0x58(%rsp), %r11
movq 0x50(%rsp), %rbx
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %r15
xorl %r14d, %r14d
movq %r14, (%rax)
movq $-0x80000000, %rax      # imm = 0x80000000
movq %rax, (%r11)
movq %rax, (%rbx)
movq $0x7fffffff, (%r10)     # imm = 0x7FFFFFFF
movq %r14, (%rdx)
movq %rsi, (%rcx)
movq -0x60(%rsp), %rax
decq %rax
movq %rax, -0x18(%rsp)
leaq 0x1c(%rdi), %rax
movq %rax, -0x10(%rsp)
xorl %r11d, %r11d
movl $0x1, %ebx
movl $0x1, %eax
movq %r9, -0x68(%rsp)
movq %rsi, -0x48(%rsp)
movq %rdi, -0x50(%rsp)
cmpq %rsi, %rbx
je 0x41c91
movq %rbx, %rcx
shlq $0x4, %rcx
movq %r11, %rdx
shlq $0x4, %rdx
movl (%rdi,%rdx), %edx
xorl (%rdi,%rcx), %edx
js 0x41c91
leaq (%rdi,%rcx), %rdx
movl 0x8(%rdx), %r10d
cmpl -0x8(%rcx,%rdi), %r10d
jbe 0x41c75
addq %rdi, %rcx
addq $-0x10, %rcx
movl 0x4(%rdx), %edx
cmpl 0x4(%rcx), %edx
ja 0x41c77
xorl %eax, %eax
movq $0x0, -0x8(%r9,%rbx,8)
movl $0x0, -0x4(%r15,%rbx,4)
movq %rax, %r13
jmp 0x4206a
movq $0x0, -0x8(%r9,%rbx,8)
movl $0x0, -0x4(%r15,%rbx,4)
movl $0x1, %r13d
testq %rax, %rax
je 0x42067
movq $-0x1, (%r8,%r11,8)
movq %r11, %rcx
shlq $0x4, %rcx
movzbl 0xc(%rdi,%rcx), %r9d
movq 0x38(%rsp), %rax
movl %r9d, (%rax,%r11,4)
leaq 0x1(%r11), %rbp
cmpq %rbx, %rbp
movq %rbx, -0x38(%rsp)
movq %r11, -0x20(%rsp)
movq %rcx, -0x8(%rsp)
jge 0x41e91
movq -0x10(%rsp), %rax
addq %rax, %rcx
movl %r9d, %r12d
movq %r11, %rdi
xorl %r13d, %r13d
xorl %r14d, %r14d
movl -0x14(%rcx), %r10d
movl -0x4(%rcx), %eax
movq %rax, %r11
subq %r10, %r11
jle 0x41ead
movq %rdi, -0x58(%rsp)
movl -0x18(%rcx), %edi
movl -0x8(%rcx), %r15d
movq %r15, %rsi
subq %rdi, %rsi
jle 0x41ea8
movq %r14, -0x28(%rsp)
movq %r11, %rdx
subq %rsi, %rdx
negq %rdx
movq %r11, %r14
subq %rsi, %r14
cmovbq %rdx, %r14
cmpq $0x11, %r14
jl 0x41dad
incl %eax
incl %r15d
movq %r13, -0x30(%rsp)
movq %r9, %r13
movq -0x60(%rsp), %r9
movq %r9, %rdx
subq %rax, %rdx
movq -0x40(%rsp), %rbx
subq %r15, %rbx
movq %rdi, %r15
subq %r10, %r15
cmpl %edi, %r10d
movl $0x0, %edi
cmovbeq %rdi, %r15
addq %rbx, %rax
cmpq %rbx, %rdx
movq -0x38(%rsp), %rbx
cmovleq %r9, %rax
movq %r13, %r9
movq -0x30(%rsp), %r13
addq %r15, %rax
cvtsi2sd %rax, %xmm3
mulsd %xmm2, %xmm3
cvttsd2si %xmm3, %eax
cltq
cmpq %rax, %r14
jg 0x41ea3
cmpq %r11, %rsi
cmovbq %rsi, %r11
movl (%rcx), %edi
movzbl %dil, %esi
cmpq %rsi, %r11
movq %rsi, %r10
cmovbq %r11, %r10
shrl $0x8, %edi
movl $0x1, %eax
cmpq %rdi, %r10
jb 0x41ddb
movzbl %r10b, %eax
divb %dil
movzbl %al, %eax
testq %r14, %r14
jne 0x41de5
cmpq %rsi, %r11
jbe 0x41e3e
xorps %xmm3, %xmm3
cvtsi2sd %r14, %xmm3
movapd %xmm3, %xmm5
mulsd %xmm0, %xmm5
cvtsi2sd %eax, %xmm6
cvtsi2sd %r11, %xmm4
divsd %xmm4, %xmm3
divsd %xmm2, %xmm3
mulsd %xmm6, %xmm3
xorl %edx, %edx
ucomisd %xmm5, %xmm3
seta %dl
xorl %edi, %edi
ucomisd %xmm3, %xmm5
seta %dil
cmpq $0x4, %r14
cmovll %edi, %edx
testb %dl, %dl
jne 0x41e2c
movapd %xmm5, %xmm3
mulsd %xmm1, %xmm4
addsd %xmm3, %xmm4
cvttsd2si %xmm4, %edx
movslq %edx, %rdx
subq %rdx, %rax
movq -0x58(%rsp), %rdi
movslq %r12d, %r12
addq %rax, %r12
cmpq %rsi, %r12
jl 0x41e9c
leaq -0x1(%rbp), %rax
movq -0x28(%rsp), %rdx
addq %r14, %rdx
movq %rdx, %r14
movslq %r12d, %rdx
cmpq %r9, %rdx
cmovgq %rdx, %r9
cmovgeq %rbp, %rdi
movq %rax, (%r8,%rbp,8)
cmpq %rdx, %r13
cmovgeq %rdx, %r13
movq 0x38(%rsp), %rax
movl %r12d, (%rax,%rbp,4)
incq %rbp
addq $0x10, %rcx
cmpq %rbx, %rbp
jl 0x41d00
jmp 0x41ead
xorl %r14d, %r14d
movq %r11, %rdi
xorl %r13d, %r13d
jmp 0x41ead
movq -0x28(%rsp), %r14
jmp 0x41ead
movq -0x28(%rsp), %r14
movq -0x58(%rsp), %rdi
cmpq %rbx, %rbp
jl 0x42045
leaq -0x1(%rbx), %rax
cmpq %rax, %rdi
jne 0x42045
movq %r13, -0x30(%rsp)
movq %rbx, %rcx
movq -0x20(%rsp), %r12
subq %r12, %rcx
cmpq $0x2, %rcx
movq -0x48(%rsp), %rsi
movq -0x50(%rsp), %rdi
movq 0x40(%rsp), %r15
movl $0x1, %r13d
jl 0x41f8f
cmpq $0x11, %r14
jl 0x41f8f
movq -0x8(%rsp), %r10
addq %rdi, %r10
movq %rax, %rcx
shlq $0x4, %rcx
movl 0x4(%r10), %edx
movl 0x8(%r10), %ebx
movq %r14, %r12
movl 0x4(%rdi,%rcx), %r14d
movl 0x8(%rdi,%rcx), %ecx
incl %ecx
incl %r14d
movq %r9, %rbp
movq -0x60(%rsp), %r9
movq %r9, %r11
subq %rcx, %r11
movq -0x40(%rsp), %r10
subq %r14, %r10
movq %rdx, %r14
subq %rbx, %r14
cmpl %edx, %ebx
movq -0x38(%rsp), %rbx
movl $0x0, %edx
cmovbeq %rdx, %r14
addq %r10, %rcx
cmpq %r10, %r11
cmovleq %r9, %rcx
movq %rbp, %r9
addq %r14, %rcx
xorps %xmm3, %xmm3
cvtsi2sd %rcx, %xmm3
mulsd %xmm2, %xmm3
cvttsd2si %xmm3, %ecx
movslq %ecx, %rcx
cmpq %rcx, %r12
movq -0x20(%rsp), %r12
setg %cl
cmpq $-0x80000000, %rax      # imm = 0x80000000
setne %dl
testb %dl, %cl
jne 0x4207f
movq 0x50(%rsp), %rcx
movq (%rcx), %rcx
cmpq %rcx, %r9
jl 0x4201a
movq %rax, %rdx
shlq $0x4, %rdx
movq %r9, %rbp
movl 0x4(%rdi,%rdx), %r9d
movl 0x8(%rdi,%rdx), %r14d
movq %r9, %rdx
subq %r14, %rdx
incq %rdx
movq %r14, %rbx
notq %rbx
addq -0x60(%rsp), %rbx
movq %r9, %r10
notq %r10
addq -0x40(%rsp), %r10
movq %r14, %r11
addq %r10, %r11
cmpq %r10, %rbx
movq -0x38(%rsp), %rbx
cmovleq -0x18(%rsp), %r11
cmpl %r9d, %r14d
movl $0x1, %r9d
cmovbeq %r9, %rdx
addq %r11, %rdx
cmpq %rcx, %rbp
jg 0x42002
movq 0x60(%rsp), %rcx
cmpq (%rcx), %rdx
jge 0x4201a
movq 0x50(%rsp), %rcx
movq %rbp, (%rcx)
movq 0x58(%rsp), %rcx
movq %rax, (%rcx)
movq 0x60(%rsp), %rax
movq %rdx, (%rax)
movq 0x48(%rsp), %rax
movq -0x30(%rsp), %rcx
cmpq (%rax), %rcx
jge 0x4202c
movq %rcx, (%rax)
movq 0x70(%rsp), %rax
cmpq %rbx, (%rax)
movq -0x68(%rsp), %r9
jle 0x42064
movq 0x68(%rsp), %rax
movq %rbx, (%rax)
jmp 0x42067
movq %rbx, %r11
movq -0x68(%rsp), %r9
movq -0x48(%rsp), %rsi
movq -0x50(%rsp), %rdi
movq 0x40(%rsp), %r15
movl $0x1, %r13d
jmp 0x4206a
movq %r12, (%rax)
movq %rbx, %r11
leaq 0x1(%rbx), %rcx
movq %r13, %rax
cmpq %rsi, %rbx
movq %rcx, %rbx
jne 0x41c3c
jmp 0x42089
movq %rbx, %r11
movq -0x68(%rsp), %r9
jmp 0x4206a
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq