// File: /workspace/github_cpp_makefile_star2_O1/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 $0x98, %rsp
movq %rdx, %r13
movq %rdi, %rbx
xorl %eax, %eax
movq %rax, 0x68(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movq %rax, 0x80(%rsp)
movaps %xmm0, 0x40(%rsp)
movq %rax, 0x50(%rsp)
cmpq %rax, 0x80(%rdx)
je 0x1400a8
xorl %eax, %eax
movq 0x28(%r13), %rdi
movq 0x40(%r13), %rcx
movl (%rcx,%rax,4), %r8d
movl 0x4(%rcx,%rax,4), %ebp
subl %r8d, %ebp
addl $-0x2, %ebp
movl (%rdi,%r8,4), %edx
leal 0x1(%r8), %ecx
movl (%rdi,%rcx,4), %ecx
testl %ebp, %ebp
je 0x13ff83
leaq (%rdi,%r8,4), %rdi
xorl %r8d, %r8d
movl 0x8(%rdi,%r8,4), %r9d
shrl %r9d
movq 0x10(%rbx), %r14
leaq (%r9,%r9,2), %r10
shlq $0x4, %r10
cmpq $0x0, 0x18(%r14,%r10)
je 0x13ff68
xorl %r11d, %r11d
movq 0x28(%r14,%r10), %r15
leaq (%r11,%r11,4), %r12
cmpb $0x0, 0x18(%r15,%r12,8)
js 0x13ff5a
leaq (%r15,%r12,8), %r15
movl 0x8(%r15), %r15d
leaq (%r15,%r15,2), %r15
shlq $0x4, %r15
movq 0x28(%r14,%r15), %r14
addq $0x18, %r14
cmpb $0x0, (%r14)
js 0x13ff50
cmpl %r9d, -0x10(%r14)
je 0x13ff56
addq $0x28, %r14
jmp 0x13ff44
orb $-0x80, (%r14)
incq %r11
movq 0x10(%rbx), %r14
cmpq 0x18(%r14,%r10), %r11
jb 0x13ff1a
leaq (%r14,%r10), %r9
addq $0x18, %r9
movq $0x0, (%r9)
incq %r8
cmpq %rbp, %r8
jne 0x13fefb
shrl %edx
movq (%r13), %r8
movq 0x80(%r13), %rdi
movl (%r8,%rdx,4), %r8d
incq %rdi
cmpq %r8, %rdi
jae 0x14000f
movq 0x10(%rbx), %r8
leaq (%rdx,%rdx,2), %r11
shlq $0x4, %r11
cmpq $0x0, 0x18(%r8,%r11)
je 0x140000
xorl %edi, %edi
movq 0x28(%r8,%r11), %r9
leaq (%rdi,%rdi,4), %r10
cmpb $0x0, 0x18(%r9,%r10,8)
js 0x13fff2
leaq (%r9,%r10,8), %r9
movl 0x8(%r9), %r9d
leaq (%r9,%r9,2), %r9
shlq $0x4, %r9
movq 0x28(%r8,%r9), %r8
addq $0x18, %r8
cmpb $0x0, (%r8)
js 0x13ffe8
cmpl %edx, -0x10(%r8)
je 0x13ffee
addq $0x28, %r8
jmp 0x13ffdc
orb $-0x80, (%r8)
incq %rdi
movq 0x10(%rbx), %r8
cmpq 0x18(%r8,%r11), %rdi
jb 0x13ffb2
leaq (%r8,%r11), %rdx
addq $0x18, %rdx
movq $0x0, (%rdx)
shrl %ecx
movq (%r13), %rdx
movq 0x80(%r13), %rdi
movl (%rdx,%rcx,4), %edx
incq %rdi
cmpq %rdx, %rdi
jae 0x140098
movq 0x10(%rbx), %rdi
leaq (%rcx,%rcx,2), %rdx
shlq $0x4, %rdx
cmpq $0x0, 0x18(%rdi,%rdx)
je 0x140089
xorl %r10d, %r10d
movq 0x28(%rdi,%rdx), %r8
leaq (%r10,%r10,4), %r9
cmpb $0x0, 0x18(%r8,%r9,8)
js 0x14007b
leaq (%r8,%r9,8), %r8
movl 0x8(%r8), %r8d
leaq (%r8,%r8,2), %r8
shlq $0x4, %r8
movq 0x28(%rdi,%r8), %rdi
addq $0x18, %rdi
cmpb $0x0, (%rdi)
js 0x140072
cmpl %ecx, -0x10(%rdi)
je 0x140078
addq $0x28, %rdi
jmp 0x140068
orb $-0x80, (%rdi)
incq %r10
movq 0x10(%rbx), %rdi
cmpq 0x18(%rdi,%rdx), %r10
jb 0x14003e
leaq (%rdi,%rdx), %rcx
addq $0x18, %rcx
movq $0x0, (%rcx)
incq %rax
cmpq 0x80(%r13), %rax
jb 0x13fecb
movq %rsi, 0x10(%rsp)
movq 0x80(%r13), %r14
testq %r14, %r14
movq %r13, 0x18(%rsp)
je 0x1401f3
xorl %edi, %edi
leaq 0x70(%rsp), %rsi
leaq 0x40(%rsp), %rdx
movq 0x10(%rsp), %r15
movq 0x28(%r13), %rax
movq 0x40(%r13), %r8
movl (%r8,%rdi,4), %ecx
movq %rdi, 0x20(%rsp)
movl 0x4(%r8,%rdi,4), %r12d
leaq (%rax,%rcx,4), %r13
subl %ecx, %r12d
addl $-0x2, %r12d
cmpl $0x2, %r12d
jne 0x140165
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 0xe5156(%rip), %xmm0    # 0x225270
movl $0x1, %edx
leaq 0x68(%rsp), %r14
movq %r14, %rcx
callq 0x13b8bb
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 0xe5124(%rip), %xmm0    # 0x225270
movl $0x1, %edx
movq %r14, %rcx
callq 0x13b8bb
leaq 0x40(%rsp), %rdx
leaq 0x70(%rsp), %rsi
jmp 0x1401d6
testl %r12d, %r12d
je 0x1401d6
leaq (%rax,%rcx,4), %r14
addq $0x8, %r14
incl %ecx
movl (%rax,%rcx,4), %eax
movl (%r13), %ecx
shrl %ecx
movl %ecx, 0x30(%rsp)
shrl %eax
movl %eax, 0x8(%rsp)
movl %r12d, %eax
movq %rax, 0x28(%rsp)
xorl %r13d, %r13d
movl (%r14,%r13,4), %ebp
shrl %ebp
movq 0x18(%r15), %rdi
movl %ebp, %ecx
movl 0x30(%rsp), %r8d
movl 0x8(%rsp), %r9d
callq 0x1386d7
movq %r14, %rdi
movl %r12d, %esi
leaq 0x40(%rsp), %rdx
movl %ebp, %ecx
movq %rbx, %r8
callq 0x13fd97
leaq 0x40(%rsp), %rdx
leaq 0x70(%rsp), %rsi
incq %r13
cmpq %r13, 0x28(%rsp)
jne 0x140192
movq 0x20(%rsp), %rdi
incq %rdi
movq 0x18(%rsp), %r13
movq 0x80(%r13), %r14
cmpq %r14, %rdi
jb 0x1400d3
movq %rbx, 0x38(%rsp)
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %edi
addl %edi, %edi
callq 0x2280
movq %rax, 0x8(%rsp)
movq 0x88(%r13), %rax
addq 0x80(%r13), %rax
cmpq %rax, %r14
jae 0x140308
leaq 0x70(%rsp), %rbp
leaq 0x40(%rsp), %rdx
movq 0x40(%r13), %rcx
movl (%rcx,%r14,4), %eax
movq %r14, 0x60(%rsp)
movl 0x4(%rcx,%r14,4), %r15d
subl %eax, %r15d
addl $-0x2, %r15d
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %r9
je 0x1402e4
movq 0x18(%rsp), %rcx
movq 0x28(%rcx), %rcx
leaq (%rcx,%rax,4), %rdi
addq $0x8, %rdi
movq %rdi, 0x30(%rsp)
movl -0x8(%rdi), %edi
movq %rdi, 0x28(%rsp)
incl %eax
movl (%rcx,%rax,4), %eax
movq %rax, 0x20(%rsp)
xorl %r12d, %r12d
movq 0x30(%rsp), %rbx
movl (%rbx,%r12,4), %r13d
movq 0x18(%rsi), %rdi
movl 0x14(%rdi), %r8d
addl %r8d, %r8d
movq %rbp, %rsi
movq %rdx, %r14
movq %r9, %rcx
movl %r13d, %r9d
pushq 0x20(%rsp)
pushq 0x30(%rsp)
callq 0x1381aa
addq $0x10, %rsp
shrl %r13d
movq %rbx, %rdi
movl %r15d, %esi
movq %r14, %rdx
movl %r13d, %ecx
movq 0x38(%rsp), %r8
callq 0x13fd97
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %rsi
movq %r14, %rdx
incq %r12
cmpq %r12, %r15
jne 0x140285
movq 0x60(%rsp), %r14
incq %r14
movq 0x18(%rsp), %r13
movq 0x88(%r13), %rax
addq 0x80(%r13), %rax
cmpq %rax, %r14
jb 0x140231
movq 0x80(%rsp), %rdi
callq 0x2220
movq 0x50(%rsp), %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
callq 0x2220
movq 0x38(%rsp), %rbx
cmpq $0x0, (%rbx)
je 0x140480
xorl %r14d, %r14d
movq 0x10(%rbx), %rsi
leaq (%r14,%r14,2), %r15
shlq $0x4, %r15
cmpq $0x0, 0x18(%rsi,%r15)
je 0x1403a3
xorl %ecx, %ecx
xorl %eax, %eax
xorl %edx, %edx
movq 0x28(%rsi,%r15), %rsi
cmpb $0x0, 0x18(%rsi,%rcx)
js 0x140387
leaq (%rsi,%rcx), %rdi
leaq (%rax,%rax,4), %r8
movq 0x20(%rdi), %r9
movq %r9, 0x20(%rsi,%r8,8)
movups (%rdi), %xmm0
movups 0x10(%rdi), %xmm1
movups %xmm1, 0x10(%rsi,%r8,8)
movups %xmm0, (%rsi,%r8,8)
incq %rax
incq %rdx
movq 0x10(%rbx), %rsi
addq $0x28, %rcx
cmpq 0x18(%rsi,%r15), %rdx
jb 0x140355
leaq (%rsi,%r15), %rcx
addq $0x18, %rcx
jmp 0x1403ad
leaq (%rsi,%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 0x133c5e
movq 0x10(%rbx), %rdi
cmpq $0x0, 0x18(%rdi,%r15)
je 0x140456
movq $-0x1, %rsi
movl $0x18, %ecx
xorl %eax, %eax
xorl %edx, %edx
movq 0x28(%rdi,%r15), %rdi
cmpb $0x0, (%rdi,%rcx)
js 0x14043a
movl -0x10(%rdi,%rcx), %r8d
cmpq %r8, %rsi
jne 0x14040d
cmpq $0x0, -0x8(%rdi,%rcx)
jne 0x14043a
leaq (%rax,%rax,4), %r8
movq $0x0, -0x18(%rdi,%r8,8)
jmp 0x14043a
leaq (%rdi,%rcx), %rsi
addq $-0x18, %rsi
leaq (%rax,%rax,4), %r9
movq 0x20(%rsi), %r10
movq %r10, 0x20(%rdi,%r9,8)
movups (%rsi), %xmm0
movups 0x10(%rsi), %xmm1
movups %xmm1, 0x10(%rdi,%r9,8)
movups %xmm0, (%rdi,%r9,8)
incq %rax
movq %r8, %rsi
incq %rdx
movq 0x10(%rbx), %rdi
addq $0x28, %rcx
cmpq 0x18(%rdi,%r15), %rdx
jb 0x1403e1
leaq (%rdi,%r15), %rcx
addq $0x18, %rcx
jmp 0x140460
leaq (%rdi,%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 0x133ef4
incq %r14
cmpq (%rbx), %r14
jb 0x14033b
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq