// File: /workspace/github_cpp_makefile_star2_O1/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 $0xb8, %rsp
movl %r9d, 0x60(%rsp)
movss %xmm0, 0x64(%rsp)
movl %ecx, 0x5c(%rsp)
movl %edx, 0x58(%rsp)
movq %rdi, 0x48(%rsp)
movq 0x100(%rsp), %rdx
movq 0xf8(%rsp), %r8
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %r9
movaps %xmm0, (%r9)
xorl %ecx, %ecx
movq %rcx, 0x10(%r9)
leaq 0xa0(%rsp), %r10
movq %rcx, 0x10(%r10)
movaps %xmm0, (%r10)
movq 0x18(%rsi), %rdi
movl 0x14(%rdi), %eax
addl %eax, %eax
testq %r8, %r8
movq %r8, %r11
cmoveq %r9, %r11
testq %rdx, %rdx
cmovneq %rdx, %r10
movq %r10, 0x78(%rsp)
movq $0x0, 0x38(%rsp)
movq %rcx, (%r11)
testl %eax, %eax
movq %rdi, 0x10(%rsp)
movq %rsi, 0x28(%rsp)
movq %r11, 0x20(%rsp)
je 0x202976
movq 0xf8(%rsp), %rbx
testq %rbx, %rbx
leaq 0x80(%rsp), %rcx
cmoveq %rcx, %rbx
movl %eax, %eax
movq %rax, 0x40(%rsp)
xorl %ebp, %ebp
movq 0x20(%rdi), %rax
movl %ebp, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x202823
movq 0x30(%rdi), %rax
cmpb $0x0, (%rax,%rbp)
je 0x202832
incq %rbp
cmpq 0x40(%rsp), %rbp
jne 0x20280b
jmp 0x202976
movq 0x28(%rdi), %rax
movq (%rax,%rbp,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x202823
movq 0x8(%rdi), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %r8
movq %r8, 0x8(%rsp)
movl %eax, %r13d
leaq (%rcx,%rdx,8), %r14
addq $0xc, %r14
xorl %ecx, %ecx
movl $0x1, %eax
movq %r14, %rdx
movl %ecx, %r8d
movl (%rdx), %ecx
notl %ecx
shrl $0x1f, %ecx
addl %r8d, %ecx
cmpq %r13, %rax
jae 0x20288c
addq $0x18, %rdx
incq %rax
cmpl $0x2, %ecx
jb 0x20286e
cmpl $0x2, %ecx
jb 0x202823
xorl %r15d, %r15d
cmpl $0x0, (%r14)
js 0x202961
movl -0x4(%r14), %edx
subq $0x8, %rsp
movq 0x50(%rsp), %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0x8(%rsp), %r9
pushq $0x0
leaq 0x44(%rsp), %rax
pushq %rax
leaq 0x1c(%rsp), %rax
pushq %rax
callq 0x202015
addq $0x20, %rsp
movl 0x4(%rsp), %ecx
movabsq $-0x100000000, %r12     # imm = 0xFFFFFFFF00000000
addl %ecx, %ecx
je 0x2028ec
movl (%rsp), %eax
xorl %edx, %edx
divl %ecx
movl %eax, %r12d
shlq $0x20, %r12
movq 0x8(%rbx), %rax
movq 0x20(%rsp), %r11
cmpq %rax, (%r11)
jne 0x202924
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x20(%rsp), %r11
movq %rax, 0x10(%rbx)
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rdi
subq 0x8(%rdi), %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rax
addq %r15, %rax
orq %r12, %rax
movq 0x10(%rbx), %rcx
movq (%r11), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r11)
movq %rax, (%rcx,%rdx,8)
movq 0x28(%rsp), %rsi
incq %r15
addq $0x18, %r14
cmpq %r15, %r13
jne 0x202894
jmp 0x202823
movq 0xf8(%rsp), %rax
testq %rax, %rax
leaq 0x80(%rsp), %rbx
cmovneq %rax, %rbx
movq 0x10(%rbx), %rdi
movq (%r11), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1e3b6a
movq 0x20(%rsp), %r10
movq 0x28(%rsp), %rsi
movq 0x10(%rsp), %rdi
cmpq $0x0, (%r10)
je 0x202e04
xorl %r15d, %r15d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq $0x0, 0x38(%rsp)
movq %rbx, 0x18(%rsp)
movq 0x8(%rdi), %rbp
movq 0x10(%rbx), %rax
movl (%rax,%r15,8), %eax
leaq (%rax,%rax,2), %r9
cmpl $0x0, 0xc(%rbp,%r9,8)
js 0x202de4
leaq (,%r9,8), %rax
addq %rbp, %rax
movq (%rax), %rcx
movq 0x20(%rdi), %rdx
movq %rcx, %r8
shrq $0x21, %r8
cmpl $0x0, (%rdx,%r8,8)
js 0x202de4
movl 0x8(%rax), %eax
movl %eax, %r8d
shrl %r8d
cmpl $0x0, (%rdx,%r8,8)
js 0x202de4
movq %r9, 0x68(%rsp)
shrq $0x20, %rcx
xorq $0x1, %rax
movq 0x28(%rdi), %rdx
movq (%rdx,%rcx,8), %r14
cmpl $0x1, %r14d
ja 0x202a49
testl $0xfffffffe, (%rdx,%rax,8) # imm = 0xFFFFFFFE
je 0x202de4
movq 0x18(%rsi), %rdx
movq 0x28(%rdx), %rcx
movl %eax, %eax
movq (%rcx,%rax,8), %rax
movq %rax, %rcx
andq %r12, %rcx
je 0x202de4
movq %r14, %r8
shrq $0x20, %r8
leaq (%r8,%r8,2), %r8
leaq (,%r8,8), %rbx
addq %rbp, %rbx
shrq $0x20, %rax
movq 0x8(%rdx), %r9
xorl %edx, %edx
movl %eax, %r8d
leaq (%r8,%r8,2), %r8
movl 0xc(%r9,%r8,8), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %edx
incq %rax
decq %rcx
jne 0x202a83
cmpl $0x1, %edx
jne 0x202ddf
movq 0x68(%rsp), %rax
movl 0x8(%rbp,%rax,8), %edx
subq $0x8, %rsp
movq 0x50(%rsp), %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0x8(%rsp), %r9
pushq $0x0
leaq 0x44(%rsp), %rax
pushq %rax
leaq 0x1c(%rsp), %rax
pushq %rax
callq 0x202015
addq $0x20, %rsp
movl 0x4(%rsp), %ecx
addl %ecx, %ecx
je 0x202aee
movl (%rsp), %eax
xorl %edx, %edx
divl %ecx
jmp 0x202af3
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $-0x1, %eax
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %r10
je 0x202ddf
movq $0x0, 0x40(%rsp)
testl %r14d, %r14d
je 0x202be4
movl %eax, 0x54(%rsp)
movl %r14d, %r14d
movq $0x0, 0x70(%rsp)
movl $0x0, 0x8(%rsp)
xorl %r13d, %r13d
cmpl $0x0, 0xc(%rbx)
js 0x202bb6
movl 0x8(%rbx), %edx
subq $0x8, %rsp
movq 0x50(%rsp), %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0x8(%rsp), %r9
pushq $0x0
leaq 0x44(%rsp), %rax
pushq %rax
leaq 0x1c(%rsp), %rax
pushq %rax
callq 0x202015
addq $0x20, %rsp
movl 0x4(%rsp), %ecx
addl %ecx, %ecx
je 0x202b7c
movl (%rsp), %eax
xorl %edx, %edx
divl %ecx
jmp 0x202b81
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
incl %r13d
cmpl %eax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %r10
jb 0x202ba5
jne 0x202bb6
movq 0x40(%rsp), %rcx
cmpl (%rsp), %ecx
jae 0x202bb6
movl (%rsp), %ecx
movq %rcx, 0x40(%rsp)
movl %eax, 0x8(%rsp)
movq %rbx, 0x70(%rsp)
addq $0x18, %rbx
decq %r14
jne 0x202b38
cmpl $0x2, %r13d
setae %al
cmpq $0x0, 0x70(%rsp)
setne %cl
andb %al, %cl
movl 0x8(%rsp), %eax
cvtsi2ss %rax, %xmm0
movl 0x54(%rsp), %eax
jmp 0x202be9
xorps %xmm0, %xmm0
xorl %ecx, %ecx
testb %cl, %cl
movq 0x18(%rsp), %rbx
movq 0x68(%rsp), %rcx
je 0x202de4
movl %eax, %eax
cvtsi2ss %rax, %xmm1
mulss 0x64(%rsp), %xmm0
ucomiss %xmm0, %xmm1
ja 0x202de4
leaq 0x8(,%rcx,8), %r14
addq %rbp, %r14
movl (%r14), %edx
subq $0x8, %rsp
movq 0x50(%rsp), %r13
movq %r13, %rdi
movl 0x60(%rsp), %ecx
movl 0x64(%rsp), %r8d
movq %rsi, %rbx
movl 0x68(%rsp), %r9d
pushq 0x80(%rsp)
pushq %r10
pushq $0x1
movq %r10, %rbp
callq 0x2023c1
addq $0x20, %rsp
testl %eax, %eax
je 0x202df2
movq %rbx, %rsi
movq (%rbp), %rbx
movl (%r14), %edx
subq $0x20, %rsp
movq %rbp, 0x10(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %r13, %rdi
movq %rsi, %r14
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x202015
addq $0x20, %rsp
movq %rbx, 0x8(%rsp)
movl %ebx, %ecx
movq (%rbp), %rax
cmpq %rcx, %rax
movq %r14, %rsi
movq %rbp, %r10
jbe 0x202dc2
movq 0x8(%rsp), %r14
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movq (%rax,%rcx,8), %rax
movq %rax, %rcx
shrq %rcx
movq 0x18(%rsi), %rdx
movq 0x20(%rdx), %rsi
movl %ecx, %ecx
orl $0x80000000, (%rsi,%rcx,8) # imm = 0x80000000
andl $-0x2, %eax
xorl %esi, %esi
leaq (%rsi,%rax), %rdi
movq 0x28(%rdx), %r8
movq (%r8,%rdi,8), %r9
movq %r9, %r8
andq %r12, %r8
je 0x202d64
shrq $0x20, %r9
leaq (%r9,%r9,2), %r9
shlq $0x3, %r9
addq 0x8(%rdx), %r9
xorl %r10d, %r10d
leaq (%r10,%r10,2), %r11
orb $-0x80, 0xf(%r9,%r11,8)
movl 0x8(%r9,%r11,8), %r11d
xorq $0x1, %r11
movq 0x28(%rdx), %r13
movq (%r13,%r11,8), %r13
movq %r13, %r11
andq %r12, %r11
je 0x202d5c
movq 0x8(%rdx), %rbp
shrq $0x20, %r13
shlq $0x3, %r11
leaq (%r11,%r11,2), %r11
leaq (%r13,%r13,2), %r13
leaq 0xc(,%r13,8), %r13
addq %rbp, %r13
xorl %ebp, %ebp
movl -0x4(%r13,%rbp), %ebx
xorl %edi, %ebx
cmpl $0x1, %ebx
jne 0x202d53
orb $-0x80, 0x3(%r13,%rbp)
addq $0x18, %rbp
cmpq %rbp, %r11
jne 0x202d41
incq %r10
cmpq %r8, %r10
jne 0x202cfb
leaq 0x1(%rsi), %rdi
testq %rsi, %rsi
movq %rdi, %rsi
je 0x202cd4
movq 0x28(%rsp), %r13
movq 0x10(%r13), %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rbx
movq 0x18(%rax,%rbx), %rdi
callq 0x2220
movq %r13, %rsi
movq 0x10(%r13), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax,%rbx)
movups %xmm0, (%rax,%rbx)
movq $0x0, 0x20(%rax,%rbx)
incl %r14d
movq 0x20(%rsp), %r10
movq (%r10), %rax
movq %r14, %rcx
cmpq %r14, %rax
ja 0x202cab
movq 0x8(%rsp), %rdx
movl %edx, %ecx
subl %edx, %eax
movq 0x38(%rsp), %rdx
addl %eax, %edx
movq %rdx, 0x38(%rsp)
movq %rcx, (%r10)
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rbx
incl %r15d
cmpq %r15, (%r10)
ja 0x2029cd
jmp 0x202e04
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
movq 0x18(%rsp), %rbx
movq %rbp, %r10
jmp 0x202de4
cmpq $0x0, 0xf8(%rsp)
jne 0x202e21
movq 0x90(%rsp), %rdi
callq 0x2220
movq 0x10(%rsp), %rdi
cmpq $0x0, 0x100(%rsp)
movq 0x38(%rsp), %rbx
jne 0x202e43
movq 0xb0(%rsp), %rdi
callq 0x2220
movq 0x10(%rsp), %rdi
testl %ebx, %ebx
je 0x202e4c
callq 0xa02fa
movl %ebx, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq