// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: dedup_src_shared1(ma_ug_t*, kv_ul_ov_t*, unsigned long, unsigned long, idx_emask_t*)
void dedup_src_shared1(ma_ug_t *ug, kv_ul_ov_t *res, uint64_t tocc, uint64_t qocc, idx_emask_t *mm)
{
    if((!tocc) || (!qocc)) return;
    uint32_t ti, qi, tn, qn, rn = res->n, id, ts, te, k, l, ff = 0;
    // qi = res->n - qocc; fi = 0; m = qi; qocc = 0;
    // while (qi < res->n && fi < flt_n) {
    //     if(res->a[qi].tn < (flt[fi]>>32)) {
    //         qi++;
    //     } else if((flt[fi]>>32) < res->a[qi].tn) {
    //         fi++;
    //     } else {///res->a[qi].tn == (flt[fi]>>32)
    //         res->a[m++] = res->a[qi]; qocc++; qi++; fi++;
    //     }
    // }
    // res->n = m;
    // if((!tocc) || (!qocc)) return;

    rn = res->n;
    ti = res->n - tocc - qocc; qi = res->n - qocc;//t first, and then q 
    tn = ti + tocc; qn = qi + qocc; kv_resize(ul_ov_t, *res, (rn+tn));///the buf size should be at least tn
    while (ti < tn && qi < qn) {
        if (res->a[ti].tn < res->a[qi].tn) {
            kv_push(ul_ov_t, *res, res->a[ti]); ti++; 
        } else if(res->a[qi].tn < res->a[ti].tn) {
            kv_push(ul_ov_t, *res, res->a[qi]); qi++; 
        } else {///ta[ti].tn == qa[qi].tn
            id = res->a[ti].tn; ts = ti;
            for (; ti < tn && res->a[ti].tn == id; ti++) {
                kv_push(ul_ov_t, *res, res->a[ti]);
            }
            te = ti;
            
            for (; qi < qn && res->a[qi].tn == id; qi++) {
                for (k = ts; k < te; k++) {
                    l = k; 
                    if(is_ul_ov_pe(res->a, te, k, (*mm))) k++;
                    if(is_cover_ul_ov_t(ug, 0.06, res->a+l, k+1-l, &(res->a[qi]))) {
                        ff = 1; break;
                    }
                }
                if(k >= te) kv_push(ul_ov_t, *res, res->a[qi]);
            }
        }
    }
    while (ti < tn) {
        kv_push(ul_ov_t, *res, res->a[ti]); ti++; 
    }
    while (qi < qn) {
        kv_push(ul_ov_t, *res, res->a[qi]); qi++; 
    }
    
    // fprintf(stderr, "[M::%s]\tff::%u\n", __func__, ff);
    if(!ff) {///nothing has been removed as duplication
        res->n = rn;
    } else if(res->n > rn) {
        l = res->n; res->n = rn - tocc - qocc;
        for (k = rn; k < l; k++) res->a[res->n++] = res->a[k];
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %r13
movq %rdi, 0x40(%rsp)
testq %rdx, %rdx
sete %al
testq %rcx, %rcx
sete %cl
orb %al, %cl
jne 0x421d0
movq %r8, %r14
movq %rsi, %rbx
movq %rdx, %rsi
movq 0x8(%rbx), %rcx
leaq (%rdx,%r13), %rax
movq %rcx, %r9
movq %rax, 0x38(%rsp)
subq %rax, %r9
addq %r9, %rsi
movq %rcx, 0x18(%rsp)
leal (%rsi,%rcx), %eax
cmpq %rax, 0x10(%rbx)
movq %rsi, 0x10(%rsp)
jae 0x41d6d
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
incq %rcx
movq %rcx, 0x10(%rbx)
movq (%rbx), %rdi
imulq $0x1c, %rcx, %rsi
movq %r9, %r15
callq 0x2370
movq %r15, %r9
movq 0x10(%rsp), %rsi
movq %rax, (%rbx)
movq %r14, 0x30(%rsp)
movq 0x18(%rsp), %rax
movl %eax, %r8d
subl %r13d, %r8d
movl %esi, %eax
movq %rax, 0x28(%rsp)
movl $0x0, 0xc(%rsp)
movl %r9d, %r12d
movl %r8d, %r14d
movl %r12d, %r12d
imulq $0x1c, %r12, %r15
imulq $0x1c, %r14, %rbp
movl %r8d, 0x8(%rsp)
cmpl %esi, %r12d
jae 0x420a6
cmpl 0x18(%rsp), %r8d
jae 0x420a6
movq (%rbx), %rax
movl 0xc(%rax,%r15), %r13d
movl 0xc(%rax,%rbp), %ecx
cmpl %ecx, %r13d
jae 0x41e2c
movq 0x8(%rbx), %rcx
cmpq 0x10(%rbx), %rcx
jne 0x41e00
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbx)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x8(%rsp), %r8d
movq 0x10(%rsp), %rsi
movq %rax, (%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
imulq $0x1c, %rcx, %rcx
movups (%rax,%r15), %xmm0
movups 0xc(%rax,%r15), %xmm1
movups %xmm1, 0xc(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
addq $0x1c, %r15
incq %r12
jmp 0x41da2
jbe 0x41e8d
movq 0x8(%rbx), %rcx
cmpq 0x10(%rbx), %rcx
jne 0x41e67
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbx)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x8(%rsp), %r8d
movq 0x10(%rsp), %rsi
movq %rax, (%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
imulq $0x1c, %rcx, %rcx
movups (%rax,%rbp), %xmm0
movups 0xc(%rax,%rbp), %xmm1
movups %xmm1, 0xc(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
incl %r8d
jmp 0x41d8f
movq %r12, %r9
cmpq %r9, 0x28(%rsp)
je 0x41f09
movq (%rbx), %rax
cmpl %r13d, 0xc(%rax,%r15)
jne 0x41f0c
movq 0x8(%rbx), %rcx
cmpq 0x10(%rbx), %rcx
jne 0x41ee0
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbx)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
movq %r9, %r14
callq 0x2370
movq %r14, %r9
movl 0x8(%rsp), %r8d
movq 0x10(%rsp), %rsi
movq %rax, (%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
imulq $0x1c, %rcx, %rcx
movups (%rax,%r15), %xmm0
movups 0xc(%rax,%r15), %xmm1
movups %xmm1, 0xc(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
incq %r9
addq $0x1c, %r15
jmp 0x41e90
movl %esi, %r9d
movq %r9, 0x20(%rsp)
cmpl 0x18(%rsp), %r8d
jae 0x41d8c
movq (%rbx), %rax
movl %r8d, %ecx
imulq $0x1c, %rcx, %rcx
cmpl %r13d, 0xc(%rax,%rcx)
jne 0x41d8c
movq %rcx, %rbp
movl %r8d, 0x8(%rsp)
movl %r12d, %edi
cmpl %r9d, %edi
jae 0x42030
leal 0x1(%rdi), %r14d
movq (%rbx), %rcx
movl %edi, %eax
cmpl %r9d, %r14d
jae 0x41fcb
movl %r14d, %edx
imulq $0x1c, %rdx, %rdx
movl (%rcx,%rdx), %esi
imulq $0x1c, %rax, %r8
cmpl (%rcx,%r8), %esi
jne 0x41fcb
addq %rcx, %rdx
addq %rcx, %r8
movl 0xc(%rdx), %r9d
cmpl 0xc(%r8), %r9d
jne 0x41fcb
movl 0x18(%rdx), %edx
movl 0x18(%r8), %r8d
movl %r8d, %r9d
xorl %edx, %r9d
js 0x41fcb
movl $0x3fffffff, %r9d       # imm = 0x3FFFFFFF
andq %r9, %rdx
andq %r9, %r8
leal 0x1(%r8), %r9d
cmpl %r9d, %edx
jne 0x41fcb
movq 0x30(%rsp), %r9
movq (%r9), %r9
imulq $0x18, %rsi, %rsi
movq 0x8(%r9,%rsi), %r10
cmpq %r8, %r10
jbe 0x41fcb
cmpq %rdx, %r10
jbe 0x41fcb
addq %rsi, %r9
movq (%r9), %r9
shlq $0x5, %r8
movb 0x1c(%r9,%r8), %sil
testb $0x20, %sil
jne 0x42002
movl %edi, %r15d
imulq $0x1c, %rax, %rsi
addq %rcx, %rsi
movl %r14d, %edx
subl %edi, %edx
addq %rbp, %rcx
movq 0x40(%rsp), %rdi
movsd 0x1a15ce(%rip), %xmm0   # 0x1e35b8
callq 0x4190b
movl %r14d, %edi
testq %rax, %rax
movq 0x20(%rsp), %r9
je 0x41f3c
jmp 0x42026
shlq $0x5, %rdx
movb 0x1c(%r9,%rdx), %dl
andb $0x1f, %sil
andb $0x3f, %dl
xorb $0x21, %dl
orb %sil, %dl
je 0x4201d
movl %edi, %r14d
movl %r14d, %r15d
leal 0x1(%r15), %r14d
jmp 0x41fce
movl %r15d, %edi
pushq $0x1
popq %rax
movl %eax, 0xc(%rsp)
movl 0x8(%rsp), %r8d
cmpl %r9d, %edi
movq 0x10(%rsp), %rsi
jb 0x4209e
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
cmpq 0x10(%rbx), %rcx
jne 0x42080
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbx)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x20(%rsp), %r9
movl 0x8(%rsp), %r8d
movq 0x10(%rsp), %rsi
movq %rax, (%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
imulq $0x1c, %rcx, %rcx
movups (%rax,%rbp), %xmm0
movups 0xc(%rax,%rbp), %xmm1
movups %xmm1, 0xc(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
incl %r8d
jmp 0x41f11
movl 0xc(%rsp), %ebp
cmpq 0x28(%rsp), %r12
jae 0x4210c
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
cmpq 0x10(%rbx), %rcx
jne 0x420e3
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbx)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
imulq $0x1c, %rcx, %rcx
movups (%rax,%r15), %xmm0
movups 0xc(%rax,%r15), %xmm1
movups %xmm1, 0xc(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
incq %r12
addq $0x1c, %r15
jmp 0x420aa
movl 0x18(%rsp), %r15d
imulq $0x1c, %r14, %r12
cmpq %r15, %r14
jae 0x42175
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
cmpq 0x10(%rbx), %rcx
jne 0x4214c
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbx)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
imulq $0x1c, %rcx, %rcx
movups (%rax,%r12), %xmm0
movups 0xc(%rax,%r12), %xmm1
movups %xmm1, 0xc(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
incq %r14
addq $0x1c, %r12
jmp 0x42115
testl %ebp, %ebp
je 0x421c8
movq 0x8(%rbx), %rax
movl 0x18(%rsp), %ecx
cmpq %rcx, %rax
jbe 0x421d0
subq 0x38(%rsp), %rcx
movq %rcx, 0x8(%rbx)
movl %eax, %eax
imulq $0x1c, %r15, %rcx
cmpq %rax, %r15
jae 0x421d0
movq (%rbx), %rdx
movq 0x8(%rbx), %rsi
leaq 0x1(%rsi), %rdi
movq %rdi, 0x8(%rbx)
imulq $0x1c, %rsi, %rsi
movups (%rdx,%rcx), %xmm0
movups 0xc(%rdx,%rcx), %xmm1
movups %xmm1, 0xc(%rdx,%rsi)
movups %xmm0, (%rdx,%rsi)
incq %r15
addq $0x1c, %rcx
jmp 0x42195
movl 0x18(%rsp), %eax
movq %rax, 0x8(%rbx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq