// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: magic_trio_phasing(asg_t*, ma_ug_t*, asg_t*, ma_sub_t*, ma_hit_t_alloc*, ma_hit_t_alloc*, long long, R_to_U*, unsigned int, float)
int magic_trio_phasing(asg_t *g, ma_ug_t *ug, asg_t *read_sg, ma_sub_t* coverage_cut, 
ma_hit_t_alloc* sources, ma_hit_t_alloc* reverse_sources, long long miniedgeLen, 
R_to_U* ruIndex, uint32_t positive_flag, float drop_rate)
{
    uint32_t v, n_vtx = g->n_seq * 2, n_reduced = 0;
    ma_utg_t* nsu = NULL;
    uint32_t flag = (uint32_t)-1, flag_occ, non_flag_occ, ambigious, del_node, keep_node;
    if(positive_flag == FATHER) flag = MOTHER;
    if(positive_flag == MOTHER) flag = FATHER;
    uint8_t* primary_flag = (uint8_t*)calloc(read_sg->n_seq, sizeof(uint8_t));

    for (v = 0; v < n_vtx; ++v) 
    {
        uint32_t i, n_arc = 0, nv = asg_arc_n(g, v);
        asg_arc_t *av = asg_arc_a(g, v);
        if (nv < 2 || g->seq[v>>1].del /**|| g->seq[v>>1].c == ALTER_LABLE**/) continue;
        n_arc = get_real_length(g, v, NULL);
        if (n_arc < 2) continue;
        del_node = keep_node = 0;
        for (i = 0; i < nv; i++)
        {
            if(av[i].del) continue;
            nsu = &(ug->u.a[av[i].v>>1]);
            get_unitig_trio_flag(nsu, flag, &flag_occ, &non_flag_occ, &ambigious);
            ///we may need it or not
            if((flag_occ <= ((non_flag_occ+flag_occ)*drop_rate))||((flag_occ+non_flag_occ) == 0))
            {
                keep_node++;
                continue;
            }

            if(nsu->n >= 100)
            {
                if(flag_occ < nsu->n*DOUBLE_CHECK_THRES)
                {
                    keep_node++;
                    continue;
                }
            }
            else if(nsu->n >= 50)
            {
                if(flag_occ < nsu->n*DOUBLE_CHECK_THRES*0.5)
                {
                    keep_node++;
                    continue;
                }
            }
            else
            {
                if(flag_occ < nsu->n*DOUBLE_CHECK_THRES*0.25)
                {
                    keep_node++;
                    continue;
                }
            }
            
            del_node++; 
        }

        if(keep_node == 0 || del_node == 0) continue;

        for (i = 0; i < nv; i++)
        {
            if(av[i].del) continue;
            nsu = &(ug->u.a[av[i].v>>1]);

            get_unitig_trio_flag(nsu, flag, &flag_occ, &non_flag_occ, &ambigious);
            ///we may need it or not
            if((flag_occ <= ((non_flag_occ+flag_occ)*drop_rate))||((flag_occ+non_flag_occ) == 0))
            {
                continue;
            }

            if(nsu->n >= 100)
            {
                if(flag_occ < nsu->n*DOUBLE_CHECK_THRES)
                {
                    continue;
                }
            }
            else if(nsu->n >= 50)
            {
                if(flag_occ < nsu->n*DOUBLE_CHECK_THRES*0.5)
                {
                    continue;
                }
            }
            else
            {
                if(flag_occ < nsu->n*DOUBLE_CHECK_THRES*0.25)
                {
                    continue;
                }
            }

            if(if_primary_unitig(nsu, read_sg, coverage_cut, sources, ruIndex, primary_flag))
            {
                continue;
            }

            g->seq[av[i].v>>1].c = ALTER_LABLE;
            asg_seq_drop(g, av[i].v>>1);
            n_reduced++;
        }
    }




    asg_cleanup(g);
    free(primary_flag);
    return n_reduced;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movss %xmm0, 0x1c(%rsp)
movq %r8, 0x48(%rsp)
movq %rcx, 0x40(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdi, %rbx
movl 0xb0(%rsp), %eax
movl 0x14(%rdi), %ebp
addl %ebp, %ebp
xorl %ecx, %ecx
cmpl $0x1, %eax
sete %cl
leal (%rcx,%rcx,2), %ecx
decl %ecx
cmpl $0x2, %eax
movl $0x1, %eax
cmovnel %ecx, %eax
movl %eax, 0x20(%rsp)
movq %rdx, 0x38(%rsp)
movl 0x14(%rdx), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x1, %esi
callq 0x2120
movq %rax, 0x28(%rsp)
testl %ebp, %ebp
je 0xd061b
movl %ebp, %eax
movq %rax, 0x50(%rsp)
xorl %esi, %esi
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl $0x0, 0xc(%rsp)
movq 0x28(%rbx), %rax
movq (%rax,%rsi,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
movq %rsi, 0x58(%rsp)
je 0xd0606
movq 0x20(%rbx), %rcx
movl %esi, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0xd0606
movq %rax, %rcx
andq %r14, %rcx
je 0xd0606
movq 0x8(%rbx), %rdx
movq %rax, %rsi
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %rsi
leaq (%rdx,%rsi,8), %r13
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rbp
xorl %ecx, %ecx
xorl %edx, %edx
movl 0xc(%r13,%rcx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %edx
addq $0x18, %rcx
cmpq %rcx, %rbp
jne 0xd02e6
cmpl $0x2, %edx
jb 0xd0606
movl %eax, %eax
movq %rax, 0x60(%rsp)
xorl %r12d, %r12d
movl $0x0, 0x8(%rsp)
movl $0x0, 0x18(%rsp)
cmpl $0x0, 0xc(%r13,%r12)
js 0xd03e1
movl 0x8(%r13,%r12), %eax
shrl %eax
leaq (%rax,%rax,4), %r15
shlq $0x3, %r15
movq 0x30(%rsp), %rax
addq 0x10(%rax), %r15
movq %r15, %rdi
movl 0x20(%rsp), %esi
leaq 0x14(%rsp), %rdx
leaq 0x10(%rsp), %rcx
leaq 0x24(%rsp), %r8
callq 0xd0648
movl 0x14(%rsp), %eax
movl 0x10(%rsp), %ecx
addl %eax, %ecx
je 0xd03d7
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %rcx, %xmm1
mulss 0x1c(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jae 0xd03d7
movl 0x10(%r15), %ecx
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
cmpl $0x64, %ecx
jb 0xd03ab
movl %ecx, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0x160867(%rip), %xmm1   # 0x230c10
jmp 0xd03d1
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
mulsd 0x160856(%rip), %xmm1   # 0x230c10
cmpl $0x32, %ecx
jb 0xd03c9
mulsd 0x161229(%rip), %xmm1   # 0x2315f0
jmp 0xd03d1
mulsd 0x1611ff(%rip), %xmm1   # 0x2315d0
ucomisd %xmm0, %xmm1
jbe 0xd03dd
incl 0x8(%rsp)
jmp 0xd03e1
incl 0x18(%rsp)
addq $0x18, %r12
cmpq %r12, %rbp
jne 0xd031e
cmpl $0x0, 0x8(%rsp)
sete %al
cmpl $0x0, 0x18(%rsp)
sete %cl
orb %al, %cl
jne 0xd0606
xorl %ebp, %ebp
leaq (,%rbp,2), %rax
addq %rbp, %rax
cmpl $0x0, 0xc(%r13,%rax,8)
js 0xd05f8
leaq (,%rax,8), %r12
addq %r13, %r12
movl 0x8(%r12), %eax
shrl %eax
leaq (%rax,%rax,4), %r15
shlq $0x3, %r15
movq 0x30(%rsp), %rax
addq 0x10(%rax), %r15
movq %r15, %rdi
movl 0x20(%rsp), %esi
leaq 0x14(%rsp), %rdx
leaq 0x10(%rsp), %rcx
leaq 0x24(%rsp), %r8
callq 0xd0648
movl 0x14(%rsp), %eax
movl 0x10(%rsp), %ecx
addl %eax, %ecx
je 0xd05f8
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %rcx, %xmm1
mulss 0x1c(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jae 0xd05f8
movl 0x10(%r15), %ecx
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
cmpl $0x64, %ecx
jb 0xd04b3
movl %ecx, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0x16075f(%rip), %xmm1   # 0x230c10
jmp 0xd04d9
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
mulsd 0x16074e(%rip), %xmm1   # 0x230c10
cmpl $0x32, %ecx
jb 0xd04d1
mulsd 0x161121(%rip), %xmm1   # 0x2315f0
jmp 0xd04d9
mulsd 0x1610f7(%rip), %xmm1   # 0x2315d0
ucomisd %xmm0, %xmm1
ja 0xd05f8
movq %r15, %rdi
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq 0xa8(%rsp), %r8
movq 0x28(%rsp), %r9
callq 0xcffc4
testb %al, %al
jne 0xd05f8
movq 0x20(%rbx), %rcx
movl 0x8(%r12), %eax
movl %eax, %edx
shrl %edx
movb $0x1, 0x4(%rcx,%rdx,8)
andl $-0x2, %eax
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 0xd05e1
shrq $0x20, %rdi
leaq (%rdi,%rdi,2), %rdi
shlq $0x3, %rdi
addq 0x8(%rbx), %rdi
xorl %r8d, %r8d
leaq (%r8,%r8,2), %r10
movl 0xc(%rdi,%r10,8), %r9d
testl %r9d, %r9d
js 0xd05d5
leaq (%rdi,%r10,8), %r11
movq 0x20(%rbx), %r15
movl 0x8(%r11), %r10d
movl %r10d, %r12d
shrl %r12d
cmpb $0x1, 0x4(%r15,%r12,8)
je 0xd05d5
orl $0x80000000, %r9d       # imm = 0x80000000
movl %r9d, 0xc(%r11)
xorq $0x1, %r10
movq 0x28(%rbx), %r9
movq (%r9,%r10,8), %r10
movq %r10, %r9
andq %r14, %r9
je 0xd05d5
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), %r15d
xorl %edx, %r15d
cmpl $0x1, %r15d
jne 0xd05cc
orb $-0x80, 0x3(%r10,%r11)
addq $0x18, %r11
cmpq %r11, %r9
jne 0xd05b8
incq %r8
cmpq %rsi, %r8
jne 0xd0552
movl $0x1, %edx
testb $0x1, %cl
movl $0x0, %ecx
jne 0xd0528
incl 0xc(%rsp)
incq %rbp
cmpq 0x60(%rsp), %rbp
jne 0xd0408
movq 0x58(%rsp), %rsi
incq %rsi
cmpq 0x50(%rsp), %rsi
jne 0xd0291
jmp 0xd0623
movl $0x0, 0xc(%rsp)
movq %rbx, %rdi
callq 0xa0a7d
movq 0x28(%rsp), %rdi
callq 0x2220
movl 0xc(%rsp), %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq