// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movl 0x28(%rbp), %eax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x20(%rbp)
movq %rsi, -0x28(%rbp)
movq %rdx, -0x30(%rbp)
movq %rcx, -0x38(%rbp)
movq %r8, -0x40(%rbp)
movq %r9, -0x48(%rbp)
callq 0x2510
movsd %xmm0, -0x50(%rbp)
movq -0x20(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
leaq -0x100(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
leaq -0x168(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
leaq -0x1d0(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
movl $0x0, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x58(%rbp), %eax
jae 0x11955e
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x119017
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
jne 0x11901c
jmp 0x119550
movq -0x20(%rbp), %rdi
movl -0x54(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x0, %eax
je 0x119036
jmp 0x119550
movq -0x20(%rbp), %rdi
movl -0x54(%rbp), %esi
xorl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x1, %eax
je 0x119053
jmp 0x119550
movq $0x0, -0xc8(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x54(%rbp), %edx
xorl $0x1, %edx
leaq -0x60(%rbp), %rcx
leaq -0x88(%rbp), %r8
leaq -0x80(%rbp), %r9
leaq -0x90(%rbp), %r11
leaq -0x98(%rbp), %r10
leaq -0x100(%rbp), %rax
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x166c30
movl %eax, -0x6c(%rbp)
cmpl $0x3, -0x6c(%rbp)
je 0x1190b9
jmp 0x119550
movq -0x28(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movq -0xc8(%rbp), %rax
movl %eax, %edx
movl 0x28(%rbp), %ecx
callq 0x167d40
cmpl $0x0, %eax
je 0x1190df
jmp 0x119550
movl -0x60(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x64(%rbp)
movq -0x20(%rbp), %rdi
movl -0x60(%rbp), %esi
leaq -0x60(%rbp), %rdx
callq 0x166b50
movl -0x60(%rbp), %eax
xorl $0x1, %eax
movl %eax, -0x60(%rbp)
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x60(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x1d8(%rbp)
movq -0x80(%rbp), %rax
movl %eax, -0x1dc(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x60(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x1e8(%rbp)
movl $0x0, -0x1d4(%rbp)
movl -0x1d4(%rbp), %eax
cmpl -0x1d8(%rbp), %eax
jae 0x1191b3
movq -0x1e8(%rbp), %rax
movl -0x1d4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x119182
jmp 0x1191a2
movq -0x1e8(%rbp), %rax
movl -0x1d4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0x64(%rbp), %eax
jne 0x1191a0
jmp 0x1191b3
jmp 0x1191a2
movl -0x1d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d4(%rbp)
jmp 0x119153
movl -0x1d4(%rbp), %eax
movl %eax, -0x70(%rbp)
movl $0x0, -0x1d4(%rbp)
movl -0x1d4(%rbp), %eax
cmpl -0x1d8(%rbp), %eax
jae 0x11954e
movq -0x1e8(%rbp), %rax
movl -0x1d4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x1191fc
jmp 0x11953a
movl -0x1d4(%rbp), %eax
cmpl -0x70(%rbp), %eax
jne 0x11920c
jmp 0x11953a
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x1e8(%rbp), %rax
movl -0x1d4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
movl 0x10(%rbp), %eax
leaq -0x60(%rbp), %rcx
leaq -0x88(%rbp), %r8
leaq -0x80(%rbp), %r9
leaq -0x90(%rbp), %r11
leaq -0x98(%rbp), %r10
xorl %ebx, %ebx
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
callq 0x166c30
movl %eax, -0x6c(%rbp)
movq -0x80(%rbp), %rax
movl -0x1dc(%rbp), %ecx
cmpq %rcx, %rax
jle 0x119538
cvtsi2sdq -0x98(%rbp), %xmm0
cvtsi2sdq -0x80(%rbp), %xmm1
movsd 0x268f7b(%rip), %xmm2   # 0x382210
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x119538
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x1e8(%rbp), %rax
movl -0x70(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq -0x1e8(%rbp), %rax
movl -0x1d4(%rbp), %r8d
imulq $0x18, %r8, %r8
addq %r8, %rax
movl 0x8(%rax), %r8d
movq -0x38(%rbp), %r9
movq -0x48(%rbp), %rbx
movq 0x18(%rbp), %rax
movq 0xa0(%rax), %r11
movq -0x40(%rbp), %rax
movl %eax, %r10d
movl 0x10(%rbp), %eax
leaq -0x168(%rbp), %r15
leaq -0x1d0(%rbp), %r14
movq %r15, (%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movl %r10d, 0x20(%rsp)
movl %eax, 0x28(%rsp)
callq 0x1674d0
movl %eax, -0x68(%rbp)
cmpl $0x0, -0x68(%rbp)
je 0x119336
jmp 0x11953a
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpq -0xc8(%rbp), %rax
jae 0x11937b
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movq -0xb8(%rbp), %rcx
movl -0x74(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
movb $0x1, 0x4(%rax,%rcx,8)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x119346
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpq -0xc8(%rbp), %rax
jae 0x1193b1
movq -0x20(%rbp), %rdi
movq -0xb8(%rbp), %rax
movl -0x74(%rbp), %ecx
movl (%rax,%rcx,4), %esi
shrl %esi
callq 0xf6800
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x119382
cmpq $0x0, 0x18(%rbp)
je 0x119425
movq -0x1e8(%rbp), %rax
movl -0x1d4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
movq -0x1e8(%rbp), %rax
movl -0x70(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %r8d
movq -0x28(%rbp), %r9
movq -0x30(%rbp), %r10
movq 0x18(%rbp), %rax
leaq 0x26eb31(%rip), %rdi    # 0x387f3a
leaq -0x1d0(%rbp), %rsi
leaq -0x168(%rbp), %rcx
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0xfab70
cmpq $0x0, 0x20(%rbp)
je 0x119490
movq -0x1e8(%rbp), %rax
movl -0x1d4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
movq -0x1e8(%rbp), %rax
movl -0x70(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %r8d
movq -0x28(%rbp), %r9
movq 0x20(%rbp), %rax
leaq 0x26eac1(%rip), %rdi    # 0x387f3a
leaq -0x1d0(%rbp), %rsi
leaq -0x168(%rbp), %rcx
movq %rax, (%rsp)
callq 0x2721e0
movq $0x0, -0x130(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x1e8(%rbp), %rax
movl -0x1d4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
movl 0x10(%rbp), %r10d
leaq -0x60(%rbp), %rcx
leaq -0x88(%rbp), %r8
leaq -0x80(%rbp), %r9
leaq -0x90(%rbp), %rbx
leaq -0x98(%rbp), %r11
leaq -0x168(%rbp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x166c30
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpq -0x130(%rbp), %rax
jae 0x119536
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x120(%rbp), %rcx
movl -0x74(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
movb $0x2, 0x4(%rax,%rcx,8)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x119501
jmp 0x11954e
jmp 0x11953a
movl -0x1d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d4(%rbp)
jmp 0x1191c6
jmp 0x119550
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x118fd8
movq -0x20(%rbp), %rdi
callq 0xcdd10
movq -0x20(%rbp), %rdi
callq 0xce020
movq -0xb8(%rbp), %rdi
callq 0x2210
movq -0x120(%rbp), %rdi
callq 0x2210
movq -0x188(%rbp), %rdi
callq 0x2210
movl -0x5c(%rbp), %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)