// File: 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
movq %rsp, %rbp
subq $0xd0, %rsp
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movl %r9d, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x28(%rbp)
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2100
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2100
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movl $0x0, -0x8c(%rbp)
movq -0x28(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x90(%rbp)
cmpq $0x0, 0x10(%rbp)
je 0x344357
movq 0x10(%rbp), %rax
movq %rax, -0xb8(%rbp)
jmp 0x344364
leaq -0x40(%rbp), %rax
movq %rax, -0xb8(%rbp)
jmp 0x344364
movq -0xb8(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, 0x18(%rbp)
je 0x344383
movq 0x18(%rbp), %rax
movq %rax, -0xc0(%rbp)
jmp 0x344390
leaq -0x58(%rbp), %rax
movq %rax, -0xc0(%rbp)
jmp 0x344390
movq -0xc0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq $0x0, (%rax)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x90(%rbp), %eax
jae 0x3447e4
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x6c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x3443db
jmp 0x3447d6
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x6c(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x6c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x84(%rbp)
cmpl $0x2, -0x84(%rbp)
jae 0x344427
jmp 0x3447d6
movl $0x0, -0x7c(%rbp)
movl $0x0, -0x9c(%rbp)
movl $0x0, -0x98(%rbp)
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x84(%rbp), %eax
jae 0x3444e9
movq -0xa8(%rbp), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x344476
jmp 0x3444db
movq -0x8(%rbp), %rdi
movq -0xa8(%rbp), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, %esi
movq -0xa8(%rbp), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
callq 0x344170
cmpl $0x0, %eax
je 0x3444c3
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0x3444d2
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x344449
cmpl $0x0, -0x98(%rbp)
je 0x344501
cmpl $0x0, -0x9c(%rbp)
je 0x344501
cmpl $0x2, -0x7c(%rbp)
jae 0x344506
jmp 0x3447d6
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x84(%rbp), %eax
jae 0x3447d4
movq -0xa8(%rbp), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x34453d
jmp 0x3447c6
movq -0x8(%rbp), %rdi
movq -0xa8(%rbp), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, %esi
movq -0xa8(%rbp), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
callq 0x344170
cmpl $0x0, %eax
je 0x34457e
jmp 0x3447c6
movq -0xa8(%rbp), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
xorl $0x1, %eax
movl %eax, -0x70(%rbp)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x70(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x3445b7
jmp 0x3447c6
movq -0x28(%rbp), %rdi
movl -0x70(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x311ba0
movl %eax, -0x80(%rbp)
cmpl $0x1, -0x7c(%rbp)
ja 0x3445dd
cmpl $0x1, -0x80(%rbp)
ja 0x3445dd
jmp 0x3447c6
movl $0x0, -0x94(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x3445fc
movl $0x1, -0x94(%rbp)
jmp 0x3446b7
cmpl $0x2, -0x20(%rbp)
jne 0x34461d
cmpl $0x1, -0x7c(%rbp)
jbe 0x344618
cmpl $0x1, -0x80(%rbp)
jbe 0x344618
movl $0x1, -0x94(%rbp)
jmp 0x3446b5
cmpl $0x1, -0x7c(%rbp)
jbe 0x344635
cmpl $0x1, -0x80(%rbp)
jbe 0x344635
movl $0x1, -0x94(%rbp)
jmp 0x3446b3
cmpl $0x1, -0x80(%rbp)
jne 0x344672
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x70(%rbp), %edx
xorl $0x1, %edx
movl -0x14(%rbp), %ecx
movl -0x18(%rbp), %r8d
movq -0x60(%rbp), %r9
movq -0x68(%rbp), %rax
movq %rax, (%rsp)
callq 0x343240
cmpl $0x0, %eax
je 0x344670
movl $0x1, -0x94(%rbp)
jmp 0x3446b1
cmpl $0x1, -0x7c(%rbp)
jne 0x3446af
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x6c(%rbp), %edx
xorl $0x1, %edx
movl -0x14(%rbp), %ecx
movl -0x18(%rbp), %r8d
movq -0x60(%rbp), %r9
movq -0x68(%rbp), %rax
movq %rax, (%rsp)
callq 0x343240
cmpl $0x0, %eax
je 0x3446ad
movl $0x1, -0x94(%rbp)
jmp 0x3446af
jmp 0x3446b1
jmp 0x3446b3
jmp 0x3446b5
jmp 0x3446b7
cmpl $0x0, -0x94(%rbp)
je 0x3447c4
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
movq -0xa8(%rbp), %rax
movl -0x74(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x70(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x70(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x88(%rbp)
movl $0x0, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x88(%rbp), %eax
jae 0x344796
movq -0xb0(%rbp), %rax
movl -0x78(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl -0x6c(%rbp), %ecx
xorl $0x1, %ecx
cmpl %ecx, %eax
jne 0x344789
movq -0xb0(%rbp), %rax
movl -0x78(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
jmp 0x344796
jmp 0x34478b
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x34473b
movl -0x78(%rbp), %eax
cmpl -0x88(%rbp), %eax
jae 0x3447a3
jmp 0x3447c2
leaq 0x54438(%rip), %rdi     # 0x398be2
leaq 0x51a08(%rip), %rsi     # 0x3961b9
movl $0x2320, %edx           # imm = 0x2320
leaq 0x5442c(%rip), %rcx     # 0x398be9
callq 0x2130
jmp 0x3447c4
jmp 0x3447c6
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x34450d
jmp 0x3447d6
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x3443ad
cmpq $0x0, 0x10(%rbp)
jne 0x3447f4
movq -0x30(%rbp), %rdi
callq 0x2210
cmpq $0x0, 0x18(%rbp)
jne 0x344804
movq -0x48(%rbp), %rdi
callq 0x2210
cmpl $0x0, -0x8c(%rbp)
jbe 0x344816
movq -0x28(%rbp), %rdi
callq 0xcdd10
movl -0x8c(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)