// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: ulg_arc_cut_occ(ul_resolve_t*, ma_ug_t*, int, unsigned int, unsigned int, unsigned int, asg64_v*, asg64_v*)
uint32_t ulg_arc_cut_occ(ul_resolve_t *uidx, ma_ug_t *ug, int32_t max_ext, uint32_t max_ext_hifi, 
uint32_t is_trio, uint32_t topo_level, asg64_v *in, asg64_v *ib)
{
    asg_t *g = ug->g; asg64_v tx = {0,0,0}, tb = {0,0,0}, *b = NULL, *ub = NULL;
    uint32_t v, w, i, z, kv, kw, nv, nw, cnt = 0, n_vtx = g->n_seq<<1, to_del, n_ul, n_ug;
    asg_arc_t *av, *aw; b = (in?(in):(&tx)); ub = (ib?(ib):(&tb));

    for (v = b->n = 0; v < n_vtx; ++v) {
        if (g->seq[v>>1].del) continue;
        av = asg_arc_a(g, v); nv = asg_arc_n(g, v);
        if (nv < 2) continue;
        
        for (i = n_ul = n_ug = kv = 0; i < nv; ++i) {
            if(av[i].del) continue;
            if(is_ul_edge(uidx, av[i].ul>>32, av[i].v)) n_ul++;
            else n_ug++;
            kv++;
        }
        if(n_ul == 0 || n_ug == 0 || kv < 2) continue;

        for (i = 0; i < nv; ++i) {
            if(av[i].del) continue;
            if(is_ul_edge(uidx, av[i].ul>>32, av[i].v)) continue;
            w = av[i].v^1; if(g->seq[w>>1].del) continue;
            kw = get_arcs(g, w, NULL, 0);
            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) {
                ++cnt; av[i].del = 1;
                aw = asg_arc_a(g, w); nw = asg_arc_n(g, w);
                for (z = 0; z < nw; ++z) {
                    if (aw[z].v == (v^1)) {
                        aw[z].del = 1;
                        break;
                    }
                }
                assert(z < nw);
            }
        }
    }

    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 $0x98, %rsp
