// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movl 0x20(%rbp), %eax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movss %xmm0, -0x34(%rbp)
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movl $0xffffffff, -0x4c(%rbp) # imm = 0xFFFFFFFF
cmpl $0x1, 0x20(%rbp)
jne 0x1189d7
movl $0x2, -0x4c(%rbp)
cmpl $0x2, 0x20(%rbp)
jne 0x1189e4
movl $0x1, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edi
movl $0x1, %esi
callq 0x2140
movq %rax, -0x68(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jae 0x118e15
movl $0x0, -0x70(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x38(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x74(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x38(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
cmpl $0x2, -0x74(%rbp)
jb 0x118a74
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x38(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x118a79
jmp 0x118e07
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
movl %eax, -0x70(%rbp)
cmpl $0x2, -0x70(%rbp)
jae 0x118a97
jmp 0x118e07
movl $0x0, -0x60(%rbp)
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x74(%rbp), %eax
jae 0x118c2e
movq -0x80(%rbp), %rax
movl -0x6c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x118ad6
jmp 0x118c20
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x80(%rbp), %rcx
movl -0x6c(%rbp), %edx
leaq (%rdx,%rdx,2), %rdx
movl 0x8(%rcx,%rdx,8), %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,8), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
movl -0x4c(%rbp), %esi
leaq -0x50(%rbp), %rdx
leaq -0x54(%rbp), %rcx
leaq -0x58(%rbp), %r8
callq 0x118e40
movl -0x50(%rbp), %eax
movl %eax, %ecx
cvtsi2ss %rax, %xmm1
movl -0x54(%rbp), %eax
addl %ecx, %eax
cvtsi2ss %rax, %xmm0
mulss -0x34(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jae 0x118b3c
movl -0x50(%rbp), %eax
addl -0x54(%rbp), %eax
cmpl $0x0, %eax
jne 0x118b4a
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x118c20
movq -0x48(%rbp), %rax
cmpl $0x64, 0x10(%rax)
jb 0x118b8d
movl -0x50(%rbp), %eax
cvtsi2sd %rax, %xmm1
movq -0x48(%rbp), %rax
movl 0x10(%rax), %eax
cvtsi2sd %rax, %xmm0
movsd 0x2664a8(%rip), %xmm2   # 0x37f018
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x118b88
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x118c20
jmp 0x118c17
movq -0x48(%rbp), %rax
cmpl $0x32, 0x10(%rax)
jb 0x118bd6
movl -0x50(%rbp), %eax
cvtsi2sd %rax, %xmm1
movq -0x48(%rbp), %rax
movl 0x10(%rax), %eax
cvtsi2sd %rax, %xmm0
movsd 0x266465(%rip), %xmm2   # 0x37f018
mulsd %xmm2, %xmm0
movsd 0x2696b1(%rip), %xmm2   # 0x382270
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x118bd4
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x118c20
jmp 0x118c15
movl -0x50(%rbp), %eax
cvtsi2sd %rax, %xmm1
movq -0x48(%rbp), %rax
movl 0x10(%rax), %eax
cvtsi2sd %rax, %xmm0
movsd 0x266426(%rip), %xmm2   # 0x37f018
mulsd %xmm2, %xmm0
movsd 0x269652(%rip), %xmm2   # 0x382250
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x118c13
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x118c20
jmp 0x118c15
jmp 0x118c17
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x118aac
cmpl $0x0, -0x60(%rbp)
je 0x118c3a
cmpl $0x0, -0x5c(%rbp)
jne 0x118c3f
jmp 0x118e07
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x74(%rbp), %eax
jae 0x118e05
movq -0x80(%rbp), %rax
movl -0x6c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x118c70
jmp 0x118df7
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x80(%rbp), %rcx
movl -0x6c(%rbp), %edx
leaq (%rdx,%rdx,2), %rdx
movl 0x8(%rcx,%rdx,8), %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,8), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
movl -0x4c(%rbp), %esi
leaq -0x50(%rbp), %rdx
leaq -0x54(%rbp), %rcx
leaq -0x58(%rbp), %r8
callq 0x118e40
movl -0x50(%rbp), %eax
movl %eax, %ecx
cvtsi2ss %rax, %xmm1
movl -0x54(%rbp), %eax
addl %ecx, %eax
cvtsi2ss %rax, %xmm0
mulss -0x34(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jae 0x118cd6
movl -0x50(%rbp), %eax
addl -0x54(%rbp), %eax
cmpl $0x0, %eax
jne 0x118cdb
jmp 0x118df7
movq -0x48(%rbp), %rax
cmpl $0x64, 0x10(%rax)
jb 0x118d12
movl -0x50(%rbp), %eax
cvtsi2sd %rax, %xmm1
movq -0x48(%rbp), %rax
movl 0x10(%rax), %eax
cvtsi2sd %rax, %xmm0
movsd 0x266317(%rip), %xmm2   # 0x37f018
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x118d10
jmp 0x118df7
jmp 0x118d8d
movq -0x48(%rbp), %rax
cmpl $0x32, 0x10(%rax)
jb 0x118d55
movl -0x50(%rbp), %eax
cvtsi2sd %rax, %xmm1
movq -0x48(%rbp), %rax
movl 0x10(%rax), %eax
cvtsi2sd %rax, %xmm0
movsd 0x2662e0(%rip), %xmm2   # 0x37f018
mulsd %xmm2, %xmm0
movsd 0x26952c(%rip), %xmm2   # 0x382270
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x118d53
jmp 0x118df7
jmp 0x118d8b
movl -0x50(%rbp), %eax
cvtsi2sd %rax, %xmm1
movq -0x48(%rbp), %rax
movl 0x10(%rax), %eax
cvtsi2sd %rax, %xmm0
movsd 0x2662a7(%rip), %xmm2   # 0x37f018
mulsd %xmm2, %xmm0
movsd 0x2694d3(%rip), %xmm2   # 0x382250
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x118d89
jmp 0x118df7
jmp 0x118d8b
jmp 0x118d8d
movq -0x48(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq 0x18(%rbp), %r8
movq -0x68(%rbp), %r9
callq 0x1185a0
cmpb $0x0, %al
je 0x118db0
jmp 0x118df7
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x80(%rbp), %rcx
movl -0x6c(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
movb $0x1, 0x4(%rax,%rcx,8)
movq -0x8(%rbp), %rdi
movq -0x80(%rbp), %rax
movl -0x6c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
shrl %esi
callq 0xf6800
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x118c46
jmp 0x118e07
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x118a09
movq -0x8(%rbp), %rdi
callq 0xcdd10
movq -0x68(%rbp), %rdi
callq 0x2210
movl -0x40(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)