// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movq %rsi, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
pushq $-0x1
popq %rax
cmpq $0x2, 0x18(%rdx)
jb 0x1b1173
cmpq $0x2, (%rdx)
jae 0x1b1182
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsd %xmm0, 0x30(%rsp)
movq %rdx, %rsi
movq %rdx, 0x8(%rsp)
callq 0x1b0eb6
movq 0x8(%rsp), %r9
pushq $0x1
popq %r15
pushq $0x8
popq %rax
movq %rax, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x90(%r9), %rax
cmpq %rax, %r15
jae 0x1b12da
pushq $0x1
popq %r14
movq 0x98(%r9), %rbp
cmpq %rbp, %r14
jae 0x1b12c6
movq 0x10(%r9), %rax
movq 0x28(%r9), %rcx
movq -0x8(%rcx,%r15,8), %rdi
movq -0x8(%rax,%r14,8), %r13
movq %rdi, %rdx
shrq $0x20, %rdx
movq %r13, %rcx
shrq $0x20, %rcx
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rbx
movl %edi, %eax
shrl %eax
movl (%rbx,%rax,8), %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movl %r13d, %esi
movsd 0x32322(%rip), %xmm0    # 0x1e3538
movl %r12d, %r8d
callq 0x1b105d
movq 0x8(%rsp), %r9
movq 0x88(%r9), %rcx
movq 0x28(%rsp), %rsi
imulq %rbp, %rsi
addq %rcx, %rsi
movq -0x8(%rsi,%r14,8), %rdx
testq %rax, %rax
je 0x1b124b
addq %rax, %rdx
imulq %r15, %rbp
xorl %eax, %eax
jmp 0x1b12a3
shrl %r13d
movl (%rbx,%r13,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl %eax, %r12d
movl %eax, %edi
cmovbl %r12d, %edi
subq %rdi, %rdx
movq (%rsi,%r14,8), %rsi
subq %rax, %rsi
movq 0x20(%rsp), %rax
imulq %rbp, %rax
imulq %r15, %rbp
addq %rcx, %rax
movq -0x8(%rax,%r14,8), %rdi
subq %r12, %rdi
cmpq %rsi, %rdx
setl %al
cmovgq %rdx, %rsi
addb %al, %al
incb %al
cmpq %rdi, %rsi
movzbl %al, %eax
pushq $0x2
popq %rdx
cmovll %edx, %eax
cmovleq %rdi, %rsi
movq %rsi, %rdx
leaq (%rcx,%rbp,8), %rcx
movq %rdx, (%rcx,%r14,8)
movq 0x98(%r9), %rcx
imulq %r15, %rcx
addq 0x70(%r9), %rcx
movb %al, (%r14,%rcx)
incq %r14
jmp 0x1b11c3
incq %r15
addq $0x8, 0x20(%rsp)
addq $0x8, 0x28(%rsp)
jmp 0x1b11af
leaq -0x1(%rax), %r8
movq 0x98(%r9), %rdx
movq %rdx, %r9
imulq %r8, %r9
shlq $0x3, %r9
pushq $-0x1
popq %rcx
pushq $0x1
popq %r10
xorl %edi, %edi
movq %rcx, %rsi
movsd 0x30(%rsp), %xmm2
cmpq %rdx, %r10
jae 0x1b1332
movq 0x8(%rsp), %r11
movq 0x88(%r11), %r11
addq %r9, %r11
movq (%r11,%r10,8), %r11
testq %rcx, %rcx
js 0x1b1324
cmpq %r11, %rdi
jge 0x1b132d
movq %r11, %rdi
movq %r8, %rcx
movq %r10, %rsi
incq %r10
jmp 0x1b1302
leaq -0x1(%rdx), %r8
movq %rdx, %r9
shlq $0x4, %r9
addq $-0x8, %r9
leaq (,%rdx,8), %r10
pushq $0x1
popq %r11
cmpq %rax, %r11
jae 0x1b137b
movq 0x8(%rsp), %rbx
movq 0x88(%rbx), %rbx
testq %rcx, %rcx
js 0x1b1369
cmpq (%rbx,%r9), %rdi
jge 0x1b1373
movq (%rbx,%r9), %rdi
movq %r11, %rcx
movq %r8, %rsi
incq %r11
addq %r10, %r9
jmp 0x1b134d
xorl %eax, %eax
testq %rcx, %rcx
js 0x1b1173
cmpq $0x1, %rcx
je 0x1b1173
cmpq $0x1, %rsi
je 0x1b1173
xorl %eax, %eax
xorl %r9d, %r9d
xorl %r10d, %r10d
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r11d, %r11d
testq %rcx, %rcx
je 0x1b14ee
testq %rsi, %rsi
je 0x1b14ee
movq %rcx, %rbx
imulq %rdx, %rbx
movq 0x8(%rsp), %r14
addq 0x70(%r14), %rbx
movzbl (%rsi,%rbx), %ebp
cmpl $0x1, %ebp
je 0x1b142c
testl %ebp, %ebp
jne 0x1b1481
movq 0x10(%rsp), %rbx
movq 0x20(%rbx), %rbx
movq 0x8(%rsp), %r14
movq 0x28(%r14), %r14
movl -0x8(%r14,%rcx,8), %r14d
shrl %r14d
movl (%rbx,%r14,8), %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
addq %rbx, %r9
movq 0x18(%rsp), %r12
movq (%r12), %r15
movq 0x80(%r12), %r12
movl (%r15,%r14,4), %r14d
incq %r12
cmpq %r14, %r12
cmovbq %rax, %rbx
sbbq $-0x1, %r11
addq %rbx, %rdi
jmp 0x1b147b
movq 0x10(%rsp), %rbx
movq 0x20(%rbx), %rbx
movq 0x8(%rsp), %r14
movq 0x28(%r14), %r14
movl -0x8(%r14,%rcx,8), %r14d
shrl %r14d
movl (%rbx,%r14,8), %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
addq %rbx, %r10
movq 0x18(%rsp), %r12
movq (%r12), %r15
movq 0x80(%r12), %r12
movl (%r15,%r14,4), %r14d
incq %r12
cmpq %r14, %r12
movl $0x0, %r14d
cmovaeq %rbx, %r14
addq %r14, %r8
decq %rcx
decq %rsi
xorl %ebx, %ebx
cmpb $0x2, %bpl
sete %bl
subq %rbx, %rsi
cmpb $0x3, %bpl
jne 0x1b13aa
movq 0x10(%rsp), %rbx
movq 0x20(%rbx), %rbx
movq 0x8(%rsp), %r14
movq 0x28(%r14), %r14
movl -0x8(%r14,%rcx,8), %r14d
shrl %r14d
movl (%rbx,%r14,8), %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
addq %rbx, %r10
movq 0x18(%rsp), %r12
movq (%r12), %r15
movq 0x80(%r12), %r12
movl (%r15,%r14,4), %r14d
incq %r12
cmpq %r14, %r12
movl $0x0, %r14d
cmovaeq %rbx, %r14
addq %r14, %r8
decq %rcx
jmp 0x1b13aa
testq %rsi, %rsi
jne 0x1b15e6
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdx
movq 0x28(%rax), %rax
movq (%rax,%rcx,8), %rax
cmpl (%rdx), %eax
jne 0x1b15e6
movq 0x10(%rsp), %rdx
movq 0x20(%rdx), %rdx
shrl %eax
movl (%rdx,%rax,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq 0x18(%rsp), %rbx
movq (%rbx), %rdx
movq 0x80(%rbx), %rbx
movl (%rdx,%rax,4), %eax
incq %rbx
xorl %edx, %edx
cmpq %rax, %rbx
cmovaeq %rsi, %rdx
adcq $-0x1, %r11
pushq $-0x1
popq %rax
testq %r11, %r11
jle 0x1b1173
subq %rsi, %r9
je 0x1b1173
cmpq %rdx, %rdi
je 0x1b1173
cvtsi2sd %r9, %xmm0
addq %r9, %r10
cvtsi2sd %r10, %xmm1
mulsd %xmm2, %xmm1
ucomisd %xmm0, %xmm1
jae 0x1b1173
subq %rdx, %rdi
cvtsi2sd %rdi, %xmm0
addq %r8, %rdi
cvtsi2sd %rdi, %xmm1
mulsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
movq 0x8(%rsp), %rdx
jae 0x1b1173
divsd %xmm1, %xmm0
movq 0x40(%rdx), %rax
movq (%rax,%rcx,8), %rax
movl %eax, %ecx
shrq $0x20, %rax
subq %rax, %rcx
movq %rcx, %xmm1
punpckldq 0x31cac(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x31cb4(%rip), %xmm1    # 0x1e3280
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %rax
jmp 0x1b1173
leaq 0x46954(%rip), %rdi     # 0x1f7f41
leaq 0x45ee5(%rip), %rsi     # 0x1f74d9
leaq 0x46989(%rip), %rcx     # 0x1f7f84
movl $0xeb0, %edx            # imm = 0xEB0
callq 0x2110