// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/tovlp.cpp
// Function: asg_arc_decompress_mul(asg_t*, ma_ug_t*, asg_t*, unsigned int, unsigned int, ma_hit_t_alloc*, R_to_U*, utg_trans_t*)
int asg_arc_decompress_mul(asg_t *g, ma_ug_t *ug, asg_t *read_sg, uint32_t positive_flag, uint32_t negative_flag,
ma_hit_t_alloc* reverse_sources, R_to_U* ruIndex, utg_trans_t *o)
{
    double startTime = Get_T();
    asg_arc_t *as = NULL, *pm = NULL;
    u_trans_t *kt = NULL;
    uint32_t v, s, sv, nc, ns, n_vtx = g->n_seq * 2, n_reduced = 0, convex, pi, qn, tn;
    uint32_t return_flag, k, m, i, p_n, a_n, *a_a = NULL;
    uint8_t fp = 1, fa = 2, found;
    long long ll, tmp, max_stop_nodeLen, max_stop_baseLen;
    kvec_t_u32_warp tt; kv_init(tt.a);
    kvec_t_u32_warp stack; kv_init(stack.a);
    uint8_t *vis = NULL; CALLOC(vis, n_vtx);
    buf_t b;
    memset(&b, 0, sizeof(buf_t));
    pdq pq_p, pq_a;
    init_pdq(&pq_p, g->n_seq<<1);
    init_pdq(&pq_a, g->n_seq<<1);
    uint32_t *path_p = NULL, *path_q = NULL; 
    CALLOC(path_p, g->n_seq<<1);
    CALLOC(path_q, g->n_seq<<1);
    clean_mul_t *cl = init_clean_mul_t(g, ug, asm_opt.thread_num, fp, fa);
    
    while (get_min_dec(cl, positive_flag, negative_flag, o, &v))
    {
        if(g->seq[v>>1].del || g->seq[v>>1].c == ALTER_LABLE) continue;
        if(asg_arc_n(g, v) == 0 || get_real_length(g, v, NULL) != 1) continue;
        get_real_length(g, v, &s);
        if(get_real_length(g, s^1, NULL) < 2) continue;
        return_flag = get_unitig(g, ug, v^1, &convex, &ll, &tmp, &max_stop_nodeLen,&max_stop_baseLen, 1, NULL);
        if(return_flag == LOOP) continue;

        as = asg_arc_a(g, convex); ns = asg_arc_n(g, convex);
        for (i = nc = 0; i < ns; i++)
        {
            if(as[i].del) continue;
            if(get_real_length(g, as[i].v^1, NULL) < 2) break;
            nc++;
        }
        if(nc == 0 || i < ns) continue;

        tt.a.n = 0; s^=1; sv = v^1;
        dfs_set(g, sv, s>>1, &stack, &tt, vis, fp);
        p_n = tt.a.n;
        as = asg_arc_a(g, s); ns = asg_arc_n(g, s); found = 0;
        for (i = 0, pm = NULL; i < ns; i++)
        {
            if(as[i].del || as[i].v != sv) continue;
            pm = &(as[i]);
            break;
        }
 
        for (i = 0; i < ns; i++)
        {
            if(as[i].del || as[i].v == sv) continue;
            nc = dfs_set(g, as[i].v, s>>1, &stack, &tt, vis, fa);
            if(nc && check_trans_relation_by_path(sv, as[i].v, &pq_p, path_p, &(o->b0), &pq_a, path_q, &(o->b1), g, 
            vis, fp+fa, nc, 0.45, NULL))
            {
                found = 1;
            }
            a_a = tt.a.a + p_n; a_n = tt.a.n - p_n;
            for (k = 0; k < a_n; k++)
            {
                if(vis[a_a[k]]&fa) vis[a_a[k]] -= fa;
            }
            tt.a.n = p_n;
            if(found) break;
        }
        a_a = tt.a.a; a_n = p_n;
        for (k = 0; k < a_n; k++) vis[a_a[k]] = 0;

        if(found == 0) fprintf(stderr, "ERROR-found\n");
        if(found)
        {
            b.b.n = 0;
            get_unitig(g, ug, sv, &convex, &ll, &tmp, &max_stop_nodeLen, &max_stop_baseLen, 1, &b);
            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);
            }

            // uint32_t ui;
            // fprintf(stderr, "\n");
            // for (ui = 0; ui < o->b0.b.n; ui++)
            // {
            //     fprintf(stderr, "p-utg%.6ul\n", (o->b0.b.a[ui]>>1)+1);
            // }
            // for (ui = 0; ui < o->b1.b.n; ui++)
            // {
            //     fprintf(stderr, "a-utg%.6ul\n", (o->b1.b.a[ui]>>1)+1);
            // }


            if(o->b0.b.n < b.b.n)  fprintf(stderr, "ERROR-ll\n"); 
            o->b0.b.n = b.b.n; pi = o->k_trans.n;
            collect_trans_ovlp(__func__, &(o->b0), pm->ol, &(o->b1), as[i].ol, ug, o);

            for (k = m = pi; k < o->k_trans.n; k++)
            {
                kt = &(o->k_trans.a[k]);
                if(is_hap_ovlp(ug, read_sg, reverse_sources, ruIndex, &(kt->qn), 1, &(kt->tn), 1) || 
                      is_hap_ovlp(ug, read_sg, reverse_sources, ruIndex, &(kt->tn), 1, &(kt->qn), 1))
                {
                    qn = get_dir_v(kt->qn, &(o->b0));
                    tn = get_dir_v(kt->tn, &(o->b1));

                    if(dfs_reach(o->cug->g, qn, tn, &stack, &tt, vis) || dfs_reach(o->cug->g, tn, qn, &stack, &tt, vis))
                    {
                        // fprintf(stderr, "<<<<<<DEL: q-utg%.6ul (qs: %u, qe: %u), t-utg%.6ul (ts: %u, te: %u)\n", 
                        // kt->qn + 1, kt->qs, kt->qe, kt->tn + 1, kt->ts, kt->te);
                        continue;
                    }

                    // fprintf(stderr, "q-utg%.6ul (qs: %u, qe: %u), t-utg%.6ul (ts: %u, te: %u)\n", 
                    // kt->qn + 1, kt->qs, kt->qe, kt->tn + 1, kt->ts, kt->te);
                    o->k_trans.a[m] = *kt;
                    m++;
                }
                // else
                // {
                //     fprintf(stderr, "******delete: q-utg%.6ul (qs: %u, qe: %u), t-utg%.6ul (ts: %u, te: %u)\n", 
                //     kt->qn + 1, kt->qs, kt->qe, kt->tn + 1, kt->ts, kt->te);
                // }      
            }
            o->k_trans.n = m;
        }
    }
    
    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);
    asg_cleanup(g);
    asg_symm(g);
    free(b.b.a);
    kv_destroy(tt.a);
    kv_destroy(stack.a);
    free(vis);
    destory_pdq(&pq_p);
    destory_pdq(&pq_a);
    free(path_p);
    free(path_q);
    destroy_clean_mul_t(&cl);
    return n_reduced;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movq %r9, 0xb0(%rsp)
