// File: /workspace/github_cpp_makefile_star2_O1/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 0x1f192a
movq %rdx, %rbx
cmpq $0x2, (%rdx)
jae 0x1f1939
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, (%rsp)
movq %rbx, %rsi
callq 0x1f1649
movq 0x90(%rbx), %rdi
cmpq $0x2, %rdi
jb 0x1f1b3a
movl $0x1, %eax
movl $0x8, %ecx
xorl %edx, %edx
movsd 0x33b75(%rip), %xmm0    # 0x2254e8
movsd 0x338ed(%rip), %xmm1    # 0x225268
movl $0x2, %esi
movq 0x98(%rbx), %r8
cmpq $0x2, %r8
jb 0x1f1b1f
movl $0x1, %edi
movq 0x10(%rbx), %r9
movq 0x28(%rbx), %r10
movq -0x8(%r10,%rax,8), %r12
movq -0x8(%r9,%rdi,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 0x1f1a05
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
jbe 0x1f1ad8
xorl %r12d, %r12d
testq %r12, %r12
je 0x1f1a30
movq 0x88(%rbx), %r9
movq %rdx, %r10
imulq %r8, %r10
addq %r9, %r10
addq -0x8(%r10,%rdi,8), %r12
imulq %rax, %r8
leaq (%r9,%r8,8), %r8
xorl %r9d, %r9d
jmp 0x1f1aac
movq 0x88(%rbx), %r13
movq %rdx, %r14
imulq %r8, %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,%rdi,8), %rbp
subq %r11, %rbp
movq (%r14,%rdi,8), %r12
subq %r10, %r12
movq %rcx, %r10
imulq %r8, %r10
imulq %rax, %r8
leaq (,%r8,8), %r8
addq %r13, %r8
addq %r13, %r10
movq -0x8(%r10,%rdi,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 %esi, %r9d
cmovleq %r10, %r12
movq %r12, (%r8,%rdi,8)
movq 0x98(%rbx), %r8
imulq %rax, %r8
addq 0x70(%rbx), %r8
movb %r9b, (%rdi,%r8)
incq %rdi
movq 0x98(%rbx), %r8
cmpq %r8, %rdi
jb 0x1f1996
jmp 0x1f1b1f
movq %r9, %r12
testq %r13, %r13
je 0x1f1a08
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
jmp 0x1f1a08
incq %rax
movq 0x90(%rbx), %rdi
addq $0x8, %rdx
addq $0x8, %rcx
cmpq %rdi, %rax
jb 0x1f1980
movq 0x98(%rbx), %rsi
cmpq $0x2, %rsi
jb 0x1f1ba4
leaq -0x1(%rdi), %r8
movq %rsi, %r9
imulq %r8, %r9
shlq $0x3, %r9
movl $0x1, %r10d
movq $-0x1, %rdx
xorl %eax, %eax
movq $-0x1, %rcx
movsd (%rsp), %xmm2
testq %rcx, %rcx
js 0x1f1b86
movq 0x88(%rbx), %r11
addq %r9, %r11
cmpq (%r11,%r10,8), %rax
jge 0x1f1b9a
movq 0x88(%rbx), %rax
addq %r9, %rax
movq (%rax,%r10,8), %rax
movq %r8, %rcx
movq %r10, %rdx
incq %r10
cmpq %r10, %rsi
jne 0x1f1b71
jmp 0x1f1bb9
movq $-0x1, %rcx
xorl %eax, %eax
movq $-0x1, %rdx
movsd (%rsp), %xmm2
cmpq $0x2, %rdi
jb 0x1f1c0a
leaq -0x1(%rsi), %r8
movq %rsi, %r9
shlq $0x4, %r9
addq $-0x8, %r9
leaq (,%rsi,8), %r10
movl $0x1, %r11d
testq %rcx, %rcx
js 0x1f1bee
movq 0x88(%rbx), %r14
cmpq (%r14,%r9), %rax
jge 0x1f1bff
movq 0x88(%rbx), %rax
movq (%rax,%r9), %rax
movq %r11, %rcx
movq %r8, %rdx
incq %r11
addq %r10, %r9
cmpq %r11, %rdi
jne 0x1f1bdc
xorl %eax, %eax
testq %rcx, %rcx
js 0x1f192a
cmpq $0x1, %rcx
je 0x1f192a
cmpq $0x1, %rdx
movq 0x10(%rsp), %rdi
je 0x1f192a
testq %rcx, %rcx
je 0x1f1d7c
testq %rdx, %rdx
je 0x1f1d7c
movq 0x70(%rbx), %rax
xorl %r11d, %r11d
xorl %r9d, %r9d
movq $0x0, 0x8(%rsp)
xorl %r10d, %r10d
movq $0x0, (%rsp)
movq %rcx, %r14
imulq %rsi, %r14
addq %rax, %r14
movb (%rdx,%r14), %bpl
testb %bpl, %bpl
jne 0x1f1cbe
movq 0x20(%r15), %r14
movq 0x28(%rbx), %r12
movl -0x8(%r12,%rcx,8), %r12d
shrl %r12d
movl (%r14,%r12,8), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
addq %r14, (%rsp)
movq (%rdi), %r13
movq 0x80(%rdi), %r8
movl (%r13,%r12,4), %r12d
incq %r8
cmpq %r12, %r8
movl $0x0, %r8d
cmovbq %r8, %r14
sbbq $-0x1, %r11
addq %r14, 0x8(%rsp)
decq %rcx
decq %rdx
cmpb $0x1, %bpl
jne 0x1f1d09
movq 0x20(%r15), %r8
movq 0x28(%rbx), %r14
movl -0x8(%r14,%rcx,8), %r14d
shrl %r14d
movl (%r8,%r14,8), %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
addq %r8, %r10
movq (%rdi), %r12
movq 0x80(%rdi), %r13
movl (%r12,%r14,4), %r14d
incq %r13
cmpq %r14, %r13
movl $0x0, %r14d
cmovaeq %r8, %r14
addq %r14, %r9
decq %rcx
decq %rdx
xorl %r13d, %r13d
cmpb $0x2, %bpl
sete %r12b
cmpb $0x3, %bpl
jne 0x1f1d66
movq 0x20(%r15), %r8
movq 0x28(%rbx), %r14
movl -0x8(%r14,%rcx,8), %r14d
shrl %r14d
movl (%r8,%r14,8), %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
addq %r8, %r10
movq (%rdi), %rbp
movq 0x10(%rsp), %rdi
movq 0x80(%rdi), %rdi
movl (%rbp,%r14,4), %r14d
incq %rdi
cmpq %r14, %rdi
movl $0x0, %edi
cmovaeq %r8, %rdi
addq %rdi, %r9
movq 0x10(%rsp), %rdi
decq %rcx
movb %r12b, %r13b
subq %r13, %rdx
testq %rcx, %rcx
je 0x1f1d96
testq %rdx, %rdx
jne 0x1f1c5e
jmp 0x1f1d96
movq $0x0, (%rsp)
xorl %r10d, %r10d
movq $0x0, 0x8(%rsp)
xorl %r9d, %r9d
xorl %r11d, %r11d
testq %rdx, %rdx
jne 0x1f1e8d
movq 0x10(%rbx), %rdx
movq 0x28(%rbx), %rax
movq (%rax,%rcx,8), %rax
cmpl (%rdx), %eax
jne 0x1f1e8d
movq 0x20(%r15), %rdx
shrl %eax
movl (%rdx,%rax,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq (%rdi), %rdx
movq 0x80(%rdi), %rdi
movl (%rdx,%rax,4), %eax
incq %rdi
xorl %edx, %edx
cmpq %rax, %rdi
cmovaeq %rsi, %rdx
adcq $-0x1, %r11
movq $-0x1, %rax
testq %r11, %r11
jle 0x1f192a
movq (%rsp), %rdi
subq %rsi, %rdi
je 0x1f192a
movq 0x8(%rsp), %rsi
cmpq %rdx, %rsi
je 0x1f192a
cvtsi2sd %rdi, %xmm0
addq %rdi, %r10
cvtsi2sd %r10, %xmm1
mulsd %xmm2, %xmm1
ucomisd %xmm0, %xmm1
jae 0x1f192a
subq %rdx, %rsi
xorps %xmm0, %xmm0
cvtsi2sd %rsi, %xmm0
addq %r9, %rsi
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
mulsd %xmm1, %xmm2
ucomisd %xmm0, %xmm2
jae 0x1f192a
divsd %xmm1, %xmm0
movq 0x40(%rbx), %rax
movq (%rax,%rcx,8), %rax
movl %eax, %ecx
shrq $0x20, %rax
subq %rax, %rcx
movq %rcx, %xmm1
punpckldq 0x333d5(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x333dd(%rip), %xmm1    # 0x225250
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %rax
jmp 0x1f192a
leaq 0x47ef9(%rip), %rdi     # 0x239d8d
leaq 0x4748a(%rip), %rsi     # 0x239325
leaq 0x47f2e(%rip), %rcx     # 0x239dd0
movl $0xeb0, %edx            # imm = 0xEB0
callq 0x2110