// File: /workspace/github_cpp_makefile_star2_O2/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 $0x88, %rsp
movss %xmm1, 0x20(%rsp)
movq %r9, 0x40(%rsp)
movss %xmm0, 0x2c(%rsp)
movl %r8d, %ebp
movq %rcx, 0x58(%rsp)
movq %rdx, 0x50(%rsp)
movq %rdi, %rbx
movq 0xc0(%rsp), %r12
xorps %xmm0, %xmm0
andq $0x0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movq 0x18(%rdi), %r13
movq %rsi, 0x48(%rsp)
movl 0x14(%rsi), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
pushq $0x1
popq %rsi
callq 0x2120
movq %rax, 0x38(%rsp)
xorl %r15d, %r15d
pushq $0x1
popq %rax
movl %eax, 0x18(%rsp)
movq %rbx, 0x10(%rsp)
movq %r13, 0x8(%rsp)
movl 0x14(%r13), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r15
jae 0x97ded
movq 0x10(%rbx), %rbx
imulq $0x28, %r15, %rax
cmpl $0x0, 0xc(%rbx,%rax)
je 0x97da0
movq 0x20(%r13), %rcx
cmpl $0x0, (%rcx,%r15,8)
js 0x97da0
cmpb $0x1, 0x4(%rcx,%r15,8)
jne 0x97daa
incq %r15
movq 0x10(%rsp), %rbx
jmp 0x97d70
addq %rax, %rbx
xorl %r14d, %r14d
movl 0x10(%rbx), %eax
cmpq %rax, %r14
jae 0x97da0
movq 0x18(%rbx), %rax
movq (%rax,%r14,8), %rsi
shrq $0x21, %rsi
movq 0x48(%rsp), %rax
movq 0x20(%rax), %rax
leaq (%rax,%rsi,8), %r8
addq $0x4, %r8
movq %r12, %rdi
movl %r15d, %edx
pushq $0x1
popq %rcx
callq 0x6ab56
movq 0x8(%rsp), %r13
incq %r14
jmp 0x97db0
xorl %eax, %eax
cmpb $0x2, %bpl
setne %al
incl %eax
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x18(%rsp)
je 0x98057
movl 0x14(%r13), %edx
addl %edx, %edx
movl $0x0, 0x18(%rsp)
xorl %r14d, %r14d
movl %edx, 0x4(%rsp)
cmpl %edx, %r14d
je 0x97dfc
movl %r14d, %r15d
shrl %r15d
movq 0x10(%rbx), %rbp
imulq $0x28, %r15, %rax
cmpl $0x0, 0xc(%rbp,%rax)
je 0x97fea
movq 0x20(%r13), %rcx
cmpl $0x0, (%rcx,%r15,8)
js 0x97fea
cmpb $0x1, 0x4(%rcx,%r15,8)
je 0x97fea
addq %rax, %rbp
movl %r14d, %ebx
xorl $0x1, %ebx
movq %r13, %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x1, %eax
jne 0x97ec7
movq %r13, %rdi
movl %ebx, %esi
leaq 0x34(%rsp), %rdx
callq 0xc2e7a
movl 0x34(%rsp), %esi
xorl $0x1, %esi
movq %r13, %rdi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x1, %eax
setne %bl
movq %rbp, %rdi
movl 0x24(%rsp), %esi
leaq 0x1c(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x30(%rsp), %r8
callq 0x94ad6
movl 0x1c(%rsp), %eax
cmpl 0xd0(%rsp), %eax
setg %cl
orb %bl, %cl
jne 0x97ee6
jmp 0x97fdc
movq %rbp, %rdi
movl 0x24(%rsp), %esi
leaq 0x1c(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x30(%rsp), %r8
callq 0x94ad6
movl 0x1c(%rsp), %eax
movl 0x28(%rsp), %ecx
addl %eax, %ecx
movq 0x10(%rsp), %rbx
movl 0x4(%rsp), %edx
je 0x97f36
movl %eax, %eax
cvtsi2ss %rax, %xmm0
cvtsi2ss %rcx, %xmm1
mulss 0x2c(%rsp), %xmm1
ucomiss %xmm0, %xmm1
movq 0x8(%rsp), %r13
jae 0x97fea
movl 0x10(%rbp), %ecx
cmpl $0x64, %ecx
jb 0x97f40
movl %ecx, %eax
cvtsi2ss %rax, %xmm1
mulss 0x20(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x97f70
jmp 0x97fea
movq 0x8(%rsp), %r13
jmp 0x97fea
cvtsi2sd %rax, %xmm0
cvtsi2ss %ecx, %xmm1
mulss 0x20(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
cmpl $0x32, %ecx
jb 0x97f62
mulsd 0x14b5e8(%rip), %xmm1   # 0x1e3548
jmp 0x97f6a
mulsd 0x14b5b6(%rip), %xmm1   # 0x1e3520
ucomisd %xmm0, %xmm1
ja 0x97fea
movq %r13, %rdi
movq %rbp, %rsi
leaq 0x60(%rsp), %rdx
movq 0x40(%rsp), %rcx
movq %r12, %r8
callq 0x98f91
movq 0x60(%rsp), %rbx
testq %rbx, %rbx
je 0x97fdc
xorl %ecx, %ecx
movq 0x70(%rsp), %r12
movl %ecx, %r13d
cmpq %r13, %rbx
jbe 0x97fcf
movl (%r12,%r13,8), %esi
movl %r15d, %edi
movq 0x10(%rsp), %rdx
movq 0x40(%rsp), %rcx
movq 0xc0(%rsp), %r8
movq 0xc8(%rsp), %r9
callq 0x99136
leal 0x1(%r13), %ecx
testl %eax, %eax
jne 0x97f99
cmpq %r13, %rbx
jne 0x97ff2
movq 0xc0(%rsp), %r12
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r13
movl 0x4(%rsp), %edx
incl %r14d
jmp 0x97e1c
movq %rbp, %rdi
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq 0x58(%rsp), %rcx
movq 0xc0(%rsp), %r12
movq %r12, %r8
movq 0x38(%rsp), %r9
callq 0x945a4
testb %al, %al
movq 0x10(%rsp), %rbx
jne 0x97fe1
cmpl $0x0, 0xc(%rbp)
je 0x98041
andl $0x0, 0x10(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, (%rbp)
movq 0x18(%rbp), %rdi
callq 0x2220
andq $0x0, 0x18(%rbp)
movq 0x8(%rsp), %r13
movq %r13, %rdi
movl %r15d, %esi
callq 0x6f8da
incl 0x18(%rsp)
jmp 0x97fe6
movq (%r12), %rax
xorl %ecx, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0x98077
movq 0x8(%r12), %rdx
cmpl $-0x2, (%rdx,%rcx,4)
jg 0x98073
orl $-0x1, (%rdx,%rcx,4)
incl %ecx
jmp 0x9805d
movq 0x70(%rsp), %rdi
callq 0x2220
movq 0x38(%rsp), %rdi
callq 0x2220
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq