// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Hash_Table.cpp
// Function: lchain_refine(k_mer_hit*, long, k_mer_hit*, Chain_Data*, long, long, long, long)
uint64_t lchain_refine(k_mer_hit* a, int64_t a_n, k_mer_hit* des, Chain_Data* dp, 
                                    int64_t max_skip, int64_t max_iter, int64_t max_dis, int64_t long_gap)
{
    if(a_n <= 0) return 0;
    int64_t *p, *t, max_f, n_skip, st, max_j, sc, msc, msc_i, dq, dr, dd; 
    int32_t *f; int64_t i, j, cL = 0; 
    resize_Chain_Data(dp, a_n, NULL);
    t = dp->tmp; f = dp->score; p = dp->pre; msc = msc_i = -1;

    for (i = 1, f[0] = 0, p[0] = -1, msc_i = a_n - 1; i < a_n; i++) {
        j = i-1;
        dq = (int64_t)(a[i].self_offset) - (int64_t)(a[j].self_offset);
        dr = (int64_t)(a[i].offset) - (int64_t)(a[j].offset);
        dd = dr > dq? dr - dq : dq - dr;//gap
        if(dd <= long_gap || dq > max_dis) {
            p[i] = i - 1; f[i] = i;
        } else {
            break;
        }
    }
    if(i >= a_n) goto ss_kip;
    

    memset(t, 0, (a_n*sizeof((*t))));
    f[0] = 0; p[0] = -1;

    for (i = 1, st = 0; i < a_n; ++i) {
        max_f = INT32_MIN; n_skip = 0; max_j = -1;
        if ((i-st) > max_iter) st = i-max_iter;
        ///i-1
        j = i - 1;
        dq = (int64_t)(a[i].self_offset) - (int64_t)(a[j].self_offset);
        dr = (int64_t)(a[i].offset) - (int64_t)(a[j].offset);
        dd = dr > dq? dr - dq : dq - dr;//gap
        if(dd <= long_gap) dd = 0;
        sc = f[j] - dd;
        if (sc > max_f) {
            max_f = sc, max_j = j;
        } 
        if (p[j] >= 0) t[p[j]] = i;
        
        ///[st, i-2]
        for (--j; (j >= st) && (a[i].self_offset <= (max_dis + a[j].self_offset)); --j) {
            dq = (int64_t)(a[i].self_offset) - (int64_t)(a[j].self_offset);
            dr = (int64_t)(a[i].offset) - (int64_t)(a[j].offset);
            dd = dr > dq? dr - dq : dq - dr;//gap
            if(dd <= long_gap) dd = 0;
            sc = f[j] - dd;
            if (sc > max_f) {
                max_f = sc, max_j = j;
                if (n_skip > 0) --n_skip;
            } else if (t[j] == (int32_t)i) {
                if (++n_skip > max_skip)
                    break;
            }
            if (p[j] >= 0) t[p[j]] = i;
        }

        f[i] = max_f; p[i] = max_j;
    }

    i = a_n-1; msc = f[i]; msc_i = i; 
    for (j = i-1; (j >= 0) && (a[i].self_offset <= (max_dis + a[j].self_offset)); --j) {
        if(msc < f[j] && p[j] >= 0) {///hold at least two hits in th final chain
            msc = f[j]; msc_i = j;
        }
    }
    
    ss_kip:
    ///a[] has been sorted by self_offset
    i = msc_i; 
    cL = 0; 
    while (i >= 0) {
        t[cL++] = i; i = p[i];
    }

    n_skip = cL>>1;
    for (i = 0; i < n_skip; i++) {
        msc_i = t[i]; t[i] = t[cL-i-1]; t[cL-i-1] = msc_i;
    }
    if(des) {
        for (i = 0; i < cL; i++) des[i] = a[t[i]];
    }
    return cL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, 0x20(%rsp)
movq %r8, 0x30(%rsp)
testq %rsi, %rsi
jle 0x175c3
movq %rcx, %r13
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r12
movq %rcx, %rdi
xorl %edx, %edx
callq 0x1383e
movq 0x28(%r13), %r15
movq (%r13), %rbp
movq 0x8(%r13), %r13
andl $0x0, (%rbp)
orq $-0x1, (%r13)
leaq -0x1(%r14), %r9
leaq 0x18(%r12), %rax
pushq $0x1
popq %rcx
cmpq %rcx, %r14
je 0x177b8
movl (%rax), %esi
movl -0x14(%rax), %edx
movl -0x10(%rax), %edi
subq %rdi, %rsi
movl -0x4(%rax), %edi
addq %rsi, %rdx
subq %rdx, %rdi
movq %rdi, %rdx
negq %rdx
cmovsq %rdi, %rdx
cmpq 0x70(%rsp), %rsi
jg 0x175ab
cmpq 0x78(%rsp), %rdx
jg 0x175ca
leaq -0x1(%rcx), %rdx
movq %rdx, (%r13,%rcx,8)
incl %edx
movl %edx, (%rbp,%rcx,4)
addq $0x10, %rax
incq %rcx
jmp 0x17576
xorl %eax, %eax
jmp 0x1781e
movq %r9, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r14, (%rsp)
leaq (,%r14,8), %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x20e0
andl $0x0, (%rbp)
orq $-0x1, (%r13)
leaq -0x8(%r12), %rbx
pushq $0x1
popq %rcx
pushq $-0x1
popq %r14
xorl %r8d, %r8d
xorl %edi, %edi
movq %r12, 0x18(%rsp)
cmpq (%rsp), %rcx
je 0x17753
movq %rcx, %rax
subq %rdi, %rax
movq %rcx, %rdx
movq 0x20(%rsp), %rsi
subq %rsi, %rdx
cmpq %rsi, %rax
cmovgq %rdx, %rdi
leaq -0x1(%rcx), %rdx
movq %rcx, %rax
shlq $0x4, %rax
movl 0x4(%r12,%rax), %r10d
movl 0x8(%r12,%rax), %r11d
movq %rdx, %rax
shlq $0x4, %rax
movl 0x4(%r12,%rax), %esi
movl 0x8(%r12,%rax), %eax
addq %r10, %rax
addq %r11, %rsi
subq %rsi, %rax
movq %rax, %rsi
negq %rsi
cmovsq %rax, %rsi
cmpq 0x78(%rsp), %rsi
movl $0x0, %eax
cmovleq %rax, %rsi
movslq %r8d, %r8
subq %rsi, %r8
cmpq $-0x7fffffff, %r8       # imm = 0x80000001
movq $-0x80000000, %rax      # imm = 0x80000000
cmovlq %rax, %r8
pushq $-0x1
popq %rax
cmovlq %rax, %rdx
testq %r14, %r14
js 0x1769a
movq %rcx, (%r15,%r14,8)
leaq -0x2(%rcx), %r9
movslq %ecx, %rsi
movq %rbx, 0x28(%rsp)
movq %rbx, %rax
xorl %r12d, %r12d
movq %rdx, %r14
cmpq %rdi, %r9
jl 0x17733
movl (%rax), %edx
movq 0x70(%rsp), %rbx
addq %rdx, %rbx
cmpq %r11, %rbx
jl 0x17733
movl -0x4(%rax), %ebx
addq %r10, %rdx
addq %r11, %rbx
subq %rbx, %rdx
movq %rdx, %rbx
negq %rbx
cmovsq %rdx, %rbx
cmpq 0x78(%rsp), %rbx
movl $0x0, %edx
cmovleq %rdx, %rbx
movslq (%rbp,%r9,4), %rdx
subq %rbx, %rdx
cmpq %r8, %rdx
jle 0x17709
xorl %r8d, %r8d
testq %r12, %r12
setg %r8b
subq %r8, %r12
movq %rdx, %r8
movq %r9, %r14
jmp 0x17719
cmpq %rsi, (%r15,%r9,8)
jne 0x17719
cmpq 0x30(%rsp), %r12
jge 0x17733
incq %r12
movq (%r13,%r9,8), %rdx
testq %rdx, %rdx
js 0x17727
movq %rcx, (%r15,%rdx,8)
decq %r9
addq $-0x10, %rax
jmp 0x176af
movl %r8d, (%rbp,%rcx,4)
movq %r14, (%r13,%rcx,8)
incq %rcx
movq 0x28(%rsp), %rbx
addq $0x10, %rbx
movq 0x18(%rsp), %r12
jmp 0x17609
movq (%rsp), %rsi
movslq -0x4(%rbp,%rsi,4), %rax
leaq -0x2(%rsi), %rcx
movq 0x8(%rsp), %r9
movq %r9, %rdx
shlq $0x4, %rdx
addq %r12, %rdx
addq $0x8, %rdx
shlq $0x4, %rsi
addq %r12, %rsi
addq $-0x18, %rsi
movq 0x10(%rsp), %rbx
testq %rcx, %rcx
js 0x177b8
movl (%rdx), %edi
movl (%rsi), %r8d
addq 0x70(%rsp), %r8
cmpq %rdi, %r8
jl 0x177b8
movslq (%rbp,%rcx,4), %rdi
cmpq %rdi, %rax
jge 0x177af
cmpq $0x0, (%r13,%rcx,8)
cmovnsq %rdi, %rax
cmovnsq %rcx, %r9
decq %rcx
addq $-0x10, %rsi
jmp 0x17783
leaq -0x8(%r15), %rcx
xorl %eax, %eax
testq %r9, %r9
js 0x177d5
incq %rax
movq %r9, 0x8(%rcx)
addq $0x8, %rcx
movq (%r13,%r9,8), %r9
jmp 0x177be
movq %rax, %rdx
shrq %rdx
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x177f9
movq (%r15,%rsi,8), %rdi
movq (%rcx), %r8
movq %r8, (%r15,%rsi,8)
movq %rdi, (%rcx)
incq %rsi
addq $-0x8, %rcx
jmp 0x177dd
testq %rbx, %rbx
je 0x1781e
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x1781e
movq (%r15,%rcx,8), %rdx
shlq $0x4, %rdx
movups (%r12,%rdx), %xmm0
movups %xmm0, (%rbx)
incq %rcx
addq $0x10, %rbx
jmp 0x17800
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq