// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movq %r9, 0x30(%rsp)
movq %r8, 0x38(%rsp)
testq %rsi, %rsi
jle 0x442a0
movq %rcx, %rbp
movq %rdx, %rbx
movq %rdi, %r14
movq %rcx, %rdi
xorl %edx, %edx
movq %rsi, 0x8(%rsp)
callq 0x3f828
movq 0x8(%rsp), %r11
movq 0x28(%rbp), %r15
movq (%rbp), %r10
movq 0x8(%rbp), %rbp
movl $0x0, (%r10)
movq $-0x1, (%rbp)
leaq -0x1(%r11), %r13
cmpq $0x1, %r11
setne %cl
je 0x444d6
movl $0x1, %eax
subq %r11, %rax
leaq 0x18(%r14), %rdx
movl $0x2, %esi
movl (%rdx), %r8d
movl -0x14(%rdx), %edi
movl -0x10(%rdx), %r9d
subq %r9, %r8
movl -0x4(%rdx), %r9d
addq %r8, %rdi
subq %rdi, %r9
movq %r9, %rdi
negq %rdi
cmovsq %r9, %rdi
cmpq 0x80(%rsp), %r8
jg 0x44270
cmpq 0x88(%rsp), %rdi
jg 0x442a7
leaq -0x2(%rsi), %rcx
movq %rcx, -0x8(%rbp,%rsi,8)
leal -0x1(%rsi), %ecx
movl %ecx, -0x4(%r10,%rsi,4)
cmpq %r11, %rsi
setl %cl
leaq (%rax,%rsi), %rdi
incq %rdi
incq %rsi
addq $0x10, %rdx
cmpq $0x2, %rdi
jne 0x4423b
jmp 0x444d6
xorl %eax, %eax
jmp 0x4454f
testb $0x1, %cl
je 0x444d6
leaq (,%r11,8), %rdx
movq %r15, %rdi
xorl %esi, %esi
movq %r10, 0x10(%rsp)
callq 0x20e0
movq 0x10(%rsp), %r10
movl $0x0, (%r10)
movq $-0x1, (%rbp)
leaq -0x8(%r14), %rax
movq %rax, 0x20(%rsp)
movl $0x1, %ecx
movq $0x0, 0x18(%rsp)
xorl %r8d, %r8d
movq %r14, 0x28(%rsp)
movq %r8, %rax
movq %rcx, %rdx
subq %r8, %rdx
movq %rcx, %r8
movq 0x30(%rsp), %rsi
subq %rsi, %r8
cmpq %rsi, %rdx
cmovleq %rax, %r8
leaq -0x1(%rcx), %r9
movq %rcx, %rax
shlq $0x4, %rax
movq %r10, %rdi
movl 0x4(%r14,%rax), %r10d
movl 0x8(%r14,%rax), %r11d
movq %r9, %rax
shlq $0x4, %rax
movl 0x4(%r14,%rax), %edx
movl 0x8(%r14,%rax), %eax
addq %r10, %rax
addq %r11, %rdx
subq %rdx, %rax
movq %rax, %rsi
negq %rsi
cmovsq %rax, %rsi
cmpq 0x88(%rsp), %rsi
movl $0x0, %eax
cmovleq %rax, %rsi
movslq -0x4(%rdi,%rcx,4), %rdx
subq %rsi, %rdx
cmpq $-0x7fffffff, %rdx      # imm = 0x80000001
movq $-0x80000000, %rax      # imm = 0x80000000
cmovlq %rax, %rdx
movq $-0x1, %rax
cmovlq %rax, %r9
movq -0x8(%rbp,%rcx,8), %rax
testq %rax, %rax
js 0x44395
movq %rcx, (%r15,%rax,8)
leaq -0x2(%rcx), %rax
cmpq %r8, %rax
jl 0x4444f
movslq %ecx, %rax
movq %rax, 0x40(%rsp)
movq 0x20(%rsp), %rax
movq 0x18(%rsp), %rdi
xorl %r14d, %r14d
movl (%rax), %esi
movq 0x80(%rsp), %r12
addq %rsi, %r12
cmpq %r11, %r12
jl 0x4444f
movl -0x4(%rax), %r12d
addq %r10, %rsi
addq %r11, %r12
subq %r12, %rsi
movq %rsi, %r12
negq %r12
cmovsq %rsi, %r12
cmpq 0x88(%rsp), %r12
movl $0x0, %esi
cmovleq %rsi, %r12
movq 0x10(%rsp), %rsi
movslq -0x4(%rsi,%rdi,4), %rsi
subq %r12, %rsi
cmpq %rdx, %rsi
jle 0x4441b
leaq -0x1(%rdi), %r9
xorl %edx, %edx
testq %r14, %r14
setg %dl
subq %rdx, %r14
movq %rsi, %rdx
jmp 0x44431
movq 0x40(%rsp), %rsi
cmpq %rsi, -0x8(%r15,%rdi,8)
jne 0x44431
cmpq 0x38(%rsp), %r14
jge 0x4444f
incq %r14
movq -0x8(%rbp,%rdi,8), %rsi
testq %rsi, %rsi
js 0x4443f
movq %rcx, (%r15,%rsi,8)
decq %rdi
addq $-0x10, %rax
cmpq %r8, %rdi
jg 0x443b7
movq 0x10(%rsp), %r10
movl %edx, (%r10,%rcx,4)
movq %r9, (%rbp,%rcx,8)
incq %rcx
incq 0x18(%rsp)
addq $0x10, 0x20(%rsp)
cmpq 0x8(%rsp), %rcx
movq 0x28(%rsp), %r14
jne 0x442fa
movq 0x8(%rsp), %rsi
leaq -0x2(%rsi), %rax
movq %r13, %rdx
shlq $0x4, %rdx
movslq -0x4(%r10,%rsi,4), %rcx
movl 0x8(%r14,%rdx), %edx
shlq $0x4, %rsi
addq %r14, %rsi
addq $-0x18, %rsi
movl (%rsi), %edi
addq 0x80(%rsp), %rdi
cmpq %rdx, %rdi
jl 0x444d6
movslq (%r10,%rax,4), %rdi
cmpq %rdi, %rcx
jge 0x444c6
cmpq $0x0, (%rbp,%rax,8)
cmovnsq %rdi, %rcx
cmovnsq %rax, %r13
leaq -0x1(%rax), %rdi
addq $-0x10, %rsi
testq %rax, %rax
movq %rdi, %rax
jg 0x444a0
xorl %eax, %eax
testq %r13, %r13
js 0x444eb
movq %r13, (%r15,%rax,8)
incq %rax
movq (%rbp,%r13,8), %r13
jmp 0x444d8
cmpq $0x2, %rax
jb 0x44521
movq %rax, %rcx
shrq %rcx
negq %rcx
leaq (%r15,%rax,8), %rdx
addq $-0x8, %rdx
xorl %esi, %esi
movq %r15, %rdi
movq (%rdi), %r8
movq (%rdx,%rsi,8), %r9
movq %r9, (%rdi)
movq %r8, (%rdx,%rsi,8)
decq %rsi
addq $0x8, %rdi
cmpq %rsi, %rcx
jne 0x44507
testq %rbx, %rbx
sete %cl
testq %rax, %rax
sete %dl
orb %cl, %dl
jne 0x4454f
xorl %ecx, %ecx
movq (%r15,%rcx,8), %rdx
shlq $0x4, %rdx
movups (%r14,%rdx), %xmm0
movups %xmm0, (%rbx)
incq %rcx
addq $0x10, %rbx
cmpq %rcx, %rax
jne 0x44533
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq