// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, %r14
movl %r8d, 0x4c(%rsp)
movl %ecx, 0x50(%rsp)
movl %edx, 0x14(%rsp)
movq %rdi, 0x38(%rsp)
movq 0xd0(%rsp), %rcx
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %rdx
andq $0x0, 0x10(%rdx)
movaps %xmm0, (%rdx)
movq 0x18(%rsi), %rbx
movl 0x14(%rbx), %eax
addl %eax, %eax
testq %rcx, %rcx
cmovneq %rcx, %rdx
movq %rdx, 0x78(%rsp)
testq %r9, %r9
je 0x1bf8ba
andq $0x0, (%r14)
movl %eax, %r8d
xorl %r12d, %r12d
xorl %ebp, %ebp
movq $0x0, 0x30(%rsp)
movq $0x0, 0x8(%rsp)
movq %rbx, 0x40(%rsp)
movq %rsi, 0x70(%rsp)
movq %r8, 0x28(%rsp)
cmpq %r8, %r12
je 0x1bfa93
movq 0x20(%rbx), %rcx
movl %r12d, %eax
shrl %eax
cmpl $0x0, (%rcx,%rax,8)
js 0x1bfa8b
movq 0x8(%rbx), %rdx
movq 0x28(%rbx), %rcx
movl %r12d, %esi
xorq $0x1, %rsi
movq (%rcx,%rsi,8), %rcx
movl %ecx, %esi
movq %rcx, %rdi
shrq $0x20, %rdi
imulq $0x18, %rsi, %rcx
addq $0x18, %rcx
imulq $0x18, %rdi, %rsi
addq %rsi, %rdx
addq $0xc, %rdx
xorl %esi, %esi
leaq 0x18(%rsi), %rdi
cmpq %rdi, %rcx
je 0x1bf947
cmpl $0x0, (%rdx,%rsi)
movq %rdi, %rsi
js 0x1bf930
jmp 0x1bfa8b
movq %r12, 0x20(%rsp)
movq 0x38(%rsp), %rcx
movq 0x2a8(%rcx), %r13
testq %r13, %r13
movq %rbp, 0x18(%rsp)
je 0x1bf971
xorl %r12d, %r12d
cmpb $0x0, (%r13,%rax)
setne %r12b
jmp 0x1bf974
xorl %r12d, %r12d
movq 0x38(%rsp), %rcx
movq 0x258(%rcx), %rbx
movl (%rbx,%rax,4), %r15d
movl 0x14(%rsp), %ebp
movq 0x20(%rsp), %rsi
subl $0x1, %ebp
jb 0x1bf9cb
xorl $0x1, %esi
movq 0x40(%rsp), %rdi
leaq 0x58(%rsp), %rdx
xorl %ecx, %ecx
callq 0x1a5f66
testl %eax, %eax
jne 0x1bf9cb
movq 0x58(%rsp), %rsi
movl %esi, %eax
shrl %eax
testq %r13, %r13
je 0x1bf9c5
cmpb $0x0, (%r13,%rax)
pushq $0x1
popq %rcx
cmovnel %ecx, %r12d
addl (%rbx,%rax,4), %r15d
jmp 0x1bf98d
cmpl 0x14(%rsp), %r15d
ja 0x1bfa0c
testl %r12d, %r12d
movq 0x40(%rsp), %rbx
movq 0x18(%rsp), %rbp
jne 0x1bfa16
testq %r14, %r14
movq 0x20(%rsp), %r12
je 0x1bfa22
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
jne 0x1bfa5c
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
jmp 0x1bfa45
movq 0x40(%rsp), %rbx
movq 0x18(%rsp), %rbp
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %r12
jmp 0x1bfa8b
cmpq 0x30(%rsp), %rbp
jne 0x1bfa69
leaq (,%rbp,2), %rsi
testq %rbp, %rbp
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x30(%rsp)
movq 0x8(%rsp), %rdi
shlq $0x3, %rsi
callq 0x2370
testq %r14, %r14
je 0x1bfa70
movq %rax, 0x10(%r14)
movq (%r14), %rcx
jmp 0x1bfa60
movq 0x10(%r14), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
jmp 0x1bfa7b
movq 0x8(%rsp), %rax
jmp 0x1bfa75
movq %rax, 0x8(%rsp)
movq %rbp, %rcx
incq %rbp
shlq $0x20, %r15
orq %r12, %r15
movq %r15, (%rax,%rcx,8)
movq 0x28(%rsp), %r8
incq %r12
jmp 0x1bf8e3
movq 0x8(%rsp), %rdi
movq %rbp, %rax
testq %r14, %r14
je 0x1bfaa7
movq (%r14), %rax
movq 0x10(%r14), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1a3def
movl 0x50(%rsp), %eax
movq %rax, 0x50(%rsp)
xorl %r13d, %r13d
movl $0x0, 0x48(%rsp)
movl %r13d, %r13d
testq %r14, %r14
je 0x1bfadb
cmpq %r13, (%r14)
jbe 0x1bfe53
movq 0x10(%r14), %rax
jmp 0x1bfae9
movq 0x8(%rsp), %rax
cmpq %r13, %rbp
jbe 0x1bfe49
movq (%rax,%r13,8), %r12
movq 0x20(%rbx), %rax
movl %r12d, %r15d
shrl %r15d
cmpl $0x0, (%rax,%r15,8)
js 0x1bfe41
movq %r13, 0x20(%rsp)
movq 0x8(%rbx), %rcx
movq 0x28(%rbx), %rax
movl %r12d, %r13d
movq %r13, %rdx
xorq $0x1, %rdx
movq (%rax,%rdx,8), %rax
movl %eax, %edx
movq %rax, %rsi
shrq $0x20, %rsi
imulq $0x18, %rdx, %rax
addq $0x18, %rax
imulq $0x18, %rsi, %rdx
addq %rdx, %rcx
addq $0xc, %rcx
xorl %edx, %edx
leaq 0x18(%rdx), %rsi
cmpq %rsi, %rax
je 0x1bfb57
cmpl $0x0, (%rcx,%rdx)
movq %rsi, %rdx
js 0x1bfb3b
movq 0x20(%rsp), %r13
jmp 0x1bfe41
testq %r14, %r14
je 0x1bfb87
movq (%r14), %rdx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rdx
movq %rdx, 0x28(%rsp)
jne 0x1bfbd0
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
movq %rdx, 0x18(%rsp)
jmp 0x1bfbb6
cmpq 0x30(%rsp), %rbp
jne 0x1bfbe4
leaq (,%rbp,2), %rsi
testq %rbp, %rbp
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movl %ebp, %eax
movq %rax, 0x28(%rsp)
movq %rbp, 0x18(%rsp)
movq %rsi, 0x30(%rsp)
movq 0x8(%rsp), %rax
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
testq %r14, %r14
je 0x1bfbf7
movq %rax, 0x10(%r14)
movq (%r14), %rcx
jmp 0x1bfbd8
movq %rdx, %rcx
movq %rdx, 0x18(%rsp)
movq %rbp, %rbx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
jmp 0x1bfc03
movq %rbp, 0x18(%rsp)
movl %ebp, %eax
movq %rax, 0x28(%rsp)
movq 0x8(%rsp), %rax
jmp 0x1bfbfc
movq %rax, 0x8(%rsp)
movq %rbp, %rcx
leaq 0x1(%rbp), %rbx
movq %r13, (%rax,%rcx,8)
movq 0x38(%rsp), %rax
movq 0x258(%rax), %rcx
movq 0x2a8(%rax), %rax
movl (%rcx,%r15,4), %ebp
xorl %r13d, %r13d
testq %rax, %rax
je 0x1bfc2f
cmpb $0x0, (%rax,%r15)
setne %r13b
movl 0x14(%rsp), %r15d
subl $0x1, %r15d
jb 0x1bfd16
xorl $0x1, %r12d
movq 0x40(%rsp), %rdi
movl %r12d, %esi
leaq 0x58(%rsp), %rdx
xorl %ecx, %ecx
callq 0x1a5f66
testl %eax, %eax
jne 0x1bfd16
movq 0x58(%rsp), %r12
testq %r14, %r14
je 0x1bfc89
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x1bfcc0
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
jmp 0x1bfca8
cmpq 0x30(%rsp), %rbx
jne 0x1bfcc9
leaq (%rbx,%rbx), %rsi
testq %rbx, %rbx
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x30(%rsp)
movq 0x8(%rsp), %rax
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
testq %r14, %r14
je 0x1bfcd0
movq %rax, 0x10(%r14)
movq (%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
jmp 0x1bfcdb
movq 0x8(%rsp), %rax
jmp 0x1bfcd5
movq %rax, 0x8(%rsp)
movq %rbx, %rcx
incq %rbx
movl %r12d, %edx
movq %rdx, (%rax,%rcx,8)
movl %r12d, %eax
shrl %eax
movq 0x38(%rsp), %rcx
movq 0x258(%rcx), %rdx
movq 0x2a8(%rcx), %rcx
addl (%rdx,%rax,4), %ebp
testq %rcx, %rcx
je 0x1bfc34
cmpb $0x0, (%rcx,%rax)
pushq $0x1
popq %rax
cmovnel %eax, %r13d
jmp 0x1bfc34
testl %r13d, %r13d
jne 0x1bfd48
cmpl 0x14(%rsp), %ebp
movq 0x20(%rsp), %r13
movq 0x18(%rsp), %r12
ja 0x1bfddd
movl %r12d, %r15d
testq %r14, %r14
je 0x1bfd5a
leaq (,%r15,8), %rdx
addq 0x10(%r14), %rdx
movq (%r14), %rcx
jmp 0x1bfd66
movq %rbx, %rbp
movq 0x20(%rsp), %r13
movq 0x18(%rsp), %r12
jmp 0x1bfe2c
movq 0x8(%rsp), %rax
leaq (%rax,%r15,8), %rdx
movq %rbx, %rcx
subq %r15, %rcx
movq 0x38(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x78(%rsp), %r8
leaq 0x64(%rsp), %r9
callq 0x1de65f
testq %rax, %rax
je 0x1bfddd
cmpl $0x0, 0x4c(%rsp)
je 0x1bfdbd
movq 0x8(%rsp), %rax
movq %rbx, %rdx
testq %r14, %r14
je 0x1bfda2
movq (%r14), %rdx
movq 0x10(%r14), %rax
leaq (%rax,%r15,8), %rsi
subq %r15, %rdx
movq 0x38(%rsp), %rdi
leaq 0x6c(%rsp), %rcx
leaq 0x68(%rsp), %r8
callq 0x1dec74
cmpl $0x0, 0x4c(%rsp)
je 0x1bfde2
cmpl $0x0, 0x6c(%rsp)
jne 0x1bfde2
cmpl $0x0, 0x68(%rsp)
jne 0x1bfde2
testl %ebp, %ebp
je 0x1bfde2
cmpl $0x0, 0x64(%rsp)
je 0x1bfde2
movq %rbx, %rbp
jmp 0x1bfe2c
movq 0x70(%rsp), %r15
movq %rbx, %rbp
movq 0x28(%rsp), %rax
movl %eax, %ebx
testq %r14, %r14
je 0x1bfe04
movq (%r14), %rax
cmpq %rbx, %rax
jbe 0x1bfe25
movq 0x10(%r14), %rax
jmp 0x1bfe0e
movq 0x8(%rsp), %rax
cmpq %rbx, %rbp
jbe 0x1bfe22
movq (%rax,%rbx,8), %rsi
shrq %rsi
movq %r15, %rdi
callq 0x1bfe8f
leal 0x1(%rbx), %eax
jmp 0x1bfdef
movq %rbp, %rax
subl %r12d, %eax
addl %eax, 0x48(%rsp)
movl %r12d, %eax
testq %r14, %r14
je 0x1bfe39
movq %rax, (%r14)
jmp 0x1bfe3c
movq %rax, %rbp
movq 0x40(%rsp), %rbx
incl %r13d
jmp 0x1bfac4
movq 0x8(%rsp), %rdi
callq 0x2220
cmpq $0x0, 0xd0(%rsp)
jne 0x1bfe6b
movq 0x90(%rsp), %rdi
callq 0x2220
movl 0x48(%rsp), %ebp
testl %ebp, %ebp
je 0x1bfe7b
movq %rbx, %rdi
callq 0x694eb
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq