// File: /workspace/github_cpp_makefile_star2_O1/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 $0xc8, %rsp
movq %r9, 0x88(%rsp)
movss %xmm0, 0x4c(%rsp)
movq %rcx, 0x80(%rsp)
movq %rdx, 0x78(%rsp)
movq %rdi, 0x30(%rsp)
movq 0x120(%rsp), %rdx
movq 0x118(%rsp), %r8
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %r9
movaps %xmm0, (%r9)
xorl %ecx, %ecx
movq %rcx, 0x10(%r9)
leaq 0xb0(%rsp), %r10
movq %rcx, 0x10(%r10)
movaps %xmm0, (%r10)
movq %rsi, 0x70(%rsp)
movq 0x18(%rsi), %rdi
movl 0x14(%rdi), %eax
addl %eax, %eax
testq %r8, %r8
cmoveq %r9, %r8
testq %rdx, %rdx
cmovneq %rdx, %r10
movq %r10, 0x28(%rsp)
movq $0x0, 0x50(%rsp)
movq %rcx, (%r8)
testl %eax, %eax
movq %rdi, 0x8(%rsp)
movq %r8, 0x18(%rsp)
je 0x205545
movq 0x118(%rsp), %rbx
testq %rbx, %rbx
movq %rbx, %r11
leaq 0x90(%rsp), %rcx
cmoveq %rcx, %r11
movl %eax, %eax
movq %rax, 0x68(%rsp)
xorl %r15d, %r15d
movabsq $-0x5555555555555555, %rbp # imm = 0xAAAAAAAAAAAAAAAB
movq %r11, 0x10(%rsp)
movq 0x20(%rdi), %rax
movl %r15d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x205462
movq 0x30(%rdi), %rax
cmpb $0x0, (%rax,%r15)
je 0x205471
incq %r15
cmpq 0x68(%rsp), %r15
jne 0x205448
jmp 0x205545
movq 0x28(%rdi), %rax
movq (%rax,%r15,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x205462
movq 0x8(%rdi), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %rbx
movl %eax, %r12d
leaq (%rcx,%rdx,8), %r13
addq $0xc, %r13
xorl %ecx, %ecx
movl $0x1, %eax
movq %r13, %rdx
movl %ecx, %esi
movl (%rdx), %ecx
notl %ecx
shrl $0x1f, %ecx
addl %esi, %ecx
cmpq %r12, %rax
jae 0x2054c4
addq $0x18, %rdx
incq %rax
cmpl $0x2, %ecx
jb 0x2054a8
cmpl $0x2, %ecx
jb 0x205462
xorl %r14d, %r14d
cmpl $0x0, (%r13)
js 0x205534
movq 0x8(%r11), %rax
cmpq %rax, (%r8)
jne 0x205510
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r11)
movq 0x10(%r11), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x10(%rsp), %r11
movq 0x18(%rsp), %r8
movq 0x8(%rsp), %rdi
movq %rax, 0x10(%r11)
movq %rbx, %rax
subq 0x8(%rdi), %rax
sarq $0x3, %rax
imulq %rbp, %rax
addq %r14, %rax
movq 0x10(%r11), %rcx
movq (%r8), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%r8)
movq %rax, (%rcx,%rdx,8)
incq %r14
addq $0x18, %r13
cmpq %r14, %r12
jne 0x2054cc
jmp 0x205462
movq 0x30(%rsp), %rbx
movq %r8, 0x290(%rbx)
movl 0x1e0(%rbx), %edi
movq (%r8), %rcx
leaq 0x4d5(%rip), %rsi       # 0x205a36
movq %rbx, %rdx
callq 0x1047b4
movq 0x118(%rsp), %rax
testq %rax, %rax
leaq 0x90(%rsp), %r14
cmovneq %rax, %r14
movq $0x0, 0x290(%rbx)
movq 0x10(%r14), %rdi
movq 0x18(%rsp), %rax
movq (%rax), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1e3b6a
movq 0x18(%rsp), %r10
movq 0x8(%rsp), %rdi
movq (%r10), %r13
testq %r13, %r13
je 0x2059da
xorl %ecx, %ecx
movq $0x0, 0x50(%rsp)
xorl %ebx, %ebx
movq %r14, 0x10(%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 0x205817
leaq (%rax,%rcx,8), %r11
movq (%r11), %r15
movq 0x20(%rdi), %rcx
movq %r15, %rdx
shrq $0x21, %rdx
cmpl $0x0, (%rcx,%rdx,8)
js 0x205817
movl 0x8(%r11), %esi
movl %esi, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0x205817
shrq $0x20, %r15
xorq $0x1, %rsi
movq 0x28(%rdi), %rcx
movq (%rcx,%r15,8), %rdx
movq (%rcx,%rsi,8), %rcx
cmpl $0x1, %edx
ja 0x205632
cmpl $0x2, %ecx
jb 0x205817
movq %rsi, 0x68(%rsp)
xorl %r12d, %r12d
movl $0x0, %ebp
testl %edx, %edx
je 0x20567d
movq %rdx, %rsi
shrq $0x20, %rsi
movl %edx, %r8d
leaq (%rsi,%rsi,2), %rdx
leaq (%rax,%rdx,8), %rdx
addq $0xc, %rdx
shlq $0x3, %r8
leaq (%r8,%r8,2), %rsi
xorl %r9d, %r9d
xorl %ebp, %ebp
movl (%rdx,%r9), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %ebp
addq $0x18, %r9
cmpq %r9, %rsi
jne 0x205666
testl %ecx, %ecx
je 0x2056b9
movq %rcx, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %rax
movl %ecx, %ecx
xorl %r14d, %r14d
xorl %r12d, %r12d
cmpl $0x0, 0xc(%rax)
js 0x2056ae
movl 0x8(%rax), %edx
xorl %r15d, %edx
cmpl $0x1, %edx
cmoveq %rax, %r14
incl %r12d
addq $0x18, %rax
decq %rcx
jne 0x205698
jmp 0x2056bc
xorl %r14d, %r14d
cmpl $0x2, %ebp
setb %al
cmpl $0x2, %r12d
setb %cl
testb %cl, %al
je 0x2056d7
movq 0x10(%rsp), %r14
jmp 0x205817
cmpl $0x0, 0x110(%rsp)
movq %r11, 0x60(%rsp)
je 0x2057a3
xorl %eax, %eax
cmpl $0x2, %ebp
jb 0x20573a
movq 0x28(%rsp), %rcx
xorl %eax, %eax
movq %rax, (%rcx)
subq $0x8, %rsp
movq 0x38(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq %r10, %rdx
movl 0x108(%rsp), %r8d
leaq 0x48(%rsp), %r9
leaq 0x40(%rsp), %rax
pushq %rax
callq 0x204e06
movq 0x28(%rsp), %r10
addq $0x10, %rsp
movq %r13, (%r10)
xorl %ecx, %ecx
movq 0x28(%rsp), %rdx
movq %rcx, (%rdx)
testl %eax, %eax
sete %dl
cmpl $0x2, %r12d
setae %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x2057a5
movq (%r10), %rax
movq %rax, 0x20(%rsp)
movq 0x28(%rsp), %r13
xorl %eax, %eax
movq %rax, (%r13)
subq $0x8, %rsp
movq 0x38(%rsp), %rdi
movq %r14, %rsi
movq %r10, %rdx
movq %r13, %rcx
movl 0x108(%rsp), %r8d
leaq 0x48(%rsp), %r9
leaq 0x40(%rsp), %rax
pushq %rax
callq 0x204e06
movq 0x28(%rsp), %r10
addq $0x10, %rsp
movq 0x20(%rsp), %rcx
movq %rcx, (%r10)
xorl %ecx, %ecx
movq %rcx, (%r13)
jmp 0x2057a5
xorl %eax, %eax
testl %eax, %eax
je 0x20582d
movq 0x30(%rsp), %rdi
movq 0x70(%rsp), %rsi
movl %r15d, %edx
movq 0x68(%rsp), %rcx
movl %ebp, %r8d
movl %r12d, %r9d
pushq 0x28(%rsp)
pushq %r10
pushq %rax
pushq 0xa0(%rsp)
pushq 0xa0(%rsp)
pushq 0xa0(%rsp)
callq 0x2052d8
addq $0x30, %rsp
testl %eax, %eax
je 0x205808
orb $-0x80, 0xf(%r14)
movq 0x60(%rsp), %rax
orb $-0x80, 0xf(%rax)
movq 0x50(%rsp), %rax
incl %eax
movq %rax, 0x50(%rsp)
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r10
incl %ebx
movq (%r10), %r13
movq %rbx, %rcx
cmpq %rbx, %r13
ja 0x2055c8
jmp 0x2059da
cmpl $0x2, %ebp
jb 0x2058fb
movq %rax, 0x20(%rsp)
movq (%r10), %rax
movq %rax, 0x58(%rsp)
movq 0x28(%rsp), %r13
xorl %eax, %eax
movq %rax, (%r13)
subq $0x8, %rsp
movq 0x38(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq %r10, %rdx
movq %r13, %rcx
movl 0x108(%rsp), %r8d
leaq 0x48(%rsp), %r9
leaq 0x40(%rsp), %rax
pushq %rax
callq 0x204934
movq 0x28(%rsp), %r10
addq $0x10, %rsp
movq 0x58(%rsp), %rax
movq %rax, (%r10)
xorl %eax, %eax
movq %rax, (%r13)
movq 0x40(%rsp), %rax
cmpq $-0x1, %rax
je 0x2059d0
testq %rax, %rax
js 0x2058ac
cvtsi2ss %rax, %xmm0
jmp 0x2058c1
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movq 0x20(%rsp), %rax
movq 0x38(%rsp), %rdx
testq %rdx, %rdx
js 0x2058d7
cvtsi2ss %rdx, %xmm1
jmp 0x2058ec
movq %rdx, %rcx
shrq %rcx
andl $0x1, %edx
orq %rcx, %rdx
cvtsi2ss %rdx, %xmm1
addss %xmm1, %xmm1
mulss 0x4c(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x2059d0
cmpl $0x2, %r12d
jb 0x2057ad
movq %rax, 0x20(%rsp)
movq (%r10), %rax
movq %rax, 0x58(%rsp)
movq 0x28(%rsp), %r13
xorl %eax, %eax
movq %rax, (%r13)
subq $0x8, %rsp
movq 0x38(%rsp), %rdi
movq %r14, %rsi
movq %r10, %rdx
movq %r13, %rcx
movl 0x108(%rsp), %r8d
leaq 0x48(%rsp), %r9
leaq 0x40(%rsp), %rax
pushq %rax
callq 0x204934
movq 0x28(%rsp), %r10
addq $0x10, %rsp
movq 0x58(%rsp), %rax
movq %rax, (%r10)
xorl %eax, %eax
movq %rax, (%r13)
movq 0x40(%rsp), %rax
cmpq $-0x1, %rax
je 0x2059d0
testq %rax, %rax
js 0x205978
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x205990
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movq 0x20(%rsp), %rax
movq 0x38(%rsp), %rdx
testq %rdx, %rdx
js 0x2059a9
xorps %xmm1, %xmm1
cvtsi2ss %rdx, %xmm1
jmp 0x2059c1
movq %rdx, %rcx
shrq %rcx
andl $0x1, %edx
orq %rcx, %rdx
xorps %xmm1, %xmm1
cvtsi2ss %rdx, %xmm1
addss %xmm1, %xmm1
mulss 0x4c(%rsp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x2057ad
movq 0x8(%rsp), %rdi
jmp 0x2056cd
cmpq $0x0, 0x118(%rsp)
jne 0x2059f7
movq 0xa0(%rsp), %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
cmpq $0x0, 0x120(%rsp)
movq 0x50(%rsp), %rbx
jne 0x205a19
movq 0xc0(%rsp), %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
testl %ebx, %ebx
je 0x205a22
callq 0xa02fa
movl %ebx, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq