// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_cut_trio_long_equal_tips_assembly_complex(asg_t*, ma_ug_t*, asg_t*, ma_hit_t_alloc*, long long, R_to_U*, unsigned int, hap_cov_t*, utg_trans_t*, unsigned int)
int asg_arc_cut_trio_long_equal_tips_assembly_complex(asg_t *g, ma_ug_t *ug, asg_t *read_sg, 
ma_hit_t_alloc* reverse_sources, long long miniedgeLen, R_to_U* ruIndex, uint32_t stops_threshold, 
hap_cov_t *cov, utg_trans_t *o, uint32_t trio_flag)
{
    double startTime = Get_T();
    uint32_t v, n_vtx = g->n_seq * 2, n_reduced = 0, convex, in, flag;
    uint32_t return_flag, convex_i, k;
    long long ll, tmp, max_stop_nodeLen, max_stop_baseLen;

    buf_t b, b_0, b_1;
    memset(&b, 0, sizeof(buf_t));
    memset(&b_0, 0, sizeof(buf_t));
    memset(&b_1, 0, sizeof(buf_t));


    for (v = 0; v < n_vtx; ++v) 
    {
        uint32_t i;
        if (g->seq[v>>1].del || g->seq[v>>1].c == ALTER_LABLE) continue;
        ///tip
        if (get_real_length(g, v, NULL) != 0) continue;
        if (get_real_length(g, v^1, NULL) != 1) continue;

        b.b.n = 0;   
        return_flag = get_unitig(g, ug, v^1, &convex, &tmp, &ll, &max_stop_nodeLen, 
        &max_stop_baseLen, 1, &b);

        if(return_flag != MUL_INPUT) continue;
        if(trio_check(ug, b.b.a, b.b.n, trio_flag)) continue;
        in = convex^1;
        get_real_length(g, convex, &convex);
        convex = convex^1;
        uint32_t n_convex = asg_arc_n(g, convex), convexLen = ll;
        asg_arc_t *a_convex = asg_arc_a(g, convex);
        for (i = 0; i < n_convex; i++)
        {
            if(a_convex[i].del) continue;
            if(a_convex[i].v == in) break;
        }
        convex_i = i;
        ///if(convex_i == n_convex) fprintf(stderr, "ERROR1\n");
        // if((v>>1) == 304 && (convex>>1) == 12875)
        // {
        //     fprintf(stderr, "\n++v-%u, convex-%u, n_convex-%u\n", v, convex, n_convex);
        // }

        for (i = 0; i < n_convex; i++)
        {
            if(a_convex[i].del) continue;
            if(i == convex_i) continue;

            return_flag = get_unitig(g, ug, a_convex[i].v, &convex, &tmp, &ll, &max_stop_nodeLen, 
            &max_stop_baseLen, stops_threshold, NULL);

            // if((v>>1) == 304 && n_convex == 2)
            // {
            //     fprintf(stderr, "---v-%u (len: %u), convex-%u, a_convex[i].v-%u (len: %lld), max_stop_baseLen: %lld\n", 
            //     v, convexLen, convex, a_convex[i].v, ll, max_stop_baseLen);
            // }

            if(ll>convexLen  && max_stop_baseLen>=ll*MAX_STOP_RATE)
            {
                flag = check_different_haps(g, ug, read_sg, a_convex[convex_i].v, a_convex[i].v,
                reverse_sources, &b_0, &b_1, ruIndex, cov->is_r_het, miniedgeLen, stops_threshold);
                // #define UNAVAILABLE (uint32_t)-1
                // #define PLOID 0
                // #define NON_PLOID 1
                if(flag != PLOID) continue;
                n_reduced++;

                for (k = 0; k < b.b.n; k++)
                {
                    g->seq[b.b.a[k]>>1].c = ALTER_LABLE;
                }

                for (k = 0; k < b.b.n; k++)
                {
                    asg_seq_drop(g, b.b.a[k]>>1);
                }

                ///note: we need to remove b_0, insetad of b_1 here
                if(cov)
                {
                    collect_trans_cov(__func__, &b_1, a_convex[i].ol, &b_0, a_convex[convex_i].ol, ug, read_sg, cov);
                } 

                if(o)
                {
                    collect_trans_ovlp(__func__, &b_1, a_convex[i].ol, &b_0, a_convex[convex_i].ol, ug, o);
                }
                

                ///lable the primary one
                b_0.b.n = 0; 
                get_unitig(g, ug, a_convex[i].v, &convex, &tmp, &ll, &max_stop_nodeLen, 
                &max_stop_baseLen, stops_threshold, &b_0);
                for (k = 0; k < b_0.b.n; k++)
                {
                    g->seq[b_0.b.a[k]>>1].c = HAP_LABLE;
                }

                break;
            }
        }

    }

    asg_cleanup(g);
    asg_symm(g);
    free(b.b.a);
    free(b_0.b.a);
    free(b_1.b.a);

    if(VERBOSE >= 1)
    {
        fprintf(stderr, "[M::%s] removed %d long tips\n", 
        __func__, n_reduced);
        fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime);
    }

    return n_reduced;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movq %r9, 0x60(%rsp)
