// File: /workspace/github_cpp_makefile_star2_O1/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 %r9d, 0xc(%rsp)
movl %ecx, 0x34(%rsp)
movl %edx, 0x30(%rsp)
movq %rdi, 0x28(%rsp)
movq 0xd8(%rsp), %rdx
movq 0xd0(%rsp), %rdi
movq %rsi, 0x50(%rsp)
movq 0x18(%rsi), %r14
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(%r14), %eax
addl %eax, %eax
testq %rdi, %rdi
cmovneq %rdi, %rsi
testq %rdx, %rdx
cmovneq %rdx, %r8
movq %r8, 0x40(%rsp)
movq $0x0, 0x10(%rsp)
movq %rsi, 0x48(%rsp)
movq %rcx, (%rsi)
testl %eax, %eax
je 0x201fa4
movl %eax, %eax
movq %rax, 0x58(%rsp)
xorl %r12d, %r12d
movq $0x0, 0x10(%rsp)
movq %r14, 0x38(%rsp)
movq 0x20(%r14), %rax
movl %r12d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x201f96
movq 0x28(%r14), %rax
movq (%rax,%r12,8), %rsi
testl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
je 0x201f96
movq 0x8(%r14), %rax
movq %rsi, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rdx
movq %rdx, 0x18(%rsp)
movq %rsi, 0x20(%rsp)
movl %esi, %r15d
leaq (%rax,%rcx,8), %rbx
addq $0xc, %rbx
xorl %ebp, %ebp
xorl %r14d, %r14d
xorl %r13d, %r13d
cmpl $0x0, (%rbx)
js 0x201e0c
movl -0x8(%rbx), %esi
movl -0x4(%rbx), %edx
movq 0x28(%rsp), %rdi
callq 0x201c76
addl %eax, %r14d
xorl $0x1, %eax
addl %eax, %ebp
incl %r13d
addq $0x18, %rbx
decq %r15
jne 0x201dec
testl %r14d, %r14d
sete %al
testl %ebp, %ebp
sete %cl
orb %al, %cl
cmpl $0x2, %r13d
setb %al
orb %cl, %al
je 0x201e37
movq 0x38(%rsp), %r14
jmp 0x201f96
movl 0x20(%rsp), %eax
movq %rax, 0x20(%rsp)
xorl %ebx, %ebx
movq 0x38(%rsp), %r14
movq 0x18(%rsp), %r15
leaq (%rbx,%rbx,2), %rax
cmpl $0x0, 0xc(%r15,%rax,8)
js 0x201f88
leaq (%r15,%rax,8), %rbp
movl 0x4(%rbp), %esi
movl 0x8(%rbp), %r13d
movq 0x28(%rsp), %rdi
movl %r13d, %edx
callq 0x201c76
testl %eax, %eax
jne 0x201f88
movq 0x20(%r14), %rax
movl %r13d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x201f88
movl %r13d, %r15d
xorl $0x1, %r15d
movq 0x28(%r14), %rax
movq (%rax,%r15,8), %rcx
movq %rcx, %rdx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %rdx
je 0x201ed0
shrq $0x20, %rcx
movq 0x8(%r14), %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 0x201eb5
jmp 0x201ed2
xorl %eax, %eax
cmpl $0x0, 0xc(%rsp)
je 0x201ef4
cmpl $0x2, 0xc(%rsp)
jne 0x201eef
cmpl $0x2, %eax
setb %al
testb %al, %al
je 0x201ef4
jmp 0x201f83
cmpl $0x1, %eax
jbe 0x201f4e
orb $-0x80, 0xf(%rbp)
movq 0x28(%r14), %rax
movq (%rax,%r15,8), %rax
testl %eax, %eax
movq 0x18(%rsp), %r15
je 0x201ff6
movq 0x10(%rsp), %rcx
incl %ecx
movq %rcx, 0x10(%rsp)
movq 0x8(%r14), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %rcx
addq $0xc, %rcx
movl -0x4(%rcx), %edx
xorl %r12d, %edx
cmpl $0x1, %edx
je 0x201f48
addq $0x18, %rcx
decl %eax
jne 0x201f30
jmp 0x201ff6
orb $-0x80, 0x3(%rcx)
jmp 0x201f88
jne 0x201f83
movq 0x40(%rsp), %rax
movq %rax, (%rsp)
movq 0x28(%rsp), %rdi
movq 0x50(%rsp), %rsi
movl %r13d, %edx
movl 0x30(%rsp), %ecx
movl 0x34(%rsp), %r8d
movq 0x48(%rsp), %r9
callq 0x20135b
testl %eax, %eax
sete %al
jmp 0x201ee6
movq 0x18(%rsp), %r15
incq %rbx
cmpq 0x20(%rsp), %rbx
jne 0x201e4c
incq %r12
cmpq 0x58(%rsp), %r12
jne 0x201d95
cmpq $0x0, 0xd0(%rsp)
jne 0x201fbc
movq 0x90(%rsp), %rdi
callq 0x2220
cmpq $0x0, 0xd8(%rsp)
movq 0x10(%rsp), %rbx
jne 0x201fd6
movq 0x70(%rsp), %rdi
callq 0x2220
testl %ebx, %ebx
je 0x201fe2
movq %r14, %rdi
callq 0xa02fa
movl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x39c11(%rip), %rdi     # 0x23bc0e
leaq 0x37321(%rip), %rsi     # 0x239325
leaq 0x39c0a(%rip), %rcx     # 0x23bc15
movl $0x2320, %edx           # imm = 0x2320
callq 0x2110