// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/rcut.cpp
// Function: gen_ref_bub(mc_clus_t*, kv_u_trans_t*)
asg64_v* gen_ref_bub(mc_clus_t *bc, kv_u_trans_t *ref)
{
	if(!ref) return NULL;
	asg64_v *aux; CALLOC(aux, 1); aux->n = aux->m = bc->n; double w, mmw;
	MALLOC(aux->a, bc->n); memset(aux->a, -1, sizeof((*(aux->a)))*bc->n);
	uint32_t k, l, i, *ba, bn, m, on, bid; ma_utg_t *u = NULL; uint64_t p, mm, st, j;
	u_trans_t *o = NULL; asg64_v buf; kv_init(buf); 

	for (i = bid = 0; i < bc->bub->b_ug->u.n; i++) {///set nodes within bubbles
        u = &(bc->bub->b_ug->u.a[i]);
        for (k = 0; k < u->n; k++, bid++) {
            get_bubbles(bc->bub, u->a[k]>>33, NULL, NULL, &ba, &bn, NULL);
            for (m = 0; m < bn; m++) aux->a[ba[m]>>1] = bid;
        }
    }
	// bin = bid;

	for (i = 0; i < bc->bub->ug->g->n_seq; i++) {
		if(aux->a[i] != ((uint64_t)-1)) continue;
		o = u_trans_a(*ref, i); on = u_trans_n(*ref, i);
		if(!on) continue;
		buf.n = 0; kv_resize(uint64_t, buf, on);
		for (k = 0; k < on; k++) {
			p = aux->a[o[k].tn]; p<<= 32; p += k;
			kv_push(uint64_t, buf, p);
		}

		radix_sort_mc64(buf.a, buf.a + buf.n); 
		mm = (uint32_t)-1; mmw = -1;
		for (l = 0, k = 1; k <= buf.n; k++) {
			if((k == buf.n) || ((buf.a[l]>>32) == ((uint32_t)-1)) || ((buf.a[l]>>32) != (buf.a[k]>>32))) {
				for (m = l, w = 0; m < k; m++) w += fabs(o[((uint32_t)buf.a[m])].nw);
				if(mmw < w) {
					mmw = w; mm = buf.a[l]>>32;
				}
				l = k;
			}
		}
		if(mm != ((uint32_t)-1)) aux->a[i] = mm;
	}


	///final cluster
	for (i = 0; i < bc->bub->ug->g->n_seq; i++) {
		if(aux->a[i] != ((uint64_t)-1)) continue;
		buf.n = 0; kv_push(uint64_t, buf, i);
        while (buf.n > 0) {
            k = buf.a[--buf.n];
			if(aux->a[k] != ((uint64_t)-1)) continue;
            aux->a[k] = bid;///group id
            o = u_trans_a(*ref, k); on = u_trans_n(*ref, k);
            for (st = 0, j = 1; j <= on; ++j) {
                if(j == on || o[j].tn != o[st].tn) {
					if(aux->a[o[st].tn] == ((uint64_t)-1)) {
						kv_push(uint64_t, buf, o[st].tn);
					}
                    st = j;
                }
            }
        }
		bid++;
	}

	assert(aux->n == bc->bub->ug->g->n_seq);

	for (i = 0; i < bc->bub->b_ug->u.n; i++) {///no need nodes in bubbles
        u = &(bc->bub->b_ug->u.a[i]);
        for (k = 0; k < u->n; k++) {
            get_bubbles(bc->bub, u->a[k]>>33, NULL, NULL, &ba, &bn, NULL);
            for (m = 0; m < bn; m++) aux->a[ba[m]>>1] = (uint64_t)-1;
        }
    }
	for (i = aux->n = 0; i < bc->bub->ug->g->n_seq; i++) {
		if(aux->a[i] == ((uint64_t)-1)) continue;
		aux->a[i] <<= 32; aux->a[i] |= i;//bub_id|node_id
		aux->a[aux->n++] = aux->a[i];
	}
	radix_sort_mc64(aux->a, aux->a + aux->n);
	kv_destroy(buf);
	return aux;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
xorl %ebp, %ebp
testq %rsi, %rsi
je 0x12ae23
movq %rsi, 0x48(%rsp)
movq %rdi, %r13
pushq $0x1
popq %rdi
pushq $0x18
popq %rsi
callq 0x2120
movq %rax, %rbx
movl 0x54(%r13), %r14d
movq %r14, 0x8(%rax)
movq %r14, (%rax)
shlq $0x3, %r14
movq %r14, %rdi
callq 0x2280
movq %rax, %r12
movq %rax, 0x10(%rbx)
movq %rax, %rdi
movl $0xff, %esi
movq %r14, %rdx
callq 0x20e0
movq %r13, %rdi
xorl %r9d, %r9d
movl %ebp, %ebp
movq 0x30(%rdi), %rax
movq 0xc0(%rax), %rcx
cmpq %rbp, (%rcx)
jbe 0x12a99c
movl %r9d, %r15d
movq %rbp, 0x28(%rsp)
imulq $0x28, %rbp, %r14
addq 0x10(%rcx), %r14
xorl %ebp, %ebp
movl 0x10(%r14), %eax
cmpq %rax, %rbp
jae 0x12a98d
movq 0x30(%rdi), %rdi
movq 0x18(%r14), %rax
movq (%rax,%rbp,8), %rsi
shrq $0x21, %rsi
andq $0x0, (%rsp)
xorl %edx, %edx
xorl %ecx, %ecx
leaq 0x40(%rsp), %r8
leaq 0x24(%rsp), %r9
callq 0xf6ab5
movl 0x24(%rsp), %eax
movl %r15d, %ecx
movq 0x40(%rsp), %rdx
xorl %esi, %esi
cmpq %rsi, %rax
je 0x12a982
movl (%rdx,%rsi,4), %edi
shrl %edi
movq %rcx, (%r12,%rdi,8)
incq %rsi
jmp 0x12a96f
incq %rbp
incl %r15d
movq %r13, %rdi
jmp 0x12a930
movq 0x28(%rsp), %rbp
incl %ebp
movl %r15d, %r9d
jmp 0x12a90c
xorl %r8d, %r8d
xorl %r12d, %r12d
xorl %esi, %esi
movq %rdi, 0x28(%rsp)
movl %r9d, 0x14(%rsp)
movq 0x10(%rax), %rcx
movq 0x18(%rcx), %rcx
movl 0x14(%rcx), %ecx
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rdx, %r8
jae 0x12aba7
movq 0x10(%rbx), %rax
cmpq $-0x1, (%rax,%r8,8)
je 0x12a9de
incq %r8
movq 0x30(%rdi), %rax
jmp 0x12a9ae
movq 0x48(%rsp), %rcx
movq 0x28(%rcx), %rax
movq (%rax,%r8,8), %rdx
testl %edx, %edx
je 0x12a9d5
movq 0x10(%rcx), %r14
movl %edx, %eax
cmpq %rax, %rsi
movq %r8, 0x30(%rsp)
jae 0x12aa58
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rdx, %rbp
movq %rax, %r13
shrq $0x10, %r13
orq %rax, %r13
leaq 0x8(,%r13,8), %rsi
incq %r13
movq %rdi, %r15
movq %r12, %rdi
callq 0x2370
movq %r13, %rsi
movq %rbp, %rdx
movq %r15, %rdi
movq %rax, %r12
movq %rdx, %rax
shrq $0x20, %rax
imulq $0x30, %rax, %rax
leaq (%r14,%rax), %rbp
movq %rdx, 0x38(%rsp)
movl %edx, %ecx
movq %rcx, 0x18(%rsp)
leaq (%r14,%rax), %rdx
addq $0x14, %rdx
xorl %r13d, %r13d
cmpq %r13, 0x18(%rsp)
je 0x12aad7
movq 0x10(%rbx), %rax
movl (%rdx), %ecx
movq (%rax,%rcx,8), %r15
cmpq %rsi, %r13
jne 0x12aabe
leaq (%rsi,%rsi), %r14
testq %rsi, %rsi
pushq $0x2
popq %rax
cmoveq %rax, %r14
leaq (,%r14,8), %rsi
movq %r12, %rdi
movq %rdx, %r12
callq 0x2370
movq %r12, %rdx
movq %rax, %r12
movq %r14, %rsi
shlq $0x20, %r15
addq %r13, %r15
movq %r15, (%r12,%r13,8)
incq %r13
addq $0x30, %rdx
movq 0x28(%rsp), %rdi
jmp 0x12aa7e
movq %rsi, %r15
movq %rdi, %r14
movq 0x18(%rsp), %rax
leaq (%r12,%rax,8), %rsi
movq %r12, %rdi
callq 0x1224a9
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %edx, %edx
movsd 0xb8a83(%rip), %xmm0    # 0x1e3580
pushq $0x1
popq %rcx
movl 0x14(%rsp), %r9d
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movapd 0xc407d(%rip), %xmm3    # 0x1eeb90
movq 0x38(%rsp), %r11
cmpl %r11d, %ecx
ja 0x12ab7a
movl %ecx, %edi
movl %edx, %esi
cmpl %edi, 0x18(%rsp)
je 0x12ab3b
movq (%r12,%rsi,8), %r8
shrq $0x20, %r8
cmpq %r10, %r8
je 0x12ab3b
cmpl 0x4(%r12,%rdi,8), %r8d
je 0x12ab76
xorpd %xmm1, %xmm1
movq %rsi, %rdx
cmpq %rdi, %rdx
jae 0x12ab63
movl (%r12,%rdx,8), %r8d
imulq $0x30, %r8, %r8
movsd 0x20(%rbp,%r8), %xmm2
andpd %xmm3, %xmm2
addsd %xmm2, %xmm1
incq %rdx
jmp 0x12ab42
ucomisd %xmm0, %xmm1
movl %ecx, %edx
jbe 0x12ab76
movl 0x4(%r12,%rsi,8), %eax
movapd %xmm1, %xmm0
movl %ecx, %edx
incl %ecx
jmp 0x12ab18
cmpq %r10, %rax
jne 0x12ab8f
movq %r14, %rdi
movq %r15, %rsi
movq 0x30(%rsp), %r8
jmp 0x12a9d5
movq 0x10(%rbx), %rcx
movq 0x30(%rsp), %r8
movq %rax, (%rcx,%r8,8)
movq %r14, %rdi
movq %r15, %rsi
jmp 0x12a9d5
xorl %r15d, %r15d
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rdx, %r15
jae 0x12ad25
movq 0x10(%rbx), %rdx
cmpq $-0x1, (%rdx,%r15,8)
jne 0x12ad1d
testq %rsi, %rsi
movl %r9d, 0x14(%rsp)
jne 0x12abf0
movq %rdi, %r14
movq %r12, %rdi
pushq $0x10
popq %rsi
callq 0x2370
movq %r14, %rdi
movl 0x14(%rsp), %r9d
movq %rax, %r12
pushq $0x2
popq %rsi
movq %r15, (%r12)
movl %r9d, %edx
pushq $0x1
popq %r13
subq $0x1, %r13
jb 0x12ad0b
movl (%r12,%r13,8), %eax
movq 0x10(%rbx), %rcx
cmpq $-0x1, (%rcx,%rax,8)
jne 0x12abfb
movq %r15, 0x38(%rsp)
movq %rdx, 0x50(%rsp)
movq %rdx, (%rcx,%rax,8)
movq 0x48(%rsp), %rdx
movq 0x10(%rdx), %rcx
movq 0x28(%rdx), %rdx
movq (%rdx,%rax,8), %rax
movl %eax, %r8d
shrq $0x20, %rax
imulq $0x30, %rax, %rax
leaq (%rcx,%rax), %r10
negq %r8
leaq (%rcx,%rax), %rbp
addq $0x44, %rbp
xorl %eax, %eax
pushq $0x1
popq %r14
movq %r10, 0x30(%rsp)
leaq (%r8,%r14), %rcx
cmpq $0x1, %rcx
je 0x12acfc
testq %rcx, %rcx
je 0x12ac7b
imulq $0x30, %rax, %rcx
movl 0x14(%r10,%rcx), %ecx
cmpl %ecx, (%rbp)
jne 0x12ac84
jmp 0x12acf0
imulq $0x30, %rax, %rcx
movl 0x14(%r10,%rcx), %ecx
movq 0x10(%rbx), %rdx
movl %ecx, %ecx
cmpq $-0x1, (%rdx,%rcx,8)
jne 0x12aced
cmpq %rsi, %r13
jne 0x12ace6
imulq $0x30, %rax, %rax
addq %r10, %rax
addq $0x14, %rax
movq %rax, 0x18(%rsp)
leaq (%rsi,%rsi), %r15
testq %rsi, %rsi
pushq $0x2
popq %rax
cmoveq %rax, %r15
leaq (,%r15,8), %rsi
movq %r12, %rdi
movq %r8, %r12
callq 0x2370
movq 0x30(%rsp), %r10
movq %r12, %r8
movl 0x14(%rsp), %r9d
movq %rax, %r12
movq 0x18(%rsp), %rax
movl (%rax), %ecx
movq %r15, %rsi
movq 0x28(%rsp), %rdi
movq %rcx, (%r12,%r13,8)
incq %r13
movq %r14, %rax
incq %r14
addq $0x30, %rbp
jmp 0x12ac58
movq 0x38(%rsp), %r15
movq 0x50(%rsp), %rdx
jmp 0x12abfb
incl %r9d
movq 0x30(%rdi), %rax
movq 0x10(%rax), %rcx
movq 0x18(%rcx), %rcx
movl 0x14(%rcx), %ecx
incq %r15
jmp 0x12abaa
cmpq %rdx, (%rbx)
jne 0x12ae37
xorl %edx, %edx
leaq 0x40(%rsp), %r15
leaq 0x24(%rsp), %r13
movl %edx, %edx
movq 0xc0(%rax), %rcx
cmpq %rdx, (%rcx)
jbe 0x12adc6
movq %rdx, 0x18(%rsp)
imulq $0x28, %rdx, %rbp
addq 0x10(%rcx), %rbp
xorl %r14d, %r14d
movl 0x10(%rbp), %eax
cmpq %rax, %r14
jae 0x12adb1
movq 0x28(%rsp), %rax
movq 0x30(%rax), %rdi
movq 0x18(%rbp), %rax
movq (%rax,%r14,8), %rsi
shrq $0x21, %rsi
andq $0x0, (%rsp)
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, %r8
movq %r13, %r9
callq 0xf6ab5
movl 0x24(%rsp), %eax
movq 0x40(%rsp), %rcx
xorl %edx, %edx
cmpq %rdx, %rax
je 0x12adac
movq 0x10(%rbx), %rsi
movl (%rcx,%rdx,4), %edi
shrl %edi
orq $-0x1, (%rsi,%rdi,8)
incq %rdx
jmp 0x12ad94
incq %r14
jmp 0x12ad58
movq 0x18(%rsp), %rdx
incl %edx
movq 0x28(%rsp), %rax
movq 0x30(%rax), %rax
jmp 0x12ad3a
andq $0x0, (%rbx)
movq 0x10(%rax), %rax
movq 0x18(%rax), %rax
movq 0x10(%rbx), %rdi
xorl %ecx, %ecx
xorl %edx, %edx
movl 0x14(%rax), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmpq %rsi, %rdx
jae 0x12ae10
movq (%rdi,%rdx,8), %rsi
cmpq $-0x1, %rsi
je 0x12ae0b
shlq $0x20, %rsi
addq %rdx, %rsi
movq %rsi, (%rdi,%rdx,8)
leaq 0x1(%rcx), %r8
movq %r8, (%rbx)
movq %rsi, (%rdi,%rcx,8)
movq %r8, %rcx
incq %rdx
jmp 0x12adda
leaq (%rdi,%rcx,8), %rsi
callq 0x1224a9
movq %r12, %rdi
callq 0x2220
jmp 0x12ae25
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc4387(%rip), %rdi     # 0x1ef1c5
leaq 0xc4103(%rip), %rsi     # 0x1eef48
leaq 0xc4399(%rip), %rcx     # 0x1ef1e5
movl $0xd6a, %edx            # imm = 0xD6A
callq 0x2110