movq %r8, 0x58(%rsp)
movq %rcx, 0x50(%rsp)
movq %rdx, 0x38(%rsp)
movq %rsi, %r15
movq %rdi, %rbx
movl 0x208(%rsp), %ebp
callq 0x251c
movl 0x14(%rbx), %r14d
addl %r14d, %r14d
xorpd %xmm0, %xmm0
leaq 0x70(%rsp), %rax
movapd %xmm0, (%rax)
movapd %xmm0, 0x10(%rax)
movapd %xmm0, 0x20(%rax)
movapd %xmm0, 0x30(%rax)
andq $0x0, 0x60(%rax)
movapd %xmm0, 0x40(%rax)
movapd %xmm0, 0x50(%rax)
leaq 0xe0(%rsp), %rax
movapd %xmm0, (%rax)
movapd %xmm0, 0x10(%rax)
movapd %xmm0, 0x20(%rax)
movapd %xmm0, 0x30(%rax)
movapd %xmm0, 0x40(%rax)
movapd %xmm0, 0x50(%rax)
andq $0x0, 0x60(%rax)
leaq 0x150(%rsp), %rax
movapd %xmm0, (%rax)
movapd %xmm0, 0x10(%rax)
movapd %xmm0, 0x20(%rax)
movapd %xmm0, 0x30(%rax)
andq $0x0, 0x60(%rax)
movapd %xmm0, 0x40(%rax)
movapd %xmm0, 0x50(%rax)
movq $0x0, 0x28(%rsp)
xorl %r13d, %r13d
cmpl %r14d, %r13d
je 0x94fea
movq 0x20(%rbx), %rax
movl %r13d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x94e4a
cmpb $0x1, 0x4(%rax,%rcx,8)
je 0x94e4a
movq %rbx, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0xc2e7a
testl %eax, %eax
jne 0x94e4a
movl %r13d, %r12d
xorl $0x1, %r12d
movq %rbx, %rdi
movl %r12d, %esi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x1, %eax
jne 0x94e4a
andq $0x0, 0xa8(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movl %r12d, %edx
leaq 0x4(%rsp), %rcx
leaq 0x48(%rsp), %r8
leaq 0x20(%rsp), %r9
leaq 0x70(%rsp), %rax
pushq %rax
pushq $0x1
leaq 0x40(%rsp), %rax
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
callq 0xc2ebf
addq $0x20, %rsp
cmpl $0x3, %eax
jne 0x94e4a
movq 0xb8(%rsp), %rsi
movl 0xa8(%rsp), %edx
movq %r15, %rdi
movl %ebp, %ecx
callq 0xc3788
testl %eax, %eax
jne 0x94e4a
movl 0x4(%rsp), %r12d
movq %rbx, %rdi
movl %r12d, %esi
leaq 0x4(%rsp), %rdx
callq 0xc2e7a
movl 0x4(%rsp), %eax
xorq $0x1, %rax
movl %eax, 0x4(%rsp)
movq 0x8(%rbx), %rcx
movq 0x28(%rbx), %rdx
movq (%rdx,%rax,8), %rdx
movl 0x20(%rsp), %eax
movq %rax, 0x68(%rsp)
movl %edx, %eax
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
leaq (%rcx,%rdx), %rsi
movq %rsi, 0x10(%rsp)
leaq (%rcx,%rdx), %r8
addq $0xc, %r8
movq %r8, %rdx
xorl %ecx, %ecx
movq %r15, 0x18(%rsp)
cmpq %rcx, %rax
je 0x94d30
cmpl $0x0, (%rdx)
js 0x94d27
movl -0x4(%rdx), %esi
xorl %r12d, %esi
cmpl $0x1, %esi
je 0x94d33
incq %rcx
addq $0x18, %rdx
jmp 0x94d12
movq %rax, %rcx
imulq $0x18, %rcx, %rbp
addq %rbp, 0x10(%rsp)
imulq $0x18, %rax, %r15
xorl %r12d, %r12d
movq %r8, 0x8(%rsp)
cmpq %r12, %r15
je 0x94e3e
cmpq %r12, %rbp
je 0x94e35
cmpl $0x0, (%r8,%r12)
js 0x94e35
movl -0x4(%r8,%r12), %edx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
leaq 0x4(%rsp), %rcx
leaq 0x48(%rsp), %r8
leaq 0x20(%rsp), %r9
pushq $0x0
movl 0x1f8(%rsp), %eax
pushq %rax
leaq 0x40(%rsp), %rax
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
callq 0xc2ebf
movq 0x28(%rsp), %r8
addq $0x20, %rsp
movq 0x20(%rsp), %rax
cmpq 0x68(%rsp), %rax
jle 0x94e35
cvtsi2sdq 0x30(%rsp), %xmm0
cvtsi2sd %rax, %xmm1
mulsd 0x14e70f(%rip), %xmm1   # 0x1e34d8
ucomisd %xmm1, %xmm0
jb 0x94e35
movq 0x10(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0x8(%rsp), %rax
movl -0x4(%rax,%r12), %r8d
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x50(%rsp), %r9
movl 0x1f0(%rsp), %eax
pushq %rax
pushq 0x60(%rsp)
movq 0x208(%rsp), %rax
pushq 0xa0(%rax)
pushq 0x78(%rsp)
leaq 0x170(%rsp), %rax
pushq %rax
leaq 0x108(%rsp), %rax
pushq %rax
callq 0xc3307
movq 0x38(%rsp), %r8
addq $0x30, %rsp
testl %eax, %eax
je 0x94e52
addq $0x18, %r12
jmp 0x94d48
movq 0x18(%rsp), %r15
movl 0x208(%rsp), %ebp
incl %r13d
jmp 0x94bf6
movq 0xa8(%rsp), %rax
movq 0xb8(%rsp), %rcx
xorl %edx, %edx
movl 0x208(%rsp), %ebp
movq 0x1f8(%rsp), %r8
movl %edx, %edx
cmpq %rdx, %rax
jbe 0x94e8c
movq 0x20(%rbx), %rsi
movl (%rcx,%rdx,4), %edi
shrl %edi
movb $0x1, 0x4(%rsi,%rdi,8)
incl %edx
jmp 0x94e73
xorl %ecx, %ecx
movl %ecx, %r15d
cmpq %r15, %rax
jbe 0x94ec2
movq 0xb8(%rsp), %rax
movl (%rax,%r15,4), %esi
shrl %esi
movq %rbx, %rdi
callq 0x80870
movq 0x1f8(%rsp), %r8
leal 0x1(%r15), %ecx
movq 0xa8(%rsp), %rax
jmp 0x94e8e
testq %r8, %r8
movq 0x18(%rsp), %r15
je 0x94f0b
movq %r8, %rax
movq 0x8(%rsp), %rcx
movl (%rcx,%r12), %edx
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andq %rcx, %rdx
movq 0x10(%rsp), %rsi
movl 0xc(%rsi), %r8d
andq %rcx, %r8
leaq 0x150(%rsp), %rsi
leaq 0xe0(%rsp), %rcx
movq %r15, %r9
pushq %rax
pushq 0x40(%rsp)
callq 0x82b96
popq %rax
popq %rcx
movq 0x200(%rsp), %rax
testq %rax, %rax
leaq 0xe0(%rsp), %r10
movq 0x8(%rsp), %rcx
je 0x94f6d
movl (%rcx,%r12), %edx
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andq %rcx, %rdx
movq 0x10(%rsp), %rsi
movl 0xc(%rsi), %r8d
andq %rcx, %r8
subq $0x8, %rsp
leaq 0x154307(%rip), %rdi    # 0x1e924f
leaq 0x158(%rsp), %rsi
movq %r10, %rcx
movq %r15, %r9
pushq %rax
callq 0x151bf8
leaq 0xf0(%rsp), %r10
movq 0x18(%rsp), %rcx
addq $0x10, %rsp
movq 0x28(%rsp), %rax
incl %eax
movq %rax, 0x28(%rsp)
andq $0x0, 0x118(%rsp)
movl -0x4(%rcx,%r12), %edx
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x4(%rsp), %rcx
leaq 0x48(%rsp), %r8
leaq 0x20(%rsp), %r9
pushq %r10
movl 0x1f8(%rsp), %eax
pushq %rax
leaq 0x40(%rsp), %rax
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
callq 0xc2ebf
addq $0x20, %rsp
movq 0x118(%rsp), %rax
movq 0x128(%rsp), %rcx
xorl %edx, %edx
movl %edx, %edx
cmpq %rdx, %rax
jbe 0x94e4a
movq 0x20(%rbx), %rsi
movl (%rcx,%rdx,4), %edi
shrl %edi
movb $0x2, 0x4(%rsi,%rdi,8)
incl %edx
jmp 0x94fcd
movq %rbx, %rdi
callq 0x694eb
movq %rbx, %rdi
callq 0x69638
movq 0xb8(%rsp), %rdi
callq 0x2220
movq 0x128(%rsp), %rdi
callq 0x2220
movq 0x198(%rsp), %rdi
callq 0x2220
movq 0x28(%rsp), %rax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq