// File: /workspace/github_cpp_makefile_star2_O2/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, 0x40(%rsp)
movq %rcx, 0x70(%rsp)
movq %rdx, 0x68(%rsp)
movq %rdi, 0x20(%rsp)
movq 0x108(%rsp), %rax
leaq 0xa0(%rsp), %r14
andq $0x0, 0x10(%r14)
xorps %xmm0, %xmm0
andq $0x0, 0x90(%rsp)
movaps %xmm0, (%r14)
movaps %xmm0, 0x80(%rsp)
movq %rsi, 0x60(%rsp)
movq 0x18(%rsi), %r9
movl 0x14(%r9), %ecx
addl %ecx, %ecx
movq %rcx, 0x58(%rsp)
testq %rax, %rax
cmovneq %rax, %r14
andq $0x0, (%r14)
xorl %ebx, %ebx
pushq $0x18
popq %r10
movq %r9, 0x8(%rsp)
cmpq 0x58(%rsp), %rbx
je 0x1c3721
movq 0x20(%r9), %rax
movl %ebx, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x1c363b
movq 0x30(%r9), %rax
cmpb $0x0, (%rax,%rbx)
je 0x1c3640
incq %rbx
jmp 0x1c3618
movq 0x28(%r9), %rax
movq (%rax,%rbx,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x1c363b
movq 0x8(%r9), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
leaq (%rcx,%rdx), %r11
movl %eax, %r13d
leaq (%rcx,%rdx), %rbp
addq $0xc, %rbp
xorl %eax, %eax
movq %rbp, %rdx
xorl %esi, %esi
cmpq %r13, %rax
jae 0x1c3690
cmpl $0x1, %esi
ja 0x1c3690
movl (%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
incq %rax
addq $0x18, %rdx
jmp 0x1c3674
cmpl $0x2, %esi
jb 0x1c363b
xorl %r15d, %r15d
movq %rbx, 0x10(%rsp)
cmpq %r15, %r13
je 0x1c363b
cmpl $0x0, (%rbp)
js 0x1c3715
movq (%r14), %r8
movq 0x10(%r14), %rdi
cmpq 0x8(%r14), %r8
jne 0x1c36fc
leaq (%r8,%r8), %rsi
testq %r8, %r8
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %r10, %rbx
movq %r14, %r12
movq %r11, %r14
callq 0x2370
movq %r14, %r11
movq %r12, %r14
movq %rbx, %r10
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r9
movq %rax, %rdi
movq %rax, 0x10(%r12)
movq 0x8(%r9), %rcx
movq (%r12), %r8
movq %r11, %rax
subq %rcx, %rax
cqto
idivq %r10
addq %r15, %rax
leaq 0x1(%r8), %rdx
movq %rdx, (%r14)
movq %rax, (%rdi,%r8,8)
incq %r15
addq $0x18, %rbp
jmp 0x1c369d
movq 0x110(%rsp), %rax
testq %rax, %rax
leaq 0x80(%rsp), %rcx
cmovneq %rax, %rcx
movq %rcx, 0x10(%rsp)
movq 0x20(%rsp), %rbx
movq %r14, 0x290(%rbx)
movl 0x1e0(%rbx), %edi
movq (%r14), %rcx
leaq 0x44a(%rip), %rsi       # 0x1c3ba3
movq %rbx, %rdx
callq 0xc67c0
andq $0x0, 0x290(%rbx)
movq 0x10(%r14), %rdi
movq (%r14), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1a3def
movq 0x8(%rsp), %rdi
movl 0xf0(%rsp), %eax
movq %rax, 0x48(%rsp)
xorl %r13d, %r13d
movl $0x0, 0x44(%rsp)
movl %r13d, %r13d
movq (%r14), %r15
cmpq %r13, %r15
jbe 0x1c3b48
movq 0x8(%rdi), %rcx
movq 0x10(%r14), %rax
movl (%rax,%r13,8), %eax
imulq $0x18, %rax, %rsi
cmpl $0x0, 0xc(%rcx,%rsi)
js 0x1c3b40
addq %rcx, %rsi
movq (%rsi), %r11
movq 0x20(%rdi), %rax
movq %r11, %rdx
shrq $0x21, %rdx
cmpl $0x0, (%rax,%rdx,8)
js 0x1c3b40
movl 0x8(%rsi), %ebx
movl %ebx, %edx
shrl %edx
cmpl $0x0, (%rax,%rdx,8)
js 0x1c3b40
shrq $0x20, %r11
xorq $0x1, %rbx
movq 0x28(%rdi), %rax
movq (%rax,%r11,8), %r9
movq (%rax,%rbx,8), %rdx
movl %edx, %eax
orl %r9d, %eax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x1c3b40
movq %r9, %r8
shrq $0x20, %r8
movq %rdx, %rax
shrq $0x20, %rax
imulq $0x18, %rax, %rax
addq %rcx, %rax
movl %r9d, %r9d
imulq $0x18, %r9, %r10
imulq $0x18, %r8, %r8
addq %r8, %rcx
addq $0xc, %rcx
xorl %r9d, %r9d
xorl %r12d, %r12d
cmpq %r9, %r10
je 0x1c3855
movl (%rcx,%r9), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %r12d
addq $0x18, %r9
jmp 0x1c383c
movq %rbx, 0x58(%rsp)
movl %edx, %ecx
xorl %ebx, %ebx
xorl %ebp, %ebp
subq $0x1, %rcx
jb 0x1c3881
cmpl $0x0, 0xc(%rax)
js 0x1c387b
movl 0x8(%rax), %edx
xorl %r11d, %edx
cmpl $0x1, %edx
cmoveq %rax, %rbp
incl %ebx
addq $0x18, %rax
jmp 0x1c3860
cmpl $0x2, %r12d
setb %al
cmpl $0x2, %ebx
setb %cl
testb %cl, %al
jne 0x1c3b40
cmpl $0x0, 0x100(%rsp)
movq %rsi, 0x50(%rsp)
movq %r11, 0x18(%rsp)
je 0x1c396c
xorl %eax, %eax
cmpl $0x2, %r12d
jb 0x1c38fa
movq 0x10(%rsp), %rcx
andq $0x0, (%rcx)
subq $0x8, %rsp
movq 0x28(%rsp), %rdi
movq %r14, %rdx
movq 0x50(%rsp), %r8
leaq 0x40(%rsp), %r9
leaq 0x38(%rsp), %rax
pushq %rax
callq 0x1c308a
movq 0x28(%rsp), %r11
movq 0x60(%rsp), %rsi
addq $0x10, %rsp
movq %r15, (%r14)
movq 0x10(%rsp), %rcx
andq $0x0, (%rcx)
testl %eax, %eax
sete %cl
cmpl $0x2, %ebx
setae %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x1c395e
movq (%r14), %rax
movq %rax, 0x28(%rsp)
movq 0x10(%rsp), %r15
andq $0x0, (%r15)
subq $0x8, %rsp
movq 0x28(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq %r15, %rcx
movq 0x50(%rsp), %r8
leaq 0x40(%rsp), %r9
leaq 0x38(%rsp), %rax
pushq %rax
callq 0x1c308a
movq 0x28(%rsp), %r11
movq 0x60(%rsp), %rsi
addq $0x10, %rsp
movq 0x28(%rsp), %rcx
movq %rcx, (%r14)
andq $0x0, (%r15)
pushq $0x1
popq %rcx
movl %ecx, %r10d
testl %eax, %eax
jne 0x1c3ae8
cmpl $0x2, %r12d
jb 0x1c3a28
movq (%r14), %rax
movq %rax, 0x28(%rsp)
movq 0x10(%rsp), %r15
andq $0x0, (%r15)
subq $0x8, %rsp
movq 0x28(%rsp), %rdi
movq %r14, %rdx
movq %r15, %rcx
movq 0x50(%rsp), %r8
leaq 0x40(%rsp), %r9
leaq 0x38(%rsp), %rax
pushq %rax
callq 0x1c2c0a
popq %rcx
popq %rdx
movq 0x28(%rsp), %rax
movq %rax, (%r14)
andq $0x0, (%r15)
movq 0x38(%rsp), %rax
cmpq $-0x1, %rax
je 0x1c3b3b
testq %rax, %rax
movq 0x8(%rsp), %rdi
js 0x1c39d9
cvtsi2ss %rax, %xmm0
jmp 0x1c39ee
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movq 0x18(%rsp), %r11
movq 0x30(%rsp), %rax
testq %rax, %rax
js 0x1c3a04
cvtsi2ss %rax, %xmm1
jmp 0x1c3a19
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm1
addss %xmm1, %xmm1
mulss 0x40(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x1c3b40
xorl %r10d, %r10d
cmpl $0x2, %ebx
jb 0x1c3ae8
movq (%r14), %rax
movq %rax, 0x28(%rsp)
movq 0x10(%rsp), %r15
andq $0x0, (%r15)
subq $0x8, %rsp
movq 0x28(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq %r15, %rcx
movq 0x50(%rsp), %r8
leaq 0x40(%rsp), %r9
leaq 0x38(%rsp), %rax
pushq %rax
callq 0x1c2c0a
popq %rcx
popq %rdx
movq 0x28(%rsp), %rax
movq %rax, (%r14)
andq $0x0, (%r15)
movq 0x38(%rsp), %rax
cmpq $-0x1, %rax
je 0x1c3b3b
testq %rax, %rax
movq 0x8(%rsp), %rdi
js 0x1c3a9a
cvtsi2ss %rax, %xmm0
jmp 0x1c3aaf
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movq 0x18(%rsp), %r11
xorl %r10d, %r10d
movq 0x30(%rsp), %rax
testq %rax, %rax
js 0x1c3ac8
cvtsi2ss %rax, %xmm1
jmp 0x1c3add
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm1
addss %xmm1, %xmm1
mulss 0x40(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x1c3b40
movq 0x20(%rsp), %rdi
movq 0x60(%rsp), %rsi
movl %r11d, %edx
movq 0x58(%rsp), %rcx
movl %r12d, %r8d
movl %ebx, %r9d
pushq 0x10(%rsp)
pushq %r14
pushq %r10
pushq 0x90(%rsp)
pushq 0x90(%rsp)
pushq 0x90(%rsp)
callq 0x1c3500
addq $0x30, %rsp
testl %eax, %eax
je 0x1c3b3b
orb $-0x80, 0xf(%rbp)
movq 0x50(%rsp), %rax
orb $-0x80, 0xf(%rax)
incl 0x44(%rsp)
movq 0x8(%rsp), %rdi
incl %r13d
jmp 0x1c3795
cmpq $0x0, 0x108(%rsp)
jne 0x1c3b65
movq 0xb0(%rsp), %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
cmpq $0x0, 0x110(%rsp)
movl 0x44(%rsp), %ebx
jne 0x1c3b86
movq 0x90(%rsp), %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
testl %ebx, %ebx
je 0x1c3b8f
callq 0x694eb
movl %ebx, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq