// File: chhylp123[P]hifiasm/gfa_ut.cpp
// Function: usg_bub_clean0(usg_t*, unsigned int*, unsigned int, unsigned int, unsigned int, unsigned int, int, float, unsigned char*, asg64_v*, asg64_v*)
uint64_t usg_bub_clean0(usg_t *g, uint32_t *a, uint32_t a_n, uint32_t sid, uint32_t eid, 
uint32_t is_topo, int32_t max_ext, float len_rat, uint8_t *f, asg64_v *in_0, asg64_v *in_1)
{
    uint32_t i, k, v, w, nv, nw, kv, kw, ol_max, ou_max, to_del, cnt = 0, tip_v, mm_ol; 
    asg64_v tx = {0,0,0}, tz = {0,0,0}, *b = NULL, *ub = NULL; int32_t n_tip, r_tip;
    uint64_t kocc[2]; usg_arc_t *av, *aw, *ve, *we;
    b = ((in_0)?(in_0):(&tx)); ub = ((in_1)?(in_1):(&tz));

    for (k = b->n = ub->n = 0; k < a_n; ++k) {
        extracr_clean_arc(g, a[k], b, ub); 
        extracr_clean_arc(g, a[k]^1, b, ub);
    }
    extracr_clean_arc(g, sid, b, ub); 
    extracr_clean_arc(g, eid, b, ub);
    

    radix_sort_srt64(b->a, b->a + b->n);
    for (k = 0; k < b->n; k++) {
        v = ub->a[(uint32_t)b->a[k]]>>32; 
        ve = &(usg_arc_a(g, v)[(uint32_t)(ub->a[(uint32_t)b->a[k]])]);
        w = ve->v^1;
        if(ve->del || g->a[v>>1].del || g->a[w>>1].del || ve->ou) continue;
        nv = usg_arc_n(g, v); nw = usg_arc_n(g, w);
        av = usg_arc_a(g, v); aw = usg_arc_a(g, w);
        if(nv<=1 && nw <= 1) continue;    

        // if(is_trio) {
        //     if(get_arcs(g, v, NULL, 0)<=1 && get_arcs(g, w, NULL, 0)<=1) continue;///speedup
        //     trioF = get_tip_trio_infor(g, v^1);
        //     ntrioF = (trioF==FATHER? MOTHER : (trioF==MOTHER? FATHER : (uint32_t)-1));
        // }    
        for (i = 0; i < nw; ++i) {
            if (aw[i].v == (v^1)) {
                we = &(aw[i]);
                break;
            }
        }
        mm_ol = MIN(ve->ol, we->ol); kocc[0] = kocc[1] = 0;

        for (i = kv = ol_max = ou_max = 0; i < nv; ++i) {
            if(av[i].del) continue;
            kv++; 
            if(av[i].ou != 1) kocc[!!(av[i].ou)]++; ///if av[i].ou == 1, ignore it
            // if(is_trio && get_tip_trio_infor(g, av[i].v) == ntrioF) continue;
            if(ol_max < av[i].ol) ol_max = av[i].ol;
        }
        if (kv < 1 || kocc[0] < 1 || kocc[1] < 1) continue;
        if (kv >= 2) {
            if (mm_ol > ol_max*len_rat) continue;
        }

        
        for (i = kw = ol_max = ou_max = 0; i < nw; ++i) {
            if(aw[i].del) continue;
            kw++;
            // if(is_trio && get_tip_trio_infor(g, aw[i].v) == ntrioF) continue;
            if(ol_max < aw[i].ol) ol_max = aw[i].ol;
        }
        if (kw < 1) continue;
        if (kw >= 2) {
            if (mm_ol > ol_max*len_rat) continue;
        }

        if (kv <= 1 && kw <= 1) continue;

        to_del = 1; n_tip = 0; tip_v = (uint32_t)-1;
        if(is_topo) {
            to_del = 0;
            if (kv > 1 && kw > 1) {
                to_del = 1;
            } else if (kw == 1) {
                n_tip = usg_naive_topocut_aux(g, w^1, max_ext, f, b, ub);
                if (n_tip < max_ext) to_del = 1; tip_v = w^1;
            } else if (kv == 1) {
                n_tip = usg_naive_topocut_aux(g, v^1, max_ext, f, b, ub);
                if (n_tip < max_ext) to_del = 1; tip_v = v^1;
            }
        } 

        if (to_del) {
            // ve->del = we->del = 1; 
            if(n_tip > 0 && tip_v != ((uint32_t)-1)) {
                r_tip = usg_tip_del(g, tip_v, b, ub);
                assert(n_tip == r_tip);
            }
            cnt++;
        }
    }

    if(in_0) free(tx.a); if(in_1) free(tz.a);
    return cnt;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movl %r9d, -0x20(%rbp)
movss %xmm0, -0x24(%rbp)
movl $0x0, -0x54(%rbp)
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2100
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2100
movq $0x0, -0x98(%rbp)
movq $0x0, -0xa0(%rbp)
cmpq $0x0, 0x20(%rbp)
je 0x36dd39
movq 0x20(%rbp), %rax
movq %rax, -0xe8(%rbp)
jmp 0x36dd46
leaq -0x78(%rbp), %rax
movq %rax, -0xe8(%rbp)
jmp 0x36dd46
movq -0xe8(%rbp), %rax
movq %rax, -0x98(%rbp)
cmpq $0x0, 0x28(%rbp)
je 0x36dd68
movq 0x28(%rbp), %rax
movq %rax, -0xf0(%rbp)
jmp 0x36dd78
leaq -0x90(%rbp), %rax
movq %rax, -0xf0(%rbp)
jmp 0x36dd78
movq -0xf0(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movq $0x0, (%rax)
movq -0x98(%rbp), %rax
movq $0x0, (%rax)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x36de01
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx,4), %esi
movq -0x98(%rbp), %rdx
movq -0xa0(%rbp), %rcx
callq 0x36d200
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx,4), %esi
xorl $0x1, %esi
movq -0x98(%rbp), %rdx
movq -0xa0(%rbp), %rcx
callq 0x36d200
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x36dda9
movq -0x8(%rbp), %rdi
movl -0x18(%rbp), %esi
movq -0x98(%rbp), %rdx
movq -0xa0(%rbp), %rcx
callq 0x36d200
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %esi
movq -0x98(%rbp), %rdx
movq -0xa0(%rbp), %rcx
callq 0x36d200
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x98(%rbp), %rax
movq (%rax), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x30ed90
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x98(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x36e46a
movq -0xa0(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x98(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x2c(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x30(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq -0xa0(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x98(%rbp), %rdx
movq 0x10(%rdx), %rdx
movl -0x2c(%rbp), %esi
movq (%rdx,%rsi,8), %rdx
movl %edx, %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movl 0x8(%rax), %eax
xorl $0x1, %eax
movl %eax, -0x34(%rbp)
movq -0xd8(%rbp), %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x36df69
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
cmpb $0x0, 0x70(%rax)
jne 0x36df69
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl -0x34(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
cmpb $0x0, 0x70(%rax)
jne 0x36df69
movq -0xd8(%rbp), %rax
cmpl $0x0, 0x10(%rax)
je 0x36df6e
jmp 0x36e45c
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x30(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl -0x34(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x34(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x3c(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl -0x30(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x30(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0xc8(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl -0x34(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x34(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0xd0(%rbp)
cmpl $0x1, -0x38(%rbp)
ja 0x36e041
cmpl $0x1, -0x3c(%rbp)
ja 0x36e041
jmp 0x36e45c
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jae 0x36e095
movq -0xd0(%rbp), %rax
movl -0x28(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl -0x30(%rbp), %ecx
xorl $0x1, %ecx
cmpl %ecx, %eax
jne 0x36e088
movq -0xd0(%rbp), %rax
movl -0x28(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0xe0(%rbp)
jmp 0x36e095
jmp 0x36e08a
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x36e048
movq -0xd8(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq -0xe0(%rbp), %rcx
movl 0xc(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jg 0x36e0cf
movq -0xd8(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0xf4(%rbp)
jmp 0x36e0e4
movq -0xe0(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0xf4(%rbp)
movl -0xf4(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq $0x0, -0xb8(%rbp)
movq $0x0, -0xc0(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x40(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x38(%rbp), %eax
jae 0x36e1ee
movq -0xc8(%rbp), %rax
movl -0x28(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x36e14c
jmp 0x36e1e0
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
movq -0xc8(%rbp), %rax
movl -0x28(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x1, 0x10(%rax)
je 0x36e1a1
movq -0xc8(%rbp), %rax
movl -0x28(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x10(%rax)
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movq -0xc0(%rbp,%rax,8), %rcx
addq $0x1, %rcx
movq %rcx, -0xc0(%rbp,%rax,8)
movl -0x48(%rbp), %eax
movq -0xc8(%rbp), %rcx
movl -0x28(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x36e1de
movq -0xc8(%rbp), %rax
movl -0x28(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x48(%rbp)
jmp 0x36e1e0
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x36e11f
cmpl $0x1, -0x40(%rbp)
jb 0x36e208
cmpq $0x1, -0xc0(%rbp)
jb 0x36e208
cmpq $0x1, -0xb8(%rbp)
jae 0x36e20d
jmp 0x36e45c
cmpl $0x2, -0x40(%rbp)
jb 0x36e234
movl -0x5c(%rbp), %eax
cvtsi2ss %rax, %xmm0
movl -0x48(%rbp), %eax
cvtsi2ss %rax, %xmm1
mulss -0x24(%rbp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x36e232
jmp 0x36e45c
jmp 0x36e234
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jae 0x36e2c9
movq -0xd0(%rbp), %rax
movl -0x28(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x36e276
jmp 0x36e2be
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
movl -0x48(%rbp), %eax
movq -0xd0(%rbp), %rcx
movl -0x28(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x36e2bc
movq -0xd0(%rbp), %rax
movl -0x28(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x48(%rbp)
jmp 0x36e2be
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x36e250
cmpl $0x1, -0x44(%rbp)
jae 0x36e2d4
jmp 0x36e45c
cmpl $0x2, -0x44(%rbp)
jb 0x36e2fb
movl -0x5c(%rbp), %eax
cvtsi2ss %rax, %xmm0
movl -0x48(%rbp), %eax
cvtsi2ss %rax, %xmm1
mulss -0x24(%rbp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x36e2f9
jmp 0x36e45c
jmp 0x36e2fb
cmpl $0x1, -0x40(%rbp)
ja 0x36e30c
cmpl $0x1, -0x44(%rbp)
ja 0x36e30c
jmp 0x36e45c
movl $0x1, -0x50(%rbp)
movl $0x0, -0xa4(%rbp)
movl $0xffffffff, -0x58(%rbp) # imm = 0xFFFFFFFF
cmpl $0x0, -0x20(%rbp)
je 0x36e3eb
movl $0x0, -0x50(%rbp)
cmpl $0x1, -0x40(%rbp)
jbe 0x36e34d
cmpl $0x1, -0x44(%rbp)
jbe 0x36e34d
movl $0x1, -0x50(%rbp)
jmp 0x36e3e9
cmpl $0x1, -0x44(%rbp)
jne 0x36e39a
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
xorl $0x1, %esi
movl 0x10(%rbp), %edx
movq 0x18(%rbp), %rcx
movq -0x98(%rbp), %r8
movq -0xa0(%rbp), %r9
callq 0x34f5a0
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
cmpl 0x10(%rbp), %eax
jge 0x36e38f
movl $0x1, -0x50(%rbp)
movl -0x34(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x36e3e7
cmpl $0x1, -0x40(%rbp)
jne 0x36e3e5
movq -0x8(%rbp), %rdi
movl -0x30(%rbp), %esi
xorl $0x1, %esi
movl 0x10(%rbp), %edx
movq 0x18(%rbp), %rcx
movq -0x98(%rbp), %r8
movq -0xa0(%rbp), %r9
callq 0x34f5a0
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
cmpl 0x10(%rbp), %eax
jge 0x36e3dc
movl $0x1, -0x50(%rbp)
movl -0x30(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x36e3e7
jmp 0x36e3e9
jmp 0x36e3eb
cmpl $0x0, -0x50(%rbp)
je 0x36e45a
cmpl $0x0, -0xa4(%rbp)
jle 0x36e451
cmpl $-0x1, -0x58(%rbp)
je 0x36e451
movq -0x8(%rbp), %rdi
movl -0x58(%rbp), %esi
movq -0x98(%rbp), %rdx
movq -0xa0(%rbp), %rcx
callq 0x36d520
movl %eax, -0xa8(%rbp)
movl -0xa4(%rbp), %eax
cmpl -0xa8(%rbp), %eax
jne 0x36e430
jmp 0x36e44f
leaq 0x2c79f(%rip), %rdi     # 0x39abd6
leaq 0x27d7b(%rip), %rsi     # 0x3961b9
movl $0x3e23, %edx           # imm = 0x3E23
leaq 0x2c79b(%rip), %rcx     # 0x39abe5
callq 0x2130
jmp 0x36e451
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x36e45c
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x36de68
cmpq $0x0, 0x20(%rbp)
je 0x36e47a
movq -0x68(%rbp), %rdi
callq 0x2210
cmpq $0x0, 0x28(%rbp)
je 0x36e48a
movq -0x80(%rbp), %rdi
callq 0x2210
movl -0x54(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)