// File: /workspace/github_cpp_makefile_star2_O1/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, 0x1c(%rsp)
movq %r9, 0x50(%rsp)
movss %xmm0, 0x40(%rsp)
movq %rcx, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movq %rsi, %r12
movq %rdi, %r15
movl 0xf0(%rsp), %eax
movl %eax, 0x3c(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movq $0x0, 0x90(%rsp)
movq 0x18(%rdi), %r14
xorl %eax, %eax
cmpl $0x2, %r8d
setne %al
movl %eax, 0x10(%rsp)
movl 0x14(%rsi), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x1, %esi
callq 0x2120
movq %rax, 0x48(%rsp)
testl $0x7fffffff, 0x14(%r14) # imm = 0x7FFFFFFF
movq %r15, 0x28(%rsp)
je 0xd33de
xorl %r13d, %r13d
movq 0x10(%r15), %rax
leaq (,%r13,4), %rcx
addq %r13, %rcx
cmpl $0x0, 0xc(%rax,%rcx,8)
je 0xd33c4
movq 0x20(%r14), %rdx
cmpl $0x0, (%rdx,%r13,8)
js 0xd33c4
cmpb $0x1, 0x4(%rdx,%r13,8)
je 0xd33c4
leaq (%rax,%rcx,8), %rbx
cmpl $0x0, 0x10(%rbx)
je 0xd33c4
xorl %r15d, %r15d
movq 0x18(%rbx), %rax
movq (%rax,%r15,8), %rsi
shrq $0x21, %rsi
movq 0x20(%r12), %rax
leaq (%rax,%rsi,8), %r8
addq $0x4, %r8
movq 0x8(%rsp), %rdi
movl %r13d, %edx
movl $0x1, %ecx
callq 0xa1cd4
incq %r15
movl 0x10(%rbx), %eax
cmpq %rax, %r15
jb 0xd338e
incq %r13
movl 0x14(%r14), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r13
movq 0x28(%rsp), %r15
jb 0xd3358
incl 0x10(%rsp)
movq %r12, 0x58(%rsp)
movl 0x14(%r14), %ebx
addl %ebx, %ebx
je 0xd37e2
xorl %r13d, %r13d
movl $0x0, 0x18(%rsp)
movl %ebx, 0x4(%rsp)
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movl %r13d, %ebp
shrl %ebp
movq 0x10(%r15), %rax
leaq (,%rbp,4), %rcx
addq %rbp, %rcx
cmpl $0x0, 0xc(%rax,%rcx,8)
je 0xd3434
movq 0x20(%r14), %rdx
cmpl $0x0, (%rdx,%rbp,8)
js 0xd3434
cmpb $0x1, 0x4(%rdx,%rbp,8)
jne 0xd3441
incl %r13d
cmpl %ebx, %r13d
jne 0xd3402
jmp 0xd37d8
leaq (%rax,%rcx,8), %rdi
movl %r13d, %ecx
xorl $0x1, %ecx
movq 0x28(%r14), %rax
movq (%rax,%rcx,8), %rcx
movq %rcx, %rdx
andq %r11, %rdx
movq %rdi, 0x30(%rsp)
je 0xd34dd
movq 0x8(%r14), %rdi
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rsi
leaq (%rdi,%rsi,8), %rdi
addq $0xc, %rdi
leaq (,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
movl (%rdi,%r8), %r10d
notl %r10d
shrl $0x1f, %r10d
addl %r10d, %r9d
addq $0x18, %r8
cmpq %r8, %rcx
jne 0xd3486
cmpl $0x1, %r9d
movq 0x30(%rsp), %rdi
jne 0xd34dd
testq %rdx, %rdx
je 0xd3580
movq 0x8(%r14), %rdx
leaq (%rdx,%rsi,8), %rdx
addq $0xc, %rdx
xorl %esi, %esi
movl 0x20(%rsp), %r8d
cmpl $0x0, (%rdx,%rsi)
js 0xd34cf
movl -0x4(%rdx,%rsi), %r8d
addq $0x18, %rsi
cmpq %rsi, %rcx
jne 0xd34c4
jmp 0xd3585
movl 0x10(%rsp), %esi
leaq 0x14(%rsp), %rdx
leaq 0x24(%rsp), %rcx
leaq 0x44(%rsp), %r8
callq 0xcf440
movl 0x14(%rsp), %eax
movl 0x24(%rsp), %ecx
addl %eax, %ecx
je 0xd3434
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %rcx, %xmm1
mulss 0x40(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jae 0xd3434
movq 0x30(%rsp), %rsi
movl 0x10(%rsi), %ecx
cmpl $0x64, %ecx
jb 0xd354f
movl %ecx, %eax
xorps %xmm1, %xmm1
cvtsi2ss %rax, %xmm1
mulss 0x1c(%rsp), %xmm1
ucomiss %xmm0, %xmm1
ja 0xd3434
jmp 0xd3626
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
mulss 0x1c(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
cmpl $0x32, %ecx
jb 0xd3614
mulsd 0x151f75(%rip), %xmm1   # 0x2254f0
jmp 0xd361c
movl 0x20(%rsp), %r8d
movl %r8d, 0x20(%rsp)
movl %r8d, %ecx
xorl $0x1, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, %rcx
andq %r11, %rcx
je 0xd35db
movq 0x8(%r14), %rdx
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %rax
addq $0xc, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
xorl %esi, %esi
movl (%rax,%rdx), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %esi
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0xd35bc
cmpl $0x1, %esi
setne %bl
jmp 0xd35dd
movb $0x1, %bl
movl 0x10(%rsp), %esi
leaq 0x14(%rsp), %rdx
leaq 0x24(%rsp), %rcx
leaq 0x44(%rsp), %r8
callq 0xcf440
testb %bl, %bl
jne 0xd36a9
movl 0x3c(%rsp), %eax
cmpl %eax, 0x14(%rsp)
movl 0x4(%rsp), %ebx
jle 0xd3434
jmp 0xd34f5
mulsd 0x151eb4(%rip), %xmm1   # 0x2254d0
ucomisd %xmm0, %xmm1
ja 0xd3434
movq %r14, %rdi
leaq 0x80(%rsp), %rdx
movq 0x50(%rsp), %rcx
movq 0x8(%rsp), %r8
callq 0xd49f8
movq 0x80(%rsp), %rbx
testq %rbx, %rbx
je 0xd37cf
movl $0x1, %r12d
movq 0x90(%rsp), %rax
movq %rax, 0x78(%rsp)
xorl %r15d, %r15d
movq 0x78(%rsp), %rax
movl (%rax,%r15,8), %esi
movl %ebp, %edi
movq 0x28(%rsp), %rdx
movq 0x50(%rsp), %rcx
movq 0x8(%rsp), %r8
movq 0x70(%rsp), %r9
callq 0xd4be6
testl %eax, %eax
je 0xd369a
movl %r12d, %r15d
incl %r12d
cmpq %r15, %rbx
ja 0xd3667
cmpq %r15, %rbx
jne 0xd36b2
movq 0x28(%rsp), %r15
jmp 0xd37cf
movl 0x4(%rsp), %ebx
jmp 0xd34f5
movq 0x30(%rsp), %r12
movq %r12, %rdi
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq 0x68(%rsp), %rcx
movq 0x8(%rsp), %r8
movq 0x48(%rsp), %r9
callq 0xced5a
testb %al, %al
movq 0x28(%rsp), %r15
movl 0x4(%rsp), %ebx
jne 0xd3434
cmpl $0x0, 0xc(%r12)
je 0xd3715
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%r12)
movl $0x0, (%r12)
movq 0x18(%r12), %rdi
callq 0x2220
movq $0x0, 0x18(%r12)
movq 0x20(%r14), %rax
orl $0x80000000, (%rax,%rbp,8) # imm = 0x80000000
movl %r13d, %eax
andl $0x7ffffffe, %eax       # imm = 0x7FFFFFFE
xorl %ecx, %ecx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
leaq (%rcx,%rax), %rdx
movq 0x28(%r14), %rsi
movq (%rsi,%rdx,8), %rdi
movq %rdi, %rsi
andq %rbp, %rsi
je 0xd37bb
shrq $0x20, %rdi
leaq (%rdi,%rdi,2), %rdi
shlq $0x3, %rdi
addq 0x8(%r14), %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(%r14), %r10
movq (%r10,%r9,8), %r10
movq %r10, %r9
andq %rbp, %r9
je 0xd37b3
movq 0x8(%r14), %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), %ebx
xorl %edx, %ebx
cmpl $0x1, %ebx
jne 0xd37aa
orb $-0x80, 0x3(%r10,%r11)
addq $0x18, %r11
cmpq %r11, %r9
jne 0xd3798
incq %r8
cmpq %rsi, %r8
jne 0xd3756
leaq 0x1(%rcx), %rdx
testq %rcx, %rcx
movq %rdx, %rcx
je 0xd372f
incl 0x18(%rsp)
movl 0x4(%rsp), %ebx
jmp 0xd3434
cmpl $0x0, 0x18(%rsp)
sete %al
jmp 0xd37e4
movb $0x1, %al
testb %al, %al
je 0xd33e7
movq 0x8(%rsp), %rax
movq (%rax), %rax
testq %rax, %rax
je 0xd3824
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rcx
xorl %edx, %edx
cmpl $-0x2, (%rcx,%rdx,4)
jg 0xd381a
movq 0x8(%rsp), %rsi
movq 0x8(%rsi), %rsi
movl $0xffffffff, (%rsi,%rdx,4) # imm = 0xFFFFFFFF
incq %rdx
movl %edx, %esi
cmpq %rsi, %rax
ja 0xd3804
movq 0x90(%rsp), %rdi
callq 0x2220
movq 0x48(%rsp), %rdi
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220