// File: /workspace/github_cpp_makefile_star2_O3/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, 0x30(%rsp)
movq 0x230(%rdi), %r11
movq 0x18(%rdi), %rax
movq %rax, 0x68(%rsp)
movq $0x0, (%r8)
movq %rcx, 0x80(%rsp)
testq %rcx, %rcx
je 0x22cecc
movq %r9, 0x60(%rsp)
xorl %r10d, %r10d
movq $0x0, 0x20(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x28(%rsp)
movq %r8, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
movq %r11, 0x78(%rsp)
movq %rdx, 0x40(%rsp)
movq 0x260(%rdi), %rax
movq (%rdx,%r10,8), %rcx
shrq %rcx
leaq (%rcx,%rcx,2), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x22ccb5
leaq (%rax,%rcx,8), %rbx
xorl %ebp, %ebp
movq %r10, 0x48(%rsp)
movq %rbx, 0x70(%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, 0x18(%rsp)
movl 0x4(%rax,%rcx,4), %r15d
subl %r13d, %r15d
je 0x22cf3a
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, 0x58(%rsp)
movq $0x0, 0x50(%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 0x22cbb4
testq %rax, %rax
js 0x22cb6b
movl %eax, %r9d
cmpq %rbp, %r9
jne 0x22cb70
movq (%rdx,%r10,8), %rsi
shrq %rsi
cmpq %rsi, %rcx
jne 0x22cb70
movq %rbp, %r14
btsq $0x3f, %rax
movq %rax, (%r13,%rbx,8)
movq 0x268(%rdi), %rax
movq 0x18(%rsp), %rcx
movl (%rax,%rcx,4), %ebp
movq (%r8), %rcx
movq 0x10(%r8), %rax
cmpq 0x8(%r8), %rcx
jne 0x22cb4b
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r8)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x48(%rsp), %r10
movq 0x40(%rsp), %rdx
movq 0x78(%rsp), %r11
movq 0x8(%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 0x22cbb4
incq %r12
jmp 0x22cbb4
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 0x22cba3
movq 0x58(%rsp), %rcx
cmpq %rax, %rcx
cmovbeq %rax, %rcx
movq %rcx, 0x58(%rsp)
jmp 0x22cbb4
movq 0x50(%rsp), %rcx
cmpq %rax, %rcx
cmovbeq %rax, %rcx
movq %rcx, 0x50(%rsp)
incq %rbx
cmpq %rbx, %r15
jne 0x22ca99
movq 0x90(%rsp), %r14
leaq 0x1(%r14), %rax
cmpq %rax, (%r8)
jne 0x22cf59
movq %r12, %r15
movq 0x58(%rsp), %r12
movq 0x50(%rsp), %r9
leaq (%r12,%r9), %rsi
movq 0x68(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x18(%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 0x22cca4
movq 0x70(%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 0x22cc98
movq 0x28(%rsp), %rcx
subq %rdx, %rcx
addq %rax, %rcx
movq %rcx, 0x28(%rsp)
movq 0x10(%r8), %rax
movq 0x18(%rsp), %rcx
movl %ecx, 0x4(%rax,%r14,8)
incq 0x20(%rsp)
testq %r15, %r15
movq 0x38(%rsp), %rax
movl $0x1, %ecx
cmovneq %rcx, %rax
movq %rax, 0x38(%rsp)
movq 0x40(%rsp), %rdx
movq 0x48(%rsp), %r10
jmp 0x22cca9
movq 0x70(%rsp), %rbx
incq %rbp
cmpq (%rbx), %rbp
jb 0x22ca2e
incq %r10
cmpq 0x80(%rsp), %r10
jne 0x22ca01
movq 0x28(%rsp), %r14
cmpq 0x30(%rsp), %r14
jbe 0x22cedc
cmpq $0x0, 0x38(%rsp)
je 0x22cedc
movq (%r8), %rax
movq 0x10(%r8), %rdi
leaq (%rdi,%rax,8), %rsi
movq %r8, %rbx
callq 0x1ee586
movq 0x20(%rsp), %r11
testq %r11, %r11
je 0x22ced1
movq 0x10(%rbx), %rax
movq %rax, 0x18(%rsp)
movl $0x1, %ecx
xorl %esi, %esi
xorl %r14d, %r14d
movq %rbx, %r8
movq 0x8(%rsp), %rdi
cmpq %r11, %rcx
jne 0x22cd2e
movq 0x18(%rsp), %rax
movq (%rax,%rsi,8), %rax
jmp 0x22cd48
movq 0x18(%rsp), %rdx
movq (%rdx,%rsi,8), %rax
movq (%rdx,%rcx,8), %rdx
xorq %rax, %rdx
shrq $0x20, %rdx
je 0x22cebe
movq %r14, 0x28(%rsp)
shrq $0x20, %rax
movq 0x268(%rdi), %rsi
movl (%rsi,%rax,4), %r10d
movl 0x4(%rsi,%rax,4), %r11d
subl %r10d, %r11d
je 0x22cf97
shlq $0x3, %r10
addq 0x270(%rdi), %r10
movq 0x78(%rsp), %rdx
movq 0x18(%rdx), %rsi
movq 0x20(%rsi), %r14
cmpl $0x1, %r11d
adcl $0x0, %r11d
xorl %r9d, %r9d
xorl %edi, %edi
xorl %esi, %esi
xorl %ebx, %ebx
xorl %r15d, %r15d
movq (%r10,%r15,8), %r13
movq %r13, %r12
shrq $0x20, %r12
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
cmpl $0x0, (%r14,%r12,8)
js 0x22ce07
movl %r13d, %edx
shlq $0x4, %rdx
movq 0x8(%rsp), %r8
movq 0x260(%r8), %rbp
leaq (%r12,%r12,2), %r12
movq 0x10(%rbp,%r12,8), %r12
movl (%r12,%rdx), %ebp
movl 0xc(%r12,%rdx), %r12d
testq %r13, %r13
js 0x22cde8
testb $0x1, %bpl
jne 0x22cdf7
cmpq %r12, %rsi
cmovbeq %r12, %rsi
jmp 0x22ce07
testb $0x1, %bpl
jne 0x22ce00
cmpq %r12, %r9
cmovbeq %r12, %r9
jmp 0x22ce07
cmpq %r12, %rbx
cmovbeq %r12, %rbx
jmp 0x22ce07
cmpq %r12, %rdi
cmovbeq %r12, %rdi
incq %r15
cmpq %r15, %r11
jne 0x22cd96
movq %r9, %r11
addq %rdi, %r11
je 0x22cfb6
leaq (%rsi,%rbx), %rdx
movq 0x68(%rsp), %r8
movq 0x10(%r8), %r10
leaq (%rax,%rax,4), %r8
movl 0x10(%r10,%r8,8), %r8d
movq %r8, %r10
subq %rbx, %r10
cmpq %r8, %rdx
cmovaeq %r8, %rsi
movl $0x0, %eax
cmovaeq %rax, %r10
cmpq %rsi, %r10
movq 0x28(%rsp), %r14
jbe 0x22cea5
cmpq %r8, %r11
jae 0x22ce9f
testq %r9, %r9
movq 0x20(%rsp), %r11
je 0x22ce72
cmpq %r9, %r10
cmovbq %r10, %r9
cmpq %r9, %rsi
jae 0x22ce72
subq %rsi, %r14
addq %r9, %r14
movq %r8, %r9
subq %rdi, %r9
cmpq %r8, %r9
jae 0x22ce93
cmpq %r9, %rsi
cmovaq %rsi, %r9
cmpq %r8, %r10
cmovbq %r10, %r8
subq %r9, %r8
jbe 0x22ce93
addq %r8, %r14
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rdi
jmp 0x22ceb4
subq %rsi, %r14
addq %r10, %r14
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %r11
movq %rcx, %rsi
cmpq 0x30(%rsp), %r14
ja 0x22cedc
incq %rcx
cmpq %r11, %rcx
jbe 0x22cd1e
jmp 0x22cedc
xorl %r14d, %r14d
jmp 0x22cf16
xorl %r14d, %r14d
movq %rbx, %r8
movq 0x8(%rsp), %rdi
cmpq $0x0, (%r8)
je 0x22cf11
movq 0x270(%rdi), %rax
movq 0x10(%r8), %rcx
xorl %edx, %edx
movl (%rcx,%rdx,8), %esi
movq (%rax,%rsi,8), %rdi
testq %rdi, %rdi
jns 0x22cf78
addq %rdi, %rdi
movq %rdi, (%rax,%rsi,8)
movl (%rcx,%rdx,8), %esi
shrq (%rax,%rsi,8)
incq %rdx
cmpq (%r8), %rdx
jb 0x22ceef
movq 0x60(%rsp), %r9
testq %r9, %r9
je 0x22cf1e
movl %r14d, (%r9)
xorl %eax, %eax
cmpq 0x30(%rsp), %r14
setbe %al
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d5bb(%rip), %rdi     # 0x24a4fc
leaq 0x186c1(%rip), %rsi     # 0x245609
leaq 0x1d5b7(%rip), %rcx     # 0x24a506
movl $0x213f, %edx           # imm = 0x213F
callq 0x2110
leaq 0x1d60a(%rip), %rdi     # 0x24a56a
leaq 0x186a2(%rip), %rsi     # 0x245609
leaq 0x1d598(%rip), %rcx     # 0x24a506
movl $0x2157, %edx           # imm = 0x2157
callq 0x2110
leaq 0x1d616(%rip), %rdi     # 0x24a595
leaq 0x18683(%rip), %rsi     # 0x245609
leaq 0x1d579(%rip), %rcx     # 0x24a506
movl $0x21a2, %edx           # imm = 0x21A2
callq 0x2110
leaq 0x1d55e(%rip), %rdi     # 0x24a4fc
leaq 0x18664(%rip), %rsi     # 0x245609
leaq 0x1d55a(%rip), %rcx     # 0x24a506
movl $0x2173, %edx           # imm = 0x2173
callq 0x2110
leaq 0x1d5be(%rip), %rdi     # 0x24a57b
leaq 0x18645(%rip), %rsi     # 0x245609
leaq 0x1d53b(%rip), %rcx     # 0x24a506
movl $0x2185, %edx           # imm = 0x2185
callq 0x2110