// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: clean_ul_g(asg_t*)
void clean_ul_g(asg_t *xg)
{
	uint32_t n_vtx = xg->n_seq * 2, v, i, nv, ie = 0, ike = 0;
	asg_arc_t *av = NULL;
	uint8_t* bs_flag = NULL; CALLOC(bs_flag, n_vtx);
	buf_t b; memset(&b, 0, sizeof(buf_t)); b.a = (binfo_t*)calloc(n_vtx, sizeof(binfo_t));
    uint64_t max_dist = get_bub_pop_max_dist_advance(xg, &b);
	for (v = 0; v < xg->n_seq; v++) xg->seq[v].c = 0;
	for (v = 0; v < n_vtx; ++v) {
		if(bs_flag[v] != 0) continue;
		if (asg_arc_n(xg, v) < 2 || xg->seq[v>>1].del) continue;
		if(asg_bub_pop1_primary_trio(xg, NULL, v, max_dist, &b, (uint32_t)-1, (uint32_t)-1, 0, NULL, NULL, NULL, 0, 0, NULL)) {
			//beg is v, end is b.S.a[0]
			//note b.b include end, does not include beg
			for (i = 0; i < b.b.n; i++) {
				if(b.b.a[i]==v || b.b.a[i]==b.S.a[0]) continue;
				bs_flag[b.b.a[i]] = bs_flag[b.b.a[i]^1] = 1;
			}
			bs_flag[v] = 2; bs_flag[b.S.a[0]^1] = 3;
		}
	}

	for (v = 0; v < n_vtx; ++v) {
		if(bs_flag[v] != 0) continue;
		nv = asg_arc_n(xg, v);
		if (nv >= 2) {
			av = asg_arc_a(xg, v);
			for (i = 0; i < nv; ++i){
				if (av[i].ol == 0) {
					av[i].del = 1;
					asg_arc_del(xg, av[i].v^1, (av[i].ul>>32)^1, 1);
					// fprintf(stderr, "---q0-utg%.6d%c, q1-utg%.6d%c\n", 
					// (int32_t)((av[i].ul>>33)+1), "lc"[ug->u.a[av[i].ul>>33].circ], 
					// (int32_t)((av[i].v)>>1)+1, "lc"[ug->u.a[av[i].v].circ]);
				}

				// fprintf(stderr, "xxxx-nv: %u, q0-utg%.6d%c, q1-utg%.6d%c\n", nv, 
				// 	(int32_t)((av[i].ul>>33)+1), "lc"[ug->u.a[av[i].ul>>33].circ], 
				// 	(int32_t)((av[i].v)>>1)+1, "lc"[ug->u.a[av[i].v].circ]);
			}
		}
	}

	for (i = 0; i < xg->n_arc; i++) {
		if(xg->arc[i].ol == 0) {
			ie++;
			if(!xg->arc[i].del) ike++;
		}
	}
	
	fprintf(stderr, "[M::%s::] ==> # fill gaps: %u, # keep gaps: %u\n", __func__, ie, ike);
	free(bs_flag); free(b.a); free(b.S.a); free(b.T.a); free(b.b.a); free(b.e.a);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %r14
movl 0x14(%rdi), %r12d
addl %r12d, %r12d
pushq $0x1
popq %rsi
movq %r12, %rdi
callq 0x2120
movq %rax, %rbx
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %r15
movaps %xmm0, 0x50(%r15)
movaps %xmm0, 0x40(%r15)
movaps %xmm0, 0x30(%r15)
movaps %xmm0, 0x20(%r15)
movaps %xmm0, 0x10(%r15)
movaps %xmm0, (%r15)
andq $0x0, 0x60(%r15)
pushq $0x1c
popq %rsi
movq %r12, %rdi
callq 0x2120
movq %rax, (%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0xbf22d
movq %rax, %r15
testl $0x7fffffff, 0x14(%r14) # imm = 0x7FFFFFFF
je 0x26a5e
movq 0x20(%r14), %rax
xorl %ecx, %ecx
movb $0x0, 0x4(%rax,%rcx,8)
incq %rcx
movl 0x14(%r14), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rdx, %rcx
jb 0x26a47
testl %r12d, %r12d
je 0x26bf5
movl %r12d, %r13d
xorl %r12d, %r12d
movb $0x1, %bpl
cmpb $0x0, (%rbx,%r12)
je 0x26a84
incq %r12
cmpq %r13, %r12
jne 0x26a70
jmp 0x26b3d
movq 0x28(%r14), %rax
testl $0xfffffffe, (%rax,%r12,8) # imm = 0xFFFFFFFE
je 0x26a77
movq 0x20(%r14), %rax
movl %r12d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x26a77
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
andq $0x0, 0x38(%rsp)
andl $0x0, 0x30(%rsp)
andl $0x0, 0x28(%rsp)
andq $0x0, 0x20(%rsp)
andl $0x0, 0x8(%rsp)
orl $-0x1, (%rsp)
movq %r14, %rdi
xorl %esi, %esi
movl %r12d, %edx
movq %r15, %rcx
leaq 0x40(%rsp), %r8
pushq $-0x1
popq %r9
callq 0xbf39f
testq %rax, %rax
je 0x26a77
movq 0x78(%rsp), %rax
testq %rax, %rax
je 0x26b24
movq 0x58(%rsp), %rcx
movq 0x88(%rsp), %rdx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
cmpq %rdi, %r12
je 0x26b1a
cmpl (%rcx), %edi
je 0x26b1a
movq %rdi, %r8
xorq $0x1, %r8
movb %bpl, (%rbx,%r8)
movb %bpl, (%rbx,%rdi)
incq %rsi
movl %esi, %edi
cmpq %rdi, %rax
ja 0x26aff
movb $0x2, (%rbx,%r12)
movq 0x58(%rsp), %rax
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%rbx,%rax)
jmp 0x26a77
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpb $0x0, (%rbx,%rax)
jne 0x26be9
movq 0x28(%r14), %rdx
movq (%rdx,%rax,8), %rsi
testl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
je 0x26be9
movq %rsi, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
addq 0x8(%r14), %rdx
movl %esi, %esi
xorl %edi, %edi
imulq $0x18, %rdi, %r8
testl $0x7fffffff, 0xc(%rdx,%r8) # imm = 0x7FFFFFFF
jne 0x26be1
addq %rdx, %r8
movl $0x80000000, 0xc(%r8)   # imm = 0x80000000
movl 0x8(%r8), %r9d
xorq $0x1, %r9
movq 0x28(%r14), %r10
movq (%r10,%r9,8), %r10
movq %r10, %r9
andq %rcx, %r9
je 0x26be1
movl 0x4(%r8), %r8d
movq 0x8(%r14), %r11
shrq $0x20, %r10
imulq $0x18, %r9, %r9
imulq $0x18, %r10, %r10
addq %r11, %r10
addq $0xc, %r10
xorl %r11d, %r11d
movl -0x4(%r10,%r11), %ebp
xorl %r8d, %ebp
cmpl $0x1, %ebp
jne 0x26bd8
orb $-0x80, 0x3(%r10,%r11)
addq $0x18, %r11
cmpq %r11, %r9
jne 0x26bc5
incq %rdi
cmpq %rsi, %rdi
jne 0x26b75
incq %rax
cmpq %r13, %rax
jne 0x26b44
movl 0x4(%r14), %edx
xorl %ecx, %ecx
movl $0x0, %r8d
andq $0x7fffffff, %rdx       # imm = 0x7FFFFFFF
je 0x26c3e
movq 0x8(%r14), %rax
imulq $0x18, %rdx, %rdx
xorl %esi, %esi
xorl %r8d, %r8d
xorl %ecx, %ecx
movl 0xc(%rax,%rsi), %edi
movl %edi, %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
cmpl $0x1, %r9d
adcl $0x0, %ecx
cmpl $0x1, %edi
adcl $0x0, %r8d
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0x26c19
movq 0x26339b(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
leaq 0x211687(%rip), %rsi    # 0x2382d6
leaq 0x2116b0(%rip), %rdx    # 0x238306
xorl %eax, %eax
callq 0x22b0
movq %rbx, %rdi
callq 0x2220
movq 0x40(%rsp), %rdi
callq 0x2220
movq 0x58(%rsp), %rdi
callq 0x2220
movq 0x70(%rsp), %rdi
callq 0x2220
movq 0x88(%rsp), %rdi
callq 0x2220
movq 0xa0(%rsp), %rdi
callq 0x2220
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq