// File: /workspace/github_cpp_makefile_star2_O3/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), %r15d
addl %r15d, %r15d
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x2120
movq %rax, %rbx
xorps %xmm0, %xmm0
leaq 0x40(%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 0xc0162
movq %rax, %r12
testl $0x7fffffff, 0x14(%r14) # imm = 0x7FFFFFFF
je 0x25be7
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 0x25bd0
testq %r15, %r15
je 0x25d7f
xorl %r13d, %r13d
movb $0x1, %bpl
cmpb $0x0, (%rbx,%r13)
je 0x25c0a
incq %r13
cmpq %r15, %r13
jne 0x25bf6
jmp 0x25cbe
movq 0x28(%r14), %rax
testl $0xfffffffe, (%rax,%r13,8) # imm = 0xFFFFFFFE
je 0x25bfd
movq 0x20(%r14), %rax
movl %r13d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x25bfd
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
leaq 0x40(%rsp), %r8
pushq $-0x1
popq %r9
callq 0xc02d6
testq %rax, %rax
je 0x25bfd
movq 0x58(%rsp), %rax
movq 0x78(%rsp), %rcx
testq %rcx, %rcx
je 0x25caa
movq 0x88(%rsp), %rdx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
cmpq %rdi, %r13
je 0x25ca0
cmpl (%rax), %edi
je 0x25ca0
movq %rdi, %r8
xorq $0x1, %r8
movb %bpl, (%rbx,%r8)
movb %bpl, (%rbx,%rdi)
incq %rsi
movl %esi, %edi
cmpq %rdi, %rcx
ja 0x25c85
movb $0x2, (%rbx,%r13)
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%rbx,%rax)
jmp 0x25bfd
testl %r15d, %r15d
je 0x25d7f
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpb $0x0, (%rbx,%rax)
jne 0x25d73
movq 0x28(%r14), %rdx
movq (%rdx,%rax,8), %rsi
testl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
je 0x25d73
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 0x25d6b
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 0x25d6b
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 0x25d62
orb $-0x80, 0x3(%r10,%r11)
addq $0x18, %r11
cmpq %r11, %r9
jne 0x25d4f
incq %rdi
cmpq %rsi, %rdi
jne 0x25cff
incq %rax
cmpq %r15, %rax
jne 0x25cce
movl 0x4(%r14), %edx
xorl %ecx, %ecx
movl $0x0, %r8d
andq $0x7fffffff, %rdx       # imm = 0x7FFFFFFF
je 0x25dc8
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 0x25da3
movq 0x26f211(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
leaq 0x21e7cd(%rip), %rsi    # 0x2445a6
leaq 0x21e7f6(%rip), %rdx    # 0x2445d6
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