// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: update_ug_by_tigs(asg_t*, hc_links*)
void update_ug_by_tigs(asg_t *sg, hc_links *link)
{
    uint32_t i, m, v, nv, x, y, qn, tn;
    asg_arc_t *av = NULL;
	kvec_t(uint32_t) stack; kv_init(stack);
    uint32_t *flag = NULL; MALLOC(flag, sg->n_seq);
    uint64_t *group = NULL; MALLOC(group, sg->n_seq);
    kvec_t_u32_warp buf; kv_init(buf.a);


    memset(flag, -1, sg->n_seq*sizeof(uint32_t));
	// connected componets
	for (i = 0; i < sg->n_seq; ++i) {
		if (flag[i] != (uint32_t)-1) continue;
		stack.n = 0;
		kv_push(uint32_t, stack, i);
		while (stack.n > 0) {
			stack.n--;
			flag[stack.a[stack.n]] = i;///group id

            v = (stack.a[stack.n])<<1;
            av = asg_arc_a(sg, v);
            nv = asg_arc_n(sg, v);
			for (m = 0; m < nv; ++m) {
				if (flag[av[m].v>>1] != (uint32_t)-1) continue;
				kv_push(uint32_t, stack, av[m].v>>1);
			}

            v++;
            av = asg_arc_a(sg, v);
            nv = asg_arc_n(sg, v);
			for (m = 0; m < nv; ++m) {
				if (flag[av[m].v>>1] != (uint32_t)-1) continue;
				kv_push(uint32_t, stack, av[m].v>>1);
			}
		}
	}
	kv_destroy(stack);



	// precalculate the size of each group
	for (i = 0; i < sg->n_seq; ++i)
		group[i] = (uint64_t)flag[i] << 32 | i;
    radix_sort_hc64(group, group + sg->n_seq);
	for (i = 1, x = y = 0; i <= sg->n_seq; ++i) {
		if (i == sg->n_seq || (group[i]>>32) != (group[x]>>32)) {
			uint32_t j;
			for (j = x; j < i; ++j)
				group[j] = (uint64_t)y << 32 | (uint32_t)group[j];///(group id)|first element in this group
			++y, x = i;
		}
	}


    for (i = 0; i < link->a.n; i++)
    {
        qn = i; 
        for (m = 0; m < link->a.a[i].e.n; m++)
        {
            link->a.a[i].e.a[m].occ = 0;
            link->a.a[i].e.a[m].is_cc = 0;
            tn = link->a.a[i].e.a[m].uID;
            if((group[qn]>>32) == (group[tn]>>32))
            {
                link->a.a[i].e.a[m].is_cc = 1;
            }
        }
    }


    for (i = 1, x = 0; i <= sg->n_seq; ++i) 
    {
		if (i == sg->n_seq || (group[i]>>32) != (group[x]>>32)) 
        {
            append_tig_link(group, x, i - x, link, sg, &buf);
			x = i;
		}
	}

    free(flag); kv_destroy(buf.a);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, 0x10(%rsp)
movq %rdi, %r14
movl 0x14(%rdi), %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
leaq (,%r12,4), %r13
movq %r13, %rdi
callq 0x2280
movq %rax, %r15
shlq $0x3, %r12
movq %r12, %rdi
callq 0x2280
movq %rax, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq %r15, %rdi
movl $0xff, %esi
movq %r13, %rdx
callq 0x20e0
testl $0x7fffffff, 0x14(%r14) # imm = 0x7FFFFFFF
je 0x13f185
xorl %ebx, %ebx
xorl %eax, %eax
xorl %edi, %edi
movq %r14, 0x18(%rsp)
cmpl $-0x1, (%r15,%rbx,4)
jne 0x13f16d
testq %rax, %rax
jne 0x13f038
movl $0x8, %esi
callq 0x2370
movq %rax, %rdi
movl $0x2, %eax
movl %ebx, (%rdi)
movl $0x1, %r13d
movq %rbx, 0x20(%rsp)
movl -0x4(%rdi,%r13,4), %ecx
movl %ebx, (%r15,%rcx,4)
movl -0x4(%rdi,%r13,4), %edx
decq %r13
addl %edx, %edx
movq 0x28(%r14), %rcx
movq %rdx, 0x28(%rsp)
movq (%rcx,%rdx,8), %rcx
testl %ecx, %ecx
je 0x13f0d5
movq 0x8(%r14), %rdx
movq %rcx, %rsi
shrq $0x20, %rsi
movl %ecx, %ecx
leaq (%rsi,%rsi,2), %rsi
leaq (%rdx,%rsi,8), %rbx
addq $0x8, %rbx
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %r12
xorl %ebp, %ebp
movl (%rbx,%rbp), %ecx
shrl %ecx
cmpl $-0x1, (%r15,%rcx,4)
jne 0x13f0cc
cmpq %rax, %r13
jne 0x13f0c0
leaq (%rax,%rax), %r14
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r14
leaq (,%r14,4), %rsi
callq 0x2370
movq %rax, %rdi
movq %r14, %rax
movl (%rbx,%rbp), %ecx
shrl %ecx
movl %ecx, (%rdi,%r13,4)
incq %r13
addq $0x18, %rbp
cmpq %rbp, %r12
jne 0x13f08c
movq 0x18(%rsp), %rsi
movq 0x28(%rsi), %rcx
movl 0x28(%rsp), %edx
movq 0x8(%rcx,%rdx,8), %rcx
testl %ecx, %ecx
je 0x13f15a
movq 0x8(%rsi), %rdx
movq %rcx, %rsi
shrq $0x20, %rsi
movl %ecx, %ecx
leaq (%rsi,%rsi,2), %rsi
leaq (%rdx,%rsi,8), %rbx
addq $0x8, %rbx
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %r14
xorl %r12d, %r12d
movl (%rbx,%r12), %ecx
shrl %ecx
cmpl $-0x1, (%r15,%rcx,4)
jne 0x13f151
cmpq %rax, %r13
jne 0x13f144
leaq (%rax,%rax), %rbp
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rbp
leaq (,%rbp,4), %rsi
callq 0x2370
movq %rax, %rdi
movq %rbp, %rax
movl (%rbx,%r12), %ecx
shrl %ecx
movl %ecx, (%rdi,%r13,4)
incq %r13
addq $0x18, %r12
cmpq %r12, %r14
jne 0x13f10f
testq %r13, %r13
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %rbx
jne 0x13f045
incq %rbx
movl 0x14(%r14), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %rbx
jb 0x13f016
jmp 0x13f187
xorl %edi, %edi
callq 0x2220
movl 0x14(%r14), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0x8(%rsp), %rdi
je 0x13f1b5
xorl %ecx, %ecx
movl (%r15,%rcx,4), %edx
shlq $0x20, %rdx
addq %rcx, %rdx
movq %rdx, (%rdi,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x13f19e
movl %eax, %eax
leaq (%rdi,%rax,8), %rsi
callq 0x13180d
movq 0x8(%rsp), %rdi
movl 0x14(%r14), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0x10(%rsp), %rcx
je 0x13f225
leal 0x1(%rax), %r10d
xorl %edx, %edx
movl $0x1, %esi
xorl %r8d, %r8d
movl %r8d, %r11d
cmpq %rax, %rsi
je 0x13f1f9
movq (%rdi,%r11,8), %r9
xorq (%rdi,%rsi,8), %r9
shrq $0x20, %r9
je 0x13f21d
cmpq %r11, %rsi
jbe 0x13f218
movq %rdx, %r8
shlq $0x20, %r8
movl (%rdi,%r11,8), %r9d
orq %r8, %r9
movq %r9, (%rdi,%r11,8)
incq %r11
cmpq %r11, %rsi
jne 0x13f205
incl %edx
movl %esi, %r8d
incq %rsi
cmpq %r10, %rsi
jne 0x13f1e3
cmpq $0x0, (%rcx)
je 0x13f2bb
xorl %eax, %eax
movq 0x10(%rcx), %r9
leaq (%rax,%rax,2), %r10
shlq $0x4, %r10
cmpq $0x0, (%r9,%r10)
je 0x13f2b0
movl $0x1, %edx
movq %rax, %rsi
shlq $0x4, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %r8d, %r8d
movq 0x10(%r9,%rsi), %r9
shlq $0x3, %r8
leaq (%r8,%r8,4), %r8
movq $0x0, 0x20(%r9,%r8)
andb $-0x80, 0x18(%r9,%r8)
movq 0x10(%rcx), %r9
movq 0x10(%r9,%rsi), %r11
movl 0x8(%r11,%r8), %r9d
movq (%rdi,%r9,8), %r9
xorq (%rdi,%rax,8), %r9
shrq $0x20, %r9
jne 0x13f2a1
addq %r8, %r11
movb 0x18(%r11), %r8b
andb $-0x80, %r8b
incb %r8b
movb %r8b, 0x18(%r11)
movl %edx, %r8d
movq 0x10(%rcx), %r9
incl %edx
cmpq %r8, (%r9,%r10)
ja 0x13f257
incl %eax
cmpq %rax, (%rcx)
ja 0x13f231
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl 0x14(%r14), %eax
je 0x13f319
xorl %esi, %esi
movl $0x1, %ebx
leaq 0x30(%rsp), %r13
movl %eax, %eax
cmpq %rax, %rbx
je 0x13f2e9
movl %esi, %eax
movq (%rdi,%rax,8), %rax
xorq (%rdi,%rbx,8), %rax
shrq $0x20, %rax
je 0x13f304
movl %ebx, %edx
subl %esi, %edx
movq %r14, %r8
movq %r13, %r9
callq 0x13ec8a
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rcx
movl %ebx, %esi
leaq 0x1(%rbx), %rdx
movl 0x14(%r14), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %rbx
movq %rdx, %rbx
jb 0x13f2d2
movq %r15, %rdi
callq 0x2220
movq 0x40(%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220