// File: /workspace/github_cpp_makefile_star2_O2/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, 0x10(%rsp)
movq %r8, %r13
movq %rcx, 0x58(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, %r14
movl 0xb0(%rsp), %eax
movl 0x14(%rdi), %r15d
addl %r15d, %r15d
xorl %ecx, %ecx
cmpl $0x1, %eax
sete %cl
leal (%rcx,%rcx,2), %ecx
decl %ecx
cmpl $0x2, %eax
pushq $0x1
popq %rax
cmovnel %ecx, %eax
movl %eax, 0x14(%rsp)
movq %rdx, 0x50(%rsp)
movl 0x14(%rdx), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
pushq $0x1
popq %rsi
callq 0x2120
movq %rax, 0x30(%rsp)
xorl %esi, %esi
movl $0x0, 0x8(%rsp)
movq %r13, 0x40(%rsp)
movq %r14, 0x18(%rsp)
movq %r15, 0x28(%rsp)
cmpq %r15, %rsi
je 0x94ab1
movq 0x28(%r14), %rax
movq (%rax,%rsi,8), %rbx
testl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
movq %rsi, 0x60(%rsp)
je 0x94993
movq 0x20(%r14), %rax
movl %esi, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x94993
movq 0x8(%r14), %rbp
movq %r14, %rdi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x2, %eax
jb 0x94993
movq %rbx, %rax
shrq $0x20, %rax
movl %ebx, %ecx
imulq $0x18, %rcx, %rbx
imulq $0x18, %rax, %rax
movq %rax, 0x48(%rsp)
leaq (%rax,%rbp), %r14
addq $0xc, %r14
xorl %r13d, %r13d
movl $0x0, 0xc(%rsp)
xorl %r15d, %r15d
cmpq %r13, %rbx
je 0x94977
cmpl $0x0, (%r14,%r13)
js 0x9496e
movl -0x4(%r14,%r13), %eax
shrl %eax
imulq $0x28, %rax, %r12
movq 0x38(%rsp), %rax
addq 0x10(%rax), %r12
movq %r12, %rdi
movl 0x14(%rsp), %esi
leaq 0x4(%rsp), %rdx
movq %rsp, %rcx
leaq 0x24(%rsp), %r8
callq 0x94ad6
movl 0x4(%rsp), %eax
movl (%rsp), %ecx
addl %eax, %ecx
je 0x94965
cvtsi2ss %rax, %xmm0
cvtsi2ss %rcx, %xmm1
mulss 0x10(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jae 0x94965
movl 0x10(%r12), %ecx
movl %eax, %eax
cvtsi2sd %rax, %xmm0
cmpl $0x64, %ecx
jb 0x9493c
movl %ecx, %eax
cvtsi2sd %rax, %xmm1
mulsd 0x14e2b6(%rip), %xmm1   # 0x1e2bf0
jmp 0x9495f
cvtsi2sd %ecx, %xmm1
mulsd 0x14e2a8(%rip), %xmm1   # 0x1e2bf0
cmpl $0x32, %ecx
jb 0x94957
mulsd 0x14ebf3(%rip), %xmm1   # 0x1e3548
jmp 0x9495f
mulsd 0x14ebc1(%rip), %xmm1   # 0x1e3520
ucomisd %xmm0, %xmm1
jbe 0x9496a
incl %r15d
jmp 0x9496e
incl 0xc(%rsp)
addq $0x18, %r13
jmp 0x948b9
testl %r15d, %r15d
sete %al
cmpl $0x0, 0xc(%rsp)
sete %cl
orb %al, %cl
je 0x949a0
movq 0x18(%rsp), %r14
movq 0x28(%rsp), %r15
movq 0x60(%rsp), %rsi
incq %rsi
jmp 0x94842
movq 0x48(%rsp), %rax
leaq (%rax,%rbp), %r15
addq $0x8, %r15
xorl %r14d, %r14d
movq 0x40(%rsp), %r13
cmpq %r14, %rbx
je 0x94989
cmpl $0x0, 0x4(%r15,%r14)
js 0x94aa8
movl (%r15,%r14), %eax
shrl %eax
imulq $0x28, %rax, %r12
movq 0x38(%rsp), %rax
addq 0x10(%rax), %r12
movq %r12, %rdi
movl 0x14(%rsp), %esi
leaq 0x4(%rsp), %rdx
movq %rsp, %rcx
leaq 0x24(%rsp), %r8
callq 0x94ad6
movl 0x4(%rsp), %eax
movl (%rsp), %ecx
addl %eax, %ecx
je 0x94aa8
cvtsi2ss %rax, %xmm0
cvtsi2ss %rcx, %xmm1
mulss 0x10(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jae 0x94aa8
movl 0x10(%r12), %ecx
movl %eax, %eax
cvtsi2sd %rax, %xmm0
cmpl $0x64, %ecx
jb 0x94a3c
movl %ecx, %eax
cvtsi2sd %rax, %xmm1
mulsd 0x14e1b6(%rip), %xmm1   # 0x1e2bf0
jmp 0x94a5f
cvtsi2sd %ecx, %xmm1
mulsd 0x14e1a8(%rip), %xmm1   # 0x1e2bf0
cmpl $0x32, %ecx
jb 0x94a57
mulsd 0x14eaf3(%rip), %xmm1   # 0x1e3548
jmp 0x94a5f
mulsd 0x14eac1(%rip), %xmm1   # 0x1e3520
ucomisd %xmm0, %xmm1
ja 0x94aa8
movq %r12, %rdi
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
movq %r13, %rcx
movq 0xa8(%rsp), %r8
movq 0x30(%rsp), %r9
callq 0x945a4
testb %al, %al
jne 0x94aa8
movq 0x18(%rsp), %rdi
movq 0x20(%rdi), %rax
movl (%r15,%r14), %esi
shrl %esi
movb $0x1, 0x4(%rax,%rsi,8)
callq 0x80870
incl 0x8(%rsp)
addq $0x18, %r14
jmp 0x949b5
movq %r14, %rdi
callq 0x694eb
movq 0x30(%rsp), %rdi
callq 0x2220
movl 0x8(%rsp), %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq