// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movl %r8d, %ebp
movq %rcx, %r12
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r15
callq 0xefea7
movsd %xmm0, 0x40(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x101904
andq $0x0, 0x20(%rsp)
testl %ebp, %ebp
movq %r14, 0x38(%rsp)
movq %r13, 0x30(%rsp)
je 0x1101a0
pushq $0x1
popq %rsi
movq %r13, %rdi
callq 0x2120
xorpd %xmm0, %xmm0
leaq 0x50(%rsp), %rcx
andq $0x0, 0x10(%rcx)
movapd %xmm0, (%rcx)
movl 0x88(%r12), %ebx
addl 0x80(%r12), %ebx
movq %rax, %rbp
xorl %r13d, %r13d
cmpq %rbx, %r13
je 0x110189
andq $0x0, (%rsp)
movq %r12, %rdi
movq %r13, %rsi
leaq 0x1c(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x14(%rsp), %r9
callq 0xf6ab5
movl 0x14(%rsp), %esi
cmpl $0x2, %esi
jne 0x10ffd7
cmpq %r13, 0x80(%r12)
jbe 0x10ffd7
incq %r13
jmp 0x10ff92
movq 0x20(%rsp), %rdi
movq %rbp, %rdx
movq %r14, %rcx
leaq 0x50(%rsp), %r8
callq 0x10fc13
movq 0x142fed(%rip), %rcx    # 0x252fe0
movq (%rcx), %rdi
cmpl $-0x1, %eax
je 0x11012c
cmpl $0x1, %eax
jne 0x11014f
movl 0x14(%rsp), %edx
movl 0x1c(%rsp), %ecx
shrl %ecx
incl %ecx
movl 0x18(%rsp), %r8d
shrl %r8d
incl %r8d
leaq 0xde081(%rip), %rsi     # 0x1ee0a7
xorl %eax, %eax
callq 0x22b0
xorl %r14d, %r14d
movl 0x14(%rsp), %eax
cmpq %rax, %r14
jae 0x11009c
movq 0x20(%rsp), %rax
movl (%rax,%r14,4), %edx
shrl %edx
testb $0x2, (%rbp,%rdx)
je 0x110057
testb $0x4, (%rbp,%rdx)
je 0x110080
incq %r14
jmp 0x110030
movq 0x142f82(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0xdad1c(%rip), %rdi     # 0x1ead84
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movq 0x20(%rsp), %rax
movl (%rax,%r14,4), %edx
shrl %edx
jmp 0x11004b
movq 0x142f59(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
incl %edx
leaq 0xde03b(%rip), %rsi     # 0x1ee0ce
xorl %eax, %eax
callq 0x22b0
jmp 0x110052
xorl %r14d, %r14d
movl %eax, %eax
movq 0x20(%rsp), %rcx
cmpq %rax, %r14
jae 0x11010d
movl (%rcx,%r14,4), %edx
shrl %edx
testb $0x2, (%rbp,%rdx)
je 0x1100c8
testb $0x4, (%rbp,%rdx)
jne 0x1100f1
incq %r14
movl 0x14(%rsp), %eax
jmp 0x11009f
movq 0x142f11(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0xdacab(%rip), %rdi     # 0x1ead84
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
movq 0x20(%rsp), %rax
movl (%rax,%r14,4), %edx
shrl %edx
jmp 0x1100b8
movq 0x142ee8(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
incl %edx
leaq 0xddfd7(%rip), %rsi     # 0x1ee0db
xorl %eax, %eax
callq 0x22b0
jmp 0x1100bf
xorl %edx, %edx
movq 0x38(%rsp), %r14
cmpq %rdx, %rax
je 0x10ffd2
movl (%rcx,%rdx,4), %esi
shrl %esi
movb $0x0, (%rbp,%rsi)
incq %rdx
jmp 0x110114
movl 0x1c(%rsp), %edx
shrl %edx
incl %edx
movl 0x18(%rsp), %ecx
shrl %ecx
incl %ecx
leaq 0xddf41(%rip), %rsi     # 0x1ee084
xorl %eax, %eax
callq 0x22b0
jmp 0x10ffd2
movl 0x1c(%rsp), %edx
shrl %edx
incl %edx
movl 0x18(%rsp), %ecx
shrl %ecx
incl %ecx
cmpl $0x4, 0x14(%rsp)
leaq 0xddf10(%rip), %r8      # 0x1ee07b
leaq 0xddf0b(%rip), %rax     # 0x1ee07d
cmoveq %rax, %r8
leaq 0xdded4(%rip), %rsi     # 0x1ee051
xorl %eax, %eax
callq 0x22b0
jmp 0x10ffd2
movq %rbp, %rdi
callq 0x2220
movq 0x60(%rsp), %rdi
callq 0x2220
movq 0x30(%rsp), %r13
pushq $-0x1
popq %r9
xorl %r10d, %r10d
movl %r9d, %r11d
movl %r10d, %ecx
cmpq %r13, %rcx
jae 0x1103c9
movq 0x10(%r14), %rax
imulq $0x30, %rcx, %rcx
movq 0x28(%rax,%rcx), %rdx
movl 0x18(%rax,%rcx), %esi
imulq $0x28, %rsi, %rsi
xorl %edi, %edi
xorl %r8d, %r8d
cmpq %rdi, %rsi
je 0x1101f1
cmpb $0x0, 0x18(%rdx,%rdi)
js 0x1101eb
cmpq $0x0, 0x10(%rdx,%rdi)
jne 0x1101f1
movl 0x8(%rdx,%rdi), %ebx
incl %r8d
addq $0x28, %rdi
jmp 0x1101d0
cmpl $0x1, %r8d
jne 0x110231
movl %ebx, %edx
imulq $0x30, %rdx, %rsi
movq 0x28(%rax,%rsi), %rdx
movl 0x18(%rax,%rsi), %esi
imulq $0x28, %rsi, %rdi
xorl %r8d, %r8d
xorl %esi, %esi
cmpq %r8, %rdi
je 0x110236
cmpb $0x0, 0x18(%rdx,%r8)
js 0x11022b
cmpq $0x0, 0x10(%rdx,%r8)
jne 0x110236
movl 0x8(%rdx,%r8), %r11d
incl %esi
addq $0x28, %r8
jmp 0x11020f
movl %r9d, %ebx
jmp 0x110245
xorl $0x1, %esi
movl %r11d, %edx
xorl %r10d, %edx
orl %esi, %edx
cmovnel %r9d, %ebx
movq 0x10(%rax,%rcx), %rdx
movl (%rax,%rcx), %ecx
imulq $0x28, %rcx, %rsi
xorl %edi, %edi
xorl %ecx, %ecx
cmpq %rdi, %rsi
je 0x11026f
xorl %r8d, %r8d
cmpb $0x0, 0x18(%rdx,%rdi)
setns %r8b
addl %r8d, %ecx
addq $0x28, %rdi
jmp 0x110255
xorl %edx, %edx
cmpl $-0x1, %ebx
je 0x1102a9
movl %ebx, %esi
imulq $0x30, %rsi, %rdi
movq 0x10(%rax,%rdi), %rsi
movl (%rax,%rdi), %eax
imulq $0x28, %rax, %rax
xorl %edi, %edi
cmpq %rdi, %rax
je 0x1102a4
xorl %r8d, %r8d
cmpb $0x0, 0x18(%rsi,%rdi)
setns %r8b
addl %r8d, %edx
addq $0x28, %rdi
jmp 0x11028a
testl %edx, %edx
setne %dl
cmpl %ebx, %r10d
seta %al
testl %ecx, %ecx
je 0x1103c1
andb %al, %dl
jne 0x1103c1
movq %r10, 0x48(%rsp)
movq (%r15), %rax
movq 0x10(%r15), %r13
cmpq 0x8(%r15), %rax
movl %r11d, 0x2c(%rsp)
jne 0x1102fe
leaq (%rax,%rax), %rcx
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rcx
movq %rcx, 0x8(%r15)
imulq $0x48, %rcx, %rsi
movq %r13, %rdi
callq 0x2370
movq %rax, %r13
movq %rax, 0x10(%r15)
movq (%r15), %rax
leaq 0x1(%rax), %rbp
movq %rbp, (%r15)
imulq $0x48, %rax, %r14
leaq (%r14,%r13), %r12
movb $0x0, 0x20(%r13,%r14)
pushq $0x1
popq %rax
movq %rax, 0x18(%r13,%r14)
movq $0x2, 0x8(%r13,%r14)
pushq $0x8
popq %rdi
callq 0x2280
movq %rax, 0x10(%r13,%r14)
pushq $0x1
popq %rcx
movq %rcx, (%r13,%r14)
movq 0x48(%rsp), %r10
movl %r10d, (%rax)
cmpl $-0x1, %ebx
je 0x11035d
movl $0x1, 0x1c(%r12)
movq $0x2, (%r12)
movl %ebx, 0x4(%rax)
pushq $0x2
popq %rcx
leal -0x1(%rbp), %esi
xorl %edx, %edx
movq 0x38(%rsp), %r14
movq 0x30(%rsp), %r13
pushq $-0x1
popq %r9
movl 0x2c(%rsp), %r11d
movl 0x18(%r12), %edi
cmpq %rdi, %rdx
jae 0x110398
movq 0x18(%r15), %rdi
movl (%rax,%rdx,4), %r8d
movl %esi, (%rdi,%r8,4)
movl (%rax,%rdx,4), %r8d
shll (%rdi,%r8,4)
incq %rdx
jmp 0x110375
decl %ebp
cmpl %edx, %ecx
jbe 0x1103c1
movl %edx, %edx
movq 0x18(%r15), %rsi
movl (%rax,%rdx,4), %edi
movl %ebp, (%rsi,%rdi,4)
movl (%rax,%rdx,4), %edi
movl (%rsi,%rdi,4), %r8d
leal 0x1(,%r8,2), %r8d
movl %r8d, (%rsi,%rdi,4)
incl %edx
jmp 0x11039a
incl %r10d
jmp 0x1101aa
movq 0x142c10(%rip), %rax    # 0x252fe0
movq (%rax), %rbx
callq 0xefea7
subsd 0x40(%rsp), %xmm0
leaq 0xdd6f6(%rip), %rsi     # 0x1edadb
leaq 0xddcfc(%rip), %rdx     # 0x1ee0e8
movq %rbx, %rdi
movb $0x1, %al
callq 0x22b0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq