// File: /workspace/github_cpp_makefile_star2_O3/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 $0x88, %rsp
movq %rsi, 0x38(%rsp)
movq %rdi, %rbx
xorl %r15d, %r15d
movq %r15, 0x80(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq %r15, 0x70(%rsp)
movaps %xmm0, 0x40(%rsp)
movq %r15, 0x50(%rsp)
movq 0x80(%rdx), %rax
testq %rax, %rax
movq %rdx, 0x28(%rsp)
je 0x145ea2
xorl %edi, %edi
movq 0x28(%rdx), %rcx
movq 0x40(%rdx), %rsi
movl (%rsi,%rdi,4), %r8d
movl 0x4(%rsi,%rdi,4), %r9d
subl %r8d, %r9d
addl $-0x2, %r9d
movl (%rcx,%r8,4), %r13d
leal 0x1(%r8), %esi
movl (%rcx,%rsi,4), %ebp
movq %r9, 0x18(%rsp)
testl %r9d, %r9d
movq %rdi, 0x10(%rsp)
movq %rbp, 0x8(%rsp)
je 0x145c18
movq %r13, 0x30(%rsp)
leaq (%rcx,%r8,4), %rax
movq %rax, 0x20(%rsp)
movq 0x10(%rbx), %rax
xorl %r12d, %r12d
movq 0x20(%rsp), %rcx
movl 0x8(%rcx,%r12,4), %r14d
shrl %r14d
leaq (%r14,%r14,2), %r13
shlq $0x4, %r13
cmpq $0x0, 0x18(%rax,%r13)
je 0x145be4
movl $0x18, %r15d
xorl %ebp, %ebp
movq 0x28(%rax,%r13), %rcx
cmpb $0x0, (%rcx,%r15)
js 0x145bd6
movl -0x10(%rcx,%r15), %esi
movl $0x1, %ecx
movq %rbx, %rdi
movq %r14, %rdx
callq 0x144201
orb $-0x80, 0x18(%rax)
movq 0x10(%rbx), %rax
incq %rbp
addq $0x28, %r15
cmpq 0x18(%rax,%r13), %rbp
jb 0x145bad
leaq (%rax,%r13), %rcx
addq $0x18, %rcx
movq $0x0, (%rcx)
incq %r12
cmpq 0x18(%rsp), %r12
jne 0x145b88
movq 0x28(%rsp), %rdx
movq 0x80(%rdx), %rax
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rbp
movq 0x30(%rsp), %r13
shrl %r13d
movq (%rdx), %rcx
movl (%rcx,%r13,4), %esi
leaq 0x1(%rax), %r8
cmpq %rsi, %r8
jae 0x145cbf
movq 0x10(%rbx), %rsi
leaq (,%r13,2), %r14
addq %r13, %r14
shlq $0x4, %r14
cmpq $0x0, 0x18(%rsi,%r14)
je 0x145cad
movq %rdi, %rbp
movl $0x18, %r15d
xorl %r12d, %r12d
movq 0x28(%rsi,%r14), %rax
cmpb $0x0, (%rax,%r15)
js 0x145c7f
movl -0x10(%rax,%r15), %esi
movl $0x1, %ecx
movq %rbx, %rdi
movq %r13, %rdx
callq 0x144201
orb $-0x80, 0x18(%rax)
movq 0x10(%rbx), %rsi
incq %r12
addq $0x28, %r15
cmpq 0x18(%rsi,%r14), %r12
jb 0x145c56
addq %r14, %rsi
addq $0x18, %rsi
movq 0x28(%rsp), %rdx
movq (%rdx), %rcx
movq 0x80(%rdx), %rax
movq %rbp, %rdi
movq 0x8(%rsp), %rbp
jmp 0x145cb4
addq %r14, %rsi
addq $0x18, %rsi
movq $0x0, (%rsi)
leaq 0x1(%rax), %r8
shrl %ebp
movl (%rcx,%rbp,4), %ecx
cmpq %rcx, %r8
jae 0x145d51
movq 0x10(%rbx), %rcx
leaq (,%rbp,2), %r14
addq %rbp, %r14
shlq $0x4, %r14
cmpq $0x0, 0x18(%rcx,%r14)
je 0x145d43
movq %rdx, %r13
movl $0x18, %r15d
xorl %r12d, %r12d
movq 0x28(%rcx,%r14), %rax
cmpb $0x0, (%rax,%r15)
js 0x145d1d
movl -0x10(%rax,%r15), %esi
movl $0x1, %ecx
movq %rbx, %rdi
movq %rbp, %rdx
callq 0x144201
orb $-0x80, 0x18(%rax)
movq 0x10(%rbx), %rcx
incq %r12
addq $0x28, %r15
cmpq 0x18(%rcx,%r14), %r12
jb 0x145cf4
addq %r14, %rcx
addq $0x18, %rcx
movq %r13, %rdx
movq 0x80(%r13), %rax
movq 0x10(%rsp), %rdi
jmp 0x145d4a
addq %r14, %rcx
addq $0x18, %rcx
movq $0x0, (%rcx)
incq %rdi
cmpq %rax, %rdi
jb 0x145b38
testq %rax, %rax
je 0x145e9f
xorl %edi, %edi
leaq 0x60(%rsp), %rsi
leaq 0x40(%rsp), %r8
movq 0x38(%rsp), %r12
movq 0x28(%rdx), %rax
movq 0x40(%rdx), %rdx
movl (%rdx,%rdi,4), %ecx
movq %rdi, 0x8(%rsp)
movl 0x4(%rdx,%rdi,4), %r14d
leaq (%rax,%rcx,4), %r13
subl %ecx, %r14d
addl $-0x2, %r14d
cmpl $0x2, %r14d
jne 0x145e0b
movl 0x8(%r13), %eax
movl 0xc(%r13), %esi
shrl %eax
leaq (%rax,%rax,2), %rdi
shlq $0x4, %rdi
addq 0x10(%rbx), %rdi
shrl %esi
movsd 0xeb553(%rip), %xmm0    # 0x231310
movl $0x1, %edx
leaq 0x80(%rsp), %r14
movq %r14, %rcx
callq 0x1416e0
movl 0x8(%r13), %esi
movl 0xc(%r13), %eax
shrl %eax
leaq (%rax,%rax,2), %rdi
shlq $0x4, %rdi
addq 0x10(%rbx), %rdi
shrl %esi
movsd 0xeb51e(%rip), %xmm0    # 0x231310
movl $0x1, %edx
movq %r14, %rcx
callq 0x1416e0
leaq 0x40(%rsp), %r8
leaq 0x60(%rsp), %rsi
jmp 0x145e80
testl %r14d, %r14d
je 0x145e80
leaq (%rax,%rcx,4), %r15
addq $0x8, %r15
incl %ecx
movl (%rax,%rcx,4), %eax
movl (%r13), %ecx
shrl %ecx
movl %ecx, 0x20(%rsp)
shrl %eax
movl %eax, 0x18(%rsp)
movl %r14d, %eax
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
movl (%r15,%r13,4), %ebp
shrl %ebp
movq 0x18(%r12), %rdi
movq %r8, %rdx
movl %ebp, %ecx
movl 0x20(%rsp), %r8d
movl 0x18(%rsp), %r9d
callq 0x13e4b7
movq %r15, %rdi
movl %r14d, %esi
leaq 0x40(%rsp), %rdx
movl %ebp, %ecx
movq %rbx, %r8
callq 0x1459fc
leaq 0x40(%rsp), %r8
leaq 0x60(%rsp), %rsi
incq %r13
cmpq %r13, 0x10(%rsp)
jne 0x145e38
movq 0x8(%rsp), %rdi
incq %rdi
movq 0x28(%rsp), %rdx
movq 0x80(%rdx), %r15
cmpq %r15, %rdi
jb 0x145d77
jmp 0x145ea2
xorl %r15d, %r15d
movq %rdx, %r14
movq 0x38(%rsp), %r12
movq 0x18(%r12), %rax
movl 0x14(%rax), %edi
addl %edi, %edi
callq 0x2280
movq %rax, %r13
movq 0x88(%r14), %rax
leaq (%rax,%r15), %rcx
cmpq %rcx, %r15
jae 0x145fc0
movq %r14, %rdx
leaq 0x60(%rsp), %rsi
movq %r15, %r8
movq %r13, 0x18(%rsp)
movq %rdx, %rdi
movq 0x40(%rdx), %rdx
movl (%rdx,%r8,4), %ecx
movl 0x4(%rdx,%r8,4), %edx
subl %ecx, %edx
addl $-0x2, %edx
je 0x145fad
movq %r8, 0x30(%rsp)
movq 0x28(%rdi), %rax
leaq (%rax,%rcx,4), %rdi
addq $0x8, %rdi
movq %rdi, 0x20(%rsp)
movl -0x8(%rdi), %edi
movq %rdi, 0x10(%rsp)
incl %ecx
movl (%rax,%rcx,4), %eax
movq %rax, 0x8(%rsp)
xorl %r12d, %r12d
movq 0x20(%rsp), %r14
movl (%r14,%r12,4), %r13d
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rdi
movl 0x14(%rdi), %r8d
addl %r8d, %r8d
movq %rdx, %r15
movq %rbx, %rbp
leaq 0x40(%rsp), %rbx
movq %rbx, %rdx
movq 0x18(%rsp), %rcx
movl %r13d, %r9d
pushq 0x8(%rsp)
pushq 0x18(%rsp)
callq 0x13df7d
addq $0x10, %rsp
shrl %r13d
movq %r14, %rdi
movl %r15d, %esi
movq %rbx, %rdx
movq %rbp, %rbx
movl %r13d, %ecx
movq %rbp, %r8
callq 0x1459fc
movq %r15, %rdx
leaq 0x60(%rsp), %rsi
incq %r12
cmpq %r12, %r15
jne 0x145f26
movq 0x28(%rsp), %rdi
movq 0x80(%rdi), %r15
movq 0x88(%rdi), %rax
movq 0x18(%rsp), %r13
movq 0x30(%rsp), %r8
movq %rdi, %rdx
incq %r8
leaq (%rax,%r15), %rcx
cmpq %rcx, %r8
jb 0x145ee0
movq 0x70(%rsp), %rdi
callq 0x2220
movq 0x50(%rsp), %rdi
callq 0x2220
movq %r13, %rdi
callq 0x2220
cmpq $0x0, (%rbx)
je 0x14612f
xorl %r14d, %r14d
movq 0x10(%rbx), %rdx
leaq (%r14,%r14,2), %r15
shlq $0x4, %r15
cmpq $0x0, 0x18(%rdx,%r15)
je 0x146051
xorl %ecx, %ecx
xorl %eax, %eax
xorl %esi, %esi
movq 0x28(%rdx,%r15), %rdi
cmpb $0x0, 0x18(%rdi,%rcx)
js 0x146039
leaq (%rdi,%rcx), %rdx
leaq (%rax,%rax,4), %r8
movq 0x20(%rdx), %r9
movq %r9, 0x20(%rdi,%r8,8)
movups (%rdx), %xmm0
movups 0x10(%rdx), %xmm1
movups %xmm1, 0x10(%rdi,%r8,8)
movups %xmm0, (%rdi,%r8,8)
incq %rax
movq 0x10(%rbx), %rdx
incq %rsi
addq $0x28, %rcx
cmpq 0x18(%rdx,%r15), %rsi
jb 0x146003
leaq (%rdx,%r15), %rcx
addq $0x18, %rcx
jmp 0x14605b
leaq (%rdx,%r15), %rcx
addq $0x18, %rcx
xorl %eax, %eax
movq %rax, (%rcx)
movq 0x10(%rcx), %rdi
leaq (%rax,%rax,4), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x139e27
movq 0x10(%rbx), %rdx
cmpq $0x0, 0x18(%rdx,%r15)
je 0x146105
movq $-0x1, %rdi
movl $0x18, %ecx
xorl %eax, %eax
xorl %esi, %esi
movq 0x28(%rdx,%r15), %r8
cmpb $0x0, (%r8,%rcx)
js 0x1460ed
movl -0x10(%r8,%rcx), %r9d
cmpq %r9, %rdi
jne 0x1460bc
cmpq $0x0, -0x8(%r8,%rcx)
jne 0x1460ed
leaq (%rax,%rax,4), %r9
movq $0x0, -0x18(%r8,%r9,8)
jmp 0x1460ed
leaq (%r8,%rcx), %rdx
addq $-0x18, %rdx
leaq (%rax,%rax,4), %rdi
movq 0x20(%rdx), %r10
movq %r10, 0x20(%r8,%rdi,8)
movups (%rdx), %xmm0
movups 0x10(%rdx), %xmm1
movups %xmm1, 0x10(%r8,%rdi,8)
movups %xmm0, (%r8,%rdi,8)
incq %rax
movq 0x10(%rbx), %rdx
movq %r9, %rdi
incq %rsi
addq $0x28, %rcx
cmpq 0x18(%rdx,%r15), %rsi
jb 0x14608f
leaq (%rdx,%r15), %rcx
addq $0x18, %rcx
jmp 0x14610f
leaq (%rdx,%r15), %rcx
addq $0x18, %rcx
xorl %eax, %eax
movq %rax, (%rcx)
movq 0x10(%rcx), %rdi
leaq (%rax,%rax,4), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x13a0b9
incq %r14
cmpq (%rbx), %r14
jb 0x145fe9
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq