// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: ulg_arc_cut_bridge(ul_resolve_t*, ma_ug_t*, int, unsigned int, float, unsigned int, unsigned int, unsigned int*, asg64_v*, asg64_v*)
uint32_t ulg_arc_cut_bridge(ul_resolve_t *uidx, ma_ug_t *ug, int32_t max_ext, uint32_t max_ext_hifi, 
float len_rat, uint32_t is_trio, uint32_t topo_level, uint32_t *max_drop_len, asg64_v *in, asg64_v *ib)
{
    asg64_v tx = {0,0,0}, tb = {0,0,0}, *b = NULL, *ub = NULL; asg_t *g = ug->g;
    uint32_t v, w, i, k, kv, nv, cnt = 0, n_vtx = g->n_seq<<1, ff, ol_max, ul_max, mm_ol;
    asg_arc_t *av, *ve, *vl_max; uint32_t ul_cnt, bridge_w, bridge_am, pb;
    // fprintf(stderr, "+++[M::%s::] idx->cc.iug_b[455]::%lu\n", __func__, uidx->uovl.cc.iug_b[455]);
    b = (in?(in):(&tx)); ub = (ib?(ib):(&tb));
    for (v = b->n = 0; v < n_vtx; ++v) {
        if (g->seq[v>>1].del) continue;
        if(g->seq_vis[v] == 0) {
            av = asg_arc_a(g, v); nv = asg_arc_n(g, v);
            if (nv < 2) continue;

            for (i = kv = 0; i < nv && kv < 2; ++i) {
                if(av[i].del) continue;
                kv++;
            }
            if(kv < 2) continue;

            for (i = 0; i < nv; ++i) {
                if(av[i].del) continue;
                get_ul_path_info(uidx, ug, av[i].v, NULL, NULL, &ul_cnt, &bridge_w, &bridge_am, NULL);
                ff = ul_path_w(ul_cnt, bridge_w, bridge_am);
                // if((v>>1) == 5) {
                //     fprintf(stderr, "+[M::%s::] v>>1::%u, v&1::%u, w>>1::%u, w&1::%u, ul_cnt::%u, bridge_w::%u, bridge_am::%u, ff::%u\n", __func__, 
                //     v>>1, v&1, av[i].v>>1, av[i].v&1, ul_cnt, bridge_w, bridge_am, ff);
                // }
                kv_push(uint64_t, *b, (((uint64_t)ff)<<32) | ((uint64_t)(av-g->arc+i)));
            }
        }
    }

    // fprintf(stderr, "---[M::%s::] idx->cc.iug_b[455]::%lu\n", __func__, uidx->uovl.cc.iug_b[455]);
    radix_sort_srt64(b->a, b->a + b->n);
    for (k = 0; k < b->n; k++) {
        if(g->arc[(uint32_t)b->a[k]].del) continue;
        v = g->arc[(uint32_t)b->a[k]].ul>>32; w = g->arc[(uint32_t)b->a[k]].v^1;
        if(g->seq[v>>1].del || g->seq[w>>1].del) continue;
        nv = asg_arc_n(g, v); av = asg_arc_a(g, v); 
        if(nv<=1 && asg_arc_n(g, w) <= 1) continue;
        if(get_arcs(ug->g, w, NULL, 0) != 1) continue;

        ve = &(g->arc[(uint32_t)b->a[k]]);
        get_ul_path_info(uidx, ug, ve->v, NULL, NULL, &ul_cnt, &bridge_w, &bridge_am, NULL);
        ff = ul_path_w(ul_cnt, bridge_w, bridge_am);
        if(ff == (uint32_t)-1) continue;
        mm_ol = ff; 

        for (i = kv = ol_max = ul_max = 0, vl_max = NULL; i < nv; ++i) {
            if(av[i].del) continue;
            kv++;
            get_ul_path_info(uidx, ug, av[i].v, NULL, NULL, &ul_cnt, &bridge_w, &bridge_am, NULL);
            ff = ul_path_w(ul_cnt, bridge_w, bridge_am);
            if((ol_max < ff) || (ol_max == ff && ul_max < ul_cnt)) {
                ol_max = ff; ul_max = ul_cnt; vl_max = &(av[i]);
            } 
        }

        // if((v>>1) == 5) {
        //     fprintf(stderr, "***[M::%s::] v>>1::%u, v&1::%u, w>>1::%u, w&1::%u, ul_cnt::%u, bridge_w::%u, bridge_am::%u, ff::%u, mm_ol::%u, ol_max::%u, kv::%u\n", __func__, 
        //     v>>1, v&1, w>>1, w&1, ul_cnt, bridge_w, bridge_am, ff, mm_ol, ol_max, kv);
        // }
        
        if (kv <= 1 || (!vl_max)) continue;
        if (kv >= 2) {
            if (mm_ol > ol_max*len_rat) continue;
        }

        // if((v>>1) == 5) {
        //     fprintf(stderr, "###[M::%s::] v>>1::%u, v&1::%u, w>>1::%u, w&1::%u, ul_cnt::%u, bridge_w::%u, bridge_am::%u, ff::%u, mm_ol::%u, ol_max::%u\n", __func__, 
        //     v>>1, v&1, w>>1, w&1, ul_cnt, bridge_w, bridge_am, ff, mm_ol, ol_max);
        // }

        
        if(usg_bridge_topocut_aux(uidx, ug, ve->v, max_ext, max_ext_hifi, topo_level, 1, b, ub)) {
            pb = b->n; 
            get_ul_path_info(uidx, ug, ve->v, NULL, NULL, NULL, NULL, NULL, b);
            for (i = pb; i < b->n; i++) ulg_seq_del(ug, (b->a[i]>>1));
            cnt += b->n - pb; b->n = pb;
        }
    }


    if(!in) free(tx.a); if(!ib) free(tb.a);
    if (cnt > 0) asg_cleanup(g);
    return cnt;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %r9d, 0x4c(%rsp)
movss %xmm0, 0x50(%rsp)
movl %ecx, 0x48(%rsp)
movl %edx, 0x44(%rsp)
movq 0xe8(%rsp), %rax
leaq 0x90(%rsp), %rbp
andq $0x0, 0x10(%rbp)
xorps %xmm0, %xmm0
andq $0x0, 0x80(%rsp)
movaps %xmm0, (%rbp)
movaps %xmm0, 0x70(%rsp)
movq 0x18(%rsi), %r8
movl 0x14(%r8), %ecx
addl %ecx, %ecx
movq %rcx, 0x38(%rsp)
testq %rax, %rax
cmovneq %rax, %rbp
andq $0x0, (%rbp)
xorl %r13d, %r13d
movq %r8, 0x8(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, 0x28(%rsp)
cmpq 0x38(%rsp), %r13
je 0x1c137e
movq 0x20(%r8), %rax
movl %r13d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x1c124b
movq 0x30(%r8), %rax
cmpb $0x0, (%rax,%r13)
je 0x1c1250
incq %r13
jmp 0x1c1226
movq 0x28(%r8), %rax
movq (%rax,%r13,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x1c124b
movq 0x8(%r8), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
leaq (%rcx,%rdx), %r9
movq %r9, 0x20(%rsp)
movl %eax, %r14d
leaq (%rcx,%rdx), %rbx
addq $0xc, %rbx
xorl %eax, %eax
movq %rbx, %rcx
xorl %edx, %edx
cmpq %r14, %rax
jae 0x1c12a9
cmpl $0x1, %edx
ja 0x1c12a9
movl (%rcx), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %edx
incq %rax
addq $0x18, %rcx
jmp 0x1c1289
cmpl $0x2, %edx
jb 0x1c124b
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x1c124b
cmpl $0x0, (%rbx)
js 0x1c1372
movl -0x4(%rbx), %edx
subq $0x8, %rsp
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0x1c(%rsp), %r9
pushq $0x0
leaq 0x44(%rsp), %rax
pushq %rax
leaq 0x28(%rsp), %rax
pushq %rax
callq 0x1c0c77
addq $0x20, %rsp
movl 0x10(%rsp), %ecx
movabsq $-0x100000000, %r15     # imm = 0xFFFFFFFF00000000
addl %ecx, %ecx
je 0x1c1308
movl 0x14(%rsp), %eax
xorl %edx, %edx
divl %ecx
movl %eax, %r15d
shlq $0x20, %r15
movq (%rbp), %rcx
movq 0x10(%rbp), %r9
cmpq 0x8(%rbp), %rcx
jne 0x1c133f
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbp)
shlq $0x3, %rsi
movq %r9, %rdi
callq 0x2370
movq %rax, %r9
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %rsi
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rax
subq 0x8(%r8), %rax
cqto
pushq $0x18
popq %r10
idivq %r10
addq %r12, %rax
orq %r15, %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
movq %rax, (%r9,%rcx,8)
incq %r12
addq $0x18, %rbx
jmp 0x1c12b1
movq 0xf0(%rsp), %rax
testq %rax, %rax
leaq 0x70(%rsp), %rcx
cmovneq %rax, %rcx
movq %rcx, 0x60(%rsp)
movq 0x10(%rbp), %rdi
movq (%rbp), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1a3def
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %rdi
xorl %r14d, %r14d
movl $0x0, 0x38(%rsp)
movq %rbp, 0x58(%rsp)
movl %r14d, %r14d
cmpq %r14, (%rbp)
jbe 0x1c1698
movq 0x8(%rdi), %r12
movq 0x10(%rbp), %rax
movl (%rax,%r14,8), %eax
imulq $0x18, %rax, %r13
cmpl $0x0, 0xc(%r12,%r13)
js 0x1c1686
leaq (%r12,%r13), %rdx
movq (%rdx), %rax
movq 0x20(%rdi), %rcx
movq %rax, %rsi
shrq $0x21, %rsi
cmpl $0x0, (%rcx,%rsi,8)
js 0x1c1686
movl 0x8(%rdx), %r15d
movl %r15d, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0x1c1686
shrq $0x20, %rax
movq %r15, %rsi
xorq $0x1, %rsi
movq 0x28(%rdi), %rcx
movq (%rcx,%rax,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jne 0x1c1441
testl $0xfffffffe, (%rcx,%rsi,8) # imm = 0xFFFFFFFE
je 0x1c1686
movq %rax, 0x20(%rsp)
movq 0x18(%r9), %rdi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r9, %rbx
callq 0x1a5422
cmpl $0x1, %eax
jne 0x1c1598
subq $0x8, %rsp
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
movl %r15d, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0x1c(%rsp), %r9
pushq $0x0
leaq 0x44(%rsp), %rax
pushq %rax
leaq 0x28(%rsp), %rax
pushq %rax
callq 0x1c0c77
addq $0x20, %rsp
movl 0x10(%rsp), %ecx
addl %ecx, %ecx
je 0x1c167c
movl 0x14(%rsp), %eax
xorl %edx, %edx
divl %ecx
cmpl $-0x1, %eax
movq 0x8(%rsp), %rdi
je 0x1c1681
movl %eax, 0x54(%rsp)
movq 0x20(%rsp), %rcx
movq %rcx, %rax
shrq $0x20, %rax
imulq $0x18, %rax, %rbx
addq %r12, %rbx
leaq (%r12,%r13), %rax
addq $0x8, %rax
movq %rax, 0x68(%rsp)
movl %ecx, %r12d
xorl %r8d, %r8d
xorl %r15d, %r15d
xorl %r13d, %r13d
movq $0x0, 0x20(%rsp)
movq 0x18(%rsp), %r9
subq $0x1, %r12
jb 0x1c15a5
cmpl $0x0, 0xc(%rbx)
js 0x1c156c
movq %r8, %rbp
movl 0x8(%rbx), %edx
subq $0x8, %rsp
movq 0x30(%rsp), %rdi
movq %r9, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0x1c(%rsp), %r9
pushq $0x0
leaq 0x44(%rsp), %rax
pushq %rax
leaq 0x28(%rsp), %rax
pushq %rax
callq 0x1c0c77
addq $0x20, %rsp
movl 0x10(%rsp), %esi
movl 0x14(%rsp), %ecx
pushq $-0x1
popq %rax
addl %esi, %esi
je 0x1c1547
movl %ecx, %eax
xorl %edx, %edx
divl %esi
movq %rbp, %r8
incl %r8d
cmpl %eax, %r15d
jb 0x1c1565
sete %dl
cmpl %ecx, %r13d
setb %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x1c1574
movq %rbx, 0x20(%rsp)
jmp 0x1c157a
movl %r15d, %eax
movl %r13d, %ecx
jmp 0x1c1589
movl %r15d, %eax
movl %r13d, %ecx
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r9
movq 0x58(%rsp), %rbp
addq $0x18, %rbx
movl %eax, %r15d
movl %ecx, %r13d
jmp 0x1c14ef
movq 0x8(%rsp), %rdi
movq %rbx, %r9
jmp 0x1c1686
cmpl $0x2, %r8d
jb 0x1c1686
cmpq $0x0, 0x20(%rsp)
je 0x1c1686
movl 0x54(%rsp), %eax
cvtsi2ss %rax, %xmm0
movl %r15d, %eax
cvtsi2ss %rax, %xmm1
mulss 0x50(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x1c1686
movq 0x68(%rsp), %r13
movl (%r13), %edx
subq $0x8, %rsp
movq 0x30(%rsp), %r12
movq %r12, %rdi
movq %r9, %rsi
movl 0x4c(%rsp), %ecx
movl 0x50(%rsp), %r8d
movq %r9, %r15
movl 0x54(%rsp), %r9d
pushq 0x68(%rsp)
pushq %rbp
pushq $0x1
callq 0x1c0f0c
addq $0x20, %rsp
testl %eax, %eax
je 0x1c168e
movq (%rbp), %rbx
movl (%r13), %edx
subq $0x20, %rsp
movq %rbp, 0x10(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %r12, %rdi
movq %r15, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1c0c77
addq $0x20, %rsp
movl %ebx, %eax
movl %eax, %r15d
movq (%rbp), %rax
cmpq %r15, %rax
jbe 0x1c1670
movq 0x10(%rbp), %rax
movq (%rax,%r15,8), %rsi
shrq %rsi
movq 0x18(%rsp), %rdi
callq 0x1bfe8f
leal 0x1(%r15), %eax
jmp 0x1c1649
movl %ebx, %ecx
subl %ebx, %eax
addl %eax, 0x38(%rsp)
movq %rcx, (%rbp)
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r9
incl %r14d
jmp 0x1c13c2
movq 0x8(%rsp), %rdi
movq %r15, %r9
jmp 0x1c1686
cmpq $0x0, 0xe8(%rsp)
jne 0x1c16b5
movq 0xa0(%rsp), %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
cmpq $0x0, 0xf0(%rsp)
movl 0x38(%rsp), %ebx
jne 0x1c16d6
movq 0x80(%rsp), %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
testl %ebx, %ebx
je 0x1c16df
callq 0x694eb
movl %ebx, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq