// File: /workspace/github_cpp_makefile_star2_O3/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, 0x44(%rsp)
movl %ecx, 0x50(%rsp)
movl %edx, 0x1c(%rsp)
movq %rdi, 0x38(%rsp)
movq 0xd0(%rsp), %rcx
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %rdx
movaps %xmm0, (%rdx)
movq $0x0, 0x10(%rdx)
movq %rsi, 0x70(%rsp)
movq 0x18(%rsi), %rbp
movl 0x14(%rbp), %eax
addl %eax, %eax
testq %rcx, %rcx
cmovneq %rcx, %rdx
movq %rdx, 0x78(%rsp)
testq %r9, %r9
je 0x20c05c
movq $0x0, (%r14)
testl %eax, %eax
movq %rbp, 0x48(%rsp)
je 0x20c2e4
movl %eax, %eax
movq %rax, 0x28(%rsp)
xorl %r13d, %r13d
movq $0x0, 0x10(%rsp)
movq $0x0, 0x30(%rsp)
xorl %ebx, %ebx
movq 0x20(%rbp), %rax
movl %r13d, %r8d
shrl %r8d
cmpl $0x0, (%rax,%r8,8)
js 0x20c2d4
movq 0x8(%rbp), %rax
movq %rax, 0x20(%rsp)
movq 0x28(%rbp), %rcx
movl %r13d, %eax
xorq $0x1, %rax
movq (%rcx,%rax,8), %rax
testl %eax, %eax
je 0x20c0ef
movq %rax, %rdx
shrq $0x20, %rdx
movl %eax, %esi
leaq (%rdx,%rdx,2), %rax
movq 0x20(%rsp), %rdx
leaq (%rdx,%rax,8), %rax
addq $0xc, %rax
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rdx
xorl %esi, %esi
cmpl $0x0, (%rax,%rsi)
jns 0x20c2d4
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0x20c0dc
movq 0x38(%rsp), %rax
movq 0x2a8(%rax), %rsi
testq %rsi, %rsi
movq %rbx, 0x8(%rsp)
je 0x20c111
xorl %edx, %edx
cmpb $0x0, (%rsi,%r8)
setne %dl
jmp 0x20c113
xorl %edx, %edx
movq 0x38(%rsp), %rax
movq 0x258(%rax), %rax
movq %rax, 0x58(%rsp)
movl (%rax,%r8,4), %ebx
cmpl $0x0, 0x1c(%rsp)
je 0x20c221
movq 0x20(%rsp), %rax
leaq 0xc(%rax), %r8
xorl %r9d, %r9d
movl %r13d, %r10d
movl %r10d, %eax
movq (%rcx,%rax,8), %r10
testl %r10d, %r10d
jle 0x20c221
movq %r10, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
movq 0x20(%rsp), %rdi
leaq (%rdi,%rax,8), %r11
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
leaq (%r8,%rax,8), %rbp
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
xorl %r15d, %r15d
xorl %eax, %eax
xorl %edi, %edi
cmpl $0x0, (%rbp)
setns %dil
cmovnsl %r15d, %r12d
addl %edi, %eax
incq %r15
addq $0x18, %rbp
cmpq %r15, %r10
jne 0x20c17c
cmpl $0x1, %eax
jne 0x20c21c
movslq %r12d, %rax
leaq (%rax,%rax,2), %rax
movl 0x8(%r11,%rax,8), %r10d
movq %r10, %rax
xorq $0x1, %rax
movq (%rcx,%rax,8), %r11
testl %r11d, %r11d
movq 0x48(%rsp), %rbp
jle 0x20c221
movq %r11, %rax
shrq $0x20, %rax
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
leaq (%rax,%rax,2), %rax
leaq (%r8,%rax,8), %r15
xorl %eax, %eax
movl (%r15), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %eax
addq $0x18, %r15
decq %r11
jne 0x20c1d6
cmpl $0x1, %eax
jne 0x20c221
movl %r10d, %eax
shrl %eax
testq %rsi, %rsi
je 0x20c204
cmpb $0x0, (%rsi,%rax)
movl $0x1, %edi
cmovnel %edi, %edx
movq 0x58(%rsp), %rdi
addl (%rdi,%rax,4), %ebx
incl %r9d
cmpl 0x1c(%rsp), %r9d
jne 0x20c142
jmp 0x20c221
movq 0x48(%rsp), %rbp
cmpl 0x1c(%rsp), %ebx
ja 0x20c2cf
testl %edx, %edx
jne 0x20c2cf
testq %r14, %r14
je 0x20c275
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x20c26c
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
jmp 0x20c2c4
movq 0x30(%rsp), %rax
cmpq %rax, 0x8(%rsp)
jne 0x20c2aa
leaq (%rax,%rax), %r15
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r15
leaq (,%r15,8), %rsi
movq 0x10(%rsp), %rdi
callq 0x2370
movq %r15, 0x30(%rsp)
jmp 0x20c2af
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %rdx
movq %rdx, %rcx
incq %rdx
movq %rax, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
shlq $0x20, %rbx
orq %r13, %rbx
movq %rbx, (%rax,%rcx,8)
movq 0x8(%rsp), %rbx
incq %r13
cmpq 0x28(%rsp), %r13
jne 0x20c087
jmp 0x20c2f8
xorl %ebx, %ebx
movq $0x0, 0x30(%rsp)
movq $0x0, 0x10(%rsp)
movq 0x10(%rsp), %rdi
movq %rbx, %rax
testq %r14, %r14
je 0x20c30c
movq (%r14), %rax
movq 0x10(%r14), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1ee586
movl 0x50(%rsp), %eax
movq %rax, 0x50(%rsp)
xorl %eax, %eax
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl $0x0, 0x40(%rsp)
movl %eax, %eax
testq %r14, %r14
je 0x20c347
cmpq %rax, (%r14)
jbe 0x20c898
movq %rax, %rcx
movq 0x10(%r14), %rax
jmp 0x20c358
movq %rax, %rcx
movq 0x10(%rsp), %rax
cmpq %rcx, %rbx
jbe 0x20c88e
movq %rcx, 0x58(%rsp)
movq (%rax,%rcx,8), %r12
movq 0x20(%rbp), %rax
movl %r12d, %r15d
shrl %r15d
cmpl $0x0, (%rax,%r15,8)
js 0x20c882
movq %rbx, 0x8(%rsp)
movq 0x28(%rbp), %rax
movl %r12d, %edi
movq %rdi, %rcx
xorq $0x1, %rcx
movq (%rax,%rcx,8), %rax
testl %eax, %eax
movq 0x10(%rsp), %rbx
je 0x20c3cc
movq 0x8(%rbp), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movl %eax, %esi
leaq (%rdx,%rdx,2), %rax
leaq (%rcx,%rax,8), %rax
addq $0xc, %rax
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rcx
xorl %edx, %edx
cmpl $0x0, (%rax,%rdx)
jns 0x20c878
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0x20c3b9
testq %r14, %r14
je 0x20c41d
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
movq %rcx, 0x28(%rsp)
jne 0x20c414
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rdi, 0x20(%rsp)
movq %rax, %rdi
callq 0x2370
movq 0x20(%rsp), %rdi
movq %rax, 0x10(%r14)
movq (%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
jmp 0x20c47b
movq 0x8(%rsp), %rbx
cmpq 0x30(%rsp), %rbx
jne 0x20c45f
leaq (%rbx,%rbx), %rax
testq %rbx, %rbx
movl $0x2, %ecx
cmoveq %rcx, %rax
movq %rax, 0x30(%rsp)
leaq (,%rax,8), %rsi
movq %rdi, 0x20(%rsp)
movq 0x10(%rsp), %rdi
callq 0x2370
movq 0x20(%rsp), %rdi
movq %rax, %rsi
jmp 0x20c464
movq 0x10(%rsp), %rsi
movq %rbx, %rcx
movq %rbx, 0x28(%rsp)
leaq 0x1(%rbx), %rdx
movq %rsi, %rax
movq %rdx, 0x8(%rsp)
movq %rsi, %rbx
movq %rbx, 0x10(%rsp)
movq %rdi, (%rax,%rcx,8)
movq 0x38(%rsp), %rax
movq 0x258(%rax), %rcx
movq 0x2a8(%rax), %rax
movl (%rcx,%r15,4), %ebx
testq %rax, %rax
je 0x20c4b0
xorl %ecx, %ecx
cmpb $0x0, (%rax,%r15)
setne %cl
movl %ecx, 0x20(%rsp)
jmp 0x20c4b8
movl $0x0, 0x20(%rsp)
cmpl $0x0, 0x1c(%rsp)
je 0x20c65e
xorl %r15d, %r15d
movq 0x28(%rbp), %rcx
movl %r12d, %eax
movq (%rcx,%rax,8), %rdx
testl %edx, %edx
jle 0x20c65e
movq 0x8(%rbp), %rax
movq %rdx, %rsi
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %rdi
leaq (%rax,%rdi,8), %rsi
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leaq (%rax,%rdi,8), %r8
addq $0xc, %r8
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
xorl %r10d, %r10d
xorl %r11d, %r11d
cmpl $0x0, (%r8)
setns %r11b
cmovnsl %r9d, %edi
addl %r11d, %r10d
incq %r9
addq $0x18, %r8
cmpq %r9, %rdx
jne 0x20c505
cmpl $0x1, %r10d
jne 0x20c65e
movslq %edi, %rdx
leaq (%rdx,%rdx,2), %rdx
movl 0x8(%rsi,%rdx,8), %r12d
movq %r12, %rdx
xorq $0x1, %rdx
movq (%rcx,%rdx,8), %rcx
testl %ecx, %ecx
jle 0x20c65e
movq %rcx, %rdx
shrq $0x20, %rdx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %rdx
addq $0xc, %rdx
xorl %eax, %eax
movl (%rdx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %eax
addq $0x18, %rdx
decq %rcx
jne 0x20c567
cmpl $0x1, %eax
jne 0x20c65e
testq %r14, %r14
je 0x20c5c4
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x20c5bb
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
jmp 0x20c618
movq 0x30(%rsp), %rax
cmpq %rax, 0x8(%rsp)
jne 0x20c5fe
leaq (%rax,%rax), %rbp
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rbp
leaq (,%rbp,8), %rsi
movq 0x10(%rsp), %rdi
callq 0x2370
movq %rbp, 0x30(%rsp)
movq 0x48(%rsp), %rbp
jmp 0x20c603
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %rdx
movq %rdx, %rcx
incq %rdx
movq %rax, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
movq %r12, (%rax,%rcx,8)
movl %r12d, %eax
shrl %eax
movq 0x38(%rsp), %rcx
movq 0x258(%rcx), %rdx
movq 0x2a8(%rcx), %rcx
addl (%rdx,%rax,4), %ebx
testq %rcx, %rcx
je 0x20c650
cmpb $0x0, (%rcx,%rax)
movl $0x1, %eax
movl 0x20(%rsp), %ecx
cmovnel %eax, %ecx
movl %ecx, 0x20(%rsp)
incl %r15d
cmpl 0x1c(%rsp), %r15d
jne 0x20c4c6
cmpl $0x0, 0x20(%rsp)
jne 0x20c71b
cmpl 0x1c(%rsp), %ebx
movq 0x28(%rsp), %rcx
ja 0x20c86e
movl %ebx, %r15d
movl %ecx, %ebx
testq %r14, %r14
je 0x20c693
leaq (,%rbx,8), %rdx
addq 0x10(%r14), %rdx
movq (%r14), %rcx
jmp 0x20c6a1
movq 0x10(%rsp), %rax
leaq (%rax,%rbx,8), %rdx
movq 0x8(%rsp), %rcx
subq %rbx, %rcx
movq 0x38(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x78(%rsp), %r8
leaq 0x64(%rsp), %r9
callq 0x22c98c
testq %rax, %rax
je 0x20c71b
cmpl $0x0, 0x44(%rsp)
je 0x20c6fa
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %rdx
testq %r14, %r14
je 0x20c6df
movq (%r14), %rdx
movq 0x10(%r14), %rax
leaq (%rax,%rbx,8), %rsi
subq %rbx, %rdx
movq 0x38(%rsp), %rdi
leaq 0x6c(%rsp), %rcx
leaq 0x68(%rsp), %r8
callq 0x22cfd5
cmpl $0x0, 0x44(%rsp)
je 0x20c725
cmpl $0x0, 0x6c(%rsp)
jne 0x20c725
cmpl $0x0, 0x68(%rsp)
jne 0x20c725
testl %r15d, %r15d
je 0x20c725
cmpl $0x0, 0x64(%rsp)
je 0x20c725
movq 0x28(%rsp), %rcx
jmp 0x20c86e
movq 0x28(%rsp), %rbx
movl %ebx, %ebx
testq %r14, %r14
je 0x20c743
movq (%r14), %rax
cmpq %rbx, %rax
jbe 0x20c863
movq 0x10(%r14), %rax
jmp 0x20c753
movq 0x10(%rsp), %rax
cmpq %rbx, 0x8(%rsp)
jbe 0x20c85e
movq (%rax,%rbx,8), %rax
movq %rax, %rcx
shrq %rcx
movq 0x70(%rsp), %rdx
movq 0x18(%rdx), %rdx
movq 0x20(%rdx), %rsi
movl %ecx, %ecx
orl $0x80000000, (%rsi,%rcx,8) # imm = 0x80000000
andl $-0x2, %eax
movb $0x1, %sil
xorl %edi, %edi
orq %rax, %rdi
movq 0x28(%rdx), %r8
movq (%r8,%rdi,8), %r9
movq %r9, %r8
andq %r13, %r8
je 0x20c806
shrq $0x20, %r9
leaq (%r9,%r9,2), %r9
shlq $0x3, %r9
addq 0x8(%rdx), %r9
xorl %r10d, %r10d
leaq (%r10,%r10,2), %r11
orb $-0x80, 0xf(%r9,%r11,8)
movl 0x8(%r9,%r11,8), %r11d
xorq $0x1, %r11
movq 0x28(%rdx), %r15
movq (%r15,%r11,8), %r15
movq %r15, %r11
andq %r13, %r11
je 0x20c7fe
movq 0x8(%rdx), %r12
shrq $0x20, %r15
shlq $0x3, %r11
leaq (%r11,%r11,2), %r11
leaq (%r15,%r15,2), %r15
leaq (%r12,%r15,8), %r15
addq $0xc, %r15
xorl %r12d, %r12d
movl -0x4(%r15,%r12), %ebp
xorl %edi, %ebp
cmpl $0x1, %ebp
jne 0x20c7f5
orb $-0x80, 0x3(%r15,%r12)
addq $0x18, %r12
cmpq %r12, %r11
jne 0x20c7e3
incq %r10
cmpq %r8, %r10
jne 0x20c7a1
movl $0x1, %edi
testb $0x1, %sil
movl $0x0, %esi
jne 0x20c77b
movq 0x70(%rsp), %r12
movq 0x10(%r12), %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %r15
movq 0x18(%rax,%r15), %rdi
callq 0x2220
movq 0x10(%r12), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax,%r15)
movups %xmm0, (%rax,%r15)
movq $0x0, 0x20(%rax,%r15)
incl %ebx
movq 0x48(%rsp), %rbp
jmp 0x20c72a
movq 0x8(%rsp), %rax
movq 0x28(%rsp), %rcx
subl %ecx, %eax
addl %eax, 0x40(%rsp)
movl %ecx, %eax
testq %r14, %r14
je 0x20c87f
movq %rax, (%r14)
movq 0x8(%rsp), %rbx
jmp 0x20c882
movq %rax, %rbx
movq 0x58(%rsp), %rax
incl %eax
jmp 0x20c32e
movq 0x10(%rsp), %rdi
callq 0x2220
cmpq $0x0, 0xd0(%rsp)
jne 0x20c8b0
movq 0x90(%rsp), %rdi
callq 0x2220
movl 0x40(%rsp), %ebx
testl %ebx, %ebx
je 0x20c8c0
movq %rbp, %rdi
callq 0xa0a7d
movl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq