// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movq %r9, 0x30(%rsp)
movq %r8, 0x28(%rsp)
testq %rsi, %rsi
jle 0x43d8a
movq %rcx, %r13
movq %rdx, %rbx
movq %rsi, %r14
xorl %r12d, %r12d
movq %rdi, (%rsp)
movq %rcx, %rdi
xorl %edx, %edx
callq 0x3ecb0
movq (%rsp), %rdi
movq 0x28(%r13), %r15
movq (%r13), %rcx
movq 0x8(%r13), %r13
movl 0xc(%rdi), %eax
movq %rcx, 0x8(%rsp)
movl %eax, (%rcx)
movq $-0x1, (%r13)
cmpq $0x1, %r14
je 0x43d11
leaq -0x1(%r14), %r12
leaq 0x1c(%rdi), %rcx
movl $0x1, %edx
movq %rbx, 0x18(%rsp)
movl -0x4(%rcx), %esi
cmpl -0x14(%rcx), %esi
jbe 0x43d9b
movl -0x8(%rcx), %esi
cmpl -0x18(%rcx), %esi
jbe 0x43d9b
leaq -0x1(%rdx), %rsi
movq %rsi, (%r13,%rdx,8)
addl (%rcx), %eax
movq 0x8(%rsp), %rsi
movl %eax, (%rsi,%rdx,4)
incq %rdx
addq $0x10, %rcx
cmpq %rdx, %r14
jne 0x43ccc
testq %r12, %r12
movq 0x18(%rsp), %rbx
movq (%rsp), %rdi
js 0x43d8a
xorl %edx, %edx
movq %r12, (%r15,%rdx,8)
incq %rdx
movq (%r13,%r12,8), %r12
testq %r12, %r12
jns 0x43d13
leaq -0x1(%rdx), %rcx
movl $0x1, %eax
cmpq $0x1, %rdx
je 0x43d60
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 0x43d3e
movq %rdx, %rax
movq (%rsp), %rdi
testq %rbx, %rbx
je 0x43d8c
movq $-0x1, %rdx
movq 0x8(%r15,%rdx,8), %rsi
shlq $0x4, %rsi
movups (%rdi,%rsi), %xmm0
movups %xmm0, (%rbx)
incq %rdx
addq $0x10, %rbx
cmpq %rdx, %rcx
jne 0x43d6c
jmp 0x43d8c
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq (,%r14,8), %rdx
xorl %ebp, %ebp
movq %r14, %rbx
movq %rdi, %r14
movq %r15, %rdi
xorl %esi, %esi
callq 0x20e0
movslq 0xc(%r14), %rax
movq 0x8(%rsp), %rcx
movl %eax, (%rcx)
movq $-0x1, (%r13)
cmpq $0x3, %rbx
movl $0x2, %ecx
cmovgeq %rbx, %rcx
movq %rcx, 0x20(%rsp)
leaq 0x8(%r14), %rcx
movl $0x1, %esi
xorl %r12d, %r12d
xorl %edi, %edi
movq %r13, %rbx
movq %rdi, %rdx
movq %rsi, %r8
subq %rdi, %r8
movq %rsi, %rdi
movq 0x30(%rsp), %r9
subq %r9, %rdi
cmpq %r9, %r8
cmovleq %rdx, %rdi
cmpq %rdi, %rsi
movq %rbp, 0x38(%rsp)
jle 0x43eba
movq %rsi, %rdx
shlq $0x4, %rdx
movq (%rsp), %r9
leaq (%r9,%rdx), %r8
movl 0x8(%r9,%rdx), %r9d
movslq %esi, %rdx
movq %rdx, 0x40(%rsp)
movq $-0x1, 0x10(%rsp)
movq $-0x80000000, %r13      # imm = 0x80000000
movq %rcx, %rdx
xorl %r14d, %r14d
cmpl (%rdx), %r9d
jbe 0x43e7b
movl 0x4(%r8), %r11d
cmpl -0x4(%rdx), %r11d
jbe 0x43e7b
movl 0xc(%r8), %r11d
movq 0x8(%rsp), %r10
addl (%r10,%rbp,4), %r11d
cmpq %r11, %r13
jge 0x43e93
xorl %r13d, %r13d
testq %r14, %r14
setg %r13b
subq %r13, %r14
movq %rbp, 0x10(%rsp)
jmp 0x43eab
movq %r13, %r11
leaq -0x1(%rbp), %r10
addq $-0x10, %rdx
movq %r11, %r13
cmpq %rdi, %rbp
movq %r10, %rbp
jg 0x43e46
jmp 0x43eca
movq 0x40(%rsp), %r10
cmpq %r10, (%r15,%rbp,8)
jne 0x43ea8
cmpq 0x28(%rsp), %r14
jge 0x43f0c
incq %r14
movq %r13, %r11
movq (%rbx,%rbp,8), %r13
testq %r13, %r13
js 0x43e7e
movq %rsi, (%r15,%r13,8)
jmp 0x43e7e
movq $-0x80000000, %r11      # imm = 0x80000000
movq $-0x1, 0x10(%rsp)
movq 0x8(%rsp), %rdx
movl %r11d, (%rdx,%rsi,4)
movq %rbx, %r13
movq 0x10(%rsp), %rdx
movq %rdx, (%rbx,%rsi,8)
movslq %r11d, %rdx
cmpq %rax, %rdx
cmovgq %rdx, %rax
cmovgq %rsi, %r12
incq %rsi
movq 0x38(%rsp), %rbp
incq %rbp
addq $0x10, %rcx
cmpq 0x20(%rsp), %rsi
jne 0x43de8
jmp 0x43d03
movq %r13, %r11
jmp 0x43eca