// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: get_remove_hifi_occ(ul_resolve_t*, unsigned long, unsigned long*, unsigned long, asg64_v*, unsigned int*)
inline uint64_t get_remove_hifi_occ(ul_resolve_t *uidx, uint64_t thres, uint64_t *v_a, uint64_t v_n, asg64_v *buf, uint32_t *del_occ)
{
    ul2ul_idx_t *idx = &(uidx->uovl);
    ma_ug_t *iug = idx->i_ug, *raw = uidx->l1_ug;
    uint64_t k, l, z, m, p, *raw_a, raw_n, raw_id, iug_id, iug_off, del_n, keep_ns[2], del_ns[2], dup_raw, n_mask, pn, fn, is, ie; 
    uinfo_srt_warp_t *iu; uint64_t ms, me; 
    for (k = del_n = fn = 0, buf->n = dup_raw = 0; k < v_n; k++) {
        iu = &(idx->cc.iug_a[v_a[k]>>1]); ///integer unitigs
        for (z = 0; z < iu->n; z++) {
            raw_id = (iu->a[z].v>>1);
            raw_a = idx->cc.iug_b + idx->cc.iug_idx[raw_id];
            raw_n = idx->cc.iug_idx[raw_id+1] - idx->cc.iug_idx[raw_id];
            assert(raw_n > 0);
            for (m = keep_ns[0] = keep_ns[1] = 0, pn = buf->n, n_mask = 0; m < raw_n; m++) {
                iug_id = (raw_a[m]<<1)>>33; iug_off = (uint32_t)raw_a[m];
                // if(iug_id >= iug->g->n_seq){
                //     fprintf(stderr, ">>>[M::%s::] v_a[%lu]>>1::%lu, raw_id::%lu, raw_n::%lu, m::%lu, raw_a[m]::%lu, uidx->uovl.cc.iug_b[455]::%lu, iug_id::%lu, iug_off::%lu, iug->g->n_seq::%u\n", __func__, 
                //         k, v_a[k]>>1, raw_id, raw_n, m, raw_a[m], uidx->uovl.cc.iug_b[455], iug_id, iug_off, (uint32_t)iug->g->n_seq);
                // }
                if(iug->g->seq[iug_id].del) continue;
                if(raw_a[m]&((uint64_t)(0x8000000000000000))) {
                    n_mask++;
                    continue;
                }
                if(iug_id == (v_a[k]>>1) && iug_off == z) {///query itself
                    raw_a[m] |= ((uint64_t)(0x8000000000000000));
                    p = (uint32_t)-1; p <<= 32; p += idx->cc.iug_idx[raw_id] + m; 
                    kv_push(uint64_t, *buf, p);
                } else {
                    if(!(idx->cc.iug_a[iug_id].a[iug_off].v&1)) {
                        keep_ns[0] = MAX(idx->cc.iug_a[iug_id].a[iug_off].n, keep_ns[0]);
                    } else {
                        keep_ns[1] = MAX(idx->cc.iug_a[iug_id].a[iug_off].n, keep_ns[1]);
                    }                    
                }
            }
            assert(buf->n == pn + 1);
            if(keep_ns[0] + keep_ns[1] >= raw->u.a[raw_id].n) {
                keep_ns[0] = keep_ns[1] = raw->u.a[raw_id].n;
            }

            is = keep_ns[0]; ie = raw->u.a[raw_id].n - keep_ns[1];///[is, ie) -> uncovered coordinates
            if(ie > is) {
                if(!(iu->a[z].v&1)) {
                    ms = 0; me = iu->a[z].n;
                } else {
                    ms = raw->u.a[raw_id].n - iu->a[z].n; me = raw->u.a[raw_id].n;
                }
                if(MAX(is, ms) < MIN(ie, me)) {
                    del_n += MIN(ie, me) - MAX(is, ms);
                    buf->a[pn] <<= 32; buf->a[pn] >>= 32; buf->a[pn] |= (raw_id<<32); fn++;
                    if(n_mask) dup_raw = 1;
                }
            }
        }
    }

    if(del_n > thres && dup_raw) {
        radix_sort_srt64(buf->a, buf->a + buf->n); del_n = 0;
        for (l = 0, k = 1; k <= fn; k++) {
            if(k == fn || (buf->a[k]>>32) != (buf->a[l]>>32)) {
                raw_id = (buf->a[l]>>32); 
                raw_a = idx->cc.iug_b + idx->cc.iug_idx[raw_id];
                raw_n = idx->cc.iug_idx[raw_id+1] - idx->cc.iug_idx[raw_id];
                assert(raw_n > 0);
                for (m = del_ns[0] = del_ns[1] = keep_ns[0] = keep_ns[1] = 0; m < raw_n; m++) {
                    iug_id = (raw_a[m]<<1)>>33; iug_off = (uint32_t)raw_a[m];
                    if(iug->g->seq[iug_id].del) continue;
                    if(raw_a[m]&((uint64_t)(0x8000000000000000))) {
                        if(!(idx->cc.iug_a[iug_id].a[iug_off].v&1)) {
                            del_ns[0] = MAX(idx->cc.iug_a[iug_id].a[iug_off].n, del_ns[0]);
                        } else {
                            del_ns[1] = MAX(idx->cc.iug_a[iug_id].a[iug_off].n, del_ns[1]);
                        } 
                    } else {
                        if(!(idx->cc.iug_a[iug_id].a[iug_off].v&1)) {
                            keep_ns[0] = MAX(idx->cc.iug_a[iug_id].a[iug_off].n, keep_ns[0]);
                        } else {
                            keep_ns[1] = MAX(idx->cc.iug_a[iug_id].a[iug_off].n, keep_ns[1]);
                        }   
                    }
                }
                assert(del_ns[0] + del_ns[1] > 0);
                if(keep_ns[0] + keep_ns[1] >= raw->u.a[raw_id].n) {
                    keep_ns[0] = keep_ns[1] = raw->u.a[raw_id].n;
                }
                

                is = keep_ns[0]; ie = raw->u.a[raw_id].n - keep_ns[1];///[is, ie) -> uncovered coordinates
                if(ie > is) {
                    if(del_ns[0] + del_ns[1] >= raw->u.a[raw_id].n) {
                        del_n += ie - is;
                    } else {
                        ms = 0; me = del_ns[0];
                        if(ms < me && MAX(is, ms) < MIN(ie, me)) {
                            del_n += MIN(ie, me) - MAX(is, ms);
                        }

                        ms = raw->u.a[raw_id].n - del_ns[1]; me = raw->u.a[raw_id].n;
                        if(ms < me && MAX(is, ms) < MIN(ie, me)) {
                            del_n += MIN(ie, me) - MAX(is, ms);
                        }
                    }
                }
                if(del_n > thres) break;
                l = k; 
            }
        }
    }

    for (k = 0; k < buf->n; k++) {
        assert(idx->cc.iug_b[(uint32_t)buf->a[k]]&((uint64_t)(0x8000000000000000)));
        idx->cc.iug_b[(uint32_t)buf->a[k]] <<= 1; idx->cc.iug_b[(uint32_t)buf->a[k]] >>= 1;
    }
    // fprintf(stderr, "*[M::%s::] del_n::%lu\n", __func__, del_n);
    if(del_occ) *del_occ = del_n;
    return ((del_n > thres)?0:1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, 0x78(%rsp)
movq %rcx, 0x80(%rsp)
movq %rsi, 0x20(%rsp)
movq 0x230(%rdi), %r11
movq 0x18(%rdi), %rax
movq %rax, 0x60(%rsp)
andq $0x0, (%r8)
movq $0x0, 0x8(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x58(%rsp)
xorl %r10d, %r10d
movq %r8, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
movq %r11, 0x70(%rsp)
movq %rdx, 0x30(%rsp)
cmpq 0x80(%rsp), %r10
je 0x1de97e
movq (%rdx,%r10,8), %rax
shrq %rax
imulq $0x18, %rax, %rbx
addq 0x260(%rdi), %rbx
xorl %ebp, %ebp
movq %r10, 0x38(%rsp)
movq %rbx, 0x68(%rsp)
cmpq (%rbx), %rbp
jae 0x1de976
movq 0x10(%rbx), %rax
movq %rbp, %rsi
shlq $0x4, %rsi
movl (%rax,%rsi), %ecx
shrl %ecx
movq 0x268(%rdi), %rax
movl (%rax,%rcx,4), %r13d
movq %rcx, 0x50(%rsp)
movl 0x4(%rax,%rcx,4), %r15d
subl %r13d, %r15d
je 0x1debf8
movq %rsi, 0x88(%rsp)
shlq $0x3, %r13
addq 0x270(%rdi), %r13
movq (%r8), %rax
movq %rax, 0x90(%rsp)
xorl %ebx, %ebx
movq $0x0, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
xorl %r12d, %r12d
cmpq %rbx, %r15
je 0x1de88a
movq (%r13,%rbx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x18(%r11), %rsi
movq 0x20(%rsi), %rsi
cmpl $0x0, (%rsi,%rcx,8)
js 0x1de882
testq %rax, %rax
js 0x1de839
movl %eax, %r9d
cmpq %rbp, %r9
jne 0x1de83e
movq (%rdx,%r10,8), %rsi
shrq %rsi
cmpq %rsi, %rcx
jne 0x1de83e
movq %rbp, %r14
btsq $0x3f, %rax
movq %rax, (%r13,%rbx,8)
movq 0x268(%rdi), %rax
movq 0x50(%rsp), %rcx
movl (%rax,%rcx,4), %ebp
movq (%r8), %rcx
movq 0x10(%r8), %rax
cmpq 0x8(%r8), %rcx
jne 0x1de819
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x38(%rsp), %r10
movq 0x30(%rsp), %rdx
movq 0x70(%rsp), %r11
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %r8
movq %rax, 0x10(%r8)
movq (%r8), %rcx
movabsq $-0x100000000, %rsi     # imm = 0xFFFFFFFF00000000
addq %rbx, %rsi
addq %rsi, %rbp
leaq 0x1(%rcx), %rsi
movq %rsi, (%r8)
movq %rbp, (%rax,%rcx,8)
movq %r14, %rbp
jmp 0x1de882
incq %r12
jmp 0x1de882
movq 0x260(%rdi), %rax
imulq $0x18, %rcx, %rcx
movq 0x10(%rax,%rcx), %rcx
shlq $0x4, %r9
movl 0xc(%rcx,%r9), %eax
testb $0x1, (%rcx,%r9)
jne 0x1de871
movq 0x48(%rsp), %rcx
cmpq %rax, %rcx
cmovbeq %rax, %rcx
movq %rcx, 0x48(%rsp)
jmp 0x1de882
movq 0x40(%rsp), %rcx
cmpq %rax, %rcx
cmovbeq %rax, %rcx
movq %rcx, 0x40(%rsp)
incq %rbx
jmp 0x1de760
movq 0x90(%rsp), %r14
leaq 0x1(%r14), %rax
cmpq %rax, (%r8)
jne 0x1debd9
movq %r12, %r15
movq 0x48(%rsp), %r12
movq 0x40(%rsp), %r9
leaq (%r12,%r9), %rsi
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rax
imulq $0x28, 0x50(%rsp), %rcx
movl 0x10(%rax,%rcx), %eax
movq %rax, %rcx
subq %r9, %rcx
cmpq %rax, %rsi
cmovaeq %rax, %r12
movl $0x0, %esi
cmovaeq %rsi, %rcx
cmpq %r12, %rcx
jbe 0x1de969
movq 0x68(%rsp), %rbx
movq 0x10(%rbx), %rdx
movq 0x88(%rsp), %r9
movl (%rdx,%r9), %esi
movl 0xc(%rdx,%r9), %r10d
andl $0x1, %esi
movl %esi, %r9d
negl %r9d
movl %eax, %edx
subl %r10d, %edx
andl %r9d, %edx
testl %esi, %esi
cmoveq %r10, %rax
cmpq %rdx, %r12
cmovaq %r12, %rdx
cmpq %rax, %rcx
cmovbq %rcx, %rax
cmpq %rax, %rdx
jae 0x1de95d
movq 0x8(%rsp), %rcx
subq %rdx, %rcx
addq %rax, %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%r8), %rax
movq 0x50(%rsp), %rcx
movl %ecx, 0x4(%rax,%r14,8)
incq 0x58(%rsp)
testq %r15, %r15
pushq $0x1
popq %rax
movq 0x28(%rsp), %rcx
cmovneq %rax, %rcx
movq %rcx, 0x28(%rsp)
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %r10
jmp 0x1de96e
movq 0x68(%rsp), %rbx
incq %rbp
jmp 0x1de6f4
incq %r10
jmp 0x1de6c8
movq 0x8(%rsp), %rax
cmpq 0x20(%rsp), %rax
jbe 0x1deb7b
cmpq $0x0, 0x28(%rsp)
je 0x1deb7b
movq (%r8), %rax
movq 0x10(%r8), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1a3def
pushq $0x1
popq %rax
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %r8
movq $0x0, 0x8(%rsp)
xorl %ecx, %ecx
cmpq 0x58(%rsp), %rax
ja 0x1deb7b
movq 0x10(%r8), %rdx
jne 0x1de9d9
movq (%rdx,%rcx,8), %rsi
jmp 0x1de9ee
movq (%rdx,%rcx,8), %rsi
movq (%rdx,%rax,8), %rdx
xorq %rsi, %rdx
shrq $0x20, %rdx
je 0x1deb73
shrq $0x20, %rsi
movq 0x268(%rdi), %rcx
movl (%rcx,%rsi,4), %r8d
movl 0x4(%rcx,%rsi,4), %r9d
subl %r8d, %r9d
je 0x1dec55
shlq $0x3, %r8
addq 0x270(%rdi), %r8
xorl %r11d, %r11d
xorl %r10d, %r10d
xorl %ecx, %ecx
xorl %edx, %edx
xorl %r13d, %r13d
cmpq %r11, %r9
je 0x1deab1
movq (%r8,%r11,8), %r14
movq %r14, %rbx
shrq $0x20, %rbx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movq 0x70(%rsp), %rdi
movq 0x18(%rdi), %r15
movq 0x20(%r15), %r15
cmpl $0x0, (%r15,%rbx,8)
js 0x1deaa9
movl %r14d, %r15d
imulq $0x18, %rbx, %rbx
shlq $0x4, %r15
movq 0x18(%rsp), %rdi
movq 0x260(%rdi), %r12
movq 0x10(%r12,%rbx), %rbx
movl (%rbx,%r15), %ebp
movl 0xc(%rbx,%r15), %ebx
testq %r14, %r14
js 0x1dea8a
testb $0x1, %bpl
jne 0x1dea99
cmpq %rbx, %rcx
cmovbeq %rbx, %rcx
jmp 0x1deaa9
testb $0x1, %bpl
jne 0x1deaa2
cmpq %rbx, %r13
cmovbeq %rbx, %r13
jmp 0x1deaa9
cmpq %rbx, %r10
cmovbeq %rbx, %r10
jmp 0x1deaa9
cmpq %rbx, %rdx
cmovbeq %rbx, %rdx
incq %r11
jmp 0x1dea23
movq %r13, %r9
addq %rdx, %r9
je 0x1dec36
leaq (%rcx,%r10), %r11
movq 0x60(%rsp), %rdi
movq 0x10(%rdi), %r8
imulq $0x28, %rsi, %rsi
movl 0x10(%r8,%rsi), %esi
movq %rsi, %rbx
subq %r10, %rbx
cmpq %rsi, %r11
cmovaeq %rsi, %rcx
movl $0x0, %edi
cmovaeq %rdi, %rbx
cmpq %rcx, %rbx
jbe 0x1deb43
cmpq %rsi, %r9
movq 0x18(%rsp), %rdi
jae 0x1deb4f
testq %r13, %r13
movq 0x10(%rsp), %r8
je 0x1deb1e
cmpq %r13, %rbx
cmovbq %rbx, %r13
cmpq %r13, %rcx
jae 0x1deb1e
movq 0x8(%rsp), %r9
subq %rcx, %r9
addq %r13, %r9
movq %r9, 0x8(%rsp)
movq %rsi, %r9
subq %rdx, %r9
cmpq %rsi, %r9
jae 0x1deb64
cmpq %r9, %rcx
cmovaq %rcx, %r9
cmpq %rsi, %rbx
cmovbq %rbx, %rsi
subq %r9, %rsi
jbe 0x1deb64
addq %rsi, 0x8(%rsp)
jmp 0x1deb64
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %rdi
jmp 0x1deb64
movq 0x8(%rsp), %rdx
subq %rcx, %rdx
addq %rbx, %rdx
movq %rdx, 0x8(%rsp)
movq 0x10(%rsp), %r8
movq %rax, %rcx
movq 0x8(%rsp), %rdx
cmpq 0x20(%rsp), %rdx
ja 0x1deb7b
incq %rax
jmp 0x1de9c2
xorl %eax, %eax
cmpq (%r8), %rax
jae 0x1debac
movq 0x270(%rdi), %rcx
movq 0x10(%r8), %rdx
movl (%rdx,%rax,8), %esi
movq (%rcx,%rsi,8), %r9
testq %r9, %r9
jns 0x1dec17
addq %r9, %r9
movq %r9, (%rcx,%rsi,8)
movl (%rdx,%rax,8), %edx
shrq (%rcx,%rdx,8)
incq %rax
jmp 0x1deb7d
movq 0x78(%rsp), %rax
testq %rax, %rax
movq 0x8(%rsp), %rcx
je 0x1debbd
movl %ecx, (%rax)
xorl %eax, %eax
cmpq 0x20(%rsp), %rcx
setbe %al
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d949(%rip), %rdi     # 0x1fc529
leaq 0x188f2(%rip), %rsi     # 0x1f74d9
leaq 0x1d8d7(%rip), %rcx     # 0x1fc4c5
movl $0x2157, %edx           # imm = 0x2157
callq 0x2110
leaq 0x1d8bc(%rip), %rdi     # 0x1fc4bb
leaq 0x188d3(%rip), %rsi     # 0x1f74d9
leaq 0x1d8b8(%rip), %rcx     # 0x1fc4c5
movl $0x213f, %edx           # imm = 0x213F
callq 0x2110
leaq 0x1d936(%rip), %rdi     # 0x1fc554
leaq 0x188b4(%rip), %rsi     # 0x1f74d9
leaq 0x1d899(%rip), %rcx     # 0x1fc4c5
movl $0x21a2, %edx           # imm = 0x21A2
callq 0x2110
leaq 0x1d8fd(%rip), %rdi     # 0x1fc53a
leaq 0x18895(%rip), %rsi     # 0x1f74d9
leaq 0x1d87a(%rip), %rcx     # 0x1fc4c5
movl $0x2185, %edx           # imm = 0x2185
callq 0x2110
leaq 0x1d85f(%rip), %rdi     # 0x1fc4bb
leaq 0x18876(%rip), %rsi     # 0x1f74d9
leaq 0x1d85b(%rip), %rcx     # 0x1fc4c5
movl $0x2173, %edx           # imm = 0x2173
callq 0x2110