// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: ulg_arc_cut_supports(ul_resolve_t*, ma_ug_t*, int, unsigned int, float, unsigned int, unsigned int, unsigned int, unsigned int*, unsigned int, asg64_v*, asg64_v*)
uint32_t ulg_arc_cut_supports(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 skip_hom, uint32_t *max_drop_len, uint32_t collapse_check,
asg64_v *in, asg64_v *ib)
{
    // fprintf(stderr, "\n[M::%s::] max_ext::%d, max_ext_hifi::%d, len_rat::%f, is_trio::%u, topo_level::%u, skip_hom::%u, collapse_check::%u\n", 
    //         __func__, max_ext, max_ext_hifi, len_rat, is_trio, topo_level, skip_hom, collapse_check);

    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, to_del, collapse;
    asg_arc_t *av, *aw, *ve, *we; uint64_t w_q, w_t, 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; 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;
                kv_push(uint64_t, *b, ((uint64_t)(av-g->arc+i)));
            }
        }
    }

    // fprintf(stderr, "\n#[M::%s::] Starting...\n", __func__);
    uidx->uovl.iug_tra = b;
    kt_for(uidx->str_b.n_thread, worker_update_ul_arc_supports, uidx, b->n);///all ul + ug
    uidx->uovl.iug_tra = NULL;
    // fprintf(stderr, "#[M::%s::] Done\n", __func__);
    // fprintf(stderr, "#[M::%s::] collapse_check::%u, len_rat::%f\n", __func__, collapse_check, len_rat);
    
    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); 
        nw = asg_arc_n(g, w); aw = asg_arc_a(g, w); 
        if(nv <= 1 && nw <= 1) continue;
        ve = &(g->arc[(uint32_t)b->a[k]]);
        for (i = kv = 0; i < nv; ++i) {
            if(av[i].del) continue; 
            kv++;
        }
        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) continue;
        collapse = 0;

        if(collapse_check) {
            if(kv > 1) {
                pb = b->n; ub->n = 0;
                collapse = check_ul_contain_arc_supports(uidx, ve, b, ub, skip_hom, &w_q, &w_t);
                b->n = pb; ub->n = 0;
            }

            if(collapse == 0 && kw > 1) {
                pb = b->n; ub->n = 0;
                collapse = check_ul_contain_arc_supports(uidx, we, b, ub, skip_hom, &w_q, &w_t);
                b->n = pb; ub->n = 0;
            }
        }

        // if(((v>>1) == 6788 && (w>>1) == 17213) || ((w>>1) == 6788 && (v>>1) == 17213)) {
        //     fprintf(stderr, "#[M::%s::] v>>1::%u, v&1::%u, kv::%u, w>>1::%u, w&1::%u, kw::%u, collapse::%u\n", 
        //     __func__, v>>1, v&1, kv, w>>1, w&1, kw, collapse);
        // }

        if(collapse == 0) {
            if(kv > 1) {
                pb = b->n; ub->n = 0;
                get_ul_arc_supports(uidx, ve, b, ub, skip_hom, &w_q, &w_t);
                b->n = pb; ub->n = 0;
                // if((v>>1) == 409 && (w>>1) == 407) {
                //     fprintf(stderr, "+[M::%s::] v>>1::%u, v&1::%u, kv::%u, w>>1::%u, w&1::%u, kw::%u, w_q::%lu, w_t::%lu\n", 
                //     __func__, v>>1, v&1, kv, w>>1, w&1, kw, w_q, w_t);
                // }
                // if(((v>>1) == 6788 && (w>>1) == 17213) || ((w>>1) == 6788 && (v>>1) == 17213)) {
                //     fprintf(stderr, "+[M::%s::] v>>1::%u, v&1::%u, w_q::%lu, w>>1::%u, w&1::%u, w_t::%lu\n", 
                //     __func__, v>>1, v&1, w_q, w>>1, w&1, w_t);
                // }
                if(w_q == (uint64_t)-1) continue;
                if(w_q > w_t*len_rat) continue;
            }

            if(kw > 1) {
                pb = b->n; ub->n = 0;
                get_ul_arc_supports(uidx, we, b, ub, skip_hom, &w_q, &w_t);
                b->n = pb; ub->n = 0;
                // if((v>>1) == 409 && (w>>1) == 407) {
                //     fprintf(stderr, "-[M::%s::] v>>1::%u, v&1::%u, kv::%u, w>>1::%u, w&1::%u, kw::%u, w_q::%lu, w_t::%lu\n", 
                //     __func__, v>>1, v&1, kv, w>>1, w&1, kw, w_q, w_t);
                // }
                // if(((v>>1) == 6788 && (w>>1) == 17213) || ((w>>1) == 6788 && (v>>1) == 17213)) {
                //     fprintf(stderr, "-[M::%s::] v>>1::%u, v&1::%u, w_q::%lu, w>>1::%u, w&1::%u, w_t::%lu\n", 
                //     __func__, v>>1, v&1, w_q, w>>1, w&1, w_t);
                // }
                if(w_q == (uint64_t)-1) continue;
                if(w_q > w_t*len_rat) continue;
            }
        }

        to_del = check_ulg_to_del(uidx, ug, v, w, kv, kw, max_ext, max_ext_hifi, topo_level, collapse, b, ub);
        
        // if(((v>>1) == 6788 && (w>>1) == 17213) || ((w>>1) == 6788 && (v>>1) == 17213)) {
        //     fprintf(stderr, "#[M::%s::] v>>1::%u, v&1::%u, kv::%u, w>>1::%u, w&1::%u, kw::%u, to_del::%u\n", 
        //     __func__, v>>1, v&1, kv, w>>1, w&1, kw, to_del);
        // }
        if (to_del) {
            ve->del = we->del = 1, ++cnt;
        }
    }

    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