movl %r8d, %ebx
movl %ecx, %ebp
movq %rdx, 0xa8(%rsp)
movq %rsi, %r14
movq %rdi, %r13
callq 0x251c
movsd %xmm0, 0xb8(%rsp)
movl 0x14(%r13), %r15d
addl %r15d, %r15d
xorpd %xmm0, %xmm0
leaq 0x60(%rsp), %rax
movapd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x2120
movq %rax, 0x38(%rsp)
leaq 0xf0(%rsp), %rax
andq $0x0, 0x10(%rax)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rax)
leaq 0x110(%rsp), %rax
movapd %xmm0, 0x50(%rax)
movapd %xmm0, 0x40(%rax)
movapd %xmm0, 0x30(%rax)
movapd %xmm0, 0x20(%rax)
movapd %xmm0, 0x10(%rax)
movapd %xmm0, (%rax)
andq $0x0, 0x60(%rax)
movslq %r15d, %rsi
leaq 0x1e0(%rsp), %rdi
callq 0xfa585
movl 0x14(%r13), %eax
addl %eax, %eax
movslq %eax, %rsi
leaq 0x178(%rsp), %rdi
callq 0xfa585
movl 0x14(%r13), %eax
addl %eax, %eax
movslq %eax, %r15
pushq $0x4
popq %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x2120
movq %rax, 0xa0(%rsp)
movq %r15, %rdi
movl %ebx, %r15d
movq %r12, %rsi
movl %ebp, %r12d
callq 0x2120
movq %rax, 0x98(%rsp)
leaq 0x10099f(%rip), %rax    # 0x253758
movslq 0x7c(%rax), %rdx
pushq $0x1
popq %rcx
pushq $0x2
popq %r8
movq %r13, %rdi
movq %r14, 0x18(%rsp)
movq %r14, %rsi
callq 0x14fbf4
movq %rax, 0xc0(%rsp)
movq %rax, 0xc8(%rsp)
movq 0x288(%rsp), %rcx
leaq 0x160(%rcx), %rax
movq %rax, 0x50(%rsp)
leaq 0x1c8(%rcx), %rax
movq %rax, 0x48(%rsp)
movq %r13, 0x28(%rsp)
movl %ebx, 0xc(%rsp)
movl %ebp, 0x4(%rsp)
movq 0xc0(%rsp), %rdi
movl %r12d, %esi
movl %r15d, %edx
movq 0x288(%rsp), %rcx
leaq 0x5c(%rsp), %r8
callq 0x1529c4
testl %eax, %eax
je 0x153420
movq 0x20(%r13), %rax
movl 0x5c(%rsp), %ebp
movl %ebp, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x152e11
cmpb $0x1, 0x4(%rax,%rcx,8)
je 0x152e11
movq 0x28(%r13), %rax
cmpl $0x0, (%rax,%rbp,8)
je 0x152e11
movq %r13, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x1, %eax
jne 0x152e11
movq %r13, %rdi
movl %ebp, %esi
leaq 0x8(%rsp), %rdx
callq 0xc2e7a
movl 0x8(%rsp), %esi
xorl $0x1, %esi
movq %r13, %rdi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x2, %eax
jl 0x152e11
xorl $0x1, %ebp
movq %r13, %rdi
movq 0x18(%rsp), %rsi
movl %ebp, %edx
leaq 0x24(%rsp), %rcx
leaq 0xe8(%rsp), %r8
leaq 0xe0(%rsp), %r9
pushq $0x0
pushq $0x1
leaq 0xe0(%rsp), %rax
pushq %rax
leaq 0xf0(%rsp), %rax
pushq %rax
callq 0xc2ebf
addq $0x20, %rsp
cmpl $0x7, %eax
je 0x152e11
movq 0x8(%r13), %rax
movq 0x28(%r13), %rcx
movl 0x24(%rsp), %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ebx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
leaq (%rax,%rcx), %r15
addq $0xc, %r15
xorl %r14d, %r14d
xorl %r12d, %r12d
cmpq %r14, %rbx
je 0x152f35
cmpl $0x0, (%r15)
js 0x152f2c
movl -0x4(%r15), %esi
xorl $0x1, %esi
movq %r13, %rdi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x2, %eax
jl 0x152f35
incl %r12d
incq %r14
addq $0x18, %r15
jmp 0x152f08
testl %r12d, %r12d
movl 0xc(%rsp), %r15d
movl 0x4(%rsp), %r12d
je 0x152e11
cmpq %rbx, %r14
jb 0x152e11
andq $0x0, 0x60(%rsp)
movl 0x8(%rsp), %edx
movl %edx, %eax
xorl $0x1, %eax
movl %eax, 0x8(%rsp)
shrl %edx
subq $0x8, %rsp
movq %r13, %rdi
movl %ebp, %esi
leaq 0xf8(%rsp), %rcx
leaq 0x68(%rsp), %r8
movq 0x40(%rsp), %rbx
movq %rbx, %r9
pushq $0x1
callq 0x151cff
movq %rbx, %r9
addq $0x10, %rsp
movq 0x28(%r13), %rax
movl 0x8(%rsp), %ecx
movq (%rax,%rcx,8), %rcx
movq %rcx, %rax
shrq $0x20, %rax
imulq $0x18, %rax, %rdi
addq 0x8(%r13), %rdi
movq 0x60(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %rcx, 0x90(%rsp)
movl %ecx, %edx
movq %rdi, %rcx
movq %rdx, %rax
subq $0x1, %rax
jb 0x152fde
cmpl $0x0, 0xc(%rcx)
js 0x152fd8
cmpl %ebp, 0x8(%rcx)
je 0x152fec
addq $0x18, %rcx
jmp 0x152fc7
movq $0x0, 0x88(%rsp)
jmp 0x152ff4
movq %rcx, 0x88(%rsp)
movl 0x10(%rsp), %r13d
xorl %r15d, %r15d
movq %rdi, 0x30(%rsp)
movq %rdx, 0x40(%rsp)
cmpq %rdx, %r15
je 0x153107
imulq $0x18, %r15, %rbx
cmpl $0x0, 0xc(%rdi,%rbx)
js 0x1530ff
addq %rdi, %rbx
movl 0x8(%rbx), %esi
cmpl %ebp, %esi
je 0x1530ff
movl 0x8(%rsp), %edx
shrl %edx
subq $0x8, %rsp
movq 0x30(%rsp), %r14
movq %r14, %rdi
leaq 0xf8(%rsp), %rcx
leaq 0x68(%rsp), %r8
pushq $0x2
movq %r9, %r12
callq 0x151cff
popq %rcx
popq %rdx
testl %eax, %eax
je 0x1530ad
movl 0x8(%rbx), %esi
subq $0x8, %rsp
movl %ebp, %edi
leaq 0x1e8(%rsp), %rdx
movq 0xa8(%rsp), %rcx
movq 0x58(%rsp), %r8
leaq 0x180(%rsp), %r9
movsd 0x9e787(%rip), %xmm0    # 0x1f1810
pushq $0x0
pushq %rax
pushq $0x3
pushq %r12
pushq %r14
pushq 0x78(%rsp)
pushq 0xd0(%rsp)
callq 0xfad11
addq $0x40, %rsp
testl %eax, %eax
sete %cl
jmp 0x1530af
movb $0x1, %cl
movq 0x70(%rsp), %rax
leaq (%rax,%r13,4), %rdx
movl 0x60(%rsp), %esi
subl 0x10(%rsp), %esi
xorl %edi, %edi
movq 0x38(%rsp), %r9
movl 0x4(%rsp), %r12d
cmpq %rdi, %rsi
je 0x1530ec
movl (%rdx,%rdi,4), %r8d
movb (%r9,%r8), %r10b
testb $0x2, %r10b
je 0x1530e7
addb $-0x2, %r10b
movb %r10b, (%r9,%r8)
incq %rdi
jmp 0x1530cc
movq %r13, 0x60(%rsp)
testb %cl, %cl
movq 0x30(%rsp), %rdi
movq 0x40(%rsp), %rdx
je 0x153110
incq %r15
jmp 0x153006
movq 0x70(%rsp), %rax
xorl %ecx, %ecx
jmp 0x15311e
cmpq %rdx, %r15
setb %cl
movq %r15, 0x90(%rsp)
xorl %edx, %edx
movl 0xc(%rsp), %r15d
cmpq %rdx, %r13
je 0x153137
movl (%rax,%rdx,4), %esi
movb $0x0, (%r9,%rsi)
incq %rdx
jmp 0x153125
testb %cl, %cl
je 0x1533d1
andq $0x0, 0x148(%rsp)
movq 0x28(%rsp), %r14
movq %r14, %rdi
movq 0x18(%rsp), %rsi
movl %ebp, %edx
leaq 0x24(%rsp), %rcx
leaq 0xe8(%rsp), %r8
leaq 0xe0(%rsp), %r9
leaq 0x110(%rsp), %rax
pushq %rax
pushq $0x1
leaq 0xe0(%rsp), %rax
pushq %rax
leaq 0xf0(%rsp), %rax
pushq %rax
callq 0xc2ebf
addq $0x20, %rsp
movq 0x148(%rsp), %rax
movq 0x158(%rsp), %rcx
xorl %edx, %edx
movq 0x288(%rsp), %r13
movl %edx, %edx
cmpq %rdx, %rax
jbe 0x1531c5
movq 0x20(%r14), %rsi
movl (%rcx,%rdx,4), %edi
shrl %edi
movb $0x1, 0x4(%rsi,%rdi,8)
incl %edx
jmp 0x1531ac
xorl %ecx, %ecx
movl %ecx, %ebx
cmpq %rbx, %rax
jbe 0x1531f0
movq 0x158(%rsp), %rax
movl (%rax,%rbx,4), %esi
shrl %esi
movq %r14, %rdi
callq 0x1534e3
leal 0x1(%rbx), %ecx
movq 0x148(%rsp), %rax
jmp 0x1531c7
cmpq %rax, 0x198(%r13)
jb 0x1533f7
movq %rax, 0x198(%r13)
movl 0xb0(%r13), %r15d
movq 0x88(%rsp), %rax
movl 0xc(%rax), %edx
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andq %rcx, %rdx
movl 0x90(%rsp), %eax
imulq $0x18, %rax, %rax
movq 0x30(%rsp), %rsi
movl 0xc(%rsi,%rax), %r8d
andq %rcx, %r8
subq $0x8, %rsp
movq 0x58(%rsp), %rsi
movq 0x50(%rsp), %rcx
movq 0x20(%rsp), %r9
pushq %r13
callq 0x151bf8
addq $0x10, %rsp
movl %r15d, 0x10(%rsp)
movl %r15d, %r15d
movl 0x10(%rsp), %r14d
cmpq %r15, 0xb0(%r13)
jbe 0x1533bb
movq 0xc0(%r13), %rbx
imulq $0x30, %r15, %rax
leaq (%rbx,%rax), %r12
addq $0x8, %r12
movq %rax, 0x30(%rsp)
leaq 0x14(%rbx,%rax), %rbp
movq 0x18(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0xb0(%rsp), %rdx
movq 0x280(%rsp), %rcx
movq %r12, %r8
pushq $0x1
popq %r9
pushq $0x1
pushq %rbp
callq 0x152124
popq %rcx
popq %rdx
testl %eax, %eax
jne 0x1532f4
movq 0x18(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0xb0(%rsp), %rdx
movq 0x280(%rsp), %rcx
movq %rbp, %r8
pushq $0x1
popq %r9
pushq $0x1
pushq %r12
callq 0x152124
popq %rcx
popq %rdx
testl %eax, %eax
je 0x1533b4
movq %r14, 0x40(%rsp)
movl (%r12), %edi
movq 0x50(%rsp), %rsi
callq 0x152318
movl %eax, %r12d
movl (%rbp), %edi
movq 0x48(%rsp), %rsi
callq 0x152318
movl %eax, %ebp
movq 0x108(%r13), %rax
movq 0x18(%rax), %rdi
movl %r12d, %esi
movl %ebp, %edx
leaq 0xf0(%rsp), %rcx
leaq 0x60(%rsp), %r8
movq %r13, %r14
movq 0x38(%rsp), %r13
movq %r13, %r9
callq 0x151f74
testl %eax, %eax
jne 0x1533a4
movq 0x108(%r14), %rax
movq 0x18(%rax), %rdi
movl %ebp, %esi
movl %r12d, %edx
leaq 0xf0(%rsp), %rcx
leaq 0x60(%rsp), %r8
movq %r13, %r9
callq 0x151f74
testl %eax, %eax
jne 0x1533a4
addq 0x30(%rsp), %rbx
movq 0xc0(%r14), %rax
imulq $0x30, 0x40(%rsp), %rcx
movupd (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x20(%rax,%rcx)
movups %xmm1, 0x10(%rax,%rcx)
movupd %xmm0, (%rax,%rcx)
incl 0x10(%rsp)
movl 0x4(%rsp), %r12d
movq %r14, %r13
incl %r15d
jmp 0x153259
movl 0x4(%rsp), %r12d
jmp 0x1533ac
movq %r14, 0xb0(%r13)
movq 0x28(%rsp), %r13
movl 0xc(%rsp), %r15d
jmp 0x152e11
movq 0xffc08(%rip), %rax     # 0x252fe0
movq (%rax), %rcx
leaq 0x9e469(%rip), %rdi     # 0x1f184b
pushq $0xc
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movq 0x28(%rsp), %r13
jmp 0x152e11
movq 0xffbe2(%rip), %rax     # 0x252fe0
movq (%rax), %rcx
leaq 0x9e450(%rip), %rdi     # 0x1f1858
pushq $0x9
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movq 0x148(%rsp), %rax
jmp 0x1531fd
movq 0xffbb9(%rip), %rax     # 0x252fe0
movq (%rax), %r15
callq 0x251c
subsd 0xb8(%rsp), %xmm0
leaq 0x94cb2(%rip), %rsi     # 0x1e80f1
leaq 0x9e41c(%rip), %rdx     # 0x1f1862
movq %r15, %rdi
movb $0x1, %al
callq 0x22b0
movq %r13, %rdi
callq 0x694eb
movq %r13, %rdi
callq 0x69638
movq 0x158(%rsp), %rdi
callq 0x2220
movq 0x70(%rsp), %rdi
callq 0x2220
movq 0x100(%rsp), %rdi
callq 0x2220
movq 0x38(%rsp), %rdi
callq 0x2220
leaq 0x1e0(%rsp), %rdi
callq 0xfa60b
leaq 0x178(%rsp), %rdi
callq 0xfa60b
movq 0xa0(%rsp), %rdi
callq 0x2220
movq 0x98(%rsp), %rdi
callq 0x2220
leaq 0xc8(%rsp), %rdi
callq 0x14fd79
xorl %eax, %eax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq