// File: /workspace/github_cpp_makefile_star2_O1/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 %rsi, 0x20(%rsp)
movq 0x230(%rdi), %r11
movq 0x18(%rdi), %rax
movq %rax, 0x60(%rsp)
movq $0x0, (%r8)
movq %rcx, 0x80(%rsp)
testq %rcx, %rcx
movq %r8, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
movq %r11, 0x70(%rsp)
movq %r9, 0x78(%rsp)
je 0x2215ea
xorl %r10d, %r10d
movq $0x0, 0x8(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movq %rdx, 0x38(%rsp)
movq 0x260(%rdi), %rax
movq (%rdx,%r10,8), %rcx
shrq %rcx
leaq (%rcx,%rcx,2), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x2215c4
leaq (%rax,%rcx,8), %rbx
xorl %ebp, %ebp
movq %r10, 0x40(%rsp)
movq %rbx, 0x68(%rsp)
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, 0x58(%rsp)
movl 0x4(%rax,%rcx,4), %r15d
subl %r13d, %r15d
je 0x22185e
movq %rsi, 0x88(%rsp)
shlq $0x3, %r13
addq 0x270(%rdi), %r13
movq (%r8), %rax
movq %rax, 0x90(%rsp)
cmpl $0x1, %r15d
adcl $0x0, %r15d
xorl %r12d, %r12d
movq $0x0, 0x50(%rsp)
movq $0x0, 0x48(%rsp)
xorl %ebx, %ebx
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 0x2214c3
testq %rax, %rax
js 0x22147a
movl %eax, %r9d
cmpq %rbp, %r9
jne 0x22147f
movq (%rdx,%r10,8), %rsi
shrq %rsi
cmpq %rsi, %rcx
jne 0x22147f
movq %rbp, %r14
btsq $0x3f, %rax
movq %rax, (%r13,%rbx,8)
movq 0x268(%rdi), %rax
movq 0x58(%rsp), %rcx
movl (%rax,%rcx,4), %ebp
movq 0x8(%r8), %rax
cmpq %rax, (%r8)
jne 0x221453
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r8)
movq 0x10(%r8), %rdi
shlq $0x3, %rsi
callq 0x2370
movq 0x40(%rsp), %r10
movq 0x38(%rsp), %rdx
movq 0x70(%rsp), %r11
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %r8
movq %rax, 0x10(%r8)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
addq %rbx, %rax
addq %rax, %rbp
movq (%r8), %rax
movq 0x10(%r8), %rcx
leaq 0x1(%rax), %rsi
movq %rsi, (%r8)
movq %rbp, (%rcx,%rax,8)
movq %r14, %rbp
jmp 0x2214c3
incq %r12
jmp 0x2214c3
movq 0x260(%rdi), %rax
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%rax,%rcx,8), %rcx
shlq $0x4, %r9
movl 0xc(%rcx,%r9), %eax
testb $0x1, (%rcx,%r9)
jne 0x2214b2
movq 0x50(%rsp), %rcx
cmpq %rax, %rcx
cmovbeq %rax, %rcx
movq %rcx, 0x50(%rsp)
jmp 0x2214c3
movq 0x48(%rsp), %rcx
cmpq %rax, %rcx
cmovbeq %rax, %rcx
movq %rcx, 0x48(%rsp)
incq %rbx
cmpq %rbx, %r15
jne 0x2213a7
movq 0x90(%rsp), %r14
leaq 0x1(%r14), %rax
cmpq %rax, (%r8)
jne 0x22187d
movq %r12, %r15
movq 0x50(%rsp), %r12
movq 0x48(%rsp), %r9
leaq (%r12,%r9), %rsi
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x58(%rsp), %rcx
leaq (%rcx,%rcx,4), %rcx
movl 0x10(%rax,%rcx,8), %eax
movq %rax, %rcx
subq %r9, %rcx
cmpq %rax, %rsi
cmovaeq %rax, %r12
movl $0x0, %esi
cmovaeq %rsi, %rcx
cmpq %r12, %rcx
jbe 0x2215b3
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 0x2215a7
movq 0x30(%rsp), %rcx
subq %rdx, %rcx
addq %rax, %rcx
movq %rcx, 0x30(%rsp)
movq 0x10(%r8), %rax
movq 0x58(%rsp), %rcx
movl %ecx, 0x4(%rax,%r14,8)
incq 0x8(%rsp)
testq %r15, %r15
movl $0x1, %eax
movq 0x28(%rsp), %rcx
cmovneq %rax, %rcx
movq %rcx, 0x28(%rsp)
movq 0x38(%rsp), %rdx
movq 0x40(%rsp), %r10
jmp 0x2215b8
movq 0x68(%rsp), %rbx
incq %rbp
cmpq (%rbx), %rbp
jb 0x22133c
incq %r10
cmpq 0x80(%rsp), %r10
jne 0x22130f
cmpq $0x0, 0x28(%rsp)
setne %al
movq 0x30(%rsp), %r11
movq 0x8(%rsp), %rcx
jmp 0x2215f1
xorl %r11d, %r11d
xorl %eax, %eax
xorl %ecx, %ecx
cmpq 0x20(%rsp), %r11
jbe 0x2217fb
testb %al, %al
je 0x2217fb
movq (%r8), %rax
movq %rcx, 0x8(%rsp)
movq 0x10(%r8), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1e3b6a
movq 0x8(%rsp), %rsi
testq %rsi, %rsi
je 0x2217f3
movl $0x1, %eax
xorl %ecx, %ecx
xorl %edx, %edx
xorl %r11d, %r11d
movq 0x10(%rsp), %r8
cmpq %rsi, %rax
je 0x221658
movq 0x10(%r8), %rsi
movq (%rsi,%rdx,8), %rdi
xorq (%rsi,%rax,8), %rdi
shrq $0x20, %rdi
movq 0x8(%rsp), %rsi
je 0x2217e5
movq 0x10(%r8), %rsi
movq (%rsi,%rdx,8), %r8
shrq $0x20, %r8
movq 0x18(%rsp), %rsi
movq 0x268(%rsi), %rdx
movl (%rdx,%r8,4), %r9d
movl 0x4(%rdx,%r8,4), %r10d
subl %r9d, %r10d
je 0x2218bb
movq %r11, %rbp
shlq $0x3, %r9
addq 0x270(%rsi), %r9
movq 0x70(%rsp), %rdx
movq 0x18(%rdx), %rdx
movq 0x20(%rdx), %rbx
cmpl $0x1, %r10d
adcl $0x0, %r10d
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %r11d, %r11d
xorl %r14d, %r14d
movq (%r9,%r14,8), %r13
movq %r13, %r12
shrq $0x20, %r12
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
cmpl $0x0, (%rbx,%r12,8)
js 0x221740
movl %r13d, %r15d
testq %r13, %r13
js 0x221701
movq 0x18(%rsp), %r13
movq 0x260(%r13), %r13
leaq (%r12,%r12,2), %r12
movq 0x10(%r13,%r12,8), %r13
shlq $0x4, %r15
movl 0xc(%r13,%r15), %r12d
testb $0x1, (%r13,%r15)
jne 0x221730
cmpq %r12, %rdx
cmovbeq %r12, %rdx
jmp 0x221740
movq 0x18(%rsp), %r13
movq 0x260(%r13), %r13
leaq (%r12,%r12,2), %r12
movq 0x10(%r13,%r12,8), %r13
shlq $0x4, %r15
movl 0xc(%r13,%r15), %r12d
testb $0x1, (%r13,%r15)
jne 0x221739
cmpq %r12, %rdi
cmovbeq %r12, %rdi
jmp 0x221740
cmpq %r12, %r11
cmovbeq %r12, %r11
jmp 0x221740
cmpq %r12, %rsi
cmovbeq %r12, %rsi
incq %r14
cmpq %r14, %r10
jne 0x2216b1
movq %rdi, %r10
addq %rsi, %r10
je 0x2218da
leaq (%rdx,%r11), %rbx
movq 0x60(%rsp), %r9
movq 0x10(%r9), %r9
leaq (%r8,%r8,4), %r8
movl 0x10(%r9,%r8,8), %r14d
movq %r14, %r9
subq %r11, %r9
cmpq %r14, %rbx
cmovaeq %r14, %rdx
cmovaeq %rcx, %r9
cmpq %rdx, %r9
movq %rbp, %r11
jbe 0x2217d1
cmpq %r14, %r10
jae 0x2217cb
testq %rdi, %rdi
movq 0x10(%rsp), %r8
je 0x2217a8
cmpq %rdi, %r9
cmovbq %r9, %rdi
cmpq %rdi, %rdx
jae 0x2217a8
subq %rdx, %r11
addq %rdi, %r11
movq %r14, %rdi
subq %rsi, %rdi
cmpq %r14, %rdi
jae 0x2217d6
cmpq %rdi, %rdx
cmovaq %rdx, %rdi
cmpq %r14, %r9
cmovbq %r9, %r14
subq %rdi, %r14
jbe 0x2217d6
addq %r14, %r11
jmp 0x2217d6
subq %rdx, %r11
addq %r9, %r11
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rsi
movq %rax, %rdx
cmpq 0x20(%rsp), %r11
ja 0x2217fb
incq %rax
cmpq %rsi, %rax
jbe 0x221638
jmp 0x2217fb
xorl %r11d, %r11d
movq 0x10(%rsp), %r8
cmpq $0x0, (%r8)
je 0x221835
movq 0x18(%rsp), %rax
movq 0x270(%rax), %rax
movq 0x10(%r8), %rcx
xorl %edx, %edx
movl (%rcx,%rdx,8), %esi
movq (%rax,%rsi,8), %rdi
testq %rdi, %rdi
jns 0x22189c
addq %rdi, %rdi
movq %rdi, (%rax,%rsi,8)
movl (%rcx,%rdx,8), %esi
shrq (%rax,%rsi,8)
incq %rdx
cmpq (%r8), %rdx
jb 0x221813
movq 0x78(%rsp), %rax
testq %rax, %rax
je 0x221842
movl %r11d, (%rax)
xorl %eax, %eax
cmpq 0x20(%rsp), %r11
setbe %al
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c9b3(%rip), %rdi     # 0x23e218
leaq 0x17ab9(%rip), %rsi     # 0x239325
leaq 0x1c9af(%rip), %rcx     # 0x23e222
movl $0x213f, %edx           # imm = 0x213F
callq 0x2110
leaq 0x1ca02(%rip), %rdi     # 0x23e286
leaq 0x17a9a(%rip), %rsi     # 0x239325
leaq 0x1c990(%rip), %rcx     # 0x23e222
movl $0x2157, %edx           # imm = 0x2157
callq 0x2110
leaq 0x1ca0e(%rip), %rdi     # 0x23e2b1
leaq 0x17a7b(%rip), %rsi     # 0x239325
leaq 0x1c971(%rip), %rcx     # 0x23e222
movl $0x21a2, %edx           # imm = 0x21A2
callq 0x2110
leaq 0x1c956(%rip), %rdi     # 0x23e218
leaq 0x17a5c(%rip), %rsi     # 0x239325
leaq 0x1c952(%rip), %rcx     # 0x23e222
movl $0x2173, %edx           # imm = 0x2173
callq 0x2110
leaq 0x1c9b6(%rip), %rdi     # 0x23e297
leaq 0x17a3d(%rip), %rsi     # 0x239325
leaq 0x1c933(%rip), %rcx     # 0x23e222
movl $0x2185, %edx           # imm = 0x2185
callq 0x2110