// File: chhylp123[P]hifiasm/gfa_ut.cpp
// Function: ulg_arc_cut_tips(ul_resolve_t*, ma_ug_t*, unsigned int, unsigned int, unsigned int, asg64_v*, asg64_v*)
uint32_t ulg_arc_cut_tips(ul_resolve_t *uidx, ma_ug_t *ug, uint32_t max_ext, uint32_t max_ext_hifi, uint32_t is_double_check, 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 n_vtx = g->n_seq<<1, v, w, i, k, cnt = 0, nv, kv, pb, w_occ, a_occ, ul_occ, del_occ, is_telo;
    asg_arc_t *av = NULL; uint64_t lw;
    b = (in?(in):(&tx)); ub = (ib?(ib):(&tb));
    for (v = 0, b->n = 0; v < n_vtx; ++v) {
        if (g->seq[v>>1].del) continue;

        av = asg_arc_a(g, v^1); nv = asg_arc_n(g, v^1);
        for (i = kv = 0; i < nv; i++) {
            if (av[i].del) continue;
            kv++; break;
        }

        if(kv) continue;
        // kv = ug->u.a[v>>1].n;/// get_ul_occ(uidx, v>>1);
        get_iug_u_raw_occ(uidx, v>>1, &ul_occ, NULL); kv = ul_occ;
        is_telo = 0; if(uidx->uovl.telo && uidx->uovl.telo[v>>1]) is_telo = 1;
        for (i = 0, w = v; i < max_ext; i++) {
            if(asg_end(g, w^1, &lw, NULL)!=0) break;
            w = (uint32_t)lw; 
            // kv += ug->u.a[w>>1].n; ///get_ul_occ(uidx, w>>1);
            get_iug_u_raw_occ(uidx, w>>1, &ul_occ, NULL); kv += ul_occ;
            if(uidx->uovl.telo && uidx->uovl.telo[w>>1]) is_telo = 1;
        }

        if((kv <= max_ext) && (!is_telo)) kv_push(uint64_t, *b, (((uint64_t)kv)<<32)|v);  
    }

    radix_sort_srt64(b->a, b->a + b->n);

    for (k = 0; k < b->n; k++) {
        v = (uint32_t)(b->a[k]);
        if (g->seq[v>>1].del) continue;

        av = asg_arc_a(g, v^1); nv = asg_arc_n(g, v^1);
        for (i = kv = 0; i < nv; i++) {
            if (av[i].del) continue;
            kv++; break;
        }

        if(kv) continue;

        pb = b->n; kv_push(uint64_t, *b, v); 
        // kv = ug->u.a[v>>1].n;/// get_ul_occ(uidx, v>>1);
        get_iug_u_raw_occ(uidx, v>>1, &ul_occ, NULL); kv = ul_occ;
        is_telo = 0; if(uidx->uovl.telo && uidx->uovl.telo[v>>1]) is_telo = 1;
        for (i = 0, w = v; i < max_ext; i++) {
            if(asg_end(g, w^1, &lw, NULL)!=0) break;
            w = (uint32_t)lw; kv_push(uint64_t, *b, w); 
            // kv += ug->u.a[w>>1].n; ///get_ul_occ(uidx, w>>1);
            get_iug_u_raw_occ(uidx, w>>1, &ul_occ, NULL); kv += ul_occ;
            if(uidx->uovl.telo && uidx->uovl.telo[w>>1]) is_telo = 1;
        }

        

        if((!is_telo) && (kv <= max_ext) && (get_remove_hifi_occ(uidx, max_ext_hifi, b->a + pb, b->n - pb, ub, &del_occ))) {
            // fprintf(stderr, "*[M::%s::] k::%u, v>>1::%u, v&1::%u, kv::%u, max_ext::%u, max_ext_hifi::%u\n\n", 
            // __func__, k, v>>1, v&1, kv, max_ext, max_ext_hifi);
            if(is_double_check) get_bridges(uidx, b->a + pb, b->n - pb, &w_occ, &a_occ);
            if(is_double_check == 0 || w_occ > 0 || a_occ > 0 || kv == 0 || del_occ == 0) {
                for (i = pb; i < b->n; i++) ulg_seq_del(ug, (b->a[i]>>1));
                cnt += b->n - pb;
            }
        }
        b->n = pb; 
    }

    // stats_sysm(g);
    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 $0xe0, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl %r8d, -0x1c(%rbp)
movq %r9, -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)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x74(%rbp)
movl $0x0, -0x88(%rbp)
movq $0x0, -0xb0(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x34250b
movq -0x28(%rbp), %rax
movq %rax, -0xc0(%rbp)
jmp 0x342518
leaq -0x40(%rbp), %rax
movq %rax, -0xc0(%rbp)
jmp 0x342518
movq -0xc0(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, 0x10(%rbp)
je 0x342537
movq 0x10(%rbp), %rax
movq %rax, -0xc8(%rbp)
jmp 0x342544
leaq -0x58(%rbp), %rax
movq %rax, -0xc8(%rbp)
jmp 0x342544
movq -0xc8(%rbp), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x78(%rbp)
movq -0x60(%rbp), %rax
movq $0x0, (%rax)
movl -0x78(%rbp), %eax
cmpl -0x74(%rbp), %eax
jae 0x342816
movq -0x70(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x78(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x34258c
jmp 0x342808
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x70(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x78(%rbp), %edx
xorl $0x1, %edx
movl %edx, %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movq -0x70(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x78(%rbp), %ecx
xorl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x8c(%rbp)
movl $0x0, -0x90(%rbp)
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x8c(%rbp), %eax
jae 0x34262a
movq -0xb0(%rbp), %rax
movl -0x80(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x34260e
jmp 0x34261f
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x34262a
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x3425e5
cmpl $0x0, -0x90(%rbp)
je 0x342638
jmp 0x342808
movq -0x8(%rbp), %rdi
movl -0x78(%rbp), %esi
shrl %esi
leaq -0xa0(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x37beb0
movl -0xa0(%rbp), %eax
movl %eax, -0x90(%rbp)
movl $0x0, -0xa8(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x2a8(%rax)
je 0x342697
movq -0x8(%rbp), %rax
movq 0x2a8(%rax), %rax
movl -0x78(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
cmpb $0x0, (%rax,%rcx)
je 0x342697
movl $0x1, -0xa8(%rbp)
movl $0x0, -0x80(%rbp)
movl -0x78(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x342746
movq -0x70(%rbp), %rdi
movl -0x7c(%rbp), %esi
xorl $0x1, %esi
leaq -0xb8(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x312a70
cmpl $0x0, %eax
je 0x3426d1
jmp 0x342746
movq -0xb8(%rbp), %rax
movl %eax, -0x7c(%rbp)
movq -0x8(%rbp), %rdi
movl -0x7c(%rbp), %esi
shrl %esi
leaq -0xa0(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x37beb0
movl -0xa0(%rbp), %eax
addl -0x90(%rbp), %eax
movl %eax, -0x90(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x2a8(%rax)
je 0x342736
movq -0x8(%rbp), %rax
movq 0x2a8(%rax), %rax
movl -0x7c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
cmpb $0x0, (%rax,%rcx)
je 0x342736
movl $0x1, -0xa8(%rbp)
jmp 0x342738
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x3426a4
movl -0x90(%rbp), %eax
cmpl -0x14(%rbp), %eax
ja 0x342806
cmpl $0x0, -0xa8(%rbp)
jne 0x342806
jmp 0x342764
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq -0x60(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x3427d5
movq -0x60(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x342794
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xd0(%rbp)
jmp 0x3427a2
movl $0x2, %eax
movq %rax, -0xd0(%rbp)
jmp 0x3427a2
movq -0xd0(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x90(%rbp), %eax
movl %eax, %edx
shlq $0x20, %rdx
movl -0x78(%rbp), %eax
orq %rax, %rdx
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x60(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x342806
jmp 0x342808
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x342561
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x60(%rbp), %rax
movq (%rax), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x30ed90
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
movq -0x60(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x342ccb
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x84(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x78(%rbp)
movq -0x70(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x78(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x34288a
jmp 0x342cb7
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x70(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x78(%rbp), %edx
xorl $0x1, %edx
movl %edx, %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movq -0x70(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x78(%rbp), %ecx
xorl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x8c(%rbp)
movl $0x0, -0x90(%rbp)
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x8c(%rbp), %eax
jae 0x342928
movq -0xb0(%rbp), %rax
movl -0x80(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x34290c
jmp 0x34291d
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x342928
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x3428e3
cmpl $0x0, -0x90(%rbp)
je 0x342936
jmp 0x342cb7
movq -0x60(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x94(%rbp)
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq -0x60(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x3429b4
movq -0x60(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x342973
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xd8(%rbp)
jmp 0x342981
movl $0x2, %eax
movq %rax, -0xd8(%rbp)
jmp 0x342981
movq -0xd8(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x78(%rbp), %eax
movl %eax, %edx
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x60(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
movq -0x8(%rbp), %rdi
movl -0x78(%rbp), %esi
shrl %esi
leaq -0xa0(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x37beb0
movl -0xa0(%rbp), %eax
movl %eax, -0x90(%rbp)
movl $0x0, -0xa8(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x2a8(%rax)
je 0x342a35
movq -0x8(%rbp), %rax
movq 0x2a8(%rax), %rax
movl -0x78(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
cmpb $0x0, (%rax,%rcx)
je 0x342a35
movl $0x1, -0xa8(%rbp)
movl $0x0, -0x80(%rbp)
movl -0x78(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x80(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x342b7a
movq -0x70(%rbp), %rdi
movl -0x7c(%rbp), %esi
xorl $0x1, %esi
leaq -0xb8(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x312a70
cmpl $0x0, %eax
je 0x342a72
jmp 0x342b7a
movq -0xb8(%rbp), %rax
movl %eax, -0x7c(%rbp)
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq -0x60(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x342aed
movq -0x60(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x342aac
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xe0(%rbp)
jmp 0x342aba
movl $0x2, %eax
movq %rax, -0xe0(%rbp)
jmp 0x342aba
movq -0xe0(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x7c(%rbp), %eax
movl %eax, %edx
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x60(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
movq -0x8(%rbp), %rdi
movl -0x7c(%rbp), %esi
shrl %esi
leaq -0xa0(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x37beb0
movl -0xa0(%rbp), %eax
addl -0x90(%rbp), %eax
movl %eax, -0x90(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x2a8(%rax)
je 0x342b6a
movq -0x8(%rbp), %rax
movq 0x2a8(%rax), %rax
movl -0x7c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
cmpb $0x0, (%rax,%rcx)
je 0x342b6a
movl $0x1, -0xa8(%rbp)
jmp 0x342b6c
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x342a42
cmpl $0x0, -0xa8(%rbp)
jne 0x342ca8
movl -0x90(%rbp), %eax
cmpl -0x14(%rbp), %eax
ja 0x342ca8
movq -0x8(%rbp), %rdi
movl -0x18(%rbp), %eax
movl %eax, %esi
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rdx
movl -0x94(%rbp), %eax
shlq $0x3, %rax
addq %rax, %rdx
movq -0x60(%rbp), %rax
movq (%rax), %rcx
movl -0x94(%rbp), %eax
subq %rax, %rcx
movq -0x68(%rbp), %r8
leaq -0xa4(%rbp), %r9
callq 0x37bf20
cmpq $0x0, %rax
je 0x342ca8
cmpl $0x0, -0x1c(%rbp)
je 0x342c20
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rsi
movl -0x94(%rbp), %eax
shlq $0x3, %rax
addq %rax, %rsi
movq -0x60(%rbp), %rax
movq (%rax), %rdx
movl -0x94(%rbp), %eax
subq %rax, %rdx
leaq -0x98(%rbp), %rcx
leaq -0x9c(%rbp), %r8
callq 0x37d020
cmpl $0x0, -0x1c(%rbp)
je 0x342c4a
cmpl $0x0, -0x98(%rbp)
ja 0x342c4a
cmpl $0x0, -0x9c(%rbp)
ja 0x342c4a
cmpl $0x0, -0x90(%rbp)
je 0x342c4a
cmpl $0x0, -0xa4(%rbp)
jne 0x342ca6
movl -0x94(%rbp), %eax
movl %eax, -0x80(%rbp)
movl -0x80(%rbp), %eax
movq -0x60(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x342c87
movq -0x10(%rbp), %rdi
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x80(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq %rax
movl %eax, %esi
callq 0x342d10
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x342c53
movq -0x60(%rbp), %rax
movq (%rax), %rcx
movl -0x94(%rbp), %eax
subq %rax, %rcx
movl -0x88(%rbp), %eax
addq %rcx, %rax
movl %eax, -0x88(%rbp)
jmp 0x342ca8
movl -0x94(%rbp), %eax
movl %eax, %ecx
movq -0x60(%rbp), %rax
movq %rcx, (%rax)
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x342843
cmpq $0x0, -0x28(%rbp)
jne 0x342cdb
movq -0x30(%rbp), %rdi
callq 0x2210
cmpq $0x0, 0x10(%rbp)
jne 0x342ceb
movq -0x48(%rbp), %rdi
callq 0x2210
cmpl $0x0, -0x88(%rbp)
jbe 0x342cfd
movq -0x70(%rbp), %rdi
callq 0xcdd10
movl -0x88(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax)