// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/hic.cpp
// Function: assign_per_unitig_G_partition(G_partition*, unsigned long, hc_links*, bubble_type*, unsigned int)
void assign_per_unitig_G_partition(G_partition* g_p, uint64_t hap_n, hc_links* link, bubble_type* bub,
uint32_t bubble_first)
{
    double index_time = yak_realtime();
    reset_G_partition(g_p, hap_n);

    partition_warp* res = NULL;
    hc_edge *a = NULL;
    uint32_t i, a_n, v, u, uv = (uint32_t)-1, k, k_n, k_nv, k_nu, beg, sink, *b_a = NULL, b_a_n;
    
    if(bubble_first)
    {
        int c;
        kvec_t_u32_warp stack; kv_init(stack.a);
        uint8_t *color = NULL; CALLOC(color, hap_n);
        uint32_t n_bub = bub->f_bub + bub->b_bub;
        for (i = 0; i < n_bub; i++)
        {
            get_bubbles(bub, i, &beg, &sink, &b_a, &b_a_n, NULL);
            if(b_a_n == 2 && i < bub->f_bub)
            {
                continue;
            }
            ///full bubble do not overlap with any others
            ///broken bubbles might be, but should do nothing
            c = graph_bipartiteness(b_a, b_a_n, color, link, &stack);

            if(c == 0)
            {
                fprintf(stderr, "too good: s-utg%.6ul && e-utg%.6ul && %s\n",(beg>>1)+1, (sink>>1)+1, b_a_n != 4? "abnormal" : "normal");
            }
            if(c == -1)
            {
                fprintf(stderr, "too bad: s-utg%.6ul && e-utg%.6ul\n",(beg>>1)+1, (sink>>1)+1);
            }
            if(c == 1)
            {
                fprintf(stderr, "\nprefect=%u: s-utg%.6ul && e-utg%.6ul\n", b_a_n, (beg>>1)+1, (sink>>1)+1);
                
                
                for (k = 0; k < b_a_n; k++)
                {
                    if((color[b_a[k]>>1] & 2) == 0) fprintf(stderr, "ERROR\n");
                    if((color[b_a[k]>>1] & 4) == 0) fprintf(stderr, "0: utg%.6ul\n", (b_a[k]>>1)+1);
                }

                for (k = 0; k < b_a_n; k++)
                {
                    if((color[b_a[k]>>1] & 2) == 0) fprintf(stderr, "ERROR\n");
                    if((color[b_a[k]>>1] & 4) != 0) fprintf(stderr, "1: utg%.6ul\n", (b_a[k]>>1)+1);
                }

                for (k = 0; k < b_a_n; k++) color[b_a[k]>>1] = 0;
            }
        }
        free(color);
        kv_destroy(stack.a);
    }

    for (i = 0; i < hap_n; i++)
    {
        v = i;
        a = link->a.a[v].f.a;
        a_n = link->a.a[v].f.n;
        for (k = k_n = 0; k < a_n; k++)
        {
            if(a[k].del) continue;
            if(a[k].dis != RC_0) break;
            u = a[k].uID;
            k_n++;
        }
        if(k_n != 1)
        {   
            u = (uint32_t)-1;
            goto push_uv;
        } 
        
        a = link->a.a[u].f.a;
        a_n = link->a.a[u].f.n;
        for (k = k_n = 0; k < a_n; k++)
        {
            if(a[k].del) continue;
            if(a[k].dis != RC_0) break;
            uv = a[k].uID;
            k_n++;
        }
        if(k_n != 1 || uv != v)
        {
            u = (uint32_t)-1;
            goto push_uv;
        } 

        push_uv:
        k_nv = 0;k_nu = 0;

        // not such easy. need to deal with here very carefully
        // if(g_p->index[v] != (uint32_t)-1) continue; 
        // if(u != (uint32_t)-1 && g_p->index[u] != (uint32_t)-1) u = (uint32_t)-1;

        a = link->a.a[v].e.a;
        a_n = link->a.a[v].e.n;
        for (k = 0; k < a_n; k++)
        {
            if(a[k].del) continue;
            k_nv++;
        }
                
        if(u != (uint32_t)-1)
        {
            a = link->a.a[u].e.a;
            a_n = link->a.a[u].e.n;
            for (k = 0; k < a_n; k++)
            {
                if(a[k].del) continue;
                k_nu++;
            }
        }
        if(k_nv == 0) continue;
        if(k_nv > 0 && k_nu > 0 && v > u) continue;

        kv_pushp(partition_warp, *g_p, &res);
        kv_init(res->a);
        res->full_bub = 0;
        res->h[0] = 1; res->h[1] = 0;
        kv_push(uint32_t, res->a, v);
        if(u != (uint32_t)-1)
        {
            res->h[1] = 1;
            kv_push(uint32_t, res->a, u);
        }

        for (k = 0; k < res->h[0]; k++)
        {
            g_p->index[res->a.a[k]] = g_p->n-1;
            g_p->index[res->a.a[k]] = g_p->index[res->a.a[k]] << 1;
        } 

        for (; k < res->a.n; k++)
        {
            g_p->index[res->a.a[k]] = g_p->n-1;
            g_p->index[res->a.a[k]] = (g_p->index[res->a.a[k]] << 1) + 1;
        } 
    }
    fprintf(stderr, "[M::%s::%.3f]\n", __func__, yak_realtime()-index_time);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r8d, %ebx
movq %rcx, (%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, %rbp
movq %rdi, %r14
callq 0x1365ef
movq %r14, %r9
movsd %xmm0, 0x28(%rsp)
movq $0x0, (%r14)
testq %rbp, %rbp
movq %r14, 0x10(%rsp)
je 0x156dca
movq 0x18(%r9), %rdi
leaq (,%rbp,4), %rdx
movl $0xff, %esi
callq 0x20e0
movq 0x10(%rsp), %r9
testl %ebx, %ebx
movq %rbp, 0x18(%rsp)
je 0x15700c
movl $0x1, %esi
movq %rbp, %rdi
callq 0x2120
movq %rax, %r13
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq (%rsp), %rsi
movq 0x88(%rsi), %rax
addq 0x80(%rsi), %rax
testl %eax, %eax
jne 0x156e17
xorl %ebx, %ebx
jmp 0x156ff7
movl %eax, %edi
xorl %r12d, %r12d
movq %rdi, 0x8(%rsp)
movq 0x28(%rsi), %rax
movq 0x40(%rsi), %rdx
movl (%rdx,%r12,4), %ecx
movl 0x4(%rdx,%r12,4), %ebp
subl %ecx, %ebp
addl $-0x2, %ebp
movl (%rax,%rcx,4), %ebx
leal 0x1(%rcx), %edx
movl (%rax,%rdx,4), %r15d
cmpl $0x2, %ebp
jne 0x156e5c
cmpq %r12, 0x80(%rsi)
jbe 0x156e5c
incq %r12
cmpq %rdi, %r12
jne 0x156e21
jmp 0x156fed
leaq (%rax,%rcx,4), %r14
addq $0x8, %r14
movq %r14, %rdi
movl %ebp, %esi
movq %r13, %rdx
movq 0x20(%rsp), %rcx
leaq 0x30(%rsp), %r8
callq 0x156a3f
movq 0x13e15e(%rip), %rcx    # 0x294fe0
movq (%rcx), %rdi
shrl %ebx
incl %ebx
shrl %r15d
incl %r15d
cmpl $-0x1, %eax
je 0x156f13
cmpl $0x1, %eax
jne 0x156f28
leaq 0xe5313(%rip), %rsi     # 0x23c1b7
movl %ebp, %edx
movl %ebx, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x22b0
testl %ebp, %ebp
movq (%rsp), %rsi
movq 0x8(%rsp), %rdi
je 0x156e4f
movl %ebp, %ebx
xorl %r15d, %r15d
movl (%r14,%r15,4), %edi
shrl %edi
testb $0x2, (%r13,%rdi)
je 0x156ee4
testb $0x4, (%r13,%rdi)
je 0x156f0c
incq %r15
cmpq %r15, %rbx
jne 0x156ec4
jmp 0x156f5e
movq 0x13e0f5(%rip), %rax    # 0x294fe0
movq (%rax), %rcx
movl $0x6, %esi
movl $0x1, %edx
leaq 0xe1f85(%rip), %rdi     # 0x238e84
callq 0x2410
movl (%r14,%r15,4), %edi
shrl %edi
jmp 0x156ed2
callq 0x1cabe
jmp 0x156eda
leaq 0xe527a(%rip), %rsi     # 0x23c194
movl %ebx, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x22b0
jmp 0x156f50
cmpl $0x4, %ebp
leaq 0xe5259(%rip), %r8      # 0x23c18b
leaq 0xe5254(%rip), %rax     # 0x23c18d
cmoveq %rax, %r8
leaq 0xe521d(%rip), %rsi     # 0x23c161
movl %ebx, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x22b0
movq (%rsp), %rsi
movq 0x8(%rsp), %rdi
jmp 0x156e4f
testl %ebp, %ebp
movq (%rsp), %rsi
movq 0x8(%rsp), %rdi
je 0x156e4f
xorl %r15d, %r15d
movl (%r14,%r15,4), %edi
shrl %edi
testb $0x2, (%r13,%rdi)
je 0x156f92
testb $0x4, (%r13,%rdi)
jne 0x156fba
incq %r15
cmpq %r15, %rbx
jne 0x156f72
jmp 0x156fc1
movq 0x13e047(%rip), %rax    # 0x294fe0
movq (%rax), %rcx
movl $0x6, %esi
movl $0x1, %edx
leaq 0xe1ed7(%rip), %rdi     # 0x238e84
callq 0x2410
movl (%r14,%r15,4), %edi
shrl %edi
jmp 0x156f80
callq 0x1cadc
jmp 0x156f88
testl %ebp, %ebp
movq (%rsp), %rsi
movq 0x8(%rsp), %rdi
je 0x156e4f
xorl %eax, %eax
movl (%r14,%rax,4), %ecx
shrl %ecx
movb $0x0, (%r13,%rcx)
incq %rax
cmpq %rax, %rbx
jne 0x156fd4
jmp 0x156e4f
movq 0x40(%rsp), %rbx
movq 0x18(%rsp), %rbp
movq %r13, %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x2220
movq 0x10(%rsp), %r9
testq %rbp, %rbp
je 0x15727b
xorl %ebx, %ebx
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rax
leaq (%rbx,%rbx,2), %rcx
shlq $0x4, %rcx
movl 0x18(%rax,%rcx), %edx
testq %rdx, %rdx
je 0x1570d2
movq 0x28(%rax,%rcx), %rsi
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %rdi
xorl %r8d, %r8d
xorl %edx, %edx
cmpb $0x0, 0x18(%rsi,%r8)
js 0x157064
cmpq $0x0, 0x10(%rsi,%r8)
jne 0x15706d
movl 0x8(%rsi,%r8), %r12d
incl %edx
addq $0x28, %r8
cmpq %r8, %rdi
jne 0x15704d
cmpl $0x1, %edx
jne 0x1570d2
movl %r12d, %edx
leaq (%rdx,%rdx,2), %rsi
shlq $0x4, %rsi
movl 0x18(%rax,%rsi), %edi
xorl %edx, %edx
testq %rdi, %rdi
je 0x1570be
movq 0x28(%rax,%rsi), %rsi
shlq $0x3, %rdi
leaq (%rdi,%rdi,4), %rdi
xorl %r8d, %r8d
cmpb $0x0, 0x18(%rsi,%r8)
js 0x1570af
cmpq $0x0, 0x10(%rsi,%r8)
jne 0x1570b8
movl 0x8(%rsi,%r8), %r15d
incl %edx
addq $0x28, %r8
cmpq %r8, %rdi
jne 0x157098
cmpl $0x1, %edx
sete %dl
cmpl %ebx, %r15d
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmovnel %esi, %r12d
testb %dl, %dl
cmovel %esi, %r12d
jmp 0x1570d8
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl (%rax,%rcx), %edx
testq %rdx, %rdx
je 0x157110
movq 0x10(%rax,%rcx), %rcx
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %rdx
xorl %esi, %esi
xorl %edi, %edi
xorl %r8d, %r8d
cmpb $0x0, 0x18(%rcx,%rsi)
setns %r8b
addl %r8d, %edi
addq $0x28, %rsi
cmpq %rsi, %rdx
jne 0x1570f1
testl %edi, %edi
sete %cl
jmp 0x157112
movb $0x1, %cl
cmpl $-0x1, %r12d
je 0x15715b
movl %r12d, %edx
leaq (%rdx,%rdx,2), %rsi
shlq $0x4, %rsi
movl (%rax,%rsi), %edi
xorl %edx, %edx
testq %rdi, %rdi
je 0x15715d
movq 0x10(%rax,%rsi), %rax
shlq $0x3, %rdi
leaq (%rdi,%rdi,4), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
cmpb $0x0, 0x18(%rax,%rdi)
setns %r8b
addl %r8d, %edx
addq $0x28, %rdi
cmpq %rdi, %rsi
jne 0x15713c
testl %edx, %edx
setne %dl
jmp 0x15715d
xorl %edx, %edx
cmpl %r12d, %ebx
seta %al
andb %dl, %al
orb %al, %cl
jne 0x157270
movq (%r9), %rcx
movq 0x10(%r9), %rax
cmpq 0x8(%r9), %rcx
jne 0x1571aa
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%r9)
shlq $0x3, %rdx
leaq (%rdx,%rdx,8), %rsi
movq %rax, %rdi
callq 0x2370
movq 0x10(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %rcx
leaq 0x1(%rcx), %r13
movq %r13, (%r9)
leaq (%rcx,%rcx,8), %rcx
leaq (%rax,%rcx,8), %rbp
movb $0x0, 0x20(%rbp)
movl $0x1, %r14d
movq %r14, 0x18(%rbp)
movq $0x2, 0x8(%rbp)
movl $0x8, %edi
callq 0x2280
movq %rax, 0x10(%rbp)
movq %r14, (%rbp)
movl %ebx, (%rax)
cmpl $-0x1, %r12d
je 0x157202
movl $0x1, 0x1c(%rbp)
movq $0x2, (%rbp)
movl %r12d, 0x4(%rax)
movl $0x2, %r14d
cmpl $0x0, 0x18(%rbp)
je 0x157230
leal -0x1(%r13), %edx
movq 0x10(%rsp), %r9
movq 0x18(%r9), %rsi
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edi
movl %edx, (%rsi,%rdi,4)
movl (%rax,%rcx,4), %edi
shll (%rsi,%rdi,4)
incq %rcx
movl 0x18(%rbp), %edi
cmpq %rdi, %rcx
jb 0x157217
jmp 0x157237
xorl %ecx, %ecx
movq 0x10(%rsp), %r9
cmpl %ecx, %r14d
movq 0x18(%rsp), %rbp
jbe 0x157270
movl %ecx, %edx
decl %r13d
movq 0x18(%r9), %rcx
leal 0x1(%rdx), %esi
movl (%rax,%rdx,4), %edi
movl %r13d, (%rcx,%rdi,4)
movl (%rax,%rdx,4), %edx
movl (%rcx,%rdx,4), %edi
leal 0x1(,%rdi,2), %edi
movl %edi, (%rcx,%rdx,4)
movl %esi, %edx
leal 0x1(%rsi), %edi
cmpl %esi, %r14d
movl %edi, %esi
ja 0x15724d
incl %ebx
cmpq %rbp, %rbx
jb 0x15701d
movq 0x13dd5e(%rip), %rax    # 0x294fe0
movq (%rax), %rbx
callq 0x1365ef
subsd 0x28(%rsp), %xmm0
leaq 0xe4954(%rip), %rsi     # 0x23bbeb
leaq 0xe4f5a(%rip), %rdx     # 0x23c1f8
movq %rbx, %rdi
movb $0x1, %al
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22b0