// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: all_to_all_deduplicate(ma_ug_t*, asg_t*, ma_sub_t*, ma_hit_t_alloc*, unsigned char, float, ma_hit_t_alloc*, R_to_U*, unsigned char*, float, int)
void all_to_all_deduplicate(ma_ug_t* ug, asg_t* read_g, ma_sub_t* coverage_cut, 
ma_hit_t_alloc* sources, uint8_t postive_flag, float drop_rate, 
ma_hit_t_alloc* reverse_sources, R_to_U* ruIndex, uint8_t* is_r_het, float double_check_rate, int non_tig_occ)
{
    

    kvec_t_u64_warp u_vecs;
    kv_init(u_vecs.a);
    uint32_t n_vtx, v, k, is_Unitig, is_tig, uId, rId, convex, flag_occ, non_flag_occ, ambigious, /**is_ambigious,**/ flag, n_reduce = 1;
    asg_t* nsg = NULL;
    ma_utg_t* nsu = NULL;
    nsg = ug->g;
    if(postive_flag == FATHER) flag = MOTHER;
    if(postive_flag == MOTHER) flag = FATHER;
    uint8_t* primary_flag = (uint8_t*)calloc(read_g->n_seq, sizeof(uint8_t));
     
    for (v = 0; v < nsg->n_seq; v++)
    {
        uId = v;
        nsu = &(ug->u.a[uId]);
        if(nsu->m == 0) continue;
        if(nsg->seq[v].del) continue;
        if(nsg->seq[v].c==ALTER_LABLE) continue;

        for (k = 0; k < nsu->n; k++)
        {
            rId = nsu->a[k]>>33;
            set_R_to_U(ruIndex, rId, uId, 1, &(read_g->seq[rId].c));
        }
    }

    n_reduce = 1;
    while (n_reduce)
    {
        n_reduce = 0;
        n_vtx = nsg->n_seq*2;
        for (v = 0; v < n_vtx; ++v) 
        {
            
            uId = v>>1;
            nsu = &(ug->u.a[uId]);
            if(nsu->m == 0) continue;
            if(nsg->seq[uId].del) continue;
            if(nsg->seq[uId].c == ALTER_LABLE) continue;
            is_tig = 1;

            if(get_real_length(nsg, v^1, NULL) == 1)
            {
                get_real_length(nsg, v^1, &convex);
                if(get_real_length(nsg, convex^1, NULL) == 1)
                {
                    is_tig = 0;
                    ///continue;
                } 
            }

            get_unitig_trio_flag(nsu, flag, &flag_occ, &non_flag_occ, &ambigious);
            /**
            is_ambigious = 0;
            if((flag_occ+non_flag_occ)==0 && ambigious > 0)
            {
                is_ambigious = 1;
            }
            else**/
            {
                ///we may need it or not
                if(is_tig == 0 && (int)flag_occ <= non_tig_occ) continue;
                if(flag_occ <= ((non_flag_occ+flag_occ)*drop_rate)) continue;
                if((flag_occ+non_flag_occ) == 0) continue;
                if(nsu->n >= 100)
                {
                    if(flag_occ < nsu->n*double_check_rate)
                    {
                        continue;
                    }
                }
                else if(nsu->n >= 50)
                {
                    if(flag_occ < nsu->n*double_check_rate*0.5)
                    {
                        continue;
                    }
                }
                else
                {
                    if(flag_occ < nsu->n*double_check_rate*0.25)
                    {
                        continue;
                    }
                }
            }
            

            get_candidate_uids(nsg, nsu, &u_vecs, reverse_sources, ruIndex);


            if(u_vecs.a.n == 0) continue;

            for (k = 0; k < u_vecs.a.n; k++)
            {
                /**
                if(is_ambigious)
                {
                    get_unitig_trio_flag(&(ug->u.a[(uint32_t)(u_vecs.a.a[k])]), postive_flag, 
                    &flag_occ, &non_flag_occ, &ambigious);
                    if(flag_occ <= ((non_flag_occ+flag_occ)*drop_rate)) continue;
                    if((flag_occ+non_flag_occ) == 0) continue;
                }**/
                if(unitig_simi(uId, (uint32_t)(u_vecs.a.a[k]), ug, reverse_sources, ruIndex, is_r_het)==PLOID)
                {
                    break;
                }
            }

            if(k != u_vecs.a.n)
            {
                if(if_primary_unitig(nsu, read_g, coverage_cut, sources, ruIndex, primary_flag))
                {
                    continue;
                }
                if(nsu->m != 0)
                {
                    nsu->circ = nsu->end = nsu->len = nsu->m = nsu->n = nsu->start = 0;
                    free(nsu->a);
                    nsu->a = NULL;
                }
                asg_seq_del(nsg, uId);
                n_reduce++;
            }
        }
    }

    for (v = 0; v < ruIndex->len; v++)
    {
        get_R_to_U(ruIndex, v, &uId, &is_Unitig);
        if(is_Unitig == 1) ruIndex->index[v] = (uint32_t)-1;
    }

    kv_destroy(u_vecs.a);
    free(primary_flag);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movss %xmm1, 0x20(%rsp)
movq %r9, 0x48(%rsp)
movss %xmm0, 0x38(%rsp)
movq %rcx, 0x60(%rsp)
movq %rdx, 0x58(%rsp)
movq %rsi, %rbp
movq %rdi, %r15
movl 0xf0(%rsp), %eax
movl %eax, 0x30(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0xe0(%rsp), %r12
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movq $0x0, 0x90(%rsp)
movq 0x18(%rdi), %rbx
xorl %eax, %eax
cmpl $0x2, %r8d
setne %al
movl %eax, 0x8(%rsp)
movl 0x14(%rsi), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x1, %esi
callq 0x2120
movq %rax, 0x40(%rsp)
movl 0x14(%rbx), %eax
testl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %r15, 0x10(%rsp)
je 0xd47e5
xorl %r13d, %r13d
movq 0x10(%r15), %rcx
leaq (,%r13,4), %rdx
addq %r13, %rdx
cmpl $0x0, 0xc(%rcx,%rdx,8)
je 0xd47d1
movq 0x20(%rbx), %rsi
cmpl $0x0, (%rsi,%r13,8)
js 0xd47d1
cmpb $0x1, 0x4(%rsi,%r13,8)
je 0xd47d1
leaq (%rcx,%rdx,8), %r14
cmpl $0x0, 0x10(%r14)
je 0xd47d1
xorl %r15d, %r15d
movq 0x18(%r14), %rax
movq (%rax,%r15,8), %rsi
shrq $0x21, %rsi
movq 0x20(%rbp), %rax
leaq (%rax,%rsi,8), %r8
addq $0x4, %r8
movq %r12, %rdi
movl %r13d, %edx
movl $0x1, %ecx
callq 0xa27e9
incq %r15
movl 0x10(%r14), %eax
cmpq %rax, %r15
jb 0xd4795
movl 0x14(%rbx), %eax
movq 0x10(%rsp), %r15
incq %r13
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %r13
jb 0xd475e
movq %rbp, 0x50(%rsp)
movq %r12, 0x28(%rsp)
incl 0x8(%rsp)
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl 0x14(%rbx), %edi
addl %edi, %edi
je 0xd4bd5
xorl %r12d, %r12d
movl $0x0, 0x1c(%rsp)
movl %edi, 0x4(%rsp)
movl %r12d, %ebp
shrl %ebp
movq 0x10(%r15), %rax
leaq (,%rbp,4), %rcx
addq %rbp, %rcx
cmpl $0x0, 0xc(%rax,%rcx,8)
je 0xd4bbe
movq 0x20(%rbx), %rdx
cmpl $0x0, (%rdx,%rbp,8)
js 0xd4bbe
cmpb $0x1, 0x4(%rdx,%rbp,8)
je 0xd4bbe
leaq (%rax,%rcx,8), %rdi
movl %r12d, %ecx
xorl $0x1, %ecx
movq 0x28(%rbx), %rax
movq (%rax,%rcx,8), %rdx
movq %rdx, %rsi
andq %r14, %rsi
je 0xd491f
movq 0x8(%rbx), %rcx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %rdx
addq $0xc, %rdx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %r10d, %r10d
xorl %r8d, %r8d
movl (%rdx,%r10), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %r8d
addq $0x18, %r10
cmpq %r10, %rsi
jne 0xd488b
cmpl $0x1, %r8d
jne 0xd491f
xorl %r9d, %r9d
movl 0x34(%rsp), %r8d
cmpl $0x0, (%rdx,%r9)
js 0xd48bc
movl -0x4(%rdx,%r9), %r8d
addq $0x18, %r9
cmpq %r9, %rsi
jne 0xd48b0
movl %r8d, %edx
xorl $0x1, %edx
movq (%rax,%rdx,8), %rax
movq %rax, %rdx
andq %r14, %rdx
movl %r8d, 0x34(%rsp)
je 0xd49bd
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax,8), %rax
addq $0xc, %rax
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rcx
xorl %edx, %edx
xorl %esi, %esi
movl (%rax,%rdx), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %esi
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0xd48fc
cmpl $0x1, %esi
setne %r14b
jmp 0xd49c0
movq %rdi, %r13
movl 0x8(%rsp), %esi
leaq 0xc(%rsp), %rdx
leaq 0x24(%rsp), %rcx
leaq 0x3c(%rsp), %r8
callq 0xd0648
movl 0xc(%rsp), %eax
movl 0x24(%rsp), %ecx
addl %eax, %ecx
movl 0x4(%rsp), %edi
je 0xd4bbe
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %rcx, %xmm1
mulss 0x38(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jae 0xd4bbe
movq %r13, %rsi
movl 0x10(%r13), %ecx
cmpl $0x64, %ecx
jb 0xd4995
movl %ecx, %eax
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
mulss 0x20(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0xd4a0c
jmp 0xd4bbe
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
mulss 0x20(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
cmpl $0x32, %ecx
jb 0xd49fa
mulsd 0x15cc35(%rip), %xmm1   # 0x2315f0
jmp 0xd4a02
movb $0x1, %r14b
movq %rdi, %r13
movl 0x8(%rsp), %esi
leaq 0xc(%rsp), %rdx
leaq 0x24(%rsp), %rcx
leaq 0x3c(%rsp), %r8
callq 0xd0648
movl 0xc(%rsp), %eax
cmpl 0x30(%rsp), %eax
setg %cl
orb %r14b, %cl
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jne 0xd493e
jmp 0xd4bba
mulsd 0x15cbce(%rip), %xmm1   # 0x2315d0
ucomisd %xmm0, %xmm1
ja 0xd4bbe
movq %rbx, %rdi
movq %rsi, 0x68(%rsp)
leaq 0x80(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq 0x28(%rsp), %r8
callq 0xd5da3
movq 0x80(%rsp), %r15
testq %r15, %r15
je 0xd4a96
movl $0x1, %r13d
movq 0x90(%rsp), %rax
movq %rax, 0x78(%rsp)
xorl %r14d, %r14d
movq 0x78(%rsp), %rax
movl (%rax,%r14,8), %esi
movl %ebp, %edi
movq 0x10(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq 0x28(%rsp), %r8
movq 0x70(%rsp), %r9
callq 0xd5f76
testl %eax, %eax
je 0xd4a81
movl %r13d, %r14d
incl %r13d
cmpq %r14, %r15
ja 0xd4a4e
cmpq %r14, %r15
jne 0xd4aa0
movq 0x10(%rsp), %r15
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0xd4bba
movq 0x10(%rsp), %r15
jmp 0xd4bba
movq 0x68(%rsp), %r13
movq %r13, %rdi
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
movq 0x60(%rsp), %rcx
movq 0x28(%rsp), %r8
movq 0x40(%rsp), %r9
callq 0xcffc4
testb %al, %al
movq 0x10(%rsp), %r15
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jne 0xd4bba
cmpl $0x0, 0xc(%r13)
je 0xd4b01
movl $0x0, 0x10(%r13)
xorps %xmm0, %xmm0
movups %xmm0, (%r13)
movq 0x18(%r13), %rdi
callq 0x2220
movq $0x0, 0x18(%r13)
movq 0x20(%rbx), %rax
orl $0x80000000, (%rax,%rbp,8) # imm = 0x80000000
movl %r12d, %eax
andl $0x7ffffffe, %eax       # imm = 0x7FFFFFFE
movb $0x1, %cl
xorl %edx, %edx
orq %rax, %rdx
movq 0x28(%rbx), %rsi
movq (%rsi,%rdx,8), %rdi
movq %rdi, %rsi
andq %r14, %rsi
je 0xd4ba3
shrq $0x20, %rdi
leaq (%rdi,%rdi,2), %rdi
shlq $0x3, %rdi
addq 0x8(%rbx), %rdi
xorl %r8d, %r8d
leaq (%r8,%r8,2), %r9
orb $-0x80, 0xf(%rdi,%r9,8)
movl 0x8(%rdi,%r9,8), %r9d
xorq $0x1, %r9
movq 0x28(%rbx), %r10
movq (%r10,%r9,8), %r10
movq %r10, %r9
andq %r14, %r9
je 0xd4b9b
movq 0x8(%rbx), %r11
shrq $0x20, %r10
shlq $0x3, %r9
leaq (%r9,%r9,2), %r9
leaq (%r10,%r10,2), %r10
leaq (%r11,%r10,8), %r10
addq $0xc, %r10
xorl %r11d, %r11d
movl -0x4(%r10,%r11), %ebp
xorl %edx, %ebp
cmpl $0x1, %ebp
jne 0xd4b92
orb $-0x80, 0x3(%r10,%r11)
addq $0x18, %r11
cmpq %r11, %r9
jne 0xd4b80
incq %r8
cmpq %rsi, %r8
jne 0xd4b3e
movl $0x1, %edx
testb $0x1, %cl
movl $0x0, %ecx
jne 0xd4b18
incl 0x1c(%rsp)
movl 0x4(%rsp), %edi
incl %r12d
cmpl %edi, %r12d
jne 0xd4813
cmpl $0x0, 0x1c(%rsp)
jne 0xd47f9
movq 0x28(%rsp), %rcx
movq (%rcx), %rax
testq %rax, %rax
je 0xd4bff
movq 0x8(%rcx), %rcx
xorl %edx, %edx
cmpl $-0x2, (%rcx,%rdx,4)
jg 0xd4bf5
movl $0xffffffff, (%rcx,%rdx,4) # imm = 0xFFFFFFFF
incq %rdx
movl %edx, %esi
cmpq %rsi, %rax
ja 0xd4be8
movq 0x90(%rsp), %rdi
callq 0x2220
movq 0x40(%rsp), %rdi
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220