// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: collect_hc_reverse_links(hc_links*, ma_ug_t*, bubble_type*)
void collect_hc_reverse_links(hc_links* link, ma_ug_t* ug, bubble_type* bub)
{
    uint64_t i, j, k, d = RC_0, m, pre;
    uint32_t beg, sink, n, v, *a = NULL;
    kvec_t_u32_warp stack, result;
    hc_edge *e = NULL;
    kv_init(stack.a); kv_init(result.a);
    ///clean all reverse overlaps within bubbles
    ///might be wrong
    for (i = 0; i < bub->f_bub; i++)
    {
        get_bubbles(bub, i, &beg, &sink, &a, &n, NULL);
        for (k = 0; k < n; k++)
        {
            v = a[k]>>1;
            for (j = 0; j < link->a.a[v].f.n; j++)
            {
                if(link->a.a[v].f.a[j].del) continue;
                e = get_hc_edge(link, link->a.a[v].f.a[j].uID, v, 1);
                e->del = 1;
            }
            link->a.a[v].f.n = 0;
        }

        v = beg>>1; 
        if(IF_HOM(v, *bub))
        {
            for (j = 0; j < link->a.a[v].f.n; j++)
            {
                if(link->a.a[v].f.a[j].del) continue;
                e = get_hc_edge(link, link->a.a[v].f.a[j].uID, v, 1);
                e->del = 1;
            }
            link->a.a[v].f.n = 0;
        }

        v = sink>>1;
        if(IF_HOM(v, *bub))
        {
            for (j = 0; j < link->a.a[v].f.n; j++)
            {
                if(link->a.a[v].f.a[j].del) continue;
                e = get_hc_edge(link, link->a.a[v].f.a[j].uID, v, 1);
                e->del = 1;
            }
            link->a.a[v].f.n = 0;
        }
    }

    for (i = 0; i < bub->f_bub; i++)
    {
        get_bubbles(bub, i, &beg, &sink, &a, &n, NULL);
        if(n == 2)
        {
            push_hc_edge(&(link->a.a[a[0]>>1]), a[1]>>1, 1, 1, &d);
            push_hc_edge(&(link->a.a[a[1]>>1]), a[0]>>1, 1, 1, &d);
            continue;
        }
        ///for complex bubbles, shouldn't have any assumption
        ///if(i >= bub->s_bub) continue;

        beg = beg>>1; sink = sink>>1;
        for (k = 0; k < n; k++)
        {
            v = a[k]>>1;
            dfs_bubble(ug->g, &stack, &result, v, beg, sink);
            set_reverse_links(a, n, &result, v, link);
        }
    }
    
    uint8_t* vis_flag = NULL;
    MALLOC(vis_flag, ug->g->n_seq*2);
    ///for broken bubbles
    for (i = bub->f_bub; i < bub->f_bub + bub->b_bub; i++)
    {
        get_bubbles(bub, i, &beg, &sink, &a, &n, NULL);

        for (k = 0; k < n; k++)
        {
            v = a[k];
            dfs_bubble_broken(ug->g, &stack, &result, vis_flag, ug->g->n_seq*2, v, beg, sink);
            set_reverse_links(a, n, &result, v>>1, link);
        }
    }
    
    kv_destroy(stack.a); kv_destroy(result.a); free(vis_flag);


    for (i = 0; i < link->a.n; i++)
    {
        for (k = m = 0; k < link->a.a[i].f.n; k++)
        {
            if(link->a.a[i].f.a[k].del) continue;
            link->a.a[i].f.a[m] = link->a.a[i].f.a[k];
            m++;
        }
        link->a.a[i].f.n = m;
        radix_sort_hc_edge_u(link->a.a[i].f.a, link->a.a[i].f.a + link->a.a[i].f.n);

        for (k = m = 0, pre = (uint64_t)-1; k < link->a.a[i].f.n; k++)
        {
            if(link->a.a[i].f.a[k].del) continue;
            if(link->a.a[i].f.a[k].uID == pre)
            {
                if(link->a.a[i].f.a[k].dis == RC_0) link->a.a[i].f.a[m-1].dis = RC_0;
                continue;
            }
             
            pre = link->a.a[i].f.a[k].uID;
            link->a.a[i].f.a[m] = link->a.a[i].f.a[k];
            m++;
        }
        link->a.a[i].f.n = m;
        radix_sort_hc_edge_d(link->a.a[i].f.a, link->a.a[i].f.a + link->a.a[i].f.n);
    }


    // hc_edge *e = NULL;
    // for (i = 0; i < link->a.n; i++)
    // {
    //     for (k = 0; k < link->a.a[i].f.n; k++)
    //     {
    //         if(link->a.a[i].f.a[k].del) continue;
    //         e = get_hc_edge(link, link->a.a[i].f.a[k].uID, i, 1);
    //         if(e == NULL) fprintf(stderr, "ERROR\n");
    //     }
    // }
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdx, %r15
movq %rsi, 0x40(%rsp)
andq $0x0, 0x58(%rsp)
leaq 0x28(%rsp), %rax
andq $0x0, (%rax)
movq %rdi, %rbx
andq $0x0, 0x90(%rsp)
xorps %xmm0, %xmm0
andq $0x0, 0x70(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x60(%rsp)
xorl %esi, %esi
pushq $0x1
popq %r12
movq %rdx, 0x30(%rsp)
movq 0x80(%r15), %r14
cmpq %r14, %rsi
jae 0xfe483
andq $0x0, (%rsp)
movq %r15, %rdi
movq %rsi, 0x48(%rsp)
leaq 0x24(%rsp), %rdx
leaq 0x20(%rsp), %rcx
leaq 0x28(%rsp), %r8
leaq 0x1c(%rsp), %r9
callq 0xf6ab5
movl 0x1c(%rsp), %eax
movq %rax, 0x38(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x50(%rsp)
xorl %r15d, %r15d
cmpq 0x38(%rsp), %r15
je 0xfe399
movq 0x50(%rsp), %rax
movl (%rax,%r15,4), %r14d
shrl %r14d
pushq $0x18
popq %r13
xorl %ebp, %ebp
movq 0x10(%rbx), %rax
imulq $0x30, %r14, %rcx
addq %rcx, %rax
addq $0x18, %rax
cmpq (%rax), %rbp
jae 0xfe390
movq 0x10(%rax), %rax
cmpb $0x0, (%rax,%r13)
js 0xfe387
movl -0x10(%rax,%r13), %esi
movq %rbx, %rdi
movq %r14, %rdx
movq %r12, %rcx
callq 0xfcabc
orb $-0x80, 0x18(%rax)
incq %rbp
addq $0x28, %r13
jmp 0xfe351
andq $0x0, (%rax)
incq %r15
jmp 0xfe338
movl 0x24(%rsp), %r14d
shrl %r14d
movq 0x30(%rsp), %r15
movq (%r15), %rcx
movq 0x80(%r15), %rax
movl (%rcx,%r14,4), %edx
incq %rax
cmpq %rdx, %rax
jae 0xfe417
imulq $0x30, %r14, %r15
pushq $0x18
popq %r13
xorl %ebp, %ebp
movq 0x10(%rbx), %rax
addq %r15, %rax
addq $0x18, %rax
cmpq (%rax), %rbp
jae 0xfe401
movq 0x10(%rax), %rax
cmpb $0x0, (%rax,%r13)
js 0xfe3f8
movl -0x10(%rax,%r13), %esi
movq %rbx, %rdi
movq %r14, %rdx
movq %r12, %rcx
callq 0xfcabc
orb $-0x80, 0x18(%rax)
incq %rbp
addq $0x28, %r13
jmp 0xfe3c6
andq $0x0, (%rax)
movq 0x30(%rsp), %r15
movq (%r15), %rcx
movq 0x80(%r15), %rax
incq %rax
movl 0x20(%rsp), %r14d
shrl %r14d
movl (%rcx,%r14,4), %ecx
cmpq %rcx, %rax
jae 0xfe476
imulq $0x30, %r14, %r15
pushq $0x18
popq %r13
xorl %ebp, %ebp
movq 0x10(%rbx), %rax
addq %r15, %rax
addq $0x18, %rax
cmpq (%rax), %rbp
jae 0xfe46d
movq 0x10(%rax), %rax
cmpb $0x0, (%rax,%r13)
js 0xfe464
movl -0x10(%rax,%r13), %esi
movq %rbx, %rdi
movq %r14, %rdx
movq %r12, %rcx
callq 0xfcabc
orb $-0x80, 0x18(%rax)
incq %rbp
addq $0x28, %r13
jmp 0xfe432
andq $0x0, (%rax)
movq 0x30(%rsp), %r15
movq 0x48(%rsp), %rsi
incq %rsi
jmp 0xfe2ec
xorl %r12d, %r12d
leaq 0x80(%rsp), %r13
leaq 0x60(%rsp), %rbp
cmpq %r14, %r12
jae 0xfe597
andq $0x0, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
leaq 0x24(%rsp), %rdx
leaq 0x20(%rsp), %rcx
leaq 0x28(%rsp), %r8
leaq 0x1c(%rsp), %r9
callq 0xf6ab5
movl 0x1c(%rsp), %eax
cmpl $0x2, %eax
jne 0xfe538
movq 0x28(%rsp), %rax
movl (%rax), %ecx
movl 0x4(%rax), %esi
shrl %ecx
imulq $0x30, %rcx, %rdi
addq 0x10(%rbx), %rdi
shrl %esi
movsd 0xe4db9(%rip), %xmm0    # 0x1e32a0
pushq $0x1
popq %r14
movl %r14d, %edx
leaq 0x58(%rsp), %r15
movq %r15, %rcx
callq 0xfa2fb
movq 0x28(%rsp), %rax
movl (%rax), %esi
movl 0x4(%rax), %eax
shrl %eax
imulq $0x30, %rax, %rdi
addq 0x10(%rbx), %rdi
shrl %esi
movsd 0xe4d87(%rip), %xmm0    # 0x1e32a0
movl %r14d, %edx
movq %r15, %rcx
callq 0xfa2fb
incq %r12
movq 0x30(%rsp), %r15
movq 0x80(%r15), %r14
jmp 0xfe493
shrl 0x24(%rsp)
shrl 0x20(%rsp)
xorl %r15d, %r15d
movl %eax, %eax
cmpq %rax, %r15
jae 0xfe524
movq 0x28(%rsp), %rax
movl (%rax,%r15,4), %r14d
shrl %r14d
movq 0x40(%rsp), %rax
movq 0x18(%rax), %rdi
movl 0x24(%rsp), %r8d
movl 0x20(%rsp), %r9d
movq %r13, %rsi
movq %rbp, %rdx
movl %r14d, %ecx
callq 0xf6fe5
movq 0x28(%rsp), %rdi
movl 0x1c(%rsp), %esi
movq %rbp, %rdx
movl %r14d, %ecx
movq %rbx, %r8
callq 0xfe1ba
incq %r15
movl 0x1c(%rsp), %eax
jmp 0xfe543
movq 0x40(%rsp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %edi
addl %edi, %edi
callq 0x2280
movq %rax, %r12
leaq 0x80(%rsp), %r13
leaq 0x60(%rsp), %rbp
movq %r14, %rsi
addq 0x88(%r15), %r14
cmpq %r14, %rsi
jae 0xfe670
andq $0x0, (%rsp)
movq %r15, %rdi
movq %rsi, 0x38(%rsp)
leaq 0x24(%rsp), %rdx
leaq 0x20(%rsp), %rcx
leaq 0x28(%rsp), %r8
leaq 0x1c(%rsp), %r9
callq 0xf6ab5
xorl %r15d, %r15d
movl 0x1c(%rsp), %eax
cmpq %rax, %r15
jae 0xfe657
movq 0x28(%rsp), %rax
movl (%rax,%r15,4), %r14d
movq 0x40(%rsp), %rax
movq 0x18(%rax), %rdi
movl 0x14(%rdi), %r8d
addl %r8d, %r8d
movl 0x24(%rsp), %eax
movl 0x20(%rsp), %ecx
movl %ecx, 0x8(%rsp)
movl %eax, (%rsp)
movq %r13, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movl %r14d, %r9d
callq 0xf6b31
movq 0x28(%rsp), %rdi
movl 0x1c(%rsp), %esi
shrl %r14d
movq %rbp, %rdx
movl %r14d, %ecx
movq %rbx, %r8
callq 0xfe1ba
incq %r15
jmp 0xfe5f6
movq 0x38(%rsp), %rsi
incq %rsi
movq 0x30(%rsp), %r15
movq 0x80(%r15), %r14
jmp 0xfe5bd
movq 0x90(%rsp), %rdi
callq 0x2220
movq 0x70(%rsp), %rdi
callq 0x2220
movq %r12, %rdi
callq 0x2220
xorl %r14d, %r14d
pushq $-0x1
popq %r15
pushq $0x18
popq %r12
cmpq (%rbx), %r14
jae 0xfe79d
imulq $0x30, %r14, %r13
xorl %ecx, %ecx
xorl %edx, %edx
xorl %eax, %eax
movq 0x10(%rbx), %rsi
addq %r13, %rsi
addq $0x18, %rsi
cmpq (%rsi), %rdx
jae 0xfe6f7
movq 0x10(%rsi), %rsi
cmpb $0x0, 0x18(%rsi,%rcx)
js 0xfe6ee
leaq (%rsi,%rcx), %rdi
imulq $0x28, %rax, %r8
movq 0x20(%rdi), %r9
movq %r9, 0x20(%rsi,%r8)
movups (%rdi), %xmm0
movups 0x10(%rdi), %xmm1
movups %xmm1, 0x10(%rsi,%r8)
movups %xmm0, (%rsi,%r8)
incq %rax
incq %rdx
addq $0x28, %rcx
jmp 0xfe6ad
movq %rax, (%rsi)
movq 0x10(%rsi), %rdi
imulq $0x28, %rax, %rsi
addq %rdi, %rsi
callq 0xf342d
movq %r12, %rcx
xorl %edx, %edx
xorl %eax, %eax
movq %r15, %rsi
movq 0x10(%rbx), %rdi
addq %r13, %rdi
addq $0x18, %rdi
cmpq (%rdi), %rdx
jae 0xfe782
movq 0x10(%rdi), %rdi
cmpb $0x0, (%rdi,%rcx)
js 0xfe779
movl -0x10(%rdi,%rcx), %r8d
cmpq %r8, %rsi
jne 0xfe74c
cmpq $0x0, -0x8(%rdi,%rcx)
jne 0xfe779
imulq $0x28, %rax, %r8
andq $0x0, -0x18(%rdi,%r8)
jmp 0xfe779
leaq (%rdi,%rcx), %rsi
addq $-0x18, %rsi
imulq $0x28, %rax, %r9
movq 0x20(%rsi), %r10
movq %r10, 0x20(%rdi,%r9)
movups (%rsi), %xmm0
movups 0x10(%rsi), %xmm1
movups %xmm1, 0x10(%rdi,%r9)
movups %xmm0, (%rdi,%r9)
incq %rax
movq %r8, %rsi
incq %rdx
addq $0x28, %rcx
jmp 0xfe714
movq %rax, (%rdi)
movq 0x10(%rdi), %rdi
imulq $0x28, %rax, %rsi
addq %rdi, %rsi
callq 0xf36b7
incq %r14
jmp 0xfe69a
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq