// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Hash_Table.cpp
// Function: lchain_simple(k_mer_hit*, long, k_mer_hit*, Chain_Data*, long, long)
uint64_t lchain_simple(k_mer_hit* a, int64_t a_n, k_mer_hit* des, Chain_Data* dp, 
                                                        int64_t max_skip, int64_t max_iter)
{
    if(a_n <= 0) return 0;
    int64_t *p, *t, max_f, n_skip, st, max_j, sc, msc, msc_i; 
    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]=a[0].cnt, p[0]=-1, msc_i=a_n-1; i<a_n; i++) {
        j = i-1;
        if((a[i].self_offset > a[j].self_offset)&&(a[i].offset > a[j].offset)) {
            p[i] = j; f[i] = f[j]+a[i].cnt;
        } else {
            break;
        }
    }
    if(i >= a_n) goto ss_kip;
    
    memset(t, 0, (a_n*sizeof((*t))));
    f[0]=a[0].cnt; p[0]=-1; msc = f[0]; msc_i = 0;

    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;
        ///[st, i-2]
        for (j=i-1; j >= st; --j) {
            if((a[i].self_offset > a[j].self_offset)&&(a[i].offset > a[j].offset)) {
                sc = f[j]+a[i].cnt;
                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;
        if(f[i] > msc) {
            msc = f[i]; msc_i = i;
        }
    }

    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, 0x18(%rsp)
testq %rsi, %rsi
jle 0x17a5d
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), %rcx
movq 0x8(%r13), %r13
movl 0xc(%r12), %eax
movq %rcx, 0x8(%rsp)
movl %eax, (%rcx)
orq $-0x1, (%r13)
leaq -0x1(%r14), %rcx
leaq 0x1c(%r12), %rdx
pushq $0x1
popq %rsi
cmpq %rsi, %r14
je 0x179f7
movl -0x4(%rdx), %edi
cmpl -0x14(%rdx), %edi
jbe 0x178c2
movl -0x8(%rdx), %edi
cmpl -0x18(%rdx), %edi
jbe 0x178c2
leaq -0x1(%rsi), %rdi
movq %rdi, (%r13,%rsi,8)
addl (%rdx), %eax
movq 0x8(%rsp), %rdi
movl %eax, (%rdi,%rsi,4)
addq $0x10, %rdx
incq %rsi
jmp 0x1788d
movq %rbx, 0x10(%rsp)
leaq (,%r14,8), %rdx
xorl %ebp, %ebp
movq %r15, %rdi
xorl %esi, %esi
callq 0x20e0
movslq 0xc(%r12), %rax
movq 0x8(%rsp), %rcx
movl %eax, (%rcx)
orq $-0x1, (%r13)
leaq -0x8(%r12), %rcx
movq %rcx, 0x30(%rsp)
pushq $0x1
popq %rsi
xorl %ecx, %ecx
cmpq %r14, %rsi
jge 0x179f2
movq %r14, %r9
movq %rsi, %rdx
subq %rbp, %rdx
movq %rsi, %rdi
movq 0x20(%rsp), %r8
subq %r8, %rdi
cmpq %r8, %rdx
cmovgq %rdi, %rbp
movq %rsi, %r8
shlq $0x4, %r8
movq %r12, %r14
addq %r12, %r8
movslq %esi, %rdx
movq %rdx, 0x28(%rsp)
movq $-0x80000000, %rdi      # imm = 0x80000000
xorl %r10d, %r10d
pushq $-0x1
popq %rbx
movq %rsi, %rdx
movq %rdx, %r11
shlq $0x4, %r11
addq 0x30(%rsp), %r11
movq %rdx, %r12
cmpq %rbp, %r12
jle 0x179c9
movl 0x8(%r8), %edx
cmpl (%r11), %edx
jbe 0x1796b
movl 0x4(%r8), %edx
cmpl -0x4(%r11), %edx
ja 0x17974
addq $-0x10, %r11
decq %r12
jmp 0x17953
movl 0xc(%r8), %r11d
movq 0x8(%rsp), %rdx
addl -0x4(%rdx,%r12,4), %r11d
leaq -0x1(%r12), %rdx
cmpq %r11, %rdi
jge 0x179a0
xorl %edi, %edi
testq %r10, %r10
setg %dil
subq %rdi, %r10
movq %rdx, %rbx
movq %r11, %rdi
jmp 0x179b6
movq 0x28(%rsp), %r11
cmpq %r11, -0x8(%r15,%r12,8)
jne 0x179b6
cmpq 0x18(%rsp), %r10
jge 0x179c9
incq %r10
movq -0x8(%r13,%r12,8), %r11
testq %r11, %r11
js 0x17944
movq %rsi, (%r15,%r11,8)
jmp 0x17944
movq 0x8(%rsp), %rdx
movl %edi, (%rdx,%rsi,4)
movslq %edi, %rdx
cmpq %rax, %rdx
cmovgq %rdx, %rax
movq %rbx, (%r13,%rsi,8)
cmovgq %rsi, %rcx
incq %rsi
movq %r14, %r12
movq %r9, %r14
jmp 0x178fb
movq 0x10(%rsp), %rbx
leaq -0x8(%r15), %rdx
xorl %eax, %eax
testq %rcx, %rcx
js 0x17a14
incq %rax
movq %rcx, 0x8(%rdx)
addq $0x8, %rdx
movq (%r13,%rcx,8), %rcx
jmp 0x179fd
movq %rax, %rcx
shrq %rcx
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x17a38
movq (%r15,%rsi,8), %rdi
movq (%rdx), %r8
movq %r8, (%r15,%rsi,8)
movq %rdi, (%rdx)
incq %rsi
addq $-0x8, %rdx
jmp 0x17a1c
testq %rbx, %rbx
je 0x17a5f
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x17a5f
movq (%r15,%rcx,8), %rdx
shlq $0x4, %rdx
movups (%r12,%rdx), %xmm0
movups %xmm0, (%rbx)
incq %rcx
addq $0x10, %rbx
jmp 0x17a3f
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq