// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/rcut.cpp
// Function: mb_g_cc_core(mb_match_t*)
uint64_t *mb_g_cc_core(mb_match_t *ma)
{
    uint32_t i, x, y, *flag;
    uint64_t *group;
    mb_edge_t *o = NULL;
    kvec_t(uint32_t) stack; kv_init(stack);

    MALLOC(flag, ma->n_seq);
    for (i = 0; i < ma->n_seq; ++i)
        flag[i] = (uint32_t)-1;

    // connected componets
    for (i = 0; i < ma->n_seq; ++i) {
        if (flag[i] != (uint32_t)-1) continue;
        stack.n = 0;
        kv_push(uint32_t, stack, i);
        while (stack.n > 0) {
            uint32_t k, j, n;
            stack.n--;
            k = stack.a[stack.n];
            flag[k] = i;///group id
            // n = (uint32_t)ma->idx[k];
            // s = ma->idx[k] >> 32;
            o = pt_a(*ma, k); 
            n = pt_n(*ma, k);
            for (j = 0; j < n; ++j) {
                uint32_t t = ma_y(o[j]);
                if (flag[t] != (uint32_t)-1) continue;
                // if (ns == ms) PT_EXPAND(stack, ms);
                // stack[ns++] = t;
                kv_push(uint32_t, stack, t);
            }
        }
    }
    kv_destroy(stack);

    // precalculate the size of each group
    CALLOC(group, ma->n_seq);
    for (i = 0; i < ma->n_seq; ++i)
        group[i] = (uint64_t)flag[i] << 32 | i;
    radix_sort_mc64(group, group + ma->n_seq);
    for (i = 1, x = y = 0; i <= ma->n_seq; ++i) {
        if (i == ma->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;
        }
    }
    free(flag);
    return group;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, (%rsp)
movl 0x38(%rdi), %r14d
leaq (,%r14,4), %r15
movq %r15, %rdi
callq 0x2280
movq %rax, %rbx
testq %r14, %r14
je 0x167cfb
movq %rbx, %rdi
movl $0xff, %esi
movq %r15, %rdx
callq 0x20e0
movq (%rsp), %r14
cmpl $0x0, 0x38(%r14)
je 0x167ded
xorl %r15d, %r15d
xorl %eax, %eax
xorl %edi, %edi
movq %rbx, 0x10(%rsp)
cmpl $-0x1, (%rbx,%r15,4)
jne 0x167ddb
testq %rax, %rax
jne 0x167d38
movl $0x8, %esi
callq 0x2370
movq %rax, %rdi
movl $0x2, %eax
movl %r15d, (%rdi)
movl $0x1, %r13d
movq %r15, 0x8(%rsp)
movl -0x4(%rdi,%r13,4), %ecx
decq %r13
movl %r15d, (%rbx,%rcx,4)
movq 0x10(%r14), %rdx
movq (%rdx,%rcx,8), %rcx
testl %ecx, %ecx
je 0x167dc9
movq %rcx, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,4), %rbp
shlq $0x3, %rbp
addq 0x28(%r14), %rbp
movl %ecx, %ecx
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %r14
xorl %r15d, %r15d
movq (%rbp,%r15), %r12
movl %r12d, %ecx
cmpl $-0x1, (%rbx,%rcx,4)
jne 0x167dc0
cmpq %rax, %r13
jne 0x167db9
leaq (%rax,%rax), %rbx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rbx
leaq (,%rbx,4), %rsi
callq 0x2370
movq %rax, %rdi
movq %rbx, %rax
movq 0x10(%rsp), %rbx
movl %r12d, (%rdi,%r13,4)
incq %r13
addq $0x28, %r15
cmpq %r15, %r14
jne 0x167d7e
testq %r13, %r13
movq (%rsp), %r14
movq 0x8(%rsp), %r15
jne 0x167d46
incq %r15
movl 0x38(%r14), %ecx
cmpq %rcx, %r15
jb 0x167d16
jmp 0x167def
xorl %edi, %edi
callq 0x2220
movl 0x38(%r14), %r12d
movl $0x8, %esi
movq %r12, %rdi
callq 0x2120
movq %rax, %r15
testq %r12, %r12
je 0x167e25
xorl %eax, %eax
movl (%rbx,%rax,4), %ecx
shlq $0x20, %rcx
addq %rax, %rcx
movq %rcx, (%r15,%rax,8)
incq %rax
cmpq %rax, %r12
jne 0x167e0f
leaq (%r15,%r12,8), %rsi
movq %r15, %rdi
callq 0x1604f0
movl 0x38(%r14), %eax
testq %rax, %rax
je 0x167e92
leal 0x1(%rax), %edx
cmpl $0x3, %edx
movl $0x2, %ecx
cmovael %edx, %ecx
xorl %edi, %edi
movl $0x1, %edx
xorl %esi, %esi
movl %edi, %r8d
cmpq %rax, %rdx
je 0x167e67
movq (%r15,%r8,8), %r9
xorq (%r15,%rdx,8), %r9
shrq $0x20, %r9
je 0x167e8a
cmpq %r8, %rdx
jbe 0x167e86
movq %rsi, %rdi
shlq $0x20, %rdi
movl (%r15,%r8,8), %r9d
orq %rdi, %r9
movq %r9, (%r15,%r8,8)
incq %r8
cmpq %r8, %rdx
jne 0x167e73
incl %esi
movl %edx, %edi
incq %rdx
cmpq %rcx, %rdx
jne 0x167e51
movq %rbx, %rdi
callq 0x2220
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq