// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movq %r9, 0x18(%rsp)
movq %r8, 0x8(%rsp)
movq %rcx, 0x50(%rsp)
movq %rsi, %rbx
movq %rdi, 0x60(%rsp)
movq 0xb0(%rsp), %rax
shlq $0x5, %rax
leaq (%rdx,%rax), %rcx
movq %rcx, 0x10(%rsp)
movl 0x18(%rdx,%rax), %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movq %r15, %rax
cmpq %r15, 0x50(%rsi)
jae 0x1b3e6c
leaq -0x1(%r15), %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(%rbx)
movq 0x58(%rbx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x58(%rbx)
movq 0x10(%rsp), %rax
movl 0x18(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, 0x68(%rbx)
jae 0x1b3ed0
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(%rbx)
movq 0x70(%rbx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x70(%rbx)
movq 0x10(%rsp), %rax
movl 0x18(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0xc8(%rsp), %r14
cmpq %rax, 0x80(%rbx)
jae 0x1b3f40
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(%rbx)
movq 0x88(%rbx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, %rbp
movq %rax, 0x88(%rbx)
jmp 0x1b3f47
movq 0x88(%rbx), %rbp
movq 0x58(%rbx), %rdi
movq 0x70(%rbx), %rsi
testq %r14, %r14
je 0x1b3f58
andq $0x0, (%r14)
movq 0xc0(%rsp), %r14
movl 0xb8(%rsp), %r9d
xorl %ecx, %ecx
movq 0x18(%rsp), %rax
testq %rax, %rax
cmovgq %rax, %rcx
movq 0x8(%rsp), %rax
addq $0x10, %rax
movq %rcx, 0x58(%rsp)
subq $0x1, %rcx
jb 0x1b3f94
andq $0x0, (%rax)
addq $0x28, %rax
jmp 0x1b3f84
pushq $-0x1
popq %r10
xorl %r13d, %r13d
movq %r10, %r11
movq %r10, %r8
movq $0x0, 0x20(%rsp)
movq %r15, 0x70(%rsp)
movq %rbx, 0x68(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, 0x30(%rsp)
cmpq %r15, %r13
je 0x1b415b
movq 0xa0(%rbx), %rax
movq (%rax,%r13,8), %r12
testl %r9d, %r9d
je 0x1b4013
testq %r13, %r13
setne %al
testq %r12, %r12
setns %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1b4077
movq %r12, 0x38(%rsp)
movq %r11, 0x40(%rsp)
movq %r8, 0x48(%rsp)
leaq (,%r13,8), %rdx
movq %rbp, %rdi
xorl %esi, %esi
callq 0x20e0
xorl %ebx, %ebx
jmp 0x1b408e
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movl (%rax,%r13,8), %eax
shrl %eax
movq 0x60(%rsp), %rdx
movq (%rdx), %rcx
movq 0x80(%rdx), %rdx
movl (%rcx,%rax,4), %eax
incq %rdx
cmpq %rax, %rdx
movq %r12, %rax
cmovbq %r10, %rax
adcq $0x0, 0x20(%rsp)
testq %r13, %r13
je 0x1b4074
testq %rax, %rax
js 0x1b4074
xorl %eax, %eax
xorl %ebx, %ebx
cmpq %rax, %r13
je 0x1b407f
andq $0x0, (%rbp,%rax,8)
cmpq $-0x1, (%rdi,%rax,8)
jne 0x1b406f
movq %r14, (%rbp,%rax,8)
incq %rbx
incq %rax
jmp 0x1b4055
movq %rax, %r12
movq %r10, %rax
jmp 0x1b4135
movq %r12, 0x38(%rsp)
movq %r11, 0x40(%rsp)
movq %r8, 0x48(%rsp)
movq 0x8(%rsp), %r12
xorl %r15d, %r15d
cmpq 0x18(%rsp), %r15
jge 0x1b40c4
cmpq %r13, %rbx
jge 0x1b40c4
movq 0x50(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %r14, %rcx
movq %rbp, %r8
callq 0x1b3b97
addq %rax, %rbx
incq %r15
addq $0x28, %r12
jmp 0x1b4096
cmpq %r13, %rbx
movq 0x30(%rsp), %rdi
jg 0x1b4250
movq %r13, %rdx
movq 0x38(%rsp), %r12
movq %r12, %rcx
pushq $-0x1
popq %r10
movq %r10, %rax
movq 0x70(%rsp), %r15
movq 0x68(%rsp), %rbx
movq 0x48(%rsp), %r8
movl 0xb8(%rsp), %r9d
movq 0x40(%rsp), %r11
decq %rdx
leaq 0x1(%rdx), %rsi
testq %rsi, %rsi
jle 0x1b412d
cmpq %r14, (%rbp,%rdx,8)
jb 0x1b4100
movq (%rdi,%rdx,8), %rsi
cmpq $-0x1, %rsi
je 0x1b4100
addq %r12, %rsi
cmpq %rcx, %rsi
cmovgq %rsi, %rcx
cmovgq %rdx, %rax
jmp 0x1b4100
movq %rcx, %r12
movq 0x28(%rsp), %rsi
cmpq %r12, %r11
setl %cl
testq %r12, %r12
setns %dl
testb %dl, %cl
movq %r12, (%rdi,%r13,8)
cmovneq %r12, %r11
movq %rax, (%rsi,%r13,8)
cmovneq %r13, %r8
incq %r13
jmp 0x1b3fbe
testq %r8, %r8
js 0x1b41ba
addq $-0x8, %rbp
xorl %r12d, %r12d
movq %r8, %rax
movq 0x58(%rsp), %rcx
testq %rax, %rax
js 0x1b4181
incq %r12
movq (%rsi,%rax,8), %rax
addq $0x8, %rbp
jmp 0x1b416f
cmpq $0x0, 0xc8(%rsp)
je 0x1b4239
movq %r15, %rax
subq 0x20(%rsp), %rax
cmpq %r12, %rax
jne 0x1b4239
movq 0x8(%rsp), %rax
addq $0x10, %rax
subq $0x1, %rcx
jb 0x1b41bf
andq $0x0, (%rax)
addq $0x28, %rax
jmp 0x1b41aa
xorl %r12d, %r12d
jmp 0x1b423e
movq 0xc8(%rsp), %rax
andq $0x0, (%rax)
pushq $0x1
popq %r13
cmpq %r15, %r13
jae 0x1b421c
movq %r8, %rbx
cmpq $0x0, (%rdi,%r13,8)
pushq $0x1
popq %r9
js 0x1b41eb
movq -0x8(%rdi,%r13,8), %r9
shrq $0x3f, %r9
movq 0x50(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r13, %rcx
movq %r14, %r8
callq 0x1b3c3e
movq 0x30(%rsp), %rdi
incq %r13
testq %rax, %rax
movq 0x28(%rsp), %rsi
movq %rbx, %r8
jne 0x1b41cf
jmp 0x1b4239
movq 0xc8(%rsp), %rax
movq $0x1, (%rax)
jmp 0x1b4239
movq %r8, (%rbp)
movq (%rsi,%r8,8), %r8
addq $-0x8, %rbp
testq %r8, %r8
jns 0x1b422d
movq %r12, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x44197(%rip), %rdi     # 0x1f83ee
leaq 0x4327b(%rip), %rsi     # 0x1f74d9
leaq 0x44195(%rip), %rcx     # 0x1f83fa
movl $0x1299, %edx           # imm = 0x1299
callq 0x2110