movl %ecx, 0x30(%rsp)
movl %edx, 0x2c(%rsp)
movq %rdi, 0x20(%rsp)
movq 0xd8(%rsp), %rdx
movq 0xd0(%rsp), %rdi
movq %rsi, 0x48(%rsp)
movq 0x18(%rsi), %rbp
xorl %ecx, %ecx
leaq 0x80(%rsp), %rsi
movq %rcx, 0x10(%rsi)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsi)
leaq 0x60(%rsp), %r8
movq %rcx, 0x10(%r8)
movaps %xmm0, (%r8)
movl 0x14(%rbp), %eax
addl %eax, %eax
testq %rdi, %rdi
cmovneq %rdi, %rsi
testq %rdx, %rdx
cmovneq %rdx, %r8
movq %r8, 0x38(%rsp)
movq $0x0, 0x18(%rsp)
movq %rsi, 0x40(%rsp)
movq %rcx, (%rsi)
testl %eax, %eax
je 0x20d60a
movl %eax, %eax
movq %rax, 0x50(%rsp)
xorl %r12d, %r12d
movq $0x0, 0x18(%rsp)
movl %r9d, 0x34(%rsp)
movq %rbp, 0x10(%rsp)
movq 0x20(%rbp), %rax
movl %r12d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x20d5fc
movq 0x28(%rbp), %rax
movq (%rax,%r12,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x20d5fc
movq 0x8(%rbp), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %rsi
movq %rsi, 0x8(%rsp)
movl %eax, %r15d
leaq (%rcx,%rdx,8), %r14
addq $0xc, %r14
xorl %ebx, %ebx
movq %r15, 0x58(%rsp)
xorl %r13d, %r13d
xorl %ebp, %ebp
cmpl $0x0, (%r14)
js 0x20d457
movl -0x8(%r14), %esi
movl -0x4(%r14), %edx
movq 0x20(%rsp), %rdi
callq 0x20d2c2
addl %eax, %r13d
xorl $0x1, %eax
addl %eax, %ebx
incl %ebp
addq $0x18, %r14
decq %r15
jne 0x20d435
testl %r13d, %r13d
sete %al
testl %ebx, %ebx
sete %cl
orb %al, %cl
cmpl $0x2, %ebp
setb %al
orb %cl, %al
je 0x20d481
movq 0x10(%rsp), %rbp
jmp 0x20d5fc
xorl %ebx, %ebx
movq 0x10(%rsp), %rbp
movq 0x8(%rsp), %rcx
leaq (%rbx,%rbx,2), %rax
movl 0xc(%rcx,%rax,8), %r15d
testl %r15d, %r15d
js 0x20d5ee
leaq (%rcx,%rax,8), %r14
movl 0x4(%r14), %esi
movl 0x8(%r14), %r13d
movq 0x20(%rsp), %rdi
movl %r13d, %edx
callq 0x20d2c2
testl %eax, %eax
jne 0x20d5e9
movq 0x20(%rbp), %rax
movl %r13d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x20d5e9
movq %rbp, %rsi
movl %r13d, %ebp
xorl $0x1, %ebp
movq 0x28(%rsi), %rax
movq (%rax,%rbp,8), %rcx
movq %rcx, %rdx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %rdx
je 0x20d516
shrq $0x20, %rcx
movq 0x8(%rsi), %rsi
xorl %eax, %eax
movl %ecx, %edi
leaq (%rdi,%rdi,2), %rdi
movl 0xc(%rsi,%rdi,8), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %eax
incq %rcx
decq %rdx
jne 0x20d4fb
jmp 0x20d518
xorl %eax, %eax
movl 0x34(%rsp), %ecx
testl %ecx, %ecx
je 0x20d56b
cmpl $0x2, %ecx
jne 0x20d52f
cmpl $0x2, %eax
jae 0x20d56b
jmp 0x20d5e4
cmpl $0x1, %eax
ja 0x20d56b
jne 0x20d5e4
movq 0x38(%rsp), %rax
movq %rax, (%rsp)
movq 0x20(%rsp), %rdi
movq 0x48(%rsp), %rsi
movl %r13d, %edx
movl 0x2c(%rsp), %ecx
movl 0x30(%rsp), %r8d
movq 0x40(%rsp), %r9
callq 0x20ca09
testl %eax, %eax
je 0x20d5e4
movl 0xc(%r14), %r15d
orl $0x80000000, %r15d      # imm = 0x80000000
movl %r15d, 0xc(%r14)
movq 0x10(%rsp), %rdx
movq 0x28(%rdx), %rax
movq (%rax,%rbp,8), %rcx
testl %ecx, %ecx
je 0x20d65c
movq %rdx, %rbp
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
movq 0x8(%rdx), %rax
movq %rcx, %rdx
shrq $0x20, %rdx
movl %ecx, %esi
leaq (%rdx,%rdx,2), %rcx
leaq (%rax,%rcx,8), %rdx
addq $0xc, %rdx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rdi
xorl %esi, %esi
movl -0x4(%rdx,%rsi), %r8d
xorl %r12d, %r8d
cmpl $0x1, %r8d
je 0x20d5d9
addq $0x18, %rsi
cmpq %rsi, %rdi
jne 0x20d5bd
jmp 0x20d65c
leaq (%rax,%rcx,8), %rax
orb $-0x80, 0xf(%rsi,%rax)
jmp 0x20d5e9
movq 0x10(%rsp), %rbp
movq 0x8(%rsp), %rcx
incq %rbx
cmpq 0x58(%rsp), %rbx
jne 0x20d48d
incq %r12
cmpq 0x50(%rsp), %r12
jne 0x20d3e0
cmpq $0x0, 0xd0(%rsp)
jne 0x20d622
movq 0x90(%rsp), %rdi
callq 0x2220
cmpq $0x0, 0xd8(%rsp)
movq 0x18(%rsp), %rbx
jne 0x20d63c
movq 0x70(%rsp), %rdi
callq 0x2220
testl %ebx, %ebx
je 0x20d648
movq %rbp, %rdi
callq 0xa0a7d
movl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3a88f(%rip), %rdi     # 0x247ef2
leaq 0x37f9f(%rip), %rsi     # 0x245609
leaq 0x3a888(%rip), %rcx     # 0x247ef9
movl $0x2320, %edx           # imm = 0x2320
callq 0x2110