// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: ul_dp0(bubble_type*, asg_t*, path_dp_t*, double)
int64_t ul_dp0(bubble_type* bub, asg_t *g, path_dp_t *dp, double pass_thres/**, uint64_t is_debug**/)
{
    if(dp->pat.n < 2 || dp->ref.n < 2) return -1;
    uint64_t i, j, d, w; int64_t sc0, sc1, sc2, sc, sc_i, sc_j;
    init_ul_dp(g, dp);
    // if(is_debug) {
    //     print_dp_matrix(dp);
    // }
    for (i = 1; i < dp->n; i++) {//pat
        for (j = 1; j < dp->m; j++) {//mat
            w = node_check(((uint32_t)dp->pat.a[i-1]), ((uint32_t)dp->ref.a[j-1]), 
                        dp->pat.a[i-1]>>32, dp->ref.a[j-1]>>32, 0.04, g->seq[((uint32_t)dp->pat.a[i-1])>>1].len);

            if(w) {
                // if(is_debug) fprintf(stderr, "\n[M::%s::] i::%lu, j::%lu, w::%lu\n", __func__, i, j, w);
                dp->m_score.a[ul_dp_idx(*dp, i, j)] = dp->m_score.a[ul_dp_idx(*dp, i-1, j-1)] + w;
                dp->m_dir.a[ul_dp_idx(*dp, i, j)] = e_mdp;
            } else {
                sc0 = dp->m_score.a[ul_dp_idx(*dp, i-1, j-1)] - 
                        (int64_t)(MIN(g->seq[((uint32_t)dp->pat.a[i-1])>>1].len, g->seq[((uint32_t)dp->ref.a[j-1])>>1].len));
                sc1 = dp->m_score.a[ul_dp_idx(*dp, i-1, j)] - (int64_t)(g->seq[((uint32_t)dp->ref.a[j-1])>>1].len);
                sc2 = dp->m_score.a[ul_dp_idx(*dp, i, j-1)] - (int64_t)(g->seq[((uint32_t)dp->pat.a[i-1])>>1].len);

                d = ue_mdp; sc = sc0;
                if(sc < sc1) {
                    sc = sc1; d = lref_dp;
                }
                if(sc < sc2) {
                    sc = sc2; d = lpat_dp;
                }
                dp->m_score.a[ul_dp_idx(*dp, i, j)] = sc;
                dp->m_dir.a[ul_dp_idx(*dp, i, j)] = d;
            }
        }
    }

    sc = 0; sc_i = sc_j = -1;
    for (j = 1, i = dp->n - 1; j < dp->m; j++) {
        if(sc_i < 0 || sc < dp->m_score.a[ul_dp_idx(*dp, i, j)]) {
            sc_i = i; sc_j = j; sc = dp->m_score.a[ul_dp_idx(*dp, i, j)];
        }
    }
    for (i = 1, j = dp->m - 1; i < dp->n; i++) {
        if(sc_i < 0 || sc < dp->m_score.a[ul_dp_idx(*dp, i, j)]) {
            sc_i = i; sc_j = j; sc = dp->m_score.a[ul_dp_idx(*dp, i, j)];
        }
    }

    // if(is_debug) {
    //     fprintf(stderr, "[M::%s::] sc_i::%ld, sc_j::%ld, sc::%ld\n", __func__, sc_i, sc_j, sc);
    //     // print_dp_matrix(dp);
    // }

    if (sc_i < 0 || sc_i == 1 || sc_j == 1) return 0;
    int64_t match_all = 0, unmatch_all = 0, het_match_all = 0, het_unmatch_all = 0, het_occ = 0;
    //pat_s = sc_i - 1; ref_s = ref_e = sc_j - 1;
    for(i = sc_i, j = sc_j; i > 0 && j > 0;) {
        d = dp->m_dir.a[ul_dp_idx(*dp, i, j)];
        //pat_s = i - 1; ref_s = j - 1;
        // if(is_debug) fprintf(stderr, "[M::%s::] i::%lu, j::%lu, dir::%lu\n", __func__, i, j, d);
        // if(pat_s == 0) continue;
        if(d == e_mdp) {
            // if(i != 1 || j != 1) {//skip (i == 1 && j == 1)
            match_all += g->seq[((uint32_t)dp->pat.a[i-1])>>1].len;
            if(!IF_HOM((((uint32_t)dp->pat.a[i-1])>>1), *bub)) {
                het_match_all += g->seq[((uint32_t)dp->pat.a[i-1])>>1].len;
                het_occ++;
            }
            i--; j--; 
            // if(is_debug) {
            //     fprintf(stderr, "[M::%s::] PAT::utg%.6dl(%c::len->%lu), REF::utg%.6dl(%c::len->%lu)\n", __func__, 
            //         ((((uint32_t)dp->pat.a[i])>>1))+1, "+-"[((uint32_t)dp->pat.a[i])&1], dp->pat.a[i]>>32,
            //         ((((uint32_t)dp->ref.a[j])>>1))+1, "+-"[((uint32_t)dp->ref.a[j])&1], dp->ref.a[j]>>32);
            // }
        }
        if(d == ue_mdp) {
            unmatch_all += g->seq[((uint32_t)dp->pat.a[i-1])>>1].len;
            if(!IF_HOM((((uint32_t)dp->pat.a[i-1])>>1), *bub)) {
                het_unmatch_all += g->seq[((uint32_t)dp->pat.a[i-1])>>1].len;
            }
            i--; j--;
        }
        if(d == lpat_dp) {
            j--;
        }
        if(d == lref_dp) {
            unmatch_all += g->seq[((uint32_t)dp->pat.a[i-1])>>1].len;
            if(!IF_HOM((((uint32_t)dp->pat.a[i-1])>>1), *bub)) {
                het_unmatch_all += g->seq[((uint32_t)dp->pat.a[i-1])>>1].len;
            }
            i--;
        }
    }
    // if(is_debug) {
    //     fprintf(stderr, "[M::%s::] het_occ::%ld, match_all::%ld, unmatch_all::%ld, het_match_all::%ld, het_unmatch_all::%ld\n", __func__, 
    //     het_occ, match_all, unmatch_all, het_match_all, het_unmatch_all);
    // }

    assert((j == 0) && (((uint32_t)dp->pat.a[i]) == ((uint32_t)dp->ref.a[j])));
    ///skip the beg node
    match_all -= g->seq[((uint32_t)dp->pat.a[i])>>1].len;
    if(!IF_HOM((((uint32_t)dp->pat.a[i])>>1), *bub)) {
        het_match_all -= g->seq[((uint32_t)dp->pat.a[i])>>1].len; het_occ--;
    }


    if(het_occ < 1 || match_all == 0 || het_match_all == 0) return -1;
    if(match_all <= (match_all + unmatch_all)*pass_thres) return -1;
    if(het_match_all <= (het_match_all + het_unmatch_all)*pass_thres) return -1;
    return (((double)het_match_all)/((double)(het_match_all + het_unmatch_all)))*
                            (((uint32_t)dp->pat_cor.a[i]) - (dp->pat_cor.a[i]>>32));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq $-0x1, %rax
cmpq $0x2, 0x18(%rdx)
jb 0x1fcecf
movq %rdx, %rbx
cmpq $0x2, (%rdx)
jae 0x1fcede
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %r15
movq %rdi, 0x10(%rsp)
movsd %xmm0, 0x8(%rsp)
movq %rbx, %rsi
callq 0x1fcbe6
movq 0x90(%rbx), %r8
cmpq $0x1, %r8
jbe 0x1fd0db
movq 0x98(%rbx), %rcx
movl $0x1, %eax
movl $0x8, %edx
xorl %esi, %esi
movsd 0x346c8(%rip), %xmm0    # 0x2315e8
movsd 0x343e0(%rip), %xmm1    # 0x231308
movl $0x2, %edi
movsd 0x8(%rsp), %xmm5
cmpq $0x2, %rcx
jb 0x1fd0c5
movl $0x1, %r8d
movq 0x10(%rbx), %r9
movq 0x28(%rbx), %r10
movq -0x8(%r10,%rax,8), %r12
movq -0x8(%r9,%r8,8), %r10
movq 0x20(%r15), %r11
movl %r12d, %r9d
shrl %r9d
movl (%r11,%r9,8), %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
cmpl %r10d, %r12d
jne 0x1fd018
movq %r10, %r13
shrq $0x20, %r13
shrq $0x20, %r12
movl %r13d, %r14d
subl %r12d, %r14d
movl %r12d, %ebp
subl %r13d, %ebp
cmpq %r13, %r12
cmovbl %r14d, %ebp
xorps %xmm3, %xmm3
cvtsi2sd %rbp, %xmm3
movl %r13d, %r14d
xorps %xmm2, %xmm2
cvtsi2sd %r14, %xmm2
movapd %xmm2, %xmm4
mulsd %xmm0, %xmm4
ucomisd %xmm4, %xmm3
ja 0x1fd018
movq %r9, %r12
testq %r13, %r13
je 0x1fcff0
subl %ebp, %r13d
xorps %xmm3, %xmm3
cvtsi2sd %r13, %xmm3
xorps %xmm4, %xmm4
cvtsi2sd %r9d, %xmm4
divsd %xmm2, %xmm3
mulsd %xmm3, %xmm4
cvttsd2si %xmm4, %r14
movq %r14, %r13
sarq $0x3f, %r13
subsd %xmm1, %xmm4
cvttsd2si %xmm4, %r12
andq %r13, %r12
orq %r14, %r12
testq %r12, %r12
je 0x1fd018
movq 0x88(%rbx), %r9
movq %rsi, %r10
imulq %rcx, %r10
addq %r9, %r10
addq -0x8(%r10,%r8,8), %r12
imulq %rax, %rcx
leaq (%r9,%rcx,8), %rcx
xorl %r9d, %r9d
jmp 0x1fd094
movq 0x88(%rbx), %r13
movq %rsi, %r14
imulq %rcx, %r14
addq %r13, %r14
shrl %r10d
movl (%r11,%r10,8), %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
cmpl %r10d, %r9d
movl %r10d, %r11d
cmovbl %r9d, %r11d
movq -0x8(%r14,%r8,8), %rbp
subq %r11, %rbp
movq (%r14,%r8,8), %r12
subq %r10, %r12
movq %rdx, %r10
imulq %rcx, %r10
imulq %rax, %rcx
leaq (,%rcx,8), %rcx
addq %r13, %rcx
addq %r13, %r10
movq -0x8(%r10,%r8,8), %r10
movl %r9d, %r9d
subq %r9, %r10
cmpq %r12, %rbp
setl %r9b
cmovgq %rbp, %r12
addb %r9b, %r9b
incb %r9b
cmpq %r10, %r12
movzbl %r9b, %r9d
cmovll %edi, %r9d
cmovleq %r10, %r12
movq %r12, (%rcx,%r8,8)
movq 0x98(%rbx), %rcx
imulq %rax, %rcx
addq 0x70(%rbx), %rcx
movb %r9b, (%r8,%rcx)
incq %r8
movq 0x98(%rbx), %rcx
cmpq %rcx, %r8
jb 0x1fcf43
movq 0x90(%rbx), %r8
incq %rax
addq $0x8, %rsi
addq $0x8, %rdx
cmpq %r8, %rax
jb 0x1fcf33
jmp 0x1fd0e8
movq 0x98(%rbx), %rcx
movsd 0x8(%rsp), %xmm5
cmpq $0x2, %rcx
jb 0x1fd141
leaq -0x1(%r8), %rdi
movq %rcx, %r9
imulq %rdi, %r9
shlq $0x3, %r9
addq 0x88(%rbx), %r9
movl $0x1, %r10d
movq $-0x1, %rsi
xorl %eax, %eax
movq $-0x1, %rdx
testq %rdx, %rdx
js 0x1fd12d
movq (%r9,%r10,8), %r11
cmpq %r11, %rax
jge 0x1fd137
movq %r11, %rax
jmp 0x1fd131
movq (%r9,%r10,8), %rax
movq %rdi, %rdx
movq %r10, %rsi
incq %r10
cmpq %r10, %rcx
jne 0x1fd11a
jmp 0x1fd151
movq $-0x1, %rdx
xorl %eax, %eax
movq $-0x1, %rsi
cmpq $0x2, %r8
jb 0x1fd19c
leaq -0x1(%rcx), %rdi
movq 0x88(%rbx), %r9
movq %rcx, %r10
shlq $0x4, %r10
addq %r10, %r9
addq $-0x8, %r9
leaq (,%rcx,8), %r10
movl $0x1, %r11d
testq %rdx, %rdx
js 0x1fd188
cmpq (%r9), %rax
jge 0x1fd191
movq (%r9), %rax
movq %r11, %rdx
movq %rdi, %rsi
incq %r11
addq %r10, %r9
cmpq %r11, %r8
jne 0x1fd17e
xorl %eax, %eax
testq %rdx, %rdx
js 0x1fcecf
cmpq $0x1, %rdx
je 0x1fcecf
cmpq $0x1, %rsi
movq 0x10(%rsp), %r8
je 0x1fcecf
xorl %edi, %edi
testq %rdx, %rdx
je 0x1fd314
testq %rsi, %rsi
je 0x1fd314
movq 0x70(%rbx), %rax
xorl %r11d, %r11d
xorl %r9d, %r9d
movq $0x0, 0x8(%rsp)
xorl %r10d, %r10d
xorl %edi, %edi
movq %rdx, %r14
imulq %rcx, %r14
addq %rax, %r14
movzbl (%rsi,%r14), %ebp
cmpl $0x1, %ebp
je 0x1fd250
testl %ebp, %ebp
jne 0x1fd2b1
movq 0x20(%r15), %r14
movq 0x28(%rbx), %r13
movl -0x8(%r13,%rdx,8), %r13d
shrl %r13d
movl (%r14,%r13,8), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
addq %r14, %rdi
movq (%r8), %rbp
movq 0x80(%r8), %r12
movl (%rbp,%r13,4), %r13d
incq %r12
cmpq %r13, %r12
movl $0x0, %r12d
cmovbq %r12, %r14
sbbq $-0x1, %r11
addq %r14, 0x8(%rsp)
jmp 0x1fd290
movq 0x20(%r15), %r14
movq 0x28(%rbx), %r12
movl -0x8(%r12,%rdx,8), %r12d
shrl %r12d
movl (%r14,%r12,8), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
addq %r14, %r10
movq (%r8), %r13
movq 0x80(%r8), %rbp
movl (%r13,%r12,4), %r12d
incq %rbp
cmpq %r12, %rbp
movl $0x0, %r12d
cmovaeq %r14, %r12
addq %r12, %r9
decq %rdx
movq $-0x1, %r13
addq %r13, %rsi
testq %rdx, %rdx
je 0x1fd326
testq %rsi, %rsi
jne 0x1fd1ec
jmp 0x1fd326
xorl %r13d, %r13d
cmpb $0x2, %bpl
sete %r13b
negq %r13
cmpb $0x3, %bpl
jne 0x1fd29a
movq 0x20(%r15), %r14
movq 0x28(%rbx), %r12
movl -0x8(%r12,%rdx,8), %r12d
shrl %r12d
movl (%r14,%r12,8), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
addq %r14, %r10
movq (%r8), %rbp
movq 0x10(%rsp), %r8
movq 0x80(%r8), %r8
movl (%rbp,%r12,4), %r12d
incq %r8
cmpq %r12, %r8
movl $0x0, %r8d
cmovaeq %r14, %r8
addq %r8, %r9
movq 0x10(%rsp), %r8
decq %rdx
jmp 0x1fd29a
xorl %r10d, %r10d
movq $0x0, 0x8(%rsp)
xorl %r9d, %r9d
xorl %r11d, %r11d
testq %rsi, %rsi
jne 0x1fd41f
movq 0x10(%rbx), %rcx
movq 0x28(%rbx), %rax
movq (%rax,%rdx,8), %rax
cmpl (%rcx), %eax
jne 0x1fd41f
movq 0x20(%r15), %rcx
shrl %eax
movl (%rcx,%rax,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq (%r8), %rcx
movq 0x80(%r8), %r8
movl (%rcx,%rax,4), %eax
incq %r8
xorl %ecx, %ecx
cmpq %rax, %r8
cmovaeq %rsi, %rcx
adcq $-0x1, %r11
movq $-0x1, %rax
testq %r11, %r11
jle 0x1fcecf
subq %rsi, %rdi
je 0x1fcecf
movq 0x8(%rsp), %rsi
cmpq %rcx, %rsi
je 0x1fcecf
xorps %xmm0, %xmm0
cvtsi2sd %rdi, %xmm0
addq %rdi, %r10
xorps %xmm1, %xmm1
cvtsi2sd %r10, %xmm1
mulsd %xmm5, %xmm1
ucomisd %xmm0, %xmm1
jae 0x1fcecf
subq %rcx, %rsi
xorps %xmm0, %xmm0
cvtsi2sd %rsi, %xmm0
addq %r9, %rsi
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
mulsd %xmm1, %xmm5
ucomisd %xmm0, %xmm5
jae 0x1fcecf
divsd %xmm1, %xmm0
movq 0x40(%rbx), %rax
movq (%rax,%rdx,8), %rax
movl %eax, %ecx
shrq $0x20, %rax
subq %rax, %rcx
movq %rcx, %xmm1
punpckldq 0x33e83(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x33e8b(%rip), %xmm1    # 0x231290
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %rax
jmp 0x1fcecf
leaq 0x48c4b(%rip), %rdi     # 0x246071
leaq 0x481dc(%rip), %rsi     # 0x245609
leaq 0x48c80(%rip), %rcx     # 0x2460b4
movl $0xeb0, %edx            # imm = 0xEB0
callq 0x2110