// File: /workspace/github_cpp_makefile_star2_O1/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 $0xd8, %rsp
movl %r9d, 0x44(%rsp)
movss %xmm0, 0x48(%rsp)
movl %ecx, 0x40(%rsp)
movl %edx, 0x3c(%rsp)
movq %rdi, 0x20(%rsp)
movq 0x128(%rsp), %rdx
movq 0x120(%rsp), %rdi
xorps %xmm0, %xmm0
leaq 0xa0(%rsp), %r8
movaps %xmm0, (%r8)
xorl %ecx, %ecx
movq %rcx, 0x10(%r8)
leaq 0xc0(%rsp), %r9
movq %rcx, 0x10(%r9)
movaps %xmm0, (%r9)
movq %rsi, 0x80(%rsp)
movq 0x18(%rsi), %rbp
movl 0x14(%rbp), %eax
addl %eax, %eax
testq %rdi, %rdi
cmoveq %r8, %rdi
testq %rdx, %rdx
cmovneq %rdx, %r9
movq %r9, 0x78(%rsp)
movq $0x0, 0x30(%rsp)
movq %rdi, 0x28(%rsp)
movq %rcx, (%rdi)
testl %eax, %eax
movq %rbp, 0x70(%rsp)
je 0x201783
movq 0x120(%rsp), %rbx
testq %rbx, %rbx
leaq 0xa0(%rsp), %rcx
cmoveq %rcx, %rbx
movq %rbx, 0x10(%rsp)
movl %eax, %eax
movq %rax, 0x18(%rsp)
xorl %r14d, %r14d
movq 0x20(%rbp), %rax
movl %r14d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x201775
movq 0x30(%rbp), %rax
cmpb $0x0, (%rax,%r14)
jne 0x201775
movq 0x28(%rbp), %rax
movq (%rax,%r14,8), %r8
cmpl $0x2, %r8d
jb 0x201775
movq 0x8(%rbp), %rcx
movq %r8, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rdx
leaq (%rcx,%rdx,8), %rax
movq %rax, 0x58(%rsp)
movl %r8d, %eax
leaq (%rcx,%rdx,8), %r13
addq $0xc, %r13
xorl %edx, %edx
movl $0x1, %ecx
movq %r13, %rsi
movl %edx, %edi
movl (%rsi), %edx
notl %edx
shrl $0x1f, %edx
addl %edi, %edx
cmpq %rax, %rcx
jae 0x20162c
addq $0x18, %rsi
incq %rcx
cmpl $0x2, %edx
jb 0x201610
cmpl $0x2, %edx
jb 0x201775
cmpl $0x0, 0x110(%rsp)
je 0x2016bb
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x50(%rsp)
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %ebx, %ebx
cmpl $0x0, (%r13,%rbp)
js 0x201698
movq %r8, %r15
movl -0x8(%r13,%rbp), %esi
movl -0x4(%r13,%rbp), %edx
movq 0x20(%rsp), %rdi
callq 0x201204
testl %eax, %eax
je 0x201683
cmpl $-0x1, %eax
je 0x201c19
incl %ebx
jmp 0x201686
incl %r12d
movq %r15, %r8
testl %ebx, %ebx
setne %al
testl %r12d, %r12d
setne %cl
testb %cl, %al
jne 0x2016a3
addq $0x18, %rbp
cmpq %rbp, 0x50(%rsp)
jne 0x201653
testl %ebx, %ebx
sete %al
testl %r12d, %r12d
sete %cl
orb %al, %cl
movq 0x70(%rsp), %rbp
jne 0x201775
cmpl $0x1, %r8d
adcl $0x0, %r8d
xorl %r15d, %r15d
cmpl $0x0, (%r13)
js 0x201765
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x28(%rsp), %rcx
cmpq %rax, (%rcx)
jne 0x201719
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq 0x10(%rsp), %rax
movq %rsi, 0x8(%rax)
movq 0x10(%rax), %rdi
shlq $0x3, %rsi
movq %r8, %r12
callq 0x2370
movq %r12, %r8
movq 0x10(%rsp), %rcx
movq %rax, 0x10(%rcx)
movl (%r13), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
shlq $0x20, %rax
movq 0x58(%rsp), %rcx
subq 0x8(%rbp), %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
addq %r15, %rcx
orq %rax, %rcx
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x28(%rsp), %rdi
movq (%rdi), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, (%rdi)
movq %rcx, (%rax,%rdx,8)
incq %r15
addq $0x18, %r13
cmpq %r15, %r8
jne 0x2016c6
incq %r14
cmpq 0x18(%rsp), %r14
jne 0x2015af
movq 0x120(%rsp), %rax
testq %rax, %rax
leaq 0xa0(%rsp), %rbx
cmovneq %rax, %rbx
movq 0x10(%rbx), %rdi
movq 0x28(%rsp), %r14
movq (%r14), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1e3b6a
cmpq $0x0, (%r14)
je 0x201ba4
xorl %r15d, %r15d
movq $0x0, 0x30(%rsp)
movq %rbx, 0x88(%rsp)
movq 0x8(%rbp), %r13
movq 0x10(%rbx), %rax
movl (%rax,%r15,8), %eax
leaq (%rax,%rax,2), %r14
cmpl $0x0, 0xc(%r13,%r14,8)
js 0x201b93
leaq (,%r14,8), %rcx
addq %r13, %rcx
movq (%rcx), %r8
movq 0x20(%rbp), %rax
movq %r8, %rdx
shrq $0x21, %rdx
cmpl $0x0, (%rax,%rdx,8)
js 0x201b93
movl 0x8(%rcx), %edx
movl %edx, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x201b93
shrq $0x20, %r8
movq %rdx, %rax
xorq $0x1, %rax
movq 0x28(%rbp), %rcx
movq (%rcx,%r8,8), %r12
movq (%rcx,%rax,8), %rax
cmpl $0x1, %r12d
ja 0x201843
cmpl $0x2, %eax
jb 0x201b93
movq %rax, 0x18(%rsp)
movq %rdx, 0x50(%rsp)
cmpl $0x0, 0x110(%rsp)
movq %r8, 0x10(%rsp)
je 0x201885
movq 0x20(%rsp), %rdi
movl %r8d, %esi
movq 0x50(%rsp), %rdx
callq 0x201204
movq 0x10(%rsp), %r8
testl %eax, %eax
je 0x201885
cmpl $-0x1, %eax
jne 0x201b93
jmp 0x201c57
movq %r14, 0x58(%rsp)
movq 0x18(%rsp), %rdx
movq %rdx, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (,%rax,8), %rax
addq %r13, %rax
movq %rax, 0x98(%rsp)
movl %edx, %ecx
testl %edx, %edx
je 0x2018d0
movl 0x8(%rax), %edx
xorl %r8d, %edx
cmpl $0x1, %edx
je 0x2018c8
addq $0x18, %rax
decl %ecx
jne 0x2018b3
jmp 0x2018d0
movq %rax, 0x90(%rsp)
movq 0x58(%rsp), %rax
movl 0xc(%r13,%rax,8), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movq 0x90(%rsp), %rdx
movl 0xc(%rdx), %edx
andl %ecx, %edx
cmpl %edx, %eax
cmovbl %eax, %edx
movl %edx, 0x4c(%rsp)
testl %r12d, %r12d
je 0x2019a2
movq %r15, 0x68(%rsp)
movq %r12, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
movq %r13, 0x60(%rsp)
leaq (%r13,%rax,8), %r13
movl %r12d, %r15d
xorl %r14d, %r14d
xorl %r12d, %r12d
xorl %edx, %edx
movl 0xc(%r13), %ebx
testl %ebx, %ebx
js 0x201975
incl %edx
cmpl $0x0, 0x110(%rsp)
je 0x201961
movl %edx, %ebp
movl 0x4(%r13), %esi
movl 0x8(%r13), %edx
movq 0x20(%rsp), %rdi
callq 0x201204
testl %eax, %eax
je 0x20196e
cmpl $-0x1, %eax
movq 0x10(%rsp), %r8
movl %ebp, %edx
je 0x201bfa
cmpl %ebx, %r12d
cmovbel %ebx, %r12d
cmovbq %r13, %r14
jmp 0x201975
movq 0x10(%rsp), %r8
movl %ebp, %edx
addq $0x18, %r13
decq %r15
jne 0x201925
testq %r14, %r14
setne %al
cvtsi2ss %r12d, %xmm0
movq 0x70(%rsp), %rbp
movq 0x88(%rsp), %rbx
movq 0x68(%rsp), %r15
movq 0x60(%rsp), %r13
jmp 0x2019a9
xorps %xmm0, %xmm0
xorl %edx, %edx
xorl %eax, %eax
testl %edx, %edx
je 0x201b93
testb %al, %al
je 0x201b93
cmpl $0x2, %edx
jb 0x2019d3
cvtsi2ssl 0x4c(%rsp), %xmm1
mulss 0x48(%rsp), %xmm0
ucomiss %xmm0, %xmm1
ja 0x201b93
movq 0x18(%rsp), %rax
testl %eax, %eax
je 0x201a94
movq %r13, 0x60(%rsp)
movq %r15, 0x68(%rsp)
movl %eax, %r15d
xorl %r12d, %r12d
xorl %edi, %edi
xorl %esi, %esi
movq 0x98(%rsp), %rax
movl 0xc(%rax), %ebx
testl %ebx, %ebx
js 0x201a65
incl %esi
cmpl $0x0, 0x110(%rsp)
je 0x201a49
movl %edi, 0x18(%rsp)
movl %esi, %ebp
movl %edx, %r13d
movl 0x4(%rax), %esi
movq %rax, %r14
movl 0x8(%rax), %edx
movq 0x20(%rsp), %rdi
callq 0x201204
testl %eax, %eax
je 0x201a54
cmpl $-0x1, %eax
movq 0x10(%rsp), %r8
movl %r13d, %edx
movl %ebp, %esi
movq %r14, %rax
movl 0x18(%rsp), %edi
je 0x201c38
cmpl %ebx, %edi
cmovbel %ebx, %edi
cmovbq %rax, %r12
jmp 0x201a65
movq 0x10(%rsp), %r8
movl %r13d, %edx
movl %ebp, %esi
movq %r14, %rax
movl 0x18(%rsp), %edi
addq $0x18, %rax
decq %r15
jne 0x2019fc
testq %r12, %r12
setne %al
xorps %xmm0, %xmm0
cvtsi2ss %edi, %xmm0
movq 0x70(%rsp), %rbp
movq 0x88(%rsp), %rbx
movq 0x68(%rsp), %r15
movq 0x60(%rsp), %r13
jmp 0x201a9b
xorps %xmm0, %xmm0
xorl %esi, %esi
xorl %eax, %eax
testl %esi, %esi
movq 0x58(%rsp), %r14
je 0x201b93
testb %al, %al
je 0x201b93
cmpl $0x2, %esi
jb 0x201acd
xorps %xmm1, %xmm1
cvtsi2ssl 0x4c(%rsp), %xmm1
mulss 0x48(%rsp), %xmm0
ucomiss %xmm0, %xmm1
ja 0x201b93
movl %edx, %eax
orl %esi, %eax
cmpl $0x2, %eax
jb 0x201b93
cmpl $0x0, 0x44(%rsp)
je 0x201b6c
cmpl $0x2, 0x44(%rsp)
jne 0x201afc
cmpl $0x2, %esi
setb %cl
cmpl $0x2, %edx
setb %al
orb %cl, %al
jmp 0x201b68
cmpl $0x2, %esi
setae %al
cmpl $0x2, %edx
setae %cl
testb %al, %cl
jne 0x201b6c
cmpl $0x1, %esi
jne 0x201b2e
movq 0x78(%rsp), %rax
movq %rax, (%rsp)
movq 0x20(%rsp), %rdi
movq 0x80(%rsp), %rsi
movq 0x50(%rsp), %rdx
jmp 0x201b50
cmpl $0x1, %edx
jne 0x201b93
xorl $0x1, %r8d
movq 0x78(%rsp), %rax
movq %rax, (%rsp)
movq 0x20(%rsp), %rdi
movq 0x80(%rsp), %rsi
movl %r8d, %edx
movl 0x3c(%rsp), %ecx
movl 0x40(%rsp), %r8d
movq 0x28(%rsp), %r9
callq 0x20135b
testl %eax, %eax
sete %al
testb %al, %al
jne 0x201b93
leaq 0xc(,%r14,8), %rax
addq %r13, %rax
movq 0x90(%rsp), %rcx
orb $-0x80, 0xf(%rcx)
orb $-0x80, 0x3(%rax)
movq 0x30(%rsp), %rax
incl %eax
movq %rax, 0x30(%rsp)
incl %r15d
movq 0x28(%rsp), %rax
cmpq %r15, (%rax)
ja 0x2017cd
cmpq $0x0, 0x120(%rsp)
jne 0x201bbc
movq 0xb0(%rsp), %rdi
callq 0x2220
cmpq $0x0, 0x128(%rsp)
jne 0x201bd4
movq 0xd0(%rsp), %rdi
callq 0x2220
cmpl $0x0, 0x30(%rsp)
je 0x201be3
movq %rbp, %rdi
callq 0xa02fa
movq 0x30(%rsp), %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x39f69(%rip), %rdi     # 0x23bb6a
leaq 0x3771d(%rip), %rsi     # 0x239325
leaq 0x39f6e(%rip), %rcx     # 0x23bb7d
movl $0x22af, %edx           # imm = 0x22AF
callq 0x2110
leaq 0x39f4a(%rip), %rdi     # 0x23bb6a
leaq 0x376fe(%rip), %rsi     # 0x239325
leaq 0x39f4f(%rip), %rcx     # 0x23bb7d
movl $0x2285, %edx           # imm = 0x2285
callq 0x2110
leaq 0x39f2b(%rip), %rdi     # 0x23bb6a
leaq 0x376df(%rip), %rsi     # 0x239325
leaq 0x39f30(%rip), %rcx     # 0x23bb7d
movl $0x22bd, %edx           # imm = 0x22BD
callq 0x2110
leaq 0x39f0c(%rip), %rdi     # 0x23bb6a
leaq 0x376c0(%rip), %rsi     # 0x239325
leaq 0x39f11(%rip), %rcx     # 0x23bb7d
movl $0x229e, %edx           # imm = 0x229E
callq 0x2110