// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_cut_trio_long_equal_tips_assembly(asg_t*, ma_ug_t*, asg_t*, ma_hit_t_alloc*, long long, R_to_U*, unsigned int, hap_cov_t*, utg_trans_t*)
int asg_arc_cut_trio_long_equal_tips_assembly(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 trio_flag,
hap_cov_t *cov, utg_trans_t *o)
{
    double startTime = Get_T();
    uint32_t v, n_vtx = g->n_seq * 2, n_reduced = 0, convex, flag, is_hap, n_tips, return_flag, k;
    long long ll, base_maxLen, base_maxLen_i, max_stop_nodeLen, max_stop_baseLen, tmp;
    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, 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;

        base_maxLen = -1;
        base_maxLen_i = -1;
        n_tips = 0;
        is_hap = 0;

        for (i = 0; i < nv; i++)
        {
            if(av[i].del) continue;
            return_flag = get_unitig(g, ug, av[i].v, &convex, &tmp, &ll, &max_stop_nodeLen, 
            &max_stop_baseLen, 1, NULL);

            if(return_flag==LOOP) continue;
            if(return_flag==END_TIPS) n_tips++;

            if(base_maxLen < ll)
            {
                base_maxLen = ll;
                base_maxLen_i = i;
            }
        }

        if(n_tips==0) continue;
        ///all the unitigs here are tips
        if(n_arc == n_tips)
        {
            renew_longest_tip_by_drop(g, ug, av, nv, &base_maxLen, 
            &base_maxLen_i, 1, &b, trio_flag);
        }

        for (i = 0; i < nv; i++)
        {
            if(i==base_maxLen_i) continue;
            if(av[i].del) continue;

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

            if(return_flag != END_TIPS) continue;
            if(trio_check(ug, b.b.a, b.b.n, trio_flag)) continue;

            flag = check_different_haps(g, ug, read_sg, av[base_maxLen_i].v, av[i].v,
            reverse_sources, &b_0, &b_1, ruIndex, cov->is_r_het, miniedgeLen, 1);

            // #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);
            }

            if(cov)
            {
                collect_trans_cov(__func__, &b_0, av[base_maxLen_i].ol, &b_1, av[i].ol, ug, read_sg, cov);
            } 

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

            is_hap++;
        }

        if(is_hap > 0)
        {
            i = base_maxLen_i;
            b.b.n = 0;
            get_unitig(g, ug, av[i].v, &convex, &tmp, &ll, &max_stop_nodeLen, 
            &max_stop_baseLen, 1, &b);

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

    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 %r14
pushq %rbx
subq $0x210, %rsp            # imm = 0x210
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movq %r9, -0x40(%rbp)
callq 0x2510
movsd %xmm0, -0x48(%rbp)
movq -0x18(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
leaq -0x108(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
leaq -0x170(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
leaq -0x1d8(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jae 0x118558
movl $0x0, -0x1e0(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x4c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x1e4(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x4c(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x1f0(%rbp)
cmpl $0x2, -0x1e4(%rbp)
jb 0x118003
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x4c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x118003
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x4c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
jne 0x118008
jmp 0x11854a
movq -0x18(%rbp), %rdi
movl -0x4c(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
movl %eax, -0x1e0(%rbp)
cmpl $0x2, -0x1e0(%rbp)
jae 0x11802c
jmp 0x11854a
movq $-0x1, -0x80(%rbp)
movq $-0x1, -0x88(%rbp)
movl $0x0, -0x64(%rbp)
movl $0x0, -0x60(%rbp)
movl $0x0, -0x1dc(%rbp)
movl -0x1dc(%rbp), %eax
cmpl -0x1e4(%rbp), %eax
jae 0x11813a
movq -0x1f0(%rbp), %rax
movl -0x1dc(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x11808d
jmp 0x118126
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x1f0(%rbp), %rax
movl -0x1dc(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
leaq -0x58(%rbp), %rcx
leaq -0xa0(%rbp), %r8
leaq -0x78(%rbp), %r9
leaq -0x90(%rbp), %r10
leaq -0x98(%rbp), %rax
xorl %r11d, %r11d
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
callq 0x166c30
movl %eax, -0x68(%rbp)
cmpl $0x7, -0x68(%rbp)
jne 0x1180f6
jmp 0x118126
cmpl $0x5, -0x68(%rbp)
jne 0x118105
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
movq -0x80(%rbp), %rax
cmpq -0x78(%rbp), %rax
jge 0x118124
movq -0x78(%rbp), %rax
movq %rax, -0x80(%rbp)
movl -0x1dc(%rbp), %eax
movq %rax, -0x88(%rbp)
jmp 0x118126
movl -0x1dc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1dc(%rbp)
jmp 0x118057
cmpl $0x0, -0x64(%rbp)
jne 0x118145
jmp 0x11854a
movl -0x1e0(%rbp), %eax
cmpl -0x64(%rbp), %eax
jne 0x11818f
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x1f0(%rbp), %rdx
movl -0x1e4(%rbp), %ecx
movl 0x10(%rbp), %eax
leaq -0x80(%rbp), %r8
leaq -0x88(%rbp), %r9
leaq -0x108(%rbp), %r10
movl $0x1, (%rsp)
movq %r10, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0x117b40
movl $0x0, -0x1dc(%rbp)
movl -0x1dc(%rbp), %eax
cmpl -0x1e4(%rbp), %eax
jae 0x11848a
movl -0x1dc(%rbp), %eax
cmpq -0x88(%rbp), %rax
jne 0x1181bf
jmp 0x118476
movq -0x1f0(%rbp), %rax
movl -0x1dc(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x1181e3
jmp 0x118476
movq $0x0, -0xd0(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x1f0(%rbp), %rax
movl -0x1dc(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
leaq -0x58(%rbp), %rcx
leaq -0xa0(%rbp), %r8
leaq -0x78(%rbp), %r9
leaq -0x90(%rbp), %r11
leaq -0x98(%rbp), %r10
leaq -0x108(%rbp), %rax
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x166c30
movl %eax, -0x68(%rbp)
cmpl $0x5, -0x68(%rbp)
je 0x11825a
jmp 0x118476
movq -0x20(%rbp), %rdi
movq -0xc0(%rbp), %rsi
movq -0xd0(%rbp), %rax
movl %eax, %edx
movl 0x10(%rbp), %ecx
callq 0x167d40
cmpl $0x0, %eax
je 0x118280
jmp 0x118476
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x1f0(%rbp), %rax
imulq $0x18, -0x88(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq -0x1f0(%rbp), %rax
movl -0x1dc(%rbp), %r8d
imulq $0x18, %r8, %r8
addq %r8, %rax
movl 0x8(%rax), %r8d
movq -0x30(%rbp), %r9
movq -0x40(%rbp), %r11
movq 0x18(%rbp), %rax
movq 0xa0(%rax), %r10
movq -0x38(%rbp), %rax
leaq -0x170(%rbp), %r14
leaq -0x1d8(%rbp), %rbx
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movl %eax, 0x20(%rsp)
movl $0x1, 0x28(%rsp)
callq 0x1674d0
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
je 0x118311
jmp 0x118476
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpq -0xd0(%rbp), %rax
jae 0x118356
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq -0xc0(%rbp), %rcx
movl -0x6c(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
movb $0x1, 0x4(%rax,%rcx,8)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x118321
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpq -0xd0(%rbp), %rax
jae 0x11838c
movq -0x18(%rbp), %rdi
movq -0xc0(%rbp), %rax
movl -0x6c(%rbp), %ecx
movl (%rax,%rcx,4), %esi
shrl %esi
callq 0xf6800
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x11835d
cmpq $0x0, 0x18(%rbp)
je 0x118401
movq -0x1f0(%rbp), %rax
imulq $0x18, -0x88(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
movq -0x1f0(%rbp), %rax
movl -0x1dc(%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 -0x20(%rbp), %r9
movq -0x28(%rbp), %r10
movq 0x18(%rbp), %rax
leaq 0x26fb2b(%rip), %rdi    # 0x387f10
leaq -0x170(%rbp), %rsi
leaq -0x1d8(%rbp), %rcx
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0xfab70
cmpq $0x0, 0x20(%rbp)
je 0x11846d
movq -0x1f0(%rbp), %rax
imulq $0x18, -0x88(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
movq -0x1f0(%rbp), %rax
movl -0x1dc(%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 -0x20(%rbp), %r9
movq 0x20(%rbp), %rax
leaq 0x26faba(%rip), %rdi    # 0x387f10
leaq -0x170(%rbp), %rsi
leaq -0x1d8(%rbp), %rcx
movq %rax, (%rsp)
callq 0x2721e0
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
movl -0x1dc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1dc(%rbp)
jmp 0x118199
cmpl $0x0, -0x60(%rbp)
jbe 0x118548
movq -0x88(%rbp), %rax
movl %eax, -0x1dc(%rbp)
movq $0x0, -0xd0(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x1f0(%rbp), %rax
movl -0x1dc(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
leaq -0x58(%rbp), %rcx
leaq -0xa0(%rbp), %r8
leaq -0x78(%rbp), %r9
leaq -0x90(%rbp), %r11
leaq -0x98(%rbp), %r10
leaq -0x108(%rbp), %rax
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x166c30
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpq -0xd0(%rbp), %rax
jae 0x118546
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq -0xc0(%rbp), %rcx
movl -0x6c(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
movb $0x2, 0x4(%rax,%rcx,8)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x118511
jmp 0x118548
jmp 0x11854a
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x117f73
movq -0x18(%rbp), %rdi
callq 0xcdd10
movq -0x18(%rbp), %rdi
callq 0xce020
movq -0xc0(%rbp), %rdi
callq 0x2210
movq -0x128(%rbp), %rdi
callq 0x2210
movq -0x190(%rbp), %rdi
callq 0x2210
movl -0x54(%rbp), %eax
addq $0x210, %rsp            # imm = 0x210
popq %rbx
popq %r14
popq %rbp
retq
nopl (%rax)