// File: /workspace/github_cpp_makefile_star2_O3/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 0x43c46
movq %rcx, %r12
movq %rsi, %r14
movq %rdi, %rbx
movq %rdx, 0x18(%rsp)
xorl %ebp, %ebp
movq %rcx, %rdi
xorl %edx, %edx
callq 0x3ecb0
movq 0x28(%r12), %r15
movq (%r12), %rax
movq 0x8(%r12), %r12
movq %rax, 0x8(%rsp)
movl $0x0, (%rax)
movq $-0x1, (%r12)
cmpq $0x1, %r14
je 0x43bcd
leaq -0x1(%r14), %rbp
movl $0x1, %eax
subq %r14, %rax
leaq 0x18(%rbx), %rcx
xorl %edx, %edx
movl (%rcx), %edi
movl -0x14(%rcx), %esi
movl -0x10(%rcx), %r8d
subq %r8, %rdi
movl -0x4(%rcx), %r8d
addq %rdi, %rsi
subq %rsi, %r8
movq %r8, %rsi
negq %rsi
cmovsq %r8, %rsi
cmpq 0x80(%rsp), %rdi
jg 0x43981
cmpq 0x88(%rsp), %rsi
jg 0x439a7
movq %rdx, 0x8(%r12,%rdx,8)
leaq 0x1(%rdx), %rsi
movq 0x8(%rsp), %rdi
movl %esi, 0x4(%rdi,%rdx,4)
addq $0x10, %rcx
movq %rax, %rdi
movq %rsi, %rdx
addq %rsi, %rdi
jne 0x4394d
jmp 0x43bc8
leaq (,%r14,8), %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x20e0
movq 0x8(%rsp), %rax
movl $0x0, (%rax)
movq $-0x1, (%r12)
leaq -0x8(%rbx), %r13
movl $0x1, %ecx
movq $0x0, 0x10(%rsp)
movq $-0x1, %rax
xorl %r9d, %r9d
xorl %r8d, %r8d
movq %rbx, 0x28(%rsp)
movq %r14, 0x20(%rsp)
movq %r8, %rdx
movq %rcx, %rsi
subq %r8, %rsi
movq %rcx, %r8
movq 0x30(%rsp), %rdi
subq %rdi, %r8
cmpq %rdi, %rsi
cmovleq %rdx, %r8
leaq -0x1(%rcx), %r10
movq %rcx, %rdx
shlq $0x4, %rdx
movl 0x4(%rbx,%rdx), %r11d
movl 0x8(%rbx,%rdx), %edx
movq %r10, %rsi
shlq $0x4, %rsi
movl 0x4(%rbx,%rsi), %edi
movl 0x8(%rbx,%rsi), %esi
addq %r11, %rsi
addq %rdx, %rdi
subq %rdi, %rsi
movq %rsi, %rdi
negq %rdi
cmovsq %rsi, %rdi
cmpq 0x88(%rsp), %rdi
movl $0x0, %esi
cmovleq %rsi, %rdi
movslq %r9d, %r9
subq %rdi, %r9
cmpq $-0x7fffffff, %r9       # imm = 0x80000001
movq $-0x80000000, %rsi      # imm = 0x80000000
cmovlq %rsi, %r9
movq $-0x1, %rsi
cmovlq %rsi, %r10
testq %rax, %rax
js 0x43a83
movq %rcx, (%r15,%rax,8)
leaq -0x2(%rcx), %rax
cmpq %r8, %rax
movq %r13, 0x40(%rsp)
jl 0x43b35
movslq %ecx, %rsi
movq %r13, %rax
movq 0x10(%rsp), %rdi
xorl %r14d, %r14d
movl (%rax), %r13d
movq 0x80(%rsp), %rbx
addq %r13, %rbx
cmpq %rdx, %rbx
jl 0x43b35
movl -0x4(%rax), %ebx
addq %r11, %r13
addq %rdx, %rbx
subq %rbx, %r13
movq %r13, %rbx
negq %rbx
cmovsq %r13, %rbx
cmpq 0x88(%rsp), %rbx
movl $0x0, %r13d
cmovleq %r13, %rbx
movq 0x8(%rsp), %r13
movslq -0x4(%r13,%rdi,4), %r13
subq %rbx, %r13
cmpq %r9, %r13
jle 0x43b06
leaq -0x1(%rdi), %r10
xorl %r9d, %r9d
testq %r14, %r14
setg %r9b
subq %r9, %r14
movq %r13, %r9
jmp 0x43b17
cmpq %rsi, -0x8(%r15,%rdi,8)
jne 0x43b17
cmpq 0x38(%rsp), %r14
jge 0x43b35
incq %r14
movq -0x8(%r12,%rdi,8), %r13
testq %r13, %r13
js 0x43b25
movq %rcx, (%r15,%r13,8)
decq %rdi
addq $-0x10, %rax
cmpq %r8, %rdi
jg 0x43aa3
movq 0x8(%rsp), %rax
movl %r9d, (%rax,%rcx,4)
movq %r10, (%r12,%rcx,8)
incq %rcx
incq 0x10(%rsp)
movq 0x40(%rsp), %r13
addq $0x10, %r13
movq %r10, %rax
movq 0x20(%rsp), %r14
cmpq %r14, %rcx
movq 0x28(%rsp), %rbx
jne 0x439f5
leaq -0x2(%r14), %rax
movq %rbp, %rdx
shlq $0x4, %rdx
movq 0x8(%rsp), %rcx
movslq -0x4(%rcx,%r14,4), %rcx
movl 0x8(%rbx,%rdx), %edx
shlq $0x4, %r14
leaq (%r14,%rbx), %rsi
addq $-0x18, %rsi
movl (%rsi), %edi
addq 0x80(%rsp), %rdi
cmpq %rdx, %rdi
jl 0x43bc8
movq 0x8(%rsp), %rdi
movslq (%rdi,%rax,4), %rdi
cmpq %rdi, %rcx
jge 0x43bb8
cmpq $0x0, (%r12,%rax,8)
cmovnsq %rdi, %rcx
cmovnsq %rax, %rbp
leaq -0x1(%rax), %rdi
addq $-0x10, %rsi
testq %rax, %rax
movq %rdi, %rax
jg 0x43b8e
testq %rbp, %rbp
js 0x43c46
xorl %edx, %edx
movq %rbp, (%r15,%rdx,8)
incq %rdx
movq (%r12,%rbp,8), %rbp
testq %rbp, %rbp
jns 0x43bcf
leaq -0x1(%rdx), %rcx
movl $0x1, %eax
cmpq $0x1, %rdx
je 0x43c17
movq %rdx, %rax
shrq %rax
xorl %esi, %esi
movq %rcx, %rdi
movq (%r15,%rsi,8), %r8
movq (%r15,%rdi,8), %r9
movq %r9, (%r15,%rsi,8)
movq %r8, (%r15,%rdi,8)
incq %rsi
decq %rdi
cmpq %rsi, %rax
jne 0x43bf9
movq %rdx, %rax
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x43c48
movq $-0x1, %rdx
movq 0x8(%r15,%rdx,8), %rsi
shlq $0x4, %rsi
movups (%rbx,%rsi), %xmm0
movups %xmm0, (%rdi)
incq %rdx
addq $0x10, %rdi
cmpq %rdx, %rcx
jne 0x43c28
jmp 0x43c48
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq