// File: /workspace/github_cpp_makefile_star2_O1/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 0x447c3
movq %rcx, %r13
movq %rdx, %rbx
movq %rsi, %rbp
movq %rdi, %r14
movq %rcx, %rdi
xorl %edx, %edx
callq 0x3f828
movq 0x28(%r13), %rax
movq %rax, (%rsp)
movq (%r13), %rcx
movq 0x8(%r13), %r15
movl 0xc(%r14), %eax
movq %rcx, 0x10(%rsp)
movl %eax, (%rcx)
movq $-0x1, (%r15)
leaq -0x1(%rbp), %rcx
cmpq $0x1, %rbp
setne %al
movq %r14, 0x8(%rsp)
je 0x447a1
movl 0x18(%r14), %edx
cmpl 0x8(%r14), %edx
jbe 0x4461a
leaq 0x28(%r14), %rsi
xorl %edx, %edx
movl -0x14(%rsi), %edi
cmpl -0x24(%rsi), %edi
jbe 0x4461a
movq %rdx, 0x8(%r15,%rdx,8)
movl -0xc(%rsi), %eax
movq 0x10(%rsp), %rdi
addl (%rdi,%rdx,4), %eax
movl %eax, 0x4(%rdi,%rdx,4)
addq $0x2, %rdx
cmpq %rbp, %rdx
je 0x447a1
setl %al
movl (%rsi), %edi
leaq 0x10(%rsi), %r8
decq %rdx
cmpl -0x10(%rsi), %edi
movq %r8, %rsi
ja 0x445dd
testb $0x1, %al
je 0x447a1
leaq (,%rbp,8), %rdx
xorl %r12d, %r12d
movq (%rsp), %rdi
xorl %esi, %esi
callq 0x20e0
movq 0x8(%rsp), %rcx
movslq 0xc(%rcx), %rax
movq 0x10(%rsp), %rdx
movl %eax, (%rdx)
movq $-0x1, (%r15)
cmpq $0x3, %rbp
movl $0x2, %edx
cmovgeq %rbp, %rdx
movq %rdx, 0x20(%rsp)
leaq 0x8(%rcx), %rdx
movl $0x1, %edi
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %r15, %r14
movq %r8, %rsi
movq %rdi, %r9
subq %r8, %r9
movq %rdi, %r8
movq 0x30(%rsp), %r10
subq %r10, %r8
cmpq %r10, %r9
cmovleq %rsi, %r8
cmpq %r8, %rdi
movq %r12, 0x38(%rsp)
jle 0x4474d
movq %rdi, %rsi
shlq $0x4, %rsi
movq 0x8(%rsp), %r10
leaq (%r10,%rsi), %r9
movl 0x8(%r10,%rsi), %r10d
movslq %edi, %rsi
movq %rsi, 0x40(%rsp)
movq $-0x1, 0x18(%rsp)
movq $-0x80000000, %r15      # imm = 0x80000000
movq %rdx, %rsi
movq %r12, %rbp
xorl %r12d, %r12d
cmpl (%rsi), %r10d
jbe 0x44706
movl 0x4(%r9), %r13d
cmpl -0x4(%rsi), %r13d
jbe 0x44706
movl 0xc(%r9), %r13d
movq 0x10(%rsp), %r11
addl (%r11,%rbp,4), %r13d
cmpq %r13, %r15
jge 0x4471e
xorl %r15d, %r15d
testq %r12, %r12
setg %r15b
subq %r15, %r12
movq %rbp, 0x18(%rsp)
jmp 0x4473a
movq %r15, %r13
leaq -0x1(%rbp), %r11
addq $-0x10, %rsi
movq %r13, %r15
cmpq %r8, %rbp
movq %r11, %rbp
jg 0x446d1
jmp 0x4475d
movq (%rsp), %r11
movq 0x40(%rsp), %r13
cmpq %r13, (%r11,%rbp,8)
jne 0x44737
cmpq 0x28(%rsp), %r12
jge 0x4479c
incq %r12
movq %r15, %r13
movq (%r14,%rbp,8), %r15
testq %r15, %r15
js 0x44709
movq (%rsp), %r11
movq %rdi, (%r11,%r15,8)
jmp 0x44709
movq $-0x80000000, %r13      # imm = 0x80000000
movq $-0x1, 0x18(%rsp)
movq 0x10(%rsp), %rsi
movl %r13d, (%rsi,%rdi,4)
movq %r14, %r15
movq 0x18(%rsp), %rsi
movq %rsi, (%r14,%rdi,8)
movslq %r13d, %rsi
cmpq %rax, %rsi
cmovgq %rsi, %rax
cmovgq %rdi, %rcx
incq %rdi
movq 0x38(%rsp), %r12
incq %r12
addq $0x10, %rdx
cmpq 0x20(%rsp), %rdi
jne 0x4466f
jmp 0x447a1
movq %r15, %r13
jmp 0x4475d
xorl %eax, %eax
testq %rcx, %rcx
js 0x447c7
movq 0x8(%rsp), %r10
movq (%rsp), %r11
movq %rcx, (%r11,%rax,8)
incq %rax
movq (%r15,%rcx,8), %rcx
testq %rcx, %rcx
jns 0x447b1
jmp 0x447d0
xorl %eax, %eax
jmp 0x44834
movq 0x8(%rsp), %r10
movq (%rsp), %r11
cmpq $0x2, %rax
jb 0x44806
movq %rax, %rcx
shrq %rcx
negq %rcx
leaq (%r11,%rax,8), %rdx
addq $-0x8, %rdx
xorl %esi, %esi
movq %r11, %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 0x447ec
testq %rbx, %rbx
sete %cl
testq %rax, %rax
sete %dl
orb %cl, %dl
jne 0x44834
xorl %ecx, %ecx
movq (%r11,%rcx,8), %rdx
shlq $0x4, %rdx
movups (%r10,%rdx), %xmm0
movups %xmm0, (%rbx)
incq %rcx
addq $0x10, %rbx
cmpq %rcx, %rax
jne 0x44818
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq