// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: ulg_arc_cut_misjoin(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_misjoin(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, kw, nw, cnt = 0, n_vtx = g->n_seq<<1, mm_ul, ul_max, to_del;
    asg_arc_t *av, *aw, *ve, *we; uint32_t ul_cnt, pb;

    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, ul_max = -1; i < nv; ++i) {
                if(av[i].del) continue; kv++;
                get_ul_path_info(uidx, ug, av[i].v, NULL, NULL, &ul_cnt, NULL, NULL, NULL);
                if(ul_cnt > ul_max) ul_max = ul_cnt;
            }
            // if((v>>1) == 5958) {
            //     fprintf(stderr, "[M::%s::] v>>1::%u, v&1::%u, kv::%u, ul_max::%u\n", 
            //     __func__, v>>1, v&1, kv, ul_max);
            // }
            if(kv < 2 || ul_max == 0) continue; //note: ul_cnt/ul_max might be 0

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

    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;

        ve = &(g->arc[(uint32_t)b->a[k]]); pb = b->n; ub->n = 0;
        get_ul_path_info(uidx, ug, ve->v, NULL, NULL, &mm_ul, NULL, NULL, NULL);
        // if((v>>1) == 5958 && (w>>1) == 11105) {
        //     fprintf(stderr, ">>>[M::%s::] v>>1::%u, v&1::%u, w>>1::%u, w&1::%u, mm_ul::%u\n", 
        //     __func__, v>>1, v&1, w>>1, w&1, mm_ul);
        // }
        if(mm_ul == 0) continue;///no UL read support this path

        for (i = kv = 0; i < nv; ++i) {
            if(av[i].del) continue; 
            kv++;
            if(av[i].v == ve->v) continue;
            get_ul_path_info(uidx, ug, av[i].v, NULL, NULL, &ul_cnt, NULL, NULL, NULL);
            // if((v>>1) == 5958 && (w>>1) == 11105) {
            //     fprintf(stderr, "*[M::%s::i->%u] v>>1::%u, v&1::%u, w>>1::%u, w&1::%u, av[i].v>>1::%u, av[i].v&1::%u, ul_cnt::%u\n", 
            //     __func__, i, v>>1, v&1, w>>1, w&1, av[i].v>>1, av[i].v&1, ul_cnt);
            // }
            if (mm_ul <= ul_cnt*len_rat) {
                ul_cnt = ((uint32_t)-1) - ul_cnt;
                kv_push(uint64_t, *b, (((uint64_t)ul_cnt)<<32)|((uint64_t)(i)));
            }
        }
        if(b->n == pb) continue;
        to_del = 0; assert(kv >= 2); 

        aw = asg_arc_a(g, w); nw = asg_arc_n(g, w);
        for (i = kw = 0, we = NULL; i < nw; ++i) {
            if (aw[i].del) continue;
            if (aw[i].v == (v^1)) we = &(aw[i]);
            kw++;
        }

        if((kv > 1 && kw > 1) || (usg_bridge_topocut_aux(uidx, ug, ve->v, max_ext, max_ext_hifi, topo_level, 0, b, ub))) {
            radix_sort_srt64(b->a + pb, b->a + b->n);
            for (i = pb; i < b->n; i++) {
                // if((v>>1) == 5958 && (w>>1) == 11105) {
                //     fprintf(stderr, "#[M::%s::srt_i->%u] v>>1::%u, v&1::%u, w>>1::%u, w&1::%u, av[(uint32_t)b->a[i]].v>>1::%u, av[(uint32_t)b->a[i]].v&1::%u\n", 
                //     __func__, (uint32_t)b->a[i], v>>1, v&1, w>>1, w&1, av[(uint32_t)b->a[i]].v>>1, av[(uint32_t)b->a[i]].v&1);
                // }
                if(usg_misjoin_topocut_aux(uidx, ug, ve->v, av[(uint32_t)b->a[i]].v, v, b, ub)) {
                    to_del = 1;
                    break;
                }
            }
        }   

        b->n = pb;
        if(to_del) {
            if(kv > 1 && kw > 1) {
                ve->del = we->del = 1; ++cnt;
            } else {
                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 $0xc8, %rsp
movl %r9d, 0x78(%rsp)
movss %xmm0, 0x7c(%rsp)
movl %ecx, 0x74(%rsp)
movq 0x108(%rsp), %rax
leaq 0xb0(%rsp), %r12
andq $0x0, 0x10(%r12)
xorps %xmm0, %xmm0
andq $0x0, 0xa0(%rsp)
movaps %xmm0, (%r12)
movaps %xmm0, 0x90(%rsp)
movq %rsi, 0x30(%rsp)
movq 0x18(%rsi), %r14
movl 0x14(%r14), %ebx
addl %ebx, %ebx
testq %rax, %rax
cmovneq %rax, %r12
movl %edx, 0x70(%rsp)
andq $0x0, (%r12)
movq %rdi, 0x40(%rsp)
xorl %ebp, %ebp
movq %r14, 0x38(%rsp)
movq %rbx, 0x68(%rsp)
cmpq %rbx, %rbp
je 0x1c2766
movq 0x20(%r14), %rax
movl %ebp, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x1c2601
movq 0x30(%r14), %rax
cmpb $0x0, (%rax,%rbp)
je 0x1c2606
incq %rbp
jmp 0x1c25e0
movq 0x28(%r14), %rax
movq (%rax,%rbp,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x1c2601
movq 0x8(%r14), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
leaq (%rcx,%rdx), %rsi
movq %rsi, 0x58(%rsp)
movl %eax, %eax
movq %rax, 0x28(%rsp)
imulq $0x18, %rax, %r15
leaq (%rcx,%rdx), %r13
addq $0xc, %r13
xorl %r14d, %r14d
xorl %ebx, %ebx
cmpq %r14, %r15
je 0x1c2685
cmpl $0x0, (%r13,%r14)
js 0x1c267f
incl %ebx
movl -0x4(%r13,%r14), %edx
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
movq 0x40(%rsp), %rdi
movq 0x30(%rsp), %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0x24(%rsp), %r9
callq 0x1c0c77
addq $0x18, %r14
jmp 0x1c2645
cmpl $0x2, %ebx
movq 0x38(%rsp), %r14
movq 0x68(%rsp), %rbx
jb 0x1c2601
xorl %r15d, %r15d
cmpq %r15, 0x28(%rsp)
je 0x1c2601
cmpl $0x0, (%r13)
js 0x1c275a
movl -0x4(%r13), %edx
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
movq 0x40(%rsp), %rdi
movq 0x30(%rsp), %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0x24(%rsp), %r9
callq 0x1c0c77
movl 0x24(%rsp), %esi
pushq $0x3
popq %rax
movl %eax, %ecx
testl %esi, %esi
je 0x1c26f3
pushq $-0x1
popq %rax
xorl %edx, %edx
divl %esi
movl %eax, %ecx
notl %ecx
movl %ecx, 0x24(%rsp)
movq (%r12), %r8
movq 0x10(%r12), %rdi
cmpq 0x8(%r12), %r8
jne 0x1c2733
leaq (%r8,%r8), %rsi
testq %r8, %r8
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r12)
shlq $0x3, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0x10(%r12)
movl 0x24(%rsp), %ecx
movq (%r12), %r8
shlq $0x20, %rcx
movq 0x58(%rsp), %rax
subq 0x8(%r14), %rax
cqto
pushq $0x18
popq %rsi
idivq %rsi
addq %r15, %rax
orq %rcx, %rax
leaq 0x1(%r8), %rcx
movq %rcx, (%r12)
movq %rax, (%rdi,%r8,8)
incq %r15
addq $0x18, %r13
jmp 0x1c269b
movq 0x110(%rsp), %rax
testq %rax, %rax
leaq 0x90(%rsp), %rbp
cmovneq %rax, %rbp
movq 0x10(%r12), %rdi
movq (%r12), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1a3def
xorl %r13d, %r13d
movl $0x0, 0x48(%rsp)
movq %rbp, 0x80(%rsp)
movl %r13d, %r13d
movq (%r12), %r15
cmpq %r13, %r15
jbe 0x1c2b3e
movq 0x8(%r14), %rbx
movq 0x10(%r12), %rax
movl (%rax,%r13,8), %eax
imulq $0x18, %rax, %rsi
cmpl $0x0, 0xc(%rbx,%rsi)
js 0x1c2acb
addq %rbx, %rsi
movq (%rsi), %rcx
movq 0x20(%r14), %rax
movq %rcx, %rdi
shrq $0x21, %rcx
cmpl $0x0, (%rax,%rcx,8)
js 0x1c2acb
movl 0x8(%rsi), %edx
movl %edx, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x1c2acb
shrq $0x20, %rdi
movq %rdx, %rcx
xorq $0x1, %rcx
movq 0x28(%r14), %rax
movq %rdi, 0x58(%rsp)
movq (%rax,%rdi,8), %r14
testl $0xfffffffe, %r14d      # imm = 0xFFFFFFFE
jne 0x1c2828
testl $0xfffffffe, (%rax,%rcx,8) # imm = 0xFFFFFFFE
je 0x1c2ac6
movq %rcx, 0x88(%rsp)
movq %rsi, 0x28(%rsp)
andq $0x0, (%rbp)
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
movq 0x40(%rsp), %rdi
movq 0x30(%rsp), %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0x4c(%rsp), %r9
callq 0x1c0c77
cmpl $0x0, 0x4c(%rsp)
je 0x1c2ac6
movq %r15, 0x60(%rsp)
movq %r14, %rax
shrq $0x20, %rax
imulq $0x18, %rax, %rax
leaq (%rbx,%rax), %rcx
movq %rcx, 0x68(%rsp)
movl %r14d, %r15d
leaq (%rbx,%rax), %r14
addq $0xc, %r14
xorl %ebp, %ebp
xorl %ebx, %ebx
movq 0x28(%rsp), %rdi
cmpq %rbp, %r15
je 0x1c296a
cmpl $0x0, (%r14)
js 0x1c295e
incl %ebx
movl -0x4(%r14), %edx
cmpl 0x8(%rdi), %edx
je 0x1c295e
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
movq 0x40(%rsp), %rdi
movq 0x30(%rsp), %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq 0x24(%rsp), %r9
callq 0x1c0c77
movl 0x4c(%rsp), %eax
cvtsi2ss %rax, %xmm0
movl 0x24(%rsp), %ecx
cvtsi2ss %rcx, %xmm1
mulss 0x7c(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jae 0x1c2904
movq 0x28(%rsp), %rdi
jmp 0x1c295e
notl %ecx
movl %ecx, 0x24(%rsp)
movq (%r12), %rdx
movq 0x10(%r12), %rax
cmpq 0x8(%r12), %rdx
jne 0x1c2946
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r12)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r12)
movl 0x24(%rsp), %ecx
movq (%r12), %rdx
movq 0x28(%rsp), %rdi
shlq $0x20, %rcx
addq %rbp, %rcx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r12)
movq %rcx, (%rax,%rdx,8)
incq %rbp
addq $0x18, %r14
jmp 0x1c2898
movq (%r12), %rax
movl 0x60(%rsp), %r9d
cmpq %r9, %rax
jne 0x1c298a
movq 0x38(%rsp), %r14
movq 0x80(%rsp), %rbp
jmp 0x1c2acb
cmpl $0x1, %ebx
movq 0x38(%rsp), %rsi
movq 0x80(%rsp), %rbp
jbe 0x1c2b92
movq 0x28(%rsi), %rcx
movl 0x88(%rsp), %edx
movq (%rcx,%rdx,8), %rdx
movl %edx, %ecx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
addq 0x8(%rsi), %rdx
xorl %r15d, %r15d
xorl %ebx, %ebx
movq 0x58(%rsp), %r8
subq $0x1, %rcx
jb 0x1c29e9
cmpl $0x0, 0xc(%rdx)
js 0x1c29e3
movl 0x8(%rdx), %esi
xorl %r8d, %esi
cmpl $0x1, %esi
cmoveq %rdx, %rbx
incl %r15d
addq $0x18, %rdx
jmp 0x1c29c7
cmpl $0x1, %r15d
movq %r9, 0x50(%rsp)
ja 0x1c2a33
movl 0x8(%rdi), %edx
andl $0x0, (%rsp)
movq %rbp, 0x10(%rsp)
movq %r12, 0x8(%rsp)
movq 0x40(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl 0x70(%rsp), %ecx
movl 0x74(%rsp), %r8d
movl 0x78(%rsp), %r9d
callq 0x1c0f0c
testl %eax, %eax
je 0x1c2abd
movq (%r12), %rax
movq 0x50(%rsp), %r9
movq 0x10(%r12), %rcx
leaq (%rcx,%r9,8), %rdi
leaq (%rcx,%rax,8), %rsi
callq 0x1a3def
movq 0x60(%rsp), %rax
movl %eax, %ecx
movl %ecx, %r14d
cmpq %r14, (%r12)
jbe 0x1c2abd
movq 0x28(%rsp), %rax
movl 0x8(%rax), %edx
movq 0x10(%r12), %rax
movl (%rax,%r14,8), %eax
imulq $0x18, %rax, %rax
movq 0x68(%rsp), %rcx
movl 0x8(%rcx,%rax), %ecx
movq %rbp, (%rsp)
movq 0x40(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x58(%rsp), %r8
movq %r12, %r9
callq 0x1c22a1
leal 0x1(%r14), %ecx
testl %eax, %eax
je 0x1c2a4c
movq 0x50(%rsp), %rax
movq %rax, (%r12)
cmpl $0x2, %r15d
movq 0x38(%rsp), %r14
jb 0x1c2ad3
orb $-0x80, 0xf(%rbx)
movq 0x28(%rsp), %rax
orb $-0x80, 0xf(%rax)
incl 0x48(%rsp)
jmp 0x1c2acb
movq 0x50(%rsp), %rax
movq %rax, (%r12)
movq 0x38(%rsp), %r14
incl %r13d
jmp 0x1c27a2
movq 0x28(%rsp), %rax
movl 0x8(%rax), %edx
movq %r12, 0x10(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq 0x40(%rsp), %rdi
movq 0x30(%rsp), %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x1c0c77
movq 0x60(%rsp), %r15
movl %r15d, %eax
movl %eax, %ebx
movq (%r12), %rax
cmpq %rbx, %rax
jbe 0x1c2b2c
movq 0x10(%r12), %rax
movq (%rax,%rbx,8), %rsi
shrq %rsi
movq 0x30(%rsp), %rdi
callq 0x1bfe8f
leal 0x1(%rbx), %eax
jmp 0x1c2b06
subl %r15d, %eax
addl %eax, 0x48(%rsp)
movq 0x50(%rsp), %rax
movq %rax, (%r12)
jmp 0x1c2acb
cmpq $0x0, 0x108(%rsp)
jne 0x1c2b56
movq 0xc0(%rsp), %rdi
callq 0x2220
cmpq $0x0, 0x110(%rsp)
movl 0x48(%rsp), %ebx
jne 0x1c2b72
movq 0xa0(%rsp), %rdi
callq 0x2220
testl %ebx, %ebx
je 0x1c2b7e
movq %r14, %rdi
callq 0x694eb
movl %ebx, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x37533(%rip), %rdi     # 0x1fa0cc
leaq 0x34939(%rip), %rsi     # 0x1f74d9
leaq 0x3752d(%rip), %rcx     # 0x1fa0d4
movl $0x2528, %edx           # imm = 0x2528
callq 0x2110