// File: /workspace/github_cpp_makefile_star2_O1/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, 0x30(%rsp)
movq %rsi, %r15
movq %rdi, 0x38(%rsp)
movq 0xa0(%rsp), %rax
shlq $0x5, %rax
leaq (%rdx,%rax), %rcx
movq %rcx, 0x18(%rsp)
movl 0x18(%rdx,%rax), %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
cmpq %rbx, 0x50(%rsi)
jae 0x1f4b18
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(%r15)
movq 0x58(%r15), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x58(%r15)
movq 0x18(%rsp), %rax
movl 0x18(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, 0x68(%r15)
jae 0x1f4b7c
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(%r15)
movq 0x70(%r15), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x70(%r15)
movq 0xb8(%rsp), %r14
movq 0x18(%rsp), %rax
movl 0x18(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, 0x80(%r15)
jae 0x1f4bf4
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(%r15)
movq 0x88(%r15), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x88(%r15)
movq 0x58(%r15), %r8
movq 0x70(%r15), %rdi
movq 0x88(%r15), %rbp
testq %r14, %r14
je 0x1f4c0f
movq $0x0, (%r14)
cmpq $0x0, (%rsp)
jle 0x1f4c33
movq 0x8(%rsp), %rax
addq $0x10, %rax
movq (%rsp), %rcx
movq $0x0, (%rax)
addq $0x28, %rax
decq %rcx
jne 0x1f4c23
movq 0xb0(%rsp), %r10
testl %ebx, %ebx
movq %rdi, 0x20(%rsp)
movq %r8, 0x28(%rsp)
je 0x1f4e37
movl 0xa8(%rsp), %r11d
movq $0x0, 0x10(%rsp)
movq $-0x1, %r12
movq $-0x1, %r9
movq $-0x1, %r14
xorl %r13d, %r13d
movq %rbx, 0x48(%rsp)
movq %r15, 0x40(%rsp)
movq %r14, 0x60(%rsp)
movq 0xa0(%r15), %rax
movq (%rax,%r13,8), %r14
testl %r11d, %r11d
jne 0x1f4ccd
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movl (%rax,%r13,8), %eax
shrl %eax
movq 0x38(%rsp), %rdx
movq (%rdx), %rcx
movq 0x80(%rdx), %rdx
movl (%rcx,%rax,4), %eax
incq %rdx
cmpq %rax, %rdx
movq $-0x1, %rax
cmovbq %rax, %r14
adcq $0x0, 0x10(%rsp)
movq $-0x1, %rax
testq %r13, %r13
je 0x1f4d06
testq %r14, %r14
js 0x1f4d06
movq %r9, 0x58(%rsp)
testl %r11d, %r11d
je 0x1f4d0e
leaq (,%r13,8), %rdx
movq %rbp, %rdi
xorl %esi, %esi
callq 0x20e0
movq 0xb0(%rsp), %r10
xorl %eax, %eax
jmp 0x1f4d32
movq %r14, %rcx
jmp 0x1f4e01
xorl %eax, %eax
xorl %ecx, %ecx
movq $0x0, (%rbp,%rcx,8)
cmpq $-0x1, (%r8,%rcx,8)
jne 0x1f4d2a
movq %r10, (%rbp,%rcx,8)
incq %rax
incq %rcx
cmpq %rcx, %r13
jne 0x1f4d12
movq %r12, 0x50(%rsp)
cmpq $0x0, (%rsp)
setg %cl
cmpq %r13, %rax
setl %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x1f4d96
movl $0x1, %ebx
movq 0x8(%rsp), %r12
movq %rax, %r15
movq 0x30(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %r10, %rcx
movq %rbp, %r8
callq 0x1f47c3
addq %r15, %rax
cmpq (%rsp), %rbx
jge 0x1f4d8e
incq %rbx
addq $0x28, %r12
cmpq %r13, %rax
movq 0xb0(%rsp), %r10
jl 0x1f4d56
jmp 0x1f4d96
movq 0xb0(%rsp), %r10
cmpq %r13, %rax
movq 0x28(%rsp), %r8
jg 0x1f4f60
movq $-0x1, %rax
movq 0x50(%rsp), %r12
movq %r12, %rdx
movq %r14, %rcx
movq 0x48(%rsp), %rbx
movq 0x40(%rsp), %r15
movq 0x20(%rsp), %rdi
movq 0x58(%rsp), %r9
movl 0xa8(%rsp), %r11d
cmpq %r10, (%rbp,%rdx,8)
jb 0x1f4df1
movq (%r8,%rdx,8), %rsi
cmpq $-0x1, %rsi
je 0x1f4df1
addq %r14, %rsi
cmpq %rcx, %rsi
cmovgq %rsi, %rcx
cmovgq %rdx, %rax
leaq -0x1(%rdx), %rsi
incq %rdx
cmpq $0x1, %rdx
movq %rsi, %rdx
jg 0x1f4dd2
movq 0x60(%rsp), %r14
cmpq %rcx, %r14
setl %dl
testq %rcx, %rcx
setns %sil
testb %sil, %dl
movq %rcx, (%r8,%r13,8)
cmovneq %rcx, %r14
cmovneq %r13, %r9
movq %rax, (%rdi,%r13,8)
incq %r13
incq %r12
cmpq %rbx, %r13
jne 0x1f4c80
jmp 0x1f4e47
movq $-0x1, %r9
movq $0x0, 0x10(%rsp)
testq %r9, %r9
js 0x1f4f1e
addq $-0x8, %rbp
xorl %r12d, %r12d
movq %r9, %r15
movq %r9, %rax
incq %r12
movq (%rdi,%rax,8), %rax
addq $0x8, %rbp
testq %rax, %rax
jns 0x1f4e5d
cmpq $0x0, 0xb8(%rsp)
je 0x1f4f38
movq %rbx, %rax
subq 0x10(%rsp), %rax
cmpq %r12, %rax
jne 0x1f4f38
cmpq $0x0, (%rsp)
jle 0x1f4eb1
movq 0x8(%rsp), %rax
addq $0x10, %rax
movq (%rsp), %rcx
movq $0x0, (%rax)
addq $0x28, %rax
decq %rcx
jne 0x1f4ea1
movq 0xb8(%rsp), %rax
movq $0x0, (%rax)
cmpl $0x2, %ebx
setae %r14b
jb 0x1f4f29
movl $0x1, %r13d
movq 0x28(%rsp), %rax
cmpq $0x0, (%rax,%r13,8)
movl $0x1, %r9d
js 0x1f4eea
movq -0x8(%rax,%r13,8), %r9
shrq $0x3f, %r9
movq 0x30(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq (%rsp), %rdx
movq %r13, %rcx
movq %r10, %r8
callq 0x1f48bb
testq %rax, %rax
je 0x1f4f23
incq %r13
cmpq %rbx, %r13
setb %r14b
movq 0xb0(%rsp), %r10
jne 0x1f4ecf
jmp 0x1f4f29
xorl %r12d, %r12d
jmp 0x1f4f4e
testb $0x1, %r14b
jne 0x1f4f38
movq 0xb8(%rsp), %rax
movq $0x1, (%rax)
movq 0x20(%rsp), %rax
movq %r15, (%rbp)
movq (%rax,%r15,8), %r15
addq $-0x8, %rbp
testq %r15, %r15
jns 0x1f4f3d
movq %r12, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x45247(%rip), %rdi     # 0x23a1ae
leaq 0x443b7(%rip), %rsi     # 0x239325
leaq 0x45245(%rip), %rcx     # 0x23a1ba
movl $0x1299, %edx           # imm = 0x1299
callq 0x2110