// File: /workspace/github_cpp_makefile_star2_O2/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 $0xb8, %rsp
movq %rdi, %r14
movl 0x14(%rdi), %r15d
addl %r15d, %r15d
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x2120
movq %rax, 0x48(%rsp)
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %r12
movups %xmm0, 0x58(%r12)
movups %xmm0, 0x48(%r12)
movups %xmm0, 0x38(%r12)
movups %xmm0, 0x28(%r12)
movups %xmm0, 0x18(%r12)
movups %xmm0, 0x8(%r12)
pushq $0x1c
popq %rsi
movq %r15, %rdi
callq 0x2120
movq %rax, (%r12)
movq %r14, %rdi
movq %r12, %rsi
callq 0x84975
movq %rax, %r12
xorl %eax, %eax
movl 0x14(%r14), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %rax
jae 0x1963c9
movq 0x20(%r14), %rcx
movb $0x0, 0x4(%rcx,%rax,8)
incq %rax
jmp 0x1963ac
xorl %r13d, %r13d
leaq 0x50(%rsp), %rbx
movb $0x1, %bpl
cmpq %r15, %r13
je 0x1964a9
movq 0x48(%rsp), %rax
cmpb $0x0, (%rax,%r13)
je 0x1963ee
incq %r13
jmp 0x1963d4
movq 0x28(%r14), %rax
testl $0xfffffffe, (%rax,%r13,8) # imm = 0xFFFFFFFE
je 0x1963e9
movq 0x20(%r14), %rax
movl %r13d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x1963e9
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 %r13d, %edx
movq %r12, %rcx
movq %rbx, %r8
pushq $-0x1
popq %r9
callq 0x84a86
testq %rax, %rax
je 0x1963e9
movq 0x68(%rsp), %rax
movq 0x88(%rsp), %rcx
movq 0x98(%rsp), %rdx
xorl %esi, %esi
movl %esi, %esi
cmpq %rsi, %rcx
jbe 0x196490
movl (%rdx,%rsi,4), %edi
cmpq %rdi, %r13
je 0x19648c
cmpl (%rax), %edi
je 0x19648c
movq %rdi, %r8
xorq $0x1, %r8
movq 0x48(%rsp), %r9
movb %bpl, (%r9,%r8)
movb %bpl, (%r9,%rdi)
incl %esi
jmp 0x196465
movq 0x48(%rsp), %rcx
movb $0x2, (%rcx,%r13)
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%rcx,%rax)
jmp 0x1963e9
xorl %r12d, %r12d
cmpq %r15, %r12
je 0x19652e
movq %r15, %rbx
movq 0x48(%rsp), %rax
cmpb $0x0, (%rax,%r12)
je 0x1964c8
incq %r12
movq %rbx, %r15
jmp 0x1964ac
movq 0x28(%r14), %rax
movq (%rax,%r12,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x1964c0
movq 0x8(%r14), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movl %eax, %eax
imulq $0x18, %rax, %r13
imulq $0x18, %rdx, %rax
leaq (%rcx,%rax), %rbp
addq $0xc, %rbp
xorl %r15d, %r15d
cmpq %r15, %r13
je 0x1964c0
testl $0x7fffffff, (%rbp,%r15) # imm = 0x7FFFFFFF
jne 0x196528
movl $0x80000000, (%rbp,%r15) # imm = 0x80000000
movl -0x8(%rbp,%r15), %edx
movl -0x4(%rbp,%r15), %esi
xorl $0x1, %esi
xorl $0x1, %edx
movq %r14, %rdi
callq 0x193111
addq $0x18, %r15
jmp 0x1964f7
movl 0x4(%r14), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
imulq $0x18, %rax, %rax
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
cmpq %rdx, %rax
je 0x19656a
movq 0x8(%r14), %rsi
movl 0xc(%rsi,%rdx), %esi
movl %esi, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
cmpl $0x1, %edi
adcl $0x0, %ecx
cmpl $0x1, %esi
adcl $0x0, %r8d
addq $0x18, %rdx
jmp 0x196542
movq 0xbca6f(%rip), %rax     # 0x252fe0
movq (%rax), %rdi
leaq 0x5fef3(%rip), %rsi     # 0x1f646e
leaq 0x5ff1c(%rip), %rdx     # 0x1f649e
xorl %eax, %eax
callq 0x22b0
movq 0x48(%rsp), %rdi
callq 0x2220
movq 0x50(%rsp), %rdi
callq 0x2220
movq 0x68(%rsp), %rdi
callq 0x2220
movq 0x80(%rsp), %rdi
callq 0x2220
movq 0x98(%rsp), %rdi
callq 0x2220
movq 0xb0(%rsp), %rdi
callq 0x2220
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq