// File: /workspace/github_cpp_makefile_star2_O3/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, 0x68(%rsp)
movss %xmm0, 0x6c(%rsp)
movl %ecx, 0x64(%rsp)
movl %edx, 0x60(%rsp)
movq 0xf8(%rsp), %rbp
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %r13
movaps %xmm0, (%r13)
xorl %ecx, %ecx
movq %rcx, 0x10(%r13)
leaq 0xa0(%rsp), %r9
movq %rcx, 0x10(%r9)
movaps %xmm0, (%r9)
movq 0x18(%rsi), %r8
movl 0x14(%r8), %eax
addl %eax, %eax
testq %rbp, %rbp
movq %rbp, %r10
cmoveq %r13, %r10
movq 0x100(%rsp), %rdx
testq %rdx, %rdx
cmovneq %rdx, %r9
movq %r9, 0x78(%rsp)
movq $0x0, 0x50(%rsp)
movq %rcx, (%r10)
movl $0x0, %ecx
testl %eax, %eax
movq %r8, 0x10(%rsp)
movq %rsi, 0x18(%rsp)
movq %r10, 0x38(%rsp)
movq %rdi, 0x40(%rsp)
je 0x20df54
testq %rbp, %rbp
cmoveq %r13, %rbp
movl %eax, %eax
movq %rax, 0x30(%rsp)
xorl %r15d, %r15d
movq 0x20(%r8), %rax
movl %r15d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x20ddf0
movq 0x30(%r8), %rax
cmpb $0x0, (%rax,%r15)
je 0x20ddff
incq %r15
cmpq 0x30(%rsp), %r15
jne 0x20ddd6
jmp 0x20df41
movq 0x28(%r8), %rax
movq (%rax,%r15,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x20ddf0
movq 0x8(%r8), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %r9
movq %r9, 0x20(%rsp)
movl %eax, %r14d
leaq (%rcx,%rdx,8), %rbx
addq $0xc, %rbx
xorl %ecx, %ecx
movl $0x1, %eax
movq %rbx, %rdx
movl %ecx, %r9d
movl (%rdx), %ecx
notl %ecx
shrl $0x1f, %ecx
addl %r9d, %ecx
cmpq %r14, %rax
jae 0x20de59
addq $0x18, %rdx
incq %rax
cmpl $0x2, %ecx
jb 0x20de3b
cmpl $0x2, %ecx
jb 0x20ddf0
xorl %r13d, %r13d
cmpl $0x0, (%rbx)
js 0x20df2c
movl -0x4(%rbx), %edx
subq $0x8, %rsp
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0x14(%rsp), %r9
pushq $0x0
leaq 0x5c(%rsp), %rax
pushq %rax
leaq 0x20(%rsp), %rax
pushq %rax
callq 0x20d67b
addq $0x20, %rsp
movl 0x8(%rsp), %ecx
movabsq $-0x100000000, %r12     # imm = 0xFFFFFFFF00000000
addl %ecx, %ecx
je 0x20deb3
movl 0xc(%rsp), %eax
xorl %edx, %edx
divl %ecx
movl %eax, %r12d
shlq $0x20, %r12
movq 0x38(%rsp), %r10
movq (%r10), %rcx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rcx
jne 0x20def1
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbp)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x38(%rsp), %r10
movq %rax, 0x10(%rbp)
movq (%r10), %rcx
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %rsi
movq 0x40(%rsp), %rdi
movq 0x20(%rsp), %rdx
subq 0x8(%r8), %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %r9 # imm = 0xAAAAAAAAAAAAAAAB
imulq %r9, %rdx
addq %r13, %rdx
orq %r12, %rdx
leaq 0x1(%rcx), %r9
movq %r9, (%r10)
movq %rdx, (%rax,%rcx,8)
incq %r13
addq $0x18, %rbx
cmpq %r13, %r14
jne 0x20de61
jmp 0x20ddf0
movq (%r10), %rcx
movq 0xf8(%rsp), %rbp
leaq 0x80(%rsp), %r13
testq %rbp, %rbp
cmovneq %rbp, %r13
movq 0x10(%r13), %rdi
leaq (%rdi,%rcx,8), %rsi
callq 0x1ee586
movq 0x38(%rsp), %r11
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdi
cmpq $0x0, (%r11)
je 0x20e3d7
xorl %r15d, %r15d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq $0x0, 0x50(%rsp)
movq %r13, 0x28(%rsp)
movq 0x8(%rdi), %rbx
movq 0x10(%r13), %rax
movl (%rax,%r15,8), %eax
leaq (%rax,%rax,2), %r14
cmpl $0x0, 0xc(%rbx,%r14,8)
js 0x20e3ba
leaq (%rbx,%r14,8), %rdx
movq (%rdx), %rax
movq 0x20(%rdi), %rcx
movq %rax, %r8
shrq $0x21, %r8
cmpl $0x0, (%rcx,%r8,8)
js 0x20e3ba
movl 0x8(%rdx), %edx
movl %edx, %r8d
shrl %r8d
cmpl $0x0, (%rcx,%r8,8)
js 0x20e3ba
shrq $0x20, %rax
movq %rdx, %rcx
xorq $0x1, %rcx
movq 0x28(%rdi), %r8
movq (%r8,%rax,8), %rbp
cmpl $0x1, %ebp
ja 0x20e00b
testl $0xfffffffe, (%r8,%rcx,8) # imm = 0xFFFFFFFE
je 0x20e3ba
movq 0x18(%rsi), %r9
movq 0x28(%r9), %rax
movq (%rax,%rcx,8), %rax
movq %rax, %rcx
andq %r12, %rcx
je 0x20e3ba
movq %rbp, %r8
shrq $0x20, %r8
leaq (%r8,%r8,2), %r8
leaq (%rbx,%r8,8), %r13
shrq $0x20, %rax
movq 0x8(%r9), %r9
xorl %r10d, %r10d
movl %eax, %r8d
leaq (%r8,%r8,2), %r8
movl 0xc(%r9,%r8,8), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %r10d
incq %rax
decq %rcx
jne 0x20e03d
cmpl $0x1, %r10d
jne 0x20e3b5
subq $0x8, %rsp
movq 0x48(%rsp), %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0x14(%rsp), %r9
pushq $0x0
leaq 0x5c(%rsp), %rax
pushq %rax
leaq 0x20(%rsp), %rax
pushq %rax
callq 0x20d67b
addq $0x20, %rsp
movl 0x8(%rsp), %ecx
addl %ecx, %ecx
je 0x20e39a
movl 0xc(%rsp), %eax
xorl %edx, %edx
divl %ecx
cmpl $-0x1, %eax
movq 0x10(%rsp), %rdi
movq 0x38(%rsp), %r11
je 0x20e3b0
testl %ebp, %ebp
movq 0x18(%rsp), %rsi
je 0x20e3b5
movl %eax, 0x5c(%rsp)
leaq (%rbx,%r14,8), %rdx
addq $0x8, %rdx
movl %ebp, %ebx
movq $0x0, 0x70(%rsp)
xorl %ebp, %ebp
movl $0x0, 0x20(%rsp)
xorl %r14d, %r14d
movq %rdx, 0x30(%rsp)
cmpl $0x0, 0xc(%r13)
js 0x20e135
movl 0x8(%r13), %edx
subq $0x8, %rsp
movq 0x48(%rsp), %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0x14(%rsp), %r9
pushq $0x0
leaq 0x5c(%rsp), %rax
pushq %rax
leaq 0x20(%rsp), %rax
pushq %rax
callq 0x20d67b
addq $0x20, %rsp
movl 0x8(%rsp), %esi
movl 0xc(%rsp), %ecx
addl %esi, %esi
je 0x20e139
movl %ecx, %eax
xorl %edx, %edx
divl %esi
jmp 0x20e13e
movl %ebp, %ecx
jmp 0x20e17a
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
incl %r14d
cmpl %eax, 0x20(%rsp)
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x38(%rsp), %r11
jb 0x20e168
sete %dl
cmpl %ecx, %ebp
setb %r8b
andb %dl, %r8b
cmpb $0x1, %r8b
jne 0x20e173
movl %eax, 0x20(%rsp)
movq %r13, 0x70(%rsp)
jmp 0x20e175
movl %ebp, %ecx
movq 0x30(%rsp), %rdx
addq $0x18, %r13
movl %ecx, %ebp
decq %rbx
jne 0x20e0ec
cmpl $0x2, %r14d
jb 0x20e3b5
cmpq $0x0, 0x70(%rsp)
movq 0x28(%rsp), %r13
je 0x20e3ba
movl 0x20(%rsp), %eax
cvtsi2ss %rax, %xmm0
movl 0x5c(%rsp), %eax
cvtsi2ss %rax, %xmm1
mulss 0x6c(%rsp), %xmm0
ucomiss %xmm0, %xmm1
ja 0x20e3ba
movq %rdx, %rbx
movl (%rdx), %edx
subq $0x8, %rsp
movq 0x48(%rsp), %rbp
movq %rbp, %rdi
movl 0x68(%rsp), %ecx
movl 0x6c(%rsp), %r8d
movl 0x70(%rsp), %r9d
pushq 0x80(%rsp)
pushq %r11
pushq $0x1
movq %r11, %r14
callq 0x20d9d3
addq $0x20, %rsp
testl %eax, %eax
je 0x20e3c8
movq (%r14), %rax
movq %rax, 0x20(%rsp)
movl (%rbx), %edx
subq $0x20, %rsp
movq %r14, 0x10(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %rbp, %rdi
movq 0x38(%rsp), %rbx
movq %rbx, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x20d67b
addq $0x20, %rsp
movl 0x20(%rsp), %ecx
movq (%r14), %rax
movq %rcx, 0x30(%rsp)
cmpq %rcx, %rax
movq %rbx, %rsi
movq %r14, %r11
jbe 0x20e37b
movq 0x30(%rsp), %rcx
movq 0x20(%rsp), %rbx
movq 0x10(%r13), %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
movb $0x1, %sil
xorl %edi, %edi
orq %rax, %rdi
movq 0x28(%rdx), %r8
movq (%r8,%rdi,8), %r9
movq %r9, %r8
andq %r12, %r8
je 0x20e313
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), %r14
movq (%r14,%r11,8), %r14
movq %r14, %r11
andq %r12, %r11
je 0x20e30b
movq 0x8(%rdx), %r13
shrq $0x20, %r14
shlq $0x3, %r11
leaq (%r11,%r11,2), %r11
leaq (%r14,%r14,2), %r14
leaq 0xc(,%r14,8), %r14
addq %r13, %r14
xorl %r13d, %r13d
movl -0x4(%r14,%r13), %ebp
xorl %edi, %ebp
cmpl $0x1, %ebp
jne 0x20e302
orb $-0x80, 0x3(%r14,%r13)
addq $0x18, %r13
cmpq %r13, %r11
jne 0x20e2f0
incq %r10
cmpq %r8, %r10
jne 0x20e2ab
movl $0x1, %edi
testb $0x1, %sil
movl $0x0, %esi
jne 0x20e285
movq 0x18(%rsp), %r13
movq 0x10(%r13), %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %r14
movq 0x18(%rax,%r14), %rdi
callq 0x2220
movq %r13, %rsi
movq 0x10(%r13), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax,%r14)
movups %xmm0, (%rax,%r14)
movq $0x0, 0x20(%rax,%r14)
incl %ebx
movq 0x38(%rsp), %r11
movq (%r11), %rax
movq %rbx, %rcx
cmpq %rbx, %rax
movq 0x28(%rsp), %r13
ja 0x20e25e
subl 0x20(%rsp), %eax
movq 0x50(%rsp), %rcx
addl %eax, %ecx
movq %rcx, 0x50(%rsp)
movq 0x30(%rsp), %rax
movq %rax, (%r11)
movq 0x10(%rsp), %rdi
jmp 0x20e3ba
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x28(%rsp), %r13
movq 0x38(%rsp), %r11
jmp 0x20e3ba
movq 0x18(%rsp), %rsi
movq 0x28(%rsp), %r13
incl %r15d
cmpq %r15, (%r11)
ja 0x20df98
jmp 0x20e3d7
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r14, %r11
jmp 0x20e3ba
cmpq $0x0, 0xf8(%rsp)
jne 0x20e3f4
movq 0x90(%rsp), %rdi
callq 0x2220
movq 0x10(%rsp), %rdi
cmpq $0x0, 0x100(%rsp)
movq 0x50(%rsp), %rbx
jne 0x20e416
movq 0xb0(%rsp), %rdi
callq 0x2220
movq 0x10(%rsp), %rdi
testl %ebx, %ebx
je 0x20e41f
callq 0xa0a7d
movl %ebx, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq