// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: ulg_arc_cut_length(ul_resolve_t*, ma_ug_t*, int, unsigned int, float, unsigned int, unsigned int, unsigned int, unsigned int*, asg64_v*, asg64_v*)
uint32_t ulg_arc_cut_length(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 hom_check, 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, kw, nv, nw, cnt = 0, n_vtx = g->n_seq<<1, n_het, n_hom, ff, ol_max, mm_ol, to_del;
    asg_arc_t *av, *aw, *ve, *we, *vl_max, *wl_max;

    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;
            if(hom_check) {
                for (i = n_het = n_hom = 0; i < nv; ++i) {
                    if(av[i].del) continue;
                    ff = is_het_ulg_edge(uidx, av[i].ul>>32, av[i].v); assert(ff != (uint32_t)-1);
                    if(ff) n_het++;
                    else n_hom++;
                    if(n_het > 0 && n_hom > 0) break;
                }
                if(n_het == 0 || n_hom == 0) continue;
            }

            for (i = 0; i < nv; ++i) {
                if(av[i].del) continue;
                kv_push(uint64_t, *b, (((uint64_t)av[i].ol)<<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); nw = asg_arc_n(g, w);
        av = asg_arc_a(g, v); aw = asg_arc_a(g, w);
        if(nv<=1 && nw <= 1) continue;
        if(hom_check) {
            ff = is_het_ulg_edge(uidx, g->arc[(uint32_t)b->a[k]].ul>>32, g->arc[(uint32_t)b->a[k]].v); 
            assert(ff != (uint32_t)-1);
            if(ff) continue;
        }

        ve = &(g->arc[(uint32_t)b->a[k]]);
        for (i = 0; i < nw; ++i) {
            if (aw[i].v == (v^1)) {
                we = &(aw[i]);
                break;
            }
        }
        mm_ol = MIN(ve->ol, we->ol); ///ve and we are hom edges

        for (i = kv = ol_max = 0, vl_max = NULL; i < nv; ++i) {
            if(av[i].del) continue;
            kv++;
            if(hom_check) { 
                ff = is_het_ulg_edge(uidx, av[i].ul>>32, av[i].v); assert(ff != (uint32_t)-1);
                if(!ff) continue;///vl_max must be a het edge
            }
            if(ol_max < av[i].ol) ol_max = av[i].ol, vl_max = &(av[i]);
        }
        if (kv < 1 || (!vl_max)) continue;
        if (kv >= 2) {
            if (mm_ol > ol_max*len_rat) continue;
        }

        for (i = kw = ol_max = 0, wl_max = NULL; i < nw; ++i) {
            if(aw[i].del) continue;
            kw++;
            if(hom_check) { 
                ff = is_het_ulg_edge(uidx, aw[i].ul>>32, aw[i].v); assert(ff != (uint32_t)-1);
                if(!ff) continue;///wl_max must be a het edge
            }
            if(ol_max < aw[i].ol) ol_max = aw[i].ol, wl_max = &(aw[i]);
        }
        if (kw < 1 || (!wl_max)) continue;
        if (kw >= 2) {
            if (mm_ol > ol_max*len_rat) continue;
        }

        if (kv <= 1 && kw <= 1) continue;

        to_del = 0;
        if(topo_level == 0) {
            to_del = 1;
        } else if(topo_level == 2) {
            if (kv > 1 && kw > 1) to_del = 1;
        } else {
            if (kv > 1 && kw > 1) {
                to_del = 1;
            } else if (kw == 1) {
                if (usg_topocut_aux(uidx, ug, w^1, max_ext, max_ext_hifi, b, ub)) to_del = 1;                    
            } else if (kv == 1) {
                if (usg_topocut_aux(uidx, ug, v^1, max_ext, max_ext_hifi, b, ub)) to_del = 1;   
            }
        }

        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
movl %r9d, 0x54(%rsp)
movss %xmm0, 0x58(%rsp)
movl %ecx, 0x50(%rsp)
movl %edx, 0x4c(%rsp)
movq %rdi, 0x30(%rsp)
movq 0x110(%rsp), %rdx
xorps %xmm0, %xmm0
leaq 0x90(%rsp), %r15
movaps %xmm0, (%r15)
xorl %ecx, %ecx
movq %rcx, 0x10(%r15)
leaq 0xb0(%rsp), %rdi
movq %rcx, 0x10(%rdi)
movaps %xmm0, (%rdi)
movq %rsi, 0x70(%rsp)
movq 0x18(%rsi), %r13
movl 0x14(%r13), %eax
addl %eax, %eax
testq %rdx, %rdx
movq %rdx, %r12
cmoveq %r15, %r12
movq 0x118(%rsp), %rsi
testq %rsi, %rsi
cmovneq %rsi, %rdi
movq %rdi, 0x68(%rsp)
movq $0x0, 0x60(%rsp)
movq %rcx, (%r12)
movl $0x0, %ecx
testl %eax, %eax
movq %r13, 0x10(%rsp)
movq %r12, 0x20(%rsp)
je 0x20ce30
testq %rdx, %rdx
movq %rdx, %rdi
cmoveq %r15, %rdi
movl %eax, %eax
movq %rax, 0x18(%rsp)
xorl %ebp, %ebp
movq %rdi, 0x8(%rsp)
movq 0x20(%r13), %rax
movl %ebp, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x20cc78
movq 0x30(%r13), %rax
cmpb $0x0, (%rax,%rbp)
je 0x20cc87
incq %rbp
cmpq 0x18(%rsp), %rbp
jne 0x20cc60
jmp 0x20ce1c
movq 0x28(%r13), %rax
movq (%rax,%rbp,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x20cc78
movq 0x8(%r13), %r9
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%r9,%rcx,8), %rdx
movq %rdx, 0x28(%rsp)
movl %eax, %ebx
leaq (%r9,%rcx,8), %r15
addq $0xc, %r15
xorl %ecx, %ecx
movl $0x1, %eax
movq %r15, %rdx
movl %ecx, %esi
movl (%rdx), %ecx
notl %ecx
shrl $0x1f, %ecx
addl %esi, %ecx
cmpq %rbx, %rax
jae 0x20ccde
addq $0x18, %rdx
incq %rax
cmpl $0x2, %ecx
jb 0x20ccc2
cmpl $0x2, %ecx
jb 0x20cc78
cmpl $0x0, 0x100(%rsp)
je 0x20cd85
xorl %r14d, %r14d
movq %rbx, %r13
movq %r15, %r12
xorl %esi, %esi
movq %r9, 0x38(%rsp)
cmpl $0x0, (%r12)
js 0x20cd5f
movl %esi, 0x40(%rsp)
movl -0x8(%r12), %esi
movl -0x4(%r12), %edx
movq 0x30(%rsp), %rdi
callq 0x20c8d4
testl %eax, %eax
je 0x20cd3f
cmpl $-0x1, %eax
je 0x20d265
movl 0x40(%rsp), %esi
incl %esi
movq 0x8(%rsp), %rdi
movq 0x38(%rsp), %r9
jmp 0x20cd50
incl %r14d
movq 0x8(%rsp), %rdi
movq 0x38(%rsp), %r9
movl 0x40(%rsp), %esi
testl %esi, %esi
setne %al
testl %r14d, %r14d
setne %cl
testb %cl, %al
jne 0x20cd68
addq $0x18, %r12
decq %r13
jne 0x20cd01
testl %esi, %esi
sete %al
testl %r14d, %r14d
sete %cl
orb %al, %cl
movq 0x10(%rsp), %r13
movq 0x20(%rsp), %r12
jne 0x20cc78
xorl %r14d, %r14d
movl (%r15), %ecx
testl %ecx, %ecx
js 0x20ce07
movq (%r12), %rdx
movq 0x10(%rdi), %rax
cmpq 0x8(%rdi), %rdx
jne 0x20cdd1
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdi)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movl (%r15), %ecx
movq 0x8(%r13), %r9
movq (%r12), %rdx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
shlq $0x20, %rcx
movq 0x28(%rsp), %rsi
subq %r9, %rsi
sarq $0x3, %rsi
movabsq $-0x5555555555555555, %r8 # imm = 0xAAAAAAAAAAAAAAAB
imulq %r8, %rsi
addq %r14, %rsi
orq %rcx, %rsi
leaq 0x1(%rdx), %rcx
movq %rcx, (%r12)
movq %rsi, (%rax,%rdx,8)
incq %r14
addq $0x18, %r15
cmpq %r14, %rbx
jne 0x20cd88
jmp 0x20cc78
movq (%r12), %rcx
movq 0x110(%rsp), %rdx
leaq 0x90(%rsp), %r15
testq %rdx, %rdx
cmovneq %rdx, %r15
movq 0x10(%r15), %rdi
leaq (%rdi,%rcx,8), %rsi
callq 0x1ee586
cmpq $0x0, (%r12)
je 0x20d1f1
xorl %esi, %esi
movq $0x0, 0x60(%rsp)
movq %r15, 0x18(%rsp)
movq 0x8(%r13), %r12
movq 0x10(%r15), %rax
movl (%rax,%rsi,8), %eax
movq %r13, %rdx
leaq (%rax,%rax,2), %r13
movl 0xc(%r12,%r13,8), %r14d
testl %r14d, %r14d
js 0x20cecd
leaq (%r12,%r13,8), %rcx
movq (%rcx), %rbp
movq 0x20(%rdx), %rax
movq %rbp, %rdx
shrq $0x21, %rdx
cmpl $0x0, (%rax,%rdx,8)
js 0x20cec6
movl 0x8(%rcx), %edx
movl %edx, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x20cec6
shrq $0x20, %rbp
movq %rdx, %rax
xorq $0x1, %rax
movq 0x10(%rsp), %rcx
movq 0x28(%rcx), %rcx
movq (%rcx,%rbp,8), %rdi
movq (%rcx,%rax,8), %rax
cmpl $0x1, %edi
ja 0x20cee2
cmpl $0x2, %eax
jae 0x20cee2
movq 0x10(%rsp), %r13
jmp 0x20ced0
movq %rdx, %r13
movq 0x20(%rsp), %r12
incl %esi
cmpq %rsi, (%r12)
ja 0x20ce5f
jmp 0x20d1f1
movq %rax, 0x28(%rsp)
movq %rdx, 0x40(%rsp)
cmpl $0x0, 0x100(%rsp)
je 0x20cf2f
movq %rdi, 0x8(%rsp)
movq 0x30(%rsp), %rdi
movq %rsi, %rbx
movl %ebp, %esi
movq 0x40(%rsp), %rdx
callq 0x20c8d4
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
testl %eax, %eax
je 0x20cf2f
cmpl $-0x1, %eax
movq 0x10(%rsp), %r13
movq 0x20(%rsp), %r12
jne 0x20ced5
jmp 0x20d2a3
movq 0x28(%rsp), %rcx
movq %rcx, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%r12,%rax,8), %rdx
testl %ecx, %ecx
movq %rdx, 0x88(%rsp)
je 0x20cf6e
movl %ecx, %eax
movq %rdx, %rcx
movl 0x8(%rcx), %edx
xorl %ebp, %edx
cmpl $0x1, %edx
je 0x20cf69
addq $0x18, %rcx
decq %rax
jne 0x20cf54
jmp 0x20cf6e
movq %rcx, 0x38(%rsp)
movq 0x38(%rsp), %rax
movl 0xc(%rax), %ecx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %ecx
cmpl %ecx, %r14d
cmovbl %r14d, %ecx
movl %ecx, 0x5c(%rsp)
testl %edi, %edi
je 0x20d10b
movq %rsi, 0x8(%rsp)
movq %rbp, 0x78(%rsp)
movq %rdi, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%r12,%rax,8), %rbp
leaq (%r12,%r13,8), %rax
addq $0xc, %rax
movq %rax, 0x80(%rsp)
movl %edi, %r12d
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %ebx, %ebx
movl 0xc(%rbp), %r13d
testl %r13d, %r13d
js 0x20d001
incl %ebx
cmpl $0x0, 0x100(%rsp)
je 0x20cff6
movl 0x4(%rbp), %esi
movl 0x8(%rbp), %edx
movq 0x30(%rsp), %rdi
callq 0x20c8d4
testl %eax, %eax
je 0x20d001
cmpl $-0x1, %eax
je 0x20d246
cmpl %r13d, %r14d
cmovbel %r13d, %r14d
cmovbq %rbp, %r15
addq $0x18, %rbp
decq %r12
jne 0x20cfc4
testl %ebx, %ebx
movq 0x10(%rsp), %r13
movq 0x20(%rsp), %r12
je 0x20d101
testq %r15, %r15
je 0x20d101
cmpl $0x2, %ebx
jb 0x20d044
cvtsi2ss %r14d, %xmm0
cvtsi2ssl 0x5c(%rsp), %xmm1
mulss 0x58(%rsp), %xmm0
ucomiss %xmm0, %xmm1
ja 0x20d101
movq 0x28(%rsp), %rax
testl %eax, %eax
je 0x20d101
movl %eax, %r12d
xorl %r13d, %r13d
xorl %edx, %edx
xorl %r14d, %r14d
movq 0x88(%rsp), %rax
movl 0xc(%rax), %ebp
testl %ebp, %ebp
js 0x20d0b5
incl %r14d
cmpl $0x0, 0x100(%rsp)
je 0x20d0a3
movl %edx, 0x28(%rsp)
movl 0x4(%rax), %esi
movq %rax, %r15
movl 0x8(%rax), %edx
movq 0x30(%rsp), %rdi
callq 0x20c8d4
testl %eax, %eax
je 0x20d0ae
cmpl $-0x1, %eax
movq %r15, %rax
movl 0x28(%rsp), %edx
je 0x20d284
cmpl %ebp, %edx
cmovbel %ebp, %edx
cmovbq %rax, %r13
jmp 0x20d0b5
movq %r15, %rax
movl 0x28(%rsp), %edx
addq $0x18, %rax
decq %r12
jne 0x20d064
testl %r14d, %r14d
je 0x20d11a
testq %r13, %r13
movq 0x18(%rsp), %r15
movq 0x20(%rsp), %r12
je 0x20d1e2
cmpl $0x2, %r14d
jb 0x20d12e
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
xorps %xmm1, %xmm1
cvtsi2ssl 0x5c(%rsp), %xmm1
mulss 0x58(%rsp), %xmm0
ucomiss %xmm0, %xmm1
movq 0x78(%rsp), %rdx
jbe 0x20d13c
jmp 0x20d1e2
movq 0x18(%rsp), %r15
jmp 0x20d1e7
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %r15
jmp 0x20ced0
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %r15
movq 0x20(%rsp), %r12
jmp 0x20d1e7
cmpl $0x2, %ebx
movq 0x78(%rsp), %rdx
jb 0x20d1e2
cmpl $0x0, 0x54(%rsp)
je 0x20d1c1
cmpl $0x2, 0x54(%rsp)
jne 0x20d160
cmpl $0x2, %r14d
setae %al
cmpl $0x2, %ebx
setae %cl
testb %al, %cl
jne 0x20d1c1
jmp 0x20d1e2
cmpl $0x2, %r14d
setae %al
cmpl $0x2, %ebx
setae %cl
testb %al, %cl
jne 0x20d1c1
cmpl $0x1, %r14d
jne 0x20d191
movq 0x68(%rsp), %rax
movq %rax, (%rsp)
movq 0x30(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq 0x40(%rsp), %rdx
jmp 0x20d1ac
cmpl $0x1, %ebx
jne 0x20d1e2
xorl $0x1, %edx
movq 0x68(%rsp), %rax
movq %rax, (%rsp)
movq 0x30(%rsp), %rdi
movq 0x70(%rsp), %rsi
movl 0x4c(%rsp), %ecx
movl 0x50(%rsp), %r8d
movq %r12, %r9
callq 0x20ca09
testl %eax, %eax
je 0x20d1e2
movq 0x38(%rsp), %rax
orb $-0x80, 0xf(%rax)
movq 0x80(%rsp), %rax
orb $-0x80, 0x3(%rax)
movq 0x60(%rsp), %rax
incl %eax
movq %rax, 0x60(%rsp)
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rsi
jmp 0x20ced5
cmpq $0x0, 0x110(%rsp)
jne 0x20d209
movq 0xa0(%rsp), %rdi
callq 0x2220
cmpq $0x0, 0x118(%rsp)
movq 0x60(%rsp), %rbx
jne 0x20d226
movq 0xc0(%rsp), %rdi
callq 0x2220
testl %ebx, %ebx
je 0x20d232
movq %r13, %rdi
callq 0xa0a7d
movl %ebx, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3ac01(%rip), %rdi     # 0x247e4e
leaq 0x383b5(%rip), %rsi     # 0x245609
leaq 0x3ac06(%rip), %rcx     # 0x247e61
movl $0x22af, %edx           # imm = 0x22AF
callq 0x2110
leaq 0x3abe2(%rip), %rdi     # 0x247e4e
leaq 0x38396(%rip), %rsi     # 0x245609
leaq 0x3abe7(%rip), %rcx     # 0x247e61
movl $0x2285, %edx           # imm = 0x2285
callq 0x2110
leaq 0x3abc3(%rip), %rdi     # 0x247e4e
leaq 0x38377(%rip), %rsi     # 0x245609
leaq 0x3abc8(%rip), %rcx     # 0x247e61
movl $0x22bd, %edx           # imm = 0x22BD
callq 0x2110
leaq 0x3aba4(%rip), %rdi     # 0x247e4e
leaq 0x38358(%rip), %rsi     # 0x245609
leaq 0x3aba9(%rip), %rcx     # 0x247e61
movl $0x229e, %edx           # imm = 0x229E
callq 0x2110