// File: /workspace/github_cpp_makefile_star2_O2/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, 0x44(%rsp)
movss %xmm0, 0x48(%rsp)
movl %ecx, 0x40(%rsp)
movl %edx, 0x3c(%rsp)
movq 0x110(%rsp), %rax
xorps %xmm0, %xmm0
leaq 0xb0(%rsp), %rbp
andq $0x0, 0x10(%rbp)
leaq 0x90(%rsp), %rcx
andq $0x0, 0x10(%rcx)
movaps %xmm0, (%rbp)
movaps %xmm0, (%rcx)
movq %rsi, 0x68(%rsp)
movq 0x18(%rsi), %r13
movl 0x14(%r13), %edx
addl %edx, %edx
movq %rdx, 0x18(%rsp)
testq %rax, %rax
cmovneq %rax, %rbp
movq 0x118(%rsp), %rax
testq %rax, %rax
cmovneq %rax, %rcx
movq %rcx, 0x60(%rsp)
movq %rdi, 0x28(%rsp)
andq $0x0, (%rbp)
xorl %r12d, %r12d
movq %r13, 0x10(%rsp)
movq %rbp, 0x8(%rsp)
cmpq 0x18(%rsp), %r12
je 0x1c0482
movq 0x20(%r13), %rax
movl %r12d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x1c02ff
movq 0x30(%r13), %rax
cmpb $0x0, (%rax,%r12)
je 0x1c0304
incq %r12
jmp 0x1c02da
movq 0x28(%r13), %rax
movq (%rax,%r12,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x1c02ff
movq 0x8(%r13), %r9
movq %rax, %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
leaq (%r9,%rcx), %rdx
movq %rdx, 0x30(%rsp)
movl %eax, %r14d
leaq (%r9,%rcx), %r15
addq $0xc, %r15
xorl %eax, %eax
movq %r15, %rcx
xorl %edx, %edx
cmpq %r14, %rax
jae 0x1c0359
cmpl $0x1, %edx
ja 0x1c0359
movl (%rcx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %edx
incq %rax
addq $0x18, %rcx
jmp 0x1c033d
cmpl $0x2, %edx
jb 0x1c02ff
cmpl $0x0, 0x100(%rsp)
je 0x1c0400
imulq $0x18, %r14, %rax
movq %rax, 0x20(%rsp)
xorl %ebp, %ebp
xorl %esi, %esi
xorl %r13d, %r13d
movq %r12, 0x50(%rsp)
cmpq %rbp, 0x20(%rsp)
je 0x1c03e3
cmpl $0x0, (%r15,%rbp)
js 0x1c03dd
movl %esi, %r12d
movq %r9, %rbx
movl -0x8(%r15,%rbp), %esi
movl -0x4(%r15,%rbp), %edx
movq 0x28(%rsp), %rdi
callq 0x1bff6c
testl %eax, %eax
je 0x1c03c0
cmpl $-0x1, %eax
je 0x1c089f
movl %r12d, %esi
incl %esi
movq %rbx, %r9
jmp 0x1c03c9
incl %r13d
movq %rbx, %r9
movl %r12d, %esi
testl %esi, %esi
setne %al
testl %r13d, %r13d
setne %cl
testb %cl, %al
movq 0x50(%rsp), %r12
jne 0x1c03e3
addq $0x18, %rbp
jmp 0x1c0381
testl %esi, %esi
sete %al
testl %r13d, %r13d
sete %cl
orb %al, %cl
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbp
jne 0x1c02ff
xorl %ebx, %ebx
cmpq %rbx, %r14
je 0x1c02ff
movl (%r15), %ecx
testl %ecx, %ecx
js 0x1c0479
movq (%rbp), %r8
movq 0x10(%rbp), %rdi
cmpq 0x8(%rbp), %r8
jne 0x1c044d
leaq (%r8,%r8), %rsi
testq %r8, %r8
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbp)
shlq $0x3, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0x10(%rbp)
movl (%r15), %ecx
movq 0x8(%r13), %r9
movq (%rbp), %r8
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
shlq $0x20, %rcx
movq 0x30(%rsp), %rax
subq %r9, %rax
cqto
pushq $0x18
popq %rsi
idivq %rsi
addq %rbx, %rax
orq %rcx, %rax
leaq 0x1(%r8), %rcx
movq %rcx, (%rbp)
movq %rax, (%rdi,%r8,8)
incq %rbx
addq $0x18, %r15
jmp 0x1c0402
movq 0x10(%rbp), %rdi
movq (%rbp), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1a3def
xorl %r12d, %r12d
movl $0x0, 0x4c(%rsp)
movl %r12d, %r12d
cmpq %r12, (%rbp)
jbe 0x1c082c
movq %rbp, %rax
movq 0x8(%r13), %rbp
movq 0x10(%rax), %rax
movl (%rax,%r12,8), %eax
imulq $0x18, %rax, %r14
movl 0xc(%rbp,%r14), %r15d
testl %r15d, %r15d
js 0x1c0514
leaq (%r14,%rbp), %rcx
movq (%rcx), %rdx
movq 0x20(%r13), %rax
movq %rdx, %rsi
shrq $0x21, %rdx
cmpl $0x0, (%rax,%rdx,8)
js 0x1c0514
movl 0x8(%rcx), %edx
movl %edx, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x1c0514
shrq $0x20, %rsi
movq %rdx, %rax
xorq $0x1, %rax
movq 0x28(%r13), %rcx
movq %rsi, 0x30(%rsp)
movq (%rcx,%rsi,8), %rsi
movq (%rcx,%rax,8), %rbx
movl %ebx, %eax
orl %esi, %eax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
jne 0x1c051e
movq 0x8(%rsp), %rbp
incl %r12d
jmp 0x1c049e
movq %rdx, 0x78(%rsp)
cmpl $0x0, 0x100(%rsp)
je 0x1c055e
movq 0x28(%rsp), %rdi
movq %rsi, 0x18(%rsp)
movq 0x30(%rsp), %rsi
movq 0x78(%rsp), %rdx
callq 0x1bff6c
movq 0x18(%rsp), %rsi
testl %eax, %eax
je 0x1c055e
cmpl $-0x1, %eax
movq 0x8(%rsp), %rbp
jne 0x1c0519
jmp 0x1c08dd
movq %rsi, %rax
shrq $0x20, %rax
imulq $0x18, %rax, %r13
addq %rbp, %r13
movq %rbx, %rax
shrq $0x20, %rax
imulq $0x18, %rax, %rax
leaq (%rax,%rbp), %rcx
movq %rcx, 0x20(%rsp)
movl %ebx, %ecx
movq %rcx, 0x88(%rsp)
incq %rcx
addq %rbp, %rax
addq $-0x18, %rax
decq %rcx
je 0x1c05ab
movl 0x20(%rax), %edx
xorl 0x30(%rsp), %edx
addq $0x18, %rax
cmpl $0x1, %edx
jne 0x1c0594
jmp 0x1c05b0
movq 0x70(%rsp), %rax
movq %r12, 0x58(%rsp)
leaq (%r14,%rbp), %rcx
addq $0xc, %rcx
movq %rcx, 0x80(%rsp)
movq %rax, 0x70(%rsp)
movl 0xc(%rax), %ecx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %ecx
cmpl %ecx, %r15d
cmovbl %r15d, %ecx
movl %ecx, 0x18(%rsp)
movl %esi, %r14d
xorl %edx, %edx
xorl %ebp, %ebp
xorl %ebx, %ebx
subq $0x1, %r14
jb 0x1c063e
movl 0xc(%r13), %r15d
testl %r15d, %r15d
js 0x1c0638
incl %edx
cmpl $0x0, 0x100(%rsp)
je 0x1c0628
movq %rdx, %r12
movl 0x4(%r13), %esi
movl 0x8(%r13), %edx
movq 0x28(%rsp), %rdi
callq 0x1bff6c
testl %eax, %eax
je 0x1c0635
cmpl $-0x1, %eax
movq %r12, %rdx
je 0x1c0880
cmpl %r15d, %ebp
cmovbel %r15d, %ebp
cmovbq %r13, %rbx
jmp 0x1c0638
movq %r12, %rdx
addq $0x18, %r13
jmp 0x1c05e8
testl %edx, %edx
je 0x1c0759
testq %rbx, %rbx
movq 0x10(%rsp), %r13
je 0x1c075e
cvtsi2ssl 0x18(%rsp), %xmm1
cmpl $0x2, %edx
movq 0x58(%rsp), %r12
jb 0x1c0677
cvtsi2ss %ebp, %xmm0
mulss 0x48(%rsp), %xmm0
ucomiss %xmm0, %xmm1
ja 0x1c0514
xorl %esi, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
movq 0x20(%rsp), %rax
movq 0x88(%rsp), %rdi
movss %xmm1, 0x50(%rsp)
subq $0x1, %rdi
jb 0x1c0732
movl 0xc(%rax), %r15d
testl %r15d, %r15d
js 0x1c0729
incl %esi
cmpl $0x0, 0x100(%rsp)
je 0x1c0701
movq %r8, 0x20(%rsp)
movq %rdi, %rbx
movl %ecx, 0x18(%rsp)
movq %rsi, %rbp
movq %rdx, %r14
movl 0x4(%rax), %esi
movq %rax, %r13
movl 0x8(%rax), %edx
movq 0x28(%rsp), %rdi
callq 0x1bff6c
testl %eax, %eax
je 0x1c070e
cmpl $-0x1, %eax
movq %r14, %rdx
movq %r13, %rax
movq %rbp, %rsi
movss 0x50(%rsp), %xmm1
movl 0x18(%rsp), %ecx
movq %rbx, %rdi
movq 0x20(%rsp), %r8
je 0x1c08be
cmpl %r15d, %ecx
cmovbel %r15d, %ecx
cmovbq %rax, %r8
jmp 0x1c0729
movq %r14, %rdx
movq %r13, %rax
movq %rbp, %rsi
movss 0x50(%rsp), %xmm1
movl 0x18(%rsp), %ecx
movq %rbx, %rdi
movq 0x20(%rsp), %r8
addq $0x18, %rax
jmp 0x1c0691
testl %esi, %esi
je 0x1c076d
testq %r8, %r8
je 0x1c076d
cmpl $0x2, %esi
jb 0x1c0777
cvtsi2ss %ecx, %xmm0
mulss 0x48(%rsp), %xmm0
ucomiss %xmm0, %xmm1
movq 0x8(%rsp), %rbp
jbe 0x1c0785
jmp 0x1c0822
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rbp
movq 0x58(%rsp), %r12
jmp 0x1c0519
movq 0x10(%rsp), %r13
jmp 0x1c0514
cmpl $0x2, %edx
movq 0x8(%rsp), %rbp
jb 0x1c0822
cmpl $0x0, 0x44(%rsp)
je 0x1c0809
cmpl $0x2, 0x44(%rsp)
jne 0x1c07a5
cmpl $0x2, %esi
setae %al
cmpl $0x2, %edx
setae %cl
testb %al, %cl
jne 0x1c0809
jmp 0x1c0822
cmpl $0x2, %esi
setae %al
cmpl $0x2, %edx
setae %cl
testb %al, %cl
jne 0x1c0809
cmpl $0x1, %esi
jne 0x1c07d4
movq 0x60(%rsp), %rax
movq %rax, (%rsp)
movq 0x28(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x78(%rsp), %rdx
jmp 0x1c07f4
cmpl $0x1, %edx
jne 0x1c0822
movq 0x30(%rsp), %rdx
xorl $0x1, %edx
movq 0x60(%rsp), %rax
movq %rax, (%rsp)
movq 0x28(%rsp), %rdi
movq 0x68(%rsp), %rsi
movl 0x3c(%rsp), %ecx
movl 0x40(%rsp), %r8d
movq %rbp, %r9
callq 0x1c00c6
testl %eax, %eax
je 0x1c0822
movq 0x70(%rsp), %rax
orb $-0x80, 0xf(%rax)
movq 0x80(%rsp), %rax
orb $-0x80, 0x3(%rax)
incl 0x4c(%rsp)
movq 0x10(%rsp), %r13
jmp 0x1c0519
cmpq $0x0, 0x110(%rsp)
jne 0x1c0844
movq 0xc0(%rsp), %rdi
callq 0x2220
cmpq $0x0, 0x118(%rsp)
movl 0x4c(%rsp), %ebx
jne 0x1c0860
movq 0xa0(%rsp), %rdi
callq 0x2220
testl %ebx, %ebx
je 0x1c086c
movq %r13, %rdi
callq 0x694eb
movl %ebx, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x39586(%rip), %rdi     # 0x1f9e0d
leaq 0x36c4b(%rip), %rsi     # 0x1f74d9
leaq 0x3958b(%rip), %rcx     # 0x1f9e20
movl $0x22af, %edx           # imm = 0x22AF
callq 0x2110
leaq 0x39567(%rip), %rdi     # 0x1f9e0d
leaq 0x36c2c(%rip), %rsi     # 0x1f74d9
leaq 0x3956c(%rip), %rcx     # 0x1f9e20
movl $0x2285, %edx           # imm = 0x2285
callq 0x2110
leaq 0x39548(%rip), %rdi     # 0x1f9e0d
leaq 0x36c0d(%rip), %rsi     # 0x1f74d9
leaq 0x3954d(%rip), %rcx     # 0x1f9e20
movl $0x22bd, %edx           # imm = 0x22BD
callq 0x2110
leaq 0x39529(%rip), %rdi     # 0x1f9e0d
leaq 0x36bee(%rip), %rsi     # 0x1f74d9
leaq 0x3952e(%rip), %rcx     # 0x1f9e20
movl $0x229e, %edx           # imm = 0x229E
callq 0x2110