// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movb %r8b, %al
movl 0x20(%rbp), %r8d
movq 0x18(%rbp), %r8
movq 0x10(%rbp), %r8
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movb %al, -0x21(%rbp)
movss %xmm0, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movss %xmm1, -0x34(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x1, -0x8c(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0xa0(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x98(%rbp)
movzbl -0x21(%rbp), %eax
cmpl $0x1, %eax
jne 0x11da75
movl $0x2, -0x88(%rbp)
movzbl -0x21(%rbp), %eax
cmpl $0x2, %eax
jne 0x11da88
movl $0x1, -0x88(%rbp)
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edi
movl $0x1, %esi
callq 0x2140
movq %rax, -0xa8(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
movq -0x98(%rbp), %rcx
movl 0x14(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x11dba8
movl -0x60(%rbp), %eax
movl %eax, -0x70(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x70(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0x11dafc
jmp 0x11db9a
movq -0x98(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x60(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x11db1a
jmp 0x11db9a
movq -0x98(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x60(%rbp), %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
jne 0x11db34
jmp 0x11db9a
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq -0xa0(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x11db98
movq -0xa0(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x64(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x74(%rbp)
movq 0x10(%rbp), %rdi
movl -0x74(%rbp), %esi
movl -0x70(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %r8
movl -0x74(%rbp), %eax
shlq $0x3, %rax
addq %rax, %r8
addq $0x4, %r8
movl $0x1, %ecx
callq 0xd0e20
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x11db3b
jmp 0x11db9a
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x11dab0
movl $0x1, -0x8c(%rbp)
cmpl $0x0, -0x8c(%rbp)
je 0x11df46
movl $0x0, -0x8c(%rbp)
movq -0x98(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jae 0x11df41
movl -0x60(%rbp), %eax
shrl %eax
movl %eax, -0x70(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x70(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0x11dc23
jmp 0x11df33
movq -0x98(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x70(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x11dc41
jmp 0x11df33
movq -0x98(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x70(%rbp), %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
jne 0x11dc5e
jmp 0x11df33
movl $0x1, -0x6c(%rbp)
movq -0x98(%rbp), %rdi
movl -0x60(%rbp), %esi
xorl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x1, %eax
jne 0x11dcba
movq -0x98(%rbp), %rdi
movl -0x60(%rbp), %esi
xorl $0x1, %esi
leaq -0x78(%rbp), %rdx
callq 0x166b50
movq -0x98(%rbp), %rdi
movl -0x78(%rbp), %esi
xorl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x1, %eax
jne 0x11dcb8
movl $0x0, -0x6c(%rbp)
jmp 0x11dcba
movq -0xa0(%rbp), %rdi
movl -0x88(%rbp), %esi
leaq -0x7c(%rbp), %rdx
leaq -0x80(%rbp), %rcx
leaq -0x84(%rbp), %r8
callq 0x118e40
cmpl $0x0, -0x6c(%rbp)
jne 0x11dcee
movl -0x7c(%rbp), %eax
cmpl 0x20(%rbp), %eax
jg 0x11dcee
jmp 0x11df33
movl -0x7c(%rbp), %eax
movl %eax, %ecx
cvtsi2ss %rax, %xmm1
movl -0x80(%rbp), %eax
addl %ecx, %eax
cvtsi2ss %rax, %xmm0
mulss -0x28(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jb 0x11dd11
jmp 0x11df33
movl -0x7c(%rbp), %eax
addl -0x80(%rbp), %eax
cmpl $0x0, %eax
jne 0x11dd21
jmp 0x11df33
movq -0xa0(%rbp), %rax
cmpl $0x64, 0x10(%rax)
jb 0x11dd59
movl -0x7c(%rbp), %eax
cvtsi2ss %rax, %xmm1
movq -0xa0(%rbp), %rax
movl 0x10(%rax), %eax
cvtsi2ss %rax, %xmm0
mulss -0x34(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x11dd54
jmp 0x11df33
jmp 0x11ddda
movq -0xa0(%rbp), %rax
cmpl $0x32, 0x10(%rax)
jb 0x11dd9f
movl -0x7c(%rbp), %eax
cvtsi2sd %rax, %xmm1
movq -0xa0(%rbp), %rax
movl 0x10(%rax), %eax
cvtsi2ss %rax, %xmm0
mulss -0x34(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x2644e2(%rip), %xmm2   # 0x382270
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x11dd9d
jmp 0x11df33
jmp 0x11ddd8
movl -0x7c(%rbp), %eax
cvtsi2sd %rax, %xmm1
movq -0xa0(%rbp), %rax
movl 0x10(%rax), %eax
cvtsi2ss %rax, %xmm0
mulss -0x34(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x264489(%rip), %xmm2   # 0x382250
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x11ddd6
jmp 0x11df33
jmp 0x11ddd8
jmp 0x11ddda
movq -0x98(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq -0x30(%rbp), %rcx
movq 0x10(%rbp), %r8
leaq -0x58(%rbp), %rdx
callq 0x11f400
cmpq $0x0, -0x58(%rbp)
jne 0x11de05
jmp 0x11df33
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpq -0x58(%rbp), %rax
jae 0x11de4e
movl -0x70(%rbp), %edi
movq -0x48(%rbp), %rax
movl -0x64(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %esi
movq -0x8(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq 0x10(%rbp), %r8
movq 0x18(%rbp), %r9
callq 0x11f6b0
cmpl $0x0, %eax
jne 0x11de41
jmp 0x11de4e
jmp 0x11de43
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x11de0c
movl -0x64(%rbp), %eax
cmpq -0x58(%rbp), %rax
je 0x11df31
movq -0xa0(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq 0x10(%rbp), %r8
movq -0xa8(%rbp), %r9
callq 0x1185a0
cmpb $0x0, %al
je 0x11de87
jmp 0x11df33
movq -0xa0(%rbp), %rax
cmpl $0x0, 0xc(%rax)
je 0x11df13
movq -0xa0(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0xa0(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0xa0(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0xa0(%rbp), %rax
movl (%rax), %ecx
andl $0x80000000, %ecx       # imm = 0x80000000
orl $0x0, %ecx
movl %ecx, (%rax)
movq -0xa0(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0xa0(%rbp), %rax
movl (%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x0, %ecx
movl %ecx, (%rax)
movq -0xa0(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x2210
movq -0xa0(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x98(%rbp), %rdi
movl -0x70(%rbp), %esi
callq 0xd9510
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x11df33
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x11dbe4
jmp 0x11dbb2
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
movq 0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x11df92
movq 0x10(%rbp), %rdi
movl -0x60(%rbp), %esi
leaq -0x70(%rbp), %rdx
leaq -0x68(%rbp), %rcx
callq 0xd3d50
cmpl $0x1, -0x68(%rbp)
jne 0x11df85
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x60(%rbp), %ecx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
jmp 0x11df87
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x11df4d
movq -0x48(%rbp), %rdi
callq 0x2210
movq -0xa8(%rbp), %rdi
callq 0x2210
addq $0xb0, %rsp
popq %rbp
retq