// File: /workspace/github_cpp_makefile_star2_O1/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, 0x24(%rsp)
movq %r8, 0x58(%rsp)
movq %rcx, 0x50(%rsp)
movq %rsi, 0x38(%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, 0x28(%rsp)
movq %rdx, 0x48(%rsp)
movl 0x14(%rdx), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x1, %esi
callq 0x2120
movq %rax, 0x30(%rsp)
testl %ebp, %ebp
je 0xcf413
movl %ebp, %eax
movq %rax, 0x60(%rsp)
xorl %r14d, %r14d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl $0x0, 0x14(%rsp)
movq 0x28(%rbx), %rax
movq (%rax,%r14,8), %rdi
cmpl $0x2, %edi
jb 0xcf1ea
movq 0x20(%rbx), %rax
movl %r14d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xcf1ea
movq %rdi, %rax
andq %r12, %rax
je 0xcf1ea
movq 0x8(%rbx), %rcx
movq %rdi, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %r13
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movl 0xc(%r13,%rcx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %edx
addq $0x18, %rcx
cmpq %rcx, %rax
jne 0xcf0b9
cmpl $0x2, %edx
jb 0xcf1ea
cmpl $0x1, %edi
adcl $0x0, %edi
movq %rdi, 0x40(%rsp)
leaq (,%rdi,8), %rax
leaq (%rax,%rax,2), %r12
xorl %ebp, %ebp
movl $0x0, 0x8(%rsp)
movl $0x0, 0x20(%rsp)
cmpl $0x0, 0xc(%r13,%rbp)
js 0xcf1c3
movl 0x8(%r13,%rbp), %eax
shrl %eax
leaq (%rax,%rax,4), %r15
shlq $0x3, %r15
movq 0x38(%rsp), %rax
addq 0x10(%rax), %r15
movq %r15, %rdi
movl 0x28(%rsp), %esi
leaq 0x1c(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x2c(%rsp), %r8
callq 0xcf440
movl 0x1c(%rsp), %eax
movl 0x18(%rsp), %ecx
addl %eax, %ecx
je 0xcf1b9
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %rcx, %xmm1
mulss 0x24(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jae 0xcf1b9
movl 0x10(%r15), %ecx
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
cmpl $0x64, %ecx
jb 0xcf18d
movl %ecx, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0x155a45(%rip), %xmm1   # 0x224bd0
jmp 0xcf1b3
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
mulsd 0x155a34(%rip), %xmm1   # 0x224bd0
cmpl $0x32, %ecx
jb 0xcf1ab
mulsd 0x156347(%rip), %xmm1   # 0x2254f0
jmp 0xcf1b3
mulsd 0x15631d(%rip), %xmm1   # 0x2254d0
ucomisd %xmm0, %xmm1
jbe 0xcf1bf
incl 0x8(%rsp)
jmp 0xcf1c3
incl 0x20(%rsp)
addq $0x18, %rbp
cmpq %rbp, %r12
jne 0xcf100
cmpl $0x0, 0x8(%rsp)
sete %al
cmpl $0x0, 0x20(%rsp)
sete %cl
orb %al, %cl
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
je 0xcf1fd
incq %r14
cmpq 0x60(%rsp), %r14
jne 0xcf06a
jmp 0xcf41b
movl 0x40(%rsp), %eax
movq %rax, 0x8(%rsp)
xorl %ebp, %ebp
movq %r14, (%rsp)
leaq (,%rbp,2), %rax
addq %rbp, %rax
cmpl $0x0, 0xc(%r13,%rax,8)
js 0xcf314
leaq (,%rax,8), %rax
addq %r13, %rax
movl 0x8(%rax), %r14d
movl %r14d, %r12d
shrl %r12d
leaq (%r12,%r12,4), %r15
shlq $0x3, %r15
movq 0x38(%rsp), %rax
addq 0x10(%rax), %r15
movq %r15, %rdi
movl 0x28(%rsp), %esi
leaq 0x1c(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x2c(%rsp), %r8
callq 0xcf440
movl 0x1c(%rsp), %eax
movl 0x18(%rsp), %ecx
addl %eax, %ecx
je 0xcf30a
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %rcx, %xmm1
mulss 0x24(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jae 0xcf30a
movl 0x10(%r15), %ecx
movl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
cmpl $0x64, %ecx
jb 0xcf2b6
movl %ecx, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0x15591c(%rip), %xmm1   # 0x224bd0
jmp 0xcf2dc
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
mulsd 0x15590b(%rip), %xmm1   # 0x224bd0
cmpl $0x32, %ecx
jb 0xcf2d4
mulsd 0x15621e(%rip), %xmm1   # 0x2254f0
jmp 0xcf2dc
mulsd 0x1561f4(%rip), %xmm1   # 0x2254d0
ucomisd %xmm0, %xmm1
ja 0xcf30a
movq %r15, %rdi
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq 0x58(%rsp), %rcx
movq 0xa8(%rsp), %r8
movq 0x30(%rsp), %r9
callq 0xced5a
testb %al, %al
je 0xcf327
movq (%rsp), %r14
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
incq %rbp
cmpq 0x8(%rsp), %rbp
jne 0xcf20c
jmp 0xcf1ea
movq 0x20(%rbx), %rax
movb $0x1, 0x4(%rax,%r12,8)
andl $-0x2, %r14d
xorl %eax, %eax
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
leaq (%rax,%r14), %rcx
movq 0x28(%rbx), %rdx
movq (%rdx,%rcx,8), %rsi
movq %rsi, %rdx
andq %r12, %rdx
je 0xcf3f6
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %rsi
shlq $0x3, %rsi
addq 0x8(%rbx), %rsi
xorl %edi, %edi
leaq (%rdi,%rdi,2), %r9
movl 0xc(%rsi,%r9,8), %r8d
testl %r8d, %r8d
js 0xcf3ea
leaq (%rsi,%r9,8), %r10
movq 0x20(%rbx), %r11
movl 0x8(%r10), %r9d
movl %r9d, %r15d
shrl %r15d
cmpb $0x1, 0x4(%r11,%r15,8)
je 0xcf3ea
orl $0x80000000, %r8d       # imm = 0x80000000
movl %r8d, 0xc(%r10)
xorq $0x1, %r9
movq 0x28(%rbx), %r8
movq (%r8,%r9,8), %r9
movq %r9, %r8
andq %r12, %r8
je 0xcf3ea
movq 0x8(%rbx), %r10
shrq $0x20, %r9
shlq $0x3, %r8
leaq (%r8,%r8,2), %r8
leaq (%r9,%r9,2), %r9
leaq (%r10,%r9,8), %r9
addq $0xc, %r9
xorl %r10d, %r10d
movl -0x4(%r9,%r10), %r11d
xorl %ecx, %r11d
cmpl $0x1, %r11d
jne 0xcf3e1
orb $-0x80, 0x3(%r9,%r10)
addq $0x18, %r10
cmpq %r10, %r8
jne 0xcf3cd
incq %rdi
cmpq %rdx, %rdi
jne 0xcf367
leaq 0x1(%rax), %rcx
testq %rax, %rax
movq %rcx, %rax
je 0xcf33d
incl 0x14(%rsp)
movq (%rsp), %r14
jmp 0xcf314
movl $0x0, 0x14(%rsp)
movq %rbx, %rdi
callq 0xa02fa
movq 0x30(%rsp), %rdi
callq 0x2220
movl 0x14(%rsp), %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq