// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: integer_chain_dp(bubble_type*, integer_t*, ul_str_t*, integer_aln_t*, ul_chain_t*, long, long, unsigned int, unsigned long, unsigned long*)
int64_t integer_chain_dp(bubble_type *bub, integer_t *buf, ul_str_t *str, integer_aln_t *aln, ul_chain_t *idx, int64_t idx_n, int64_t qid, uint32_t is_hom, 
uint64_t occ_thres, uint64_t *corrected)
{
    ul_str_t *qstr = &(str[qid]); int64_t k, q_n = qstr->cn, *f, *p, z, max_f, tf, tk, max_p, done_z, sc, csc, n_skip;
    kv_resize(int64_t, buf->f, qstr->cn); kv_resize(int64_t, buf->p, qstr->cn); 
    kv_resize(uint64_t, buf->o, qstr->cn); uint64_t *o;
    f = buf->f.a; p = buf->p.a; o = buf->o.a; if(corrected) (*corrected) = 0;

    // radix_sort_ul_chain_t_srt(idx, idx + idx_n);
    for (k = 0; k < idx_n; k++) idx[k].sc = 0;

    for (k = 0, tf = tk = -1, n_skip = 0; k < q_n; k++) {
        csc = buf->u.a[k]; 
        if((!is_hom) && (IF_HOM((((uint32_t)qstr->a[k])>>1), (*bub)))) {
            csc = -1; n_skip++;
        }
        max_p = -1; max_f = csc;
        if(k > 0 && max_f >= 0) {
            done_z = 0;
            if(is_hom) {//check all nodes
                memset(o, 0, sizeof((*o))*k); 
            } else {///mask hom nodes
                for (z = 0; z < k; z++) {
                    o[z] = 0;
                    if(f[z] == -1) {
                        o[z] = occ_thres; ///if node is hom, ignore it
                        done_z++;
                    }
                }
            }
            
            for (z = 0; z < idx_n && done_z < k; z++) {
                done_z += append_connective(aln, &(idx[z]), k, occ_thres, o);
            }
            assert(done_z <= k);
            for (z = k - 1; z >= 0; z--) {
                if(o[z] < occ_thres) continue;
                if(f[z] == -1) continue;///masked hom nodes
                sc = csc + f[z];
                if(sc > max_f) {
                    max_f = sc; max_p = z;
                }
            }
        }
        f[k] = max_f; p[k] = max_p;
        if(tf < max_f && max_f >= 0) {
            tf = max_f; tk = k;
        }

        // fprintf(stderr, "[M::%s::k->%ld] f[k]->%ld, p[k]->%ld, csc->%ld\n", __func__, k, f[k], p[k], csc);
    }
    if(tk < 0) return 0;
    for (k = tk, done_z = 0; k >= 0; k = p[k]) done_z++;
    ///might be fully corrected; but some of hom nodes have been skipped
    if((corrected) && ((n_skip+done_z) == q_n) ) {
        for (k = 0; k < idx_n; k++) idx[k].sc = 0;
        for (k = 1, (*corrected) = 0; k < q_n; k++) {
            if(!connective_conform(aln, idx, idx_n, k, occ_thres, (f[k] >= 0 && f[k-1] >= 0)?0:1)) break;
        }
        if(k >= q_n) (*corrected) = 1;
    }

    for (k = tk, sc = done_z; k >= 0; k = p[k]) o[--done_z] = k;
    return sc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, (%rsp)
movq %r8, 0x8(%rsp)
movq %rcx, 0x40(%rsp)
movq %rsi, %r13
movq %rdi, 0x48(%rsp)
movq 0xa0(%rsp), %rax
shlq $0x5, %rax
leaq (%rdx,%rax), %rcx
movq %rcx, 0x10(%rsp)
movl 0x18(%rdx,%rax), %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movq %rbx, %rax
cmpq %rbx, 0x50(%rsi)
jae 0x1fff60
leaq -0x1(%rbx), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x50(%r13)
movq 0x58(%r13), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x58(%r13)
movq 0x10(%rsp), %rax
movl 0x18(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, 0x68(%r13)
jae 0x1fffc4
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x68(%r13)
movq 0x70(%r13), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x70(%r13)
movq 0x10(%rsp), %rax
movl 0x18(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0xb8(%rsp), %r14
cmpq %rax, 0x80(%r13)
jae 0x200034
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x80(%r13)
movq 0x88(%r13), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, %r12
movq %rax, 0x88(%r13)
jmp 0x20003b
movq 0x88(%r13), %r12
movq 0x58(%r13), %rdi
movq 0x70(%r13), %r15
testq %r14, %r14
je 0x20004f
movq $0x0, (%r14)
cmpq $0x0, (%rsp)
jle 0x2000f8
movq (%rsp), %rcx
leaq 0x1(%rcx), %rax
andq $-0x2, %rax
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movq 0x8(%rsp), %rcx
addq $0x38, %rcx
movdqa 0x3124c(%rip), %xmm1    # 0x2312d0
movdqa 0x31254(%rip), %xmm2    # 0x2312e0
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x31464(%rip), %xmm4    # 0x231500
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %edx
notl %edx
testb $0x1, %dl
je 0x2000d5
movq $0x0, -0x28(%rcx)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %edx
testb $0x1, %dl
je 0x2000ea
movq $0x0, (%rcx)
paddq %xmm4, %xmm1
addq $0x50, %rcx
addq $-0x2, %rax
jne 0x20009c
testl %ebx, %ebx
je 0x20049b
movq 0xb0(%rsp), %rcx
movl 0xa8(%rsp), %r9d
movq $-0x1, %r10
movq $0x0, 0x20(%rsp)
movq $-0x1, %rsi
movq $-0x1, %r11
xorl %ebp, %ebp
movq %rdi, 0x28(%rsp)
movq %rbx, 0x58(%rsp)
movq %r15, 0x50(%rsp)
movq %r13, 0x60(%rsp)
movq 0xa0(%r13), %rax
movq (%rax,%rbp,8), %r14
testl %r9d, %r9d
je 0x20019f
testq %rbp, %rbp
setne %al
testq %r14, %r14
setns %dl
andb %al, %dl
movq $-0x1, %rax
cmpb $0x1, %dl
jne 0x2002f7
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq %rsi, 0x18(%rsp)
leaq (,%rbp,8), %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x20e0
movq 0xb0(%rsp), %rcx
xorl %eax, %eax
jmp 0x20021d
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movl (%rax,%rbp,8), %eax
shrl %eax
movq 0x48(%rsp), %rdx
movq (%rdx), %r8
movq 0x80(%rdx), %rdx
movl (%r8,%rax,4), %eax
incq %rdx
cmpq %rax, %rdx
movq $-0x1, %rax
movq %r14, %rdx
cmovbq %rax, %rdx
adcq $0x0, 0x20(%rsp)
testq %rbp, %rbp
je 0x200273
testq %rdx, %rdx
js 0x200273
xorl %eax, %eax
xorl %edx, %edx
movq $0x0, (%r12,%rdx,8)
cmpq $-0x1, (%rdi,%rdx,8)
jne 0x200206
movq %rcx, (%r12,%rdx,8)
incq %rax
incq %rdx
cmpq %rdx, %rbp
jne 0x2001f0
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq %rsi, 0x18(%rsp)
cmpq $0x0, (%rsp)
setg %sil
cmpq %rbp, %rax
setl %dl
andb %sil, %dl
cmpb $0x1, %dl
jne 0x200280
movl $0x1, %ebx
movq 0x8(%rsp), %r13
movq %rax, %r15
movq 0x40(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r12, %r8
callq 0x1ffc9e
addq %r15, %rax
cmpq (%rsp), %rbx
jge 0x200278
incq %rbx
addq $0x28, %r13
cmpq %rbp, %rax
movq 0xb0(%rsp), %rcx
jl 0x20023e
jmp 0x200280
movq %rdx, %r14
jmp 0x2002f7
movq 0xb0(%rsp), %rcx
cmpq %rbp, %rax
movq 0x60(%rsp), %r13
jg 0x2004b0
testq %rbp, %rbp
movq 0x58(%rsp), %rbx
movq 0x50(%rsp), %r15
movq 0x28(%rsp), %rdi
movl 0xa8(%rsp), %r9d
movq 0x38(%rsp), %r10
movq 0x30(%rsp), %r11
movq $-0x1, %rax
je 0x2002f2
movq %r10, %rdx
movq %r14, %r8
cmpq %rcx, (%r12,%rdx,8)
jb 0x2002df
movq (%rdi,%rdx,8), %rsi
cmpq $-0x1, %rsi
je 0x2002df
addq %r14, %rsi
cmpq %r8, %rsi
cmovgq %rsi, %r8
cmovgq %rdx, %rax
leaq -0x1(%rdx), %rsi
incq %rdx
cmpq $0x1, %rdx
movq %rsi, %rdx
jg 0x2002c1
movq %r8, %r14
movq 0x18(%rsp), %rsi
cmpq %r14, %r11
setl %r8b
testq %r14, %r14
setns %dl
testb %dl, %r8b
movq %r14, (%rdi,%rbp,8)
cmovneq %r14, %r11
cmovneq %rbp, %rsi
movq %rax, (%r15,%rbp,8)
incq %rbp
incq %r10
cmpq %rbx, %rbp
jne 0x200144
testq %rsi, %rsi
js 0x20049b
addq $-0x8, %r12
xorl %r13d, %r13d
movq %rsi, %rax
incq %r13
movq (%r15,%rax,8), %rax
addq $0x8, %r12
testq %rax, %rax
jns 0x200339
movq 0xb8(%rsp), %r14
testq %r14, %r14
je 0x200488
movq %rbx, %rax
subq 0x20(%rsp), %rax
cmpq %r13, %rax
jne 0x200488
cmpq $0x0, (%rsp)
jle 0x200414
movq (%rsp), %rcx
leaq 0x1(%rcx), %rax
andq $-0x2, %rax
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movq 0x8(%rsp), %rcx
addq $0x38, %rcx
movdqa 0x30f30(%rip), %xmm1    # 0x2312d0
movdqa 0x30f38(%rip), %xmm2    # 0x2312e0
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x31148(%rip), %xmm4    # 0x231500
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %edx
notl %edx
testb $0x1, %dl
je 0x2003f1
movq $0x0, -0x28(%rcx)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %edx
testb $0x1, %dl
je 0x200406
movq $0x0, (%rcx)
paddq %xmm4, %xmm1
addq $0x50, %rcx
addq $-0x2, %rax
jne 0x2003b8
movq 0xb8(%rsp), %rax
movq $0x0, (%rax)
cmpl $0x1, %ebx
je 0x200479
movl $0x1, %ebp
movq %rsi, %r14
cmpq $0x0, (%rdi,%rbp,8)
movl $0x1, %r9d
js 0x200446
movq -0x8(%rdi,%rbp,8), %r9
shrq $0x3f, %r9
movq 0x40(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq (%rsp), %rdx
movq %rbp, %rcx
movq 0xb0(%rsp), %r8
callq 0x1ffd60
movq 0x28(%rsp), %rdi
testq %rax, %rax
movq %r14, %rsi
je 0x200488
incq %rbp
cmpq %rbp, %rbx
jne 0x20042d
movq 0xb8(%rsp), %rax
movq $0x1, (%rax)
movq %rsi, (%r12)
movq (%r15,%rsi,8), %rsi
addq $-0x8, %r12
testq %rsi, %rsi
jns 0x200488
jmp 0x20049e
xorl %r13d, %r13d
movq %r13, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x45fdb(%rip), %rdi     # 0x246492
leaq 0x4514b(%rip), %rsi     # 0x245609
leaq 0x45fd9(%rip), %rcx     # 0x24649e
movl $0x1299, %edx           # imm = 0x1299
callq 0x2110