// File: 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
movq %rsp, %rbp
subq $0xe0, %rsp
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
cmpq $0x0, -0x18(%rbp)
jg 0x29ebf
movq $0x0, -0x8(%rbp)
jmp 0x2a71e
movq $0x0, -0xb8(%rbp)
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x226f0
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq $-0x1, -0x80(%rbp)
movq $-0x1, -0x78(%rbp)
movq $0x1, -0xa8(%rbp)
movq -0xa0(%rbp), %rax
movl $0x0, (%rax)
movq -0x40(%rbp), %rax
movq $-0x1, (%rax)
movq -0x18(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x80(%rbp)
movq -0xa8(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x2a080
movq -0xa8(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xb0(%rbp)
movq -0x10(%rbp), %rax
movq -0xa8(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
movq -0xb0(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
subq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rax
movq -0xa8(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
movq -0xb0(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl 0x4(%rcx), %ecx
subq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x88(%rbp), %rax
jle 0x29ff2
movq -0x90(%rbp), %rax
subq -0x88(%rbp), %rax
movq %rax, -0xc0(%rbp)
jmp 0x2a007
movq -0x88(%rbp), %rax
subq -0x90(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
cmpq 0x18(%rbp), %rax
jle 0x2a02f
movq -0x88(%rbp), %rax
cmpq 0x10(%rbp), %rax
jle 0x2a065
movq -0xa8(%rbp), %rdx
subq $0x1, %rdx
movq -0x40(%rbp), %rax
movq -0xa8(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0xa8(%rbp), %rax
movl %eax, %edx
movq -0xa0(%rbp), %rax
movq -0xa8(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x2a067
jmp 0x2a080
jmp 0x2a069
movq -0xa8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa8(%rbp)
jmp 0x29f40
movq -0xa8(%rbp), %rax
cmpq -0x18(%rbp), %rax
jl 0x2a092
jmp 0x2a5b1
movq -0x48(%rbp), %rdi
movq -0x18(%rbp), %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x2100
movq -0xa0(%rbp), %rax
movl $0x0, (%rax)
movq -0x40(%rbp), %rax
movq $-0x1, (%rax)
movq $0x1, -0xa8(%rbp)
movq $0x0, -0x60(%rbp)
movq -0xa8(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x2a4ab
movq $-0x80000000, -0x50(%rbp) # imm = 0x80000000
movq $0x0, -0x58(%rbp)
movq $-0x1, -0x68(%rbp)
movq -0xa8(%rbp), %rax
subq -0x60(%rbp), %rax
cmpq -0x38(%rbp), %rax
jle 0x2a119
movq -0xa8(%rbp), %rax
subq -0x38(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0xa8(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xb0(%rbp)
movq -0x10(%rbp), %rax
movq -0xa8(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
movq -0xb0(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
subq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rax
movq -0xa8(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
movq -0xb0(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl 0x4(%rcx), %ecx
subq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x88(%rbp), %rax
jle 0x2a1ba
movq -0x90(%rbp), %rax
subq -0x88(%rbp), %rax
movq %rax, -0xc8(%rbp)
jmp 0x2a1cf
movq -0x88(%rbp), %rax
subq -0x90(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
cmpq 0x18(%rbp), %rax
jg 0x2a1f5
movq $0x0, -0x98(%rbp)
movq -0xa0(%rbp), %rax
movq -0xb0(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
subq -0x98(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x50(%rbp), %rax
jle 0x2a22f
movq -0x70(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x40(%rbp), %rax
movq -0xb0(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jl 0x2a25f
movq -0xa8(%rbp), %rdx
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
movq -0xb0(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0xb0(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rcx
xorl %eax, %eax
cmpq -0x60(%rbp), %rcx
movb %al, -0xc9(%rbp)
jl 0x2a2c3
movq -0x10(%rbp), %rax
movq -0xa8(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq 0x10(%rbp), %rcx
movq -0x10(%rbp), %rdx
movq -0xb0(%rbp), %rsi
shlq $0x4, %rsi
addq %rsi, %rdx
movl 0x8(%rdx), %edx
addq %rdx, %rcx
cmpq %rcx, %rax
setle %al
movb %al, -0xc9(%rbp)
movb -0xc9(%rbp), %al
testb $0x1, %al
jne 0x2a2d2
jmp 0x2a46a
movq -0x10(%rbp), %rax
movq -0xa8(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
movq -0xb0(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
subq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rax
movq -0xa8(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq -0x10(%rbp), %rcx
movq -0xb0(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl 0x4(%rcx), %ecx
subq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x88(%rbp), %rax
jle 0x2a361
movq -0x90(%rbp), %rax
subq -0x88(%rbp), %rax
movq %rax, -0xd8(%rbp)
jmp 0x2a376
movq -0x88(%rbp), %rax
subq -0x90(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
cmpq 0x18(%rbp), %rax
jg 0x2a39c
movq $0x0, -0x98(%rbp)
movq -0xa0(%rbp), %rax
movq -0xb0(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
subq -0x98(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x50(%rbp), %rax
jle 0x2a3eb
movq -0x70(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x58(%rbp)
jle 0x2a3e9
movq -0x58(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x2a421
movq -0x48(%rbp), %rax
movq -0xb0(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq -0xa8(%rbp), %rcx
movslq %ecx, %rcx
cmpq %rcx, %rax
jne 0x2a41f
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
cmpq -0x30(%rbp), %rax
jle 0x2a41d
jmp 0x2a46a
jmp 0x2a41f
jmp 0x2a421
movq -0x40(%rbp), %rax
movq -0xb0(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jl 0x2a451
movq -0xa8(%rbp), %rdx
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
movq -0xb0(%rbp), %rsi
movq (%rcx,%rsi,8), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x2a453
movq -0xb0(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xb0(%rbp)
jmp 0x2a271
movq -0x50(%rbp), %rax
movl %eax, %edx
movq -0xa0(%rbp), %rax
movq -0xa8(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x68(%rbp), %rdx
movq -0x40(%rbp), %rax
movq -0xa8(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0xa8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa8(%rbp)
jmp 0x2a0d0
movq -0x18(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rax
movq -0xa8(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
movq %rax, -0x78(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0xa8(%rbp), %rax
subq $0x1, %rax
movq %rax, -0xb0(%rbp)
xorl %eax, %eax
cmpq $0x0, -0xb0(%rbp)
movb %al, -0xd9(%rbp)
jl 0x2a53c
movq -0x10(%rbp), %rax
movq -0xa8(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq 0x10(%rbp), %rcx
movq -0x10(%rbp), %rdx
movq -0xb0(%rbp), %rsi
shlq $0x4, %rsi
addq %rsi, %rdx
movl 0x8(%rdx), %edx
addq %rdx, %rcx
cmpq %rcx, %rax
setle %al
movb %al, -0xd9(%rbp)
movb -0xd9(%rbp), %al
testb $0x1, %al
jne 0x2a548
jmp 0x2a5af
movq -0x78(%rbp), %rax
movq -0xa0(%rbp), %rcx
movq -0xb0(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
cmpq %rcx, %rax
jge 0x2a596
movq -0x40(%rbp), %rax
movq -0xb0(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jl 0x2a596
movq -0xa0(%rbp), %rax
movq -0xb0(%rbp), %rcx
movslq (%rax,%rcx,4), %rax
movq %rax, -0x78(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0x2a598
movq -0xb0(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xb0(%rbp)
jmp 0x2a4ed
jmp 0x2a5b1
movq -0x80(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq $0x0, -0xb8(%rbp)
cmpq $0x0, -0xa8(%rbp)
jl 0x2a60d
movq -0xa8(%rbp), %rdx
movq -0x48(%rbp), %rax
movq -0xb8(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0xb8(%rbp)
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rax
movq -0xa8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xa8(%rbp)
jmp 0x2a5c7
movq -0xb8(%rbp), %rax
sarq %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
cmpq -0x58(%rbp), %rax
jge 0x2a6a1
movq -0x48(%rbp), %rax
movq -0xa8(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x80(%rbp)
movq -0x48(%rbp), %rax
movq -0xb8(%rbp), %rcx
subq -0xa8(%rbp), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rdx
movq -0x48(%rbp), %rax
movq -0xa8(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x80(%rbp), %rdx
movq -0x48(%rbp), %rax
movq -0xb8(%rbp), %rcx
subq -0xa8(%rbp), %rcx
subq $0x1, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0xa8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa8(%rbp)
jmp 0x2a626
cmpq $0x0, -0x20(%rbp)
je 0x2a713
movq $0x0, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
cmpq -0xb8(%rbp), %rax
jge 0x2a711
movq -0x10(%rbp), %rcx
movq -0x48(%rbp), %rax
movq -0xa8(%rbp), %rdx
movq (%rax,%rdx,8), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq -0x20(%rbp), %rax
movq -0xa8(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq -0xa8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa8(%rbp)
jmp 0x2a6b3
jmp 0x2a713
movq -0xb8(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax,%rax)