movq %r9, 0x78(%rsp)
movss %xmm0, 0x44(%rsp)
movq %rcx, 0x70(%rsp)
movq %rdx, 0x68(%rsp)
movq 0x108(%rsp), %rbx
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %r14
movaps %xmm0, (%r14)
xorl %ecx, %ecx
movq %rcx, 0x10(%r14)
leaq 0xa0(%rsp), %r9
movq %rcx, 0x10(%r9)
movaps %xmm0, (%r9)
movq %rsi, 0x60(%rsp)
movq 0x18(%rsi), %r8
movl 0x14(%r8), %eax
addl %eax, %eax
testq %rbx, %rbx
movq %rbx, %r10
cmoveq %r14, %r10
movq 0x110(%rsp), %rdx
testq %rdx, %rdx
cmovneq %rdx, %r9
movq %r9, 0x18(%rsp)
movq %rdi, 0x20(%rsp)
movq $0x0, 0x48(%rsp)
movq %rcx, (%r10)
movl $0x0, %ecx
testl %eax, %eax
movq %r8, (%rsp)
movq %r10, 0x10(%rsp)
je 0x210a91
testq %rbx, %rbx
cmoveq %r14, %rbx
movl %eax, %eax
movq %rax, 0x58(%rsp)
xorl %r15d, %r15d
movabsq $-0x5555555555555555, %rbp # imm = 0xAAAAAAAAAAAAAAAB
movq 0x20(%r8), %rax
movl %r15d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x210985
movq 0x30(%r8), %rax
cmpb $0x0, (%rax,%r15)
je 0x210994
incq %r15
cmpq 0x58(%rsp), %r15
jne 0x21096b
jmp 0x210a7e
movq 0x28(%r8), %rax
movq (%rax,%r15,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x210985
movq 0x8(%r8), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %r11
movl %eax, %r12d
leaq (%rcx,%rdx,8), %r13
addq $0xc, %r13
xorl %edx, %edx
movl $0x1, %eax
movq %r13, %rsi
movl %edx, %edi
movl (%rsi), %edx
notl %edx
shrl $0x1f, %edx
addl %edi, %edx
cmpq %r12, %rax
jae 0x2109e7
addq $0x18, %rsi
incq %rax
cmpl $0x2, %edx
jb 0x2109cb
cmpl $0x2, %edx
jb 0x210985
xorl %r14d, %r14d
movq %r15, 0x8(%rsp)
cmpl $0x0, (%r13)
js 0x210a69
movq (%r10), %rdx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rdx
jne 0x210a4d
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
movq %rbx, %r15
movq %r11, %rbx
callq 0x2370
movq %rbx, %r11
movq %r15, %rbx
movq 0x8(%rsp), %r15
movq 0x10(%rsp), %r10
movq (%rsp), %r8
movq %rax, 0x10(%rbx)
movq 0x8(%r8), %rcx
movq (%r10), %rdx
movq %r11, %rsi
subq %rcx, %rsi
sarq $0x3, %rsi
imulq %rbp, %rsi
addq %r14, %rsi
leaq 0x1(%rdx), %rdi
movq %rdi, (%r10)
movq %rsi, (%rax,%rdx,8)
incq %r14
addq $0x18, %r13
cmpq %r14, %r12
jne 0x2109f4
jmp 0x210985
movq (%r10), %rcx
movq 0x108(%rsp), %rbx
leaq 0x80(%rsp), %r14
movq 0x20(%rsp), %r15
movq %r10, 0x290(%r15)
movl 0x1e0(%r15), %edi
leaq 0x4bb(%rip), %rsi       # 0x210f66
movq %r15, %rdx
callq 0x108f2c
testq %rbx, %rbx
cmovneq %rbx, %r14
movq $0x0, 0x290(%r15)
movq 0x10(%r14), %rdi
movq 0x10(%rsp), %rax
movq (%rax), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1ee586
movq 0x10(%rsp), %r10
movq (%rsp), %rdi
movq (%r10), %r12
testq %r12, %r12
je 0x210f0c
xorl %ecx, %ecx
movq $0x0, 0x48(%rsp)
xorl %ebx, %ebx
movq %r14, 0x8(%rsp)
movq 0x8(%rdi), %rax
movq 0x10(%r14), %rdx
movl (%rdx,%rcx,8), %ecx
leaq (%rcx,%rcx,2), %rcx
cmpl $0x0, 0xc(%rax,%rcx,8)
js 0x210efb
leaq (%rax,%rcx,8), %rsi
movq (%rsi), %r13
movq 0x20(%rdi), %rcx
movq %r13, %rdx
shrq $0x21, %rdx
cmpl $0x0, (%rcx,%rdx,8)
js 0x210efb
movl 0x8(%rsi), %r8d
movl %r8d, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0x210efb
shrq $0x20, %r13
xorq $0x1, %r8
movq 0x28(%rdi), %rcx
movq (%rcx,%r13,8), %rdx
movq (%rcx,%r8,8), %rcx
cmpl $0x1, %edx
ja 0x210b6c
cmpl $0x2, %ecx
jb 0x210efb
movq %r8, 0x58(%rsp)
xorl %r15d, %r15d
movl $0x0, %r14d
testl %edx, %edx
je 0x210bb9
movq %rdx, %r9
shrq $0x20, %r9
movl %edx, %r8d
leaq (%r9,%r9,2), %rdx
leaq (%rax,%rdx,8), %rdx
addq $0xc, %rdx
shlq $0x3, %r8
leaq (%r8,%r8,2), %r11
xorl %r9d, %r9d
xorl %r14d, %r14d
movl (%rdx,%r9), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %r14d
addq $0x18, %r9
cmpq %r9, %r11
jne 0x210ba2
testl %ecx, %ecx
je 0x210bf4
movq %rcx, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %rax
movl %ecx, %ecx
xorl %ebp, %ebp
xorl %r15d, %r15d
cmpl $0x0, 0xc(%rax)
js 0x210be9
movl 0x8(%rax), %edx
xorl %r13d, %edx
cmpl $0x1, %edx
cmoveq %rax, %rbp
incl %r15d
addq $0x18, %rax
decq %rcx
jne 0x210bd3
jmp 0x210bf6
xorl %ebp, %ebp
cmpl $0x2, %r14d
setb %al
cmpl $0x2, %r15d
setb %cl
testb %cl, %al
je 0x210c12
movq 0x8(%rsp), %r14
jmp 0x210efb
cmpl $0x0, 0x100(%rsp)
movq %rsi, 0x50(%rsp)
je 0x210cf0
xorl %eax, %eax
cmpl $0x2, %r14d
jb 0x210c76
movq 0x18(%rsp), %rcx
xorl %eax, %eax
movq %rax, (%rcx)
subq $0x8, %rsp
movq 0x28(%rsp), %rdi
movq %r10, %rdx
movl 0xf8(%rsp), %r8d
leaq 0x40(%rsp), %r9
leaq 0x38(%rsp), %rax
pushq %rax
callq 0x210318
movq 0x60(%rsp), %rsi
movq 0x20(%rsp), %r10
addq $0x10, %rsp
movq %r12, (%r10)
xorl %ecx, %ecx
movq 0x18(%rsp), %rdx
movq %rcx, (%rdx)
testl %eax, %eax
sete %cl
cmpl $0x2, %r15d
setae %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x210ce2
movq (%r10), %rax
movq %rax, 0x28(%rsp)
movq 0x18(%rsp), %r12
xorl %eax, %eax
movq %rax, (%r12)
subq $0x8, %rsp
movq 0x28(%rsp), %rdi
movq %rbp, %rsi
movq %r10, %rdx
movq %r12, %rcx
movl 0xf8(%rsp), %r8d
leaq 0x40(%rsp), %r9
leaq 0x38(%rsp), %rax
pushq %rax
callq 0x210318
movq 0x60(%rsp), %rsi
movq 0x20(%rsp), %r10
addq $0x10, %rsp
movq 0x28(%rsp), %rcx
movq %rcx, (%r10)
xorl %ecx, %ecx
movq %rcx, (%r12)
movl $0x1, %r11d
testl %eax, %eax
jne 0x210e92
cmpl $0x2, %r14d
jb 0x210db4
movq (%r10), %rax
movq %rax, 0x28(%rsp)
movq 0x18(%rsp), %r12
xorl %eax, %eax
movq %rax, (%r12)
subq $0x8, %rsp
movq 0x28(%rsp), %rdi
movq %r10, %rdx
movq %r12, %rcx
movl 0xf8(%rsp), %r8d
leaq 0x40(%rsp), %r9
leaq 0x38(%rsp), %rax
pushq %rax
callq 0x20fe1a
movq 0x20(%rsp), %r10
addq $0x10, %rsp
movq 0x28(%rsp), %rax
movq %rax, (%r10)
xorl %eax, %eax
movq %rax, (%r12)
movq 0x38(%rsp), %rax
cmpq $-0x1, %rax
je 0x210e33
testq %rax, %rax
movq (%rsp), %rdi
js 0x210d6a
cvtsi2ss %rax, %xmm0
jmp 0x210d7f
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movq 0x30(%rsp), %rax
testq %rax, %rax
js 0x210d90
cvtsi2ss %rax, %xmm1
jmp 0x210da5
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm1
addss %xmm1, %xmm1
mulss 0x44(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x210c08
xorl %r11d, %r11d
cmpl $0x2, %r15d
jb 0x210e92
movq (%r10), %rax
movq %rax, 0x28(%rsp)
movq 0x18(%rsp), %r12
xorl %eax, %eax
movq %rax, (%r12)
subq $0x8, %rsp
movq 0x28(%rsp), %rdi
movq %rbp, %rsi
movq %r10, %rdx
movq %r12, %rcx
movl 0xf8(%rsp), %r8d
leaq 0x40(%rsp), %r9
leaq 0x38(%rsp), %rax
pushq %rax
callq 0x20fe1a
movq 0x20(%rsp), %r10
addq $0x10, %rsp
movq 0x28(%rsp), %rax
movq %rax, (%r10)
xorl %eax, %eax
movq %rax, (%r12)
movq 0x38(%rsp), %rax
cmpq $-0x1, %rax
je 0x210e33
testq %rax, %rax
movq (%rsp), %rdi
js 0x210e3c
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x210e54
movq (%rsp), %rdi
jmp 0x210c08
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
xorl %r11d, %r11d
movq 0x30(%rsp), %rax
testq %rax, %rax
js 0x210e6b
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
jmp 0x210e83
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
addss %xmm1, %xmm1
mulss 0x44(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x210c08
movq 0x20(%rsp), %rdi
movq 0x60(%rsp), %rsi
movl %r13d, %edx
movq 0x58(%rsp), %rcx
movl %r14d, %r8d
movl %r15d, %r9d
pushq 0x18(%rsp)
pushq %r10
pushq %r11
pushq 0x90(%rsp)
pushq 0x90(%rsp)
pushq 0x90(%rsp)
callq 0x210811
addq $0x30, %rsp
testl %eax, %eax
je 0x210eed
orb $-0x80, 0xf(%rbp)
movq 0x50(%rsp), %rax
orb $-0x80, 0xf(%rax)
movq 0x48(%rsp), %rax
incl %eax
movq %rax, 0x48(%rsp)
movq (%rsp), %rdi
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %r10
incl %ebx
movq (%r10), %r12
movq %rbx, %rcx
cmpq %rbx, %r12
ja 0x210b01
cmpq $0x0, 0x108(%rsp)
jne 0x210f28
movq 0x90(%rsp), %rdi
callq 0x2220
movq (%rsp), %rdi
cmpq $0x0, 0x110(%rsp)
movq 0x48(%rsp), %rbx
jne 0x210f49
movq 0xb0(%rsp), %rdi
callq 0x2220
movq (%rsp), %rdi
testl %ebx, %ebx
je 0x210f52
callq 0xa0a7d
movl %ebx, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq