// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: asg_clean_idx_tig(asg_t*, unsigned long*, unsigned long, unsigned long*, unsigned long, utepdat_t*)
uint64_t asg_clean_idx_tig(asg_t *g, uint64_t *idx, uint64_t idx_n, uint64_t *srt, uint64_t srt_n, utepdat_t *s)
{
	uint64_t i, k, *a, an, sp, *del_a, del_n, *ref_a, ref_n, *ka, kk, cnt = 0, min_ou;
	uint64_t ref_s, ref_e, del_s, del_e;
	for (i = 0; i < srt_n; i++) {
		a = s->ll[idx[(uint32_t)srt[i]]>>32].srt.a.a; 
		an = s->ll[idx[(uint32_t)srt[i]]>>32].srt.a.n;
		sp = (uint32_t)idx[(uint32_t)srt[i]];
		assert((((uint32_t)a[sp])==((uint32_t)-1)) && ((a[sp]>>32)!=((uint32_t)-1)));

		// fprintf(stderr, "\n+[M::%s::] ((uint32_t)(a[sp]>>32))::%u, ((uint32_t)a[sp])::%u, sp::%lu, an::%lu\n", __func__, 
		// ((uint32_t)(a[sp]>>32)), ((uint32_t)a[sp]), sp, an); 

		k = sp + 1; ref_a = a + k;
		for (sp = k; k < an; k++) {
			if(a[k] == (uint64_t)-1) break;
		}
		assert(k < an);
		ref_n = k - sp;
		// fprintf(stderr, "+[M::%s::] k::%lu\n", __func__, k); 
		// fprintf(stderr, "+[M::%s::] k::%lu, an::%lu\n", __func__, k, an); 

		k++; del_a = a + k;
		for (sp = k; k < an; k++) {
			if((a[k] != (uint64_t)-1) && (((uint32_t)a[k]) == ((uint32_t)-1))) break;
		}
		
		// assert(k < an);//for the last batch of reads, there is no end marker, so k == an
		del_n = k - sp;

		ref_s = ref_e = del_s = del_e = (uint64_t)-1;
		if(ref_n > 0 && (ref_a[0]>>32)) ref_s = (uint32_t)ref_a[0];
		if(ref_n > 1 && (ref_a[ref_n-1]>>32)) ref_e = (uint32_t)ref_a[ref_n-1];

		if(del_n > 0 && (del_a[0]>>32)) del_s = (uint32_t)del_a[0];
		if(del_n > 1 && (del_a[del_n-1]>>32)) del_e = (uint32_t)del_a[del_n-1];

		// if((ref_e != del_e) || (ref_s != del_s)) {
		// 	fprintf(stderr, "-[M::%s::] idx_n::%lu, idx_n::%lu, ref_n::%lu, del_n::%lu, ref_s::%lu, ref_e::%lu, del_s::%lu, del_e::%lu\n", 
		// 	__func__, idx_n, idx_n, ref_n, del_n, ref_s, ref_e, del_s, del_e); 
		// 	for (k = 0; k < ref_n; k++) {
		// 		fprintf(stderr, "-[M::ref::] ref_a[k]>>1::%lu\n", ref_a[k]>>1); 
		// 	}
		// 	for (k = 0; k < del_n; k++) {
		// 		fprintf(stderr, "-[M::del::] del_a[k]>>1::%lu\n", del_a[k]>>1); 
		// 	}
		// }
		assert(ref_s == del_s); assert(ref_e == del_e); 

		if(ref_s != (uint64_t)-1) {
			ref_a = ref_a + 1; ref_n--;
		}
		if(ref_e != (uint64_t)-1) ref_n--;

		if(del_s != (uint64_t)-1) {
			del_a = del_a + 1; del_n--;
		}
		if(del_e != (uint64_t)-1) del_n--;

		if(ref_n == 0 && del_n > 0) {
			ka = ref_a; ref_a = del_a; del_a = ka;
			kk = ref_n; ref_n = del_n; del_n = kk;
			kk = ref_s; ref_s = del_s; del_s = kk;
			kk = ref_e; ref_e = del_e; del_e = kk;
		}

		// fprintf(stderr, "-[M::%s::] idx_n::%lu, idx_n::%lu, ref_n::%lu, del_n::%lu, ref_s::%lu, ref_e::%lu\n", 
		// __func__, idx_n, idx_n, ref_n, del_n, ref_s, ref_e); 
		// for (k = 0; k < ref_n; k++) {
		// 	fprintf(stderr, "-[M::ref::] ref_a[k]>>1::%lu\n", ref_a[k]>>1); 
		// }
		// for (k = 0; k < del_n; k++) {
		// 	fprintf(stderr, "-[M::del::] del_a[k]>>1::%lu\n", del_a[k]>>1); 
		// }
		


		///just for debug
		// if(ref_s != (uint64_t)-1) {
		// 	assert(get_arcs(g, ref_s, NULL, 0) > 1);
		// }
		// if(ref_e != (uint64_t)-1) {
		// 	assert(get_arcs(g, ref_e^1, NULL, 0) > 1);
		// }
		// assert(tes_path_connec(g, ref_s, ref_e, ref_a, ref_n, 0, NULL));
		// assert(tes_path_connec(g, del_s, del_e, del_a, del_n, 1, NULL));
		
		min_ou = 0;
		if((ref_s!=(uint64_t)-1) && (get_arcs(g, ref_s, NULL, 0)<=1)) continue;
		if((ref_e!=(uint64_t)-1) && (get_arcs(g, ref_e^1, NULL, 0)<=1)) continue;
		if(!tes_path_connec(g, ref_s, ref_e, ref_a, ref_n, 0, NULL)) continue;
		if(!tes_path_connec(g, del_s, del_e, del_a, del_n, 1, &min_ou)) continue;

		if(del_n > 0) {
			for (k = 0; k < del_n; k++) asg_seq_del(g, del_a[k]>>1);
			cnt += del_n;
		} else {
			asg_arc_del(g, del_s, del_e, 1);
			asg_arc_del(g, del_e^1, del_s^1, 1);
			cnt++;
		}
		if(min_ou > 0) append_path_connec(g, ref_s, ref_e, ref_a, ref_n, min_ou);
	}
	if(cnt) {
		asg_cleanup(g);
	}
	return cnt;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x58(%rsp)
movq %rcx, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movq %r8, 0x40(%rsp)
testq %r8, %r8
je 0x1e04e5
movq %rdi, %r14
xorl %r15d, %r15d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movq 0x58(%rsp), %rax
movq 0x88(%rax), %rcx
movq 0x50(%rsp), %rax
movl (%rax,%r13,8), %eax
movq 0x48(%rsp), %rdx
movq (%rdx,%rax,8), %rdx
movl %edx, %eax
shrq $0x20, %rdx
imulq $0x68, %rdx, %rsi
movq 0x58(%rcx,%rsi), %rdx
movq (%rdx,%rax,8), %rdi
movabsq $-0x100000001, %r8      # imm = 0xFFFFFFFEFFFFFFFF
cmpq %r8, %rdi
ja 0x1e0519
movl %edi, %edi
cmpl $-0x1, %edi
jne 0x1e0519
movq 0x48(%rcx,%rsi), %r9
leaq 0x1(%rax), %rcx
cmpq %r9, %rcx
jae 0x1e04fa
leaq (%rdx,%rax,8), %rcx
addq $0x8, %rcx
movq %rax, %r8
subq %r9, %r8
incq %r8
xorl %edi, %edi
xorl %esi, %esi
cmpq $-0x1, (%rcx,%rsi,8)
je 0x1e00a0
incq %rsi
addq $-0x8, %rdi
movq %r8, %r10
addq %rsi, %r10
jne 0x1e0085
jmp 0x1e04fa
leaq (%rax,%rsi), %r8
addq $0x2, %r8
cmpq %r9, %r8
jae 0x1e00d1
movq (%rdx,%r8,8), %r10
cmpq $-0x1, %r10
setne %r11b
cmpl $-0x1, %r10d
sete %r10b
testb %r10b, %r11b
jne 0x1e00d1
incq %r8
cmpq %r9, %r8
jb 0x1e00ad
movq %r9, %r8
movq $-0x1, %r9
movq $-0x1, %rbx
movq $-0x1, %r10
testq %rsi, %rsi
je 0x1e0132
movq (%rcx), %r10
movl %r10d, %r11d
shrq $0x20, %r10
cmpq $0x1, %r10
movl $0x0, %ebx
sbbq %rbx, %rbx
orq %r11, %rbx
cmpq $0x2, %rsi
jb 0x1e012b
leaq (%rdx,%rax,8), %r10
movq (%r10,%rsi,8), %r10
movl %r10d, %r11d
shrq $0x20, %r10
cmpq $0x1, %r10
movl $0x0, %r10d
sbbq %r10, %r10
orq %r11, %r10
jmp 0x1e0132
movq $-0x1, %r10
movq %r10, 0x10(%rsp)
movq %rax, %r10
subq %r8, %r10
addq %rsi, %r10
addq $0x2, %r10
testq %r10, %r10
je 0x1e019c
movq %rbx, %rbp
movq %r8, %r11
subq %rax, %r11
subq %rsi, %r11
addq $-0x2, %r11
movq 0x8(%rcx,%rsi,8), %r10
movl %r10d, %ebx
shrq $0x20, %r10
cmpq $0x1, %r10
movl $0x0, %r10d
sbbq %r10, %r10
orq %rbx, %r10
cmpq $0x2, %r11
jb 0x1e0197
movq -0x8(%rdx,%r8,8), %r9
movl %r9d, %r11d
shrq $0x20, %r9
cmpq $0x1, %r9
movl $0x0, %r9d
sbbq %r9, %r9
orq %r11, %r9
movq %rbp, %rbx
jmp 0x1e01a3
movq $-0x1, %r10
cmpq %r10, %rbx
jne 0x1e0538
movq %r13, 0x60(%rsp)
movq 0x10(%rsp), %r10
cmpq %r9, %r10
jne 0x1e0541
xorl %r9d, %r9d
movq %rbx, 0x18(%rsp)
cmpq $-0x1, %rbx
setne %r9b
leal (,%r9,8), %ebx
addq %rbx, %rcx
xorl %r11d, %r11d
cmpq $-0x1, %r10
setne %r11b
leaq (%r11,%r9), %r10
leaq (%rdx,%rax,8), %r13
addq %rbx, %r13
subq %rdi, %r13
addq $0x10, %r13
movq %r8, %rbp
subq %r10, %rbp
negq %r10
subq %rax, %rbp
subq %rsi, %rbp
addq $-0x2, %rbp
addq %rsi, %r10
jne 0x1e0234
subq %r8, %r9
addq %r11, %rax
addq %r9, %rax
addq %rsi, %rax
addq $0x2, %rax
testq %rax, %rax
je 0x1e0234
movq %rcx, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
jmp 0x1e0244
movq %r13, 0x20(%rsp)
movq %rbp, 0x28(%rsp)
movq %rcx, %r13
movq %r10, %rbp
movq 0x18(%rsp), %rbx
movq $0x0, 0x38(%rsp)
cmpq $-0x1, %rbx
je 0x1e026f
movq %r14, %rdi
movl %ebx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1efacc
cmpl $0x2, %eax
jb 0x1e04c3
movq 0x10(%rsp), %rax
cmpq $-0x1, %rax
je 0x1e029a
movl %eax, %esi
xorl $0x1, %esi
movq %r14, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1efacc
movq 0x10(%rsp), %rdx
cmpl $0x2, %eax
jae 0x1e02a1
jmp 0x1e04c3
movq $-0x1, %rdx
movq $0x0, (%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rcx
movq %rbp, %r8
xorl %r9d, %r9d
callq 0x1df74e
testq %rax, %rax
je 0x1e04c3
movq %r15, 0x30(%rsp)
leaq 0x38(%rsp), %rax
movq %rax, (%rsp)
movl $0x1, %r9d
movq %r14, %rdi
movq %rbx, %rsi
movq 0x10(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %r15
movq %r15, %r8
callq 0x1df74e
testq %rax, %rax
je 0x1e03fa
testq %r15, %r15
je 0x1e0404
xorl %eax, %eax
movq 0x20(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
movq 0x20(%r14), %rdx
movq %rcx, %rsi
movabsq $0x1fffffffe, %rdi      # imm = 0x1FFFFFFFE
andq %rdi, %rsi
orl $0x80000000, (%rdx,%rsi,4) # imm = 0x80000000
andl $-0x2, %ecx
movb $0x1, %dl
xorl %esi, %esi
orq %rcx, %rsi
movq 0x28(%r14), %rdi
movq (%rdi,%rsi,8), %r8
movq %r8, %rdi
andq %r12, %rdi
je 0x1e03c2
shrq $0x20, %r8
leaq (%r8,%r8,2), %r8
shlq $0x3, %r8
addq 0x8(%r14), %r8
xorl %r9d, %r9d
leaq (%r9,%r9,2), %r10
orb $-0x80, 0xf(%r8,%r10,8)
movl 0x8(%r8,%r10,8), %r10d
xorq $0x1, %r10
movq 0x28(%r14), %r11
movq (%r11,%r10,8), %r11
movq %r11, %r10
andq %r12, %r10
je 0x1e03ba
movq 0x8(%r14), %rbx
shrq $0x20, %r11
shlq $0x3, %r10
leaq (%r10,%r10,2), %r10
leaq (%r11,%r11,2), %r11
leaq (%rbx,%r11,8), %r11
addq $0xc, %r11
xorl %ebx, %ebx
movl -0x4(%r11,%rbx), %r15d
xorl %esi, %r15d
cmpl $0x1, %r15d
jne 0x1e03b1
orb $-0x80, 0x3(%r11,%rbx)
addq $0x18, %rbx
cmpq %rbx, %r10
jne 0x1e039d
incq %r9
cmpq %rdi, %r9
jne 0x1e035c
movl $0x1, %esi
testb $0x1, %dl
movl $0x0, %edx
jne 0x1e0336
incq %rax
movq 0x28(%rsp), %r8
cmpq %r8, %rax
jne 0x1e030b
movq 0x30(%rsp), %r15
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %rdx
jmp 0x1e04a5
movq 0x30(%rsp), %r15
jmp 0x1e04c3
movq 0x8(%r14), %rax
movq 0x28(%r14), %rcx
movl %ebx, %edx
movq (%rcx,%rdx,8), %rdi
movq %rdi, %rsi
andq %r12, %rsi
movq 0x10(%rsp), %rdx
movq 0x30(%rsp), %r15
je 0x1e045a
shrq $0x20, %rdi
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rcx
leaq (%rdi,%rdi,2), %rsi
leaq (%rax,%rsi,8), %rax
addq $0xc, %rax
xorl %esi, %esi
cmpl %edx, -0x4(%rax,%rsi)
jne 0x1e0449
orb $-0x80, 0x3(%rax,%rsi)
addq $0x18, %rsi
cmpq %rsi, %rcx
jne 0x1e043e
movq 0x8(%r14), %rax
movq 0x28(%r14), %rcx
movl %edx, %esi
xorq $0x1, %rsi
movq (%rcx,%rsi,8), %rsi
movq %rsi, %rcx
movl $0x1, %r8d
andq %r12, %rcx
je 0x1e04a5
shrq $0x20, %rsi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rsi,%rsi,2), %rsi
leaq (%rax,%rsi,8), %rax
addq $0xc, %rax
xorl %edi, %edi
movl -0x4(%rax,%rdi), %esi
xorl %ebx, %esi
cmpl $0x1, %esi
jne 0x1e049c
orb $-0x80, 0x3(%rax,%rdi)
addq $0x18, %rdi
cmpq %rdi, %rcx
jne 0x1e048c
addq %r8, %r15
movq 0x38(%rsp), %r9
testq %r9, %r9
je 0x1e04c3
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rcx
movq %rbp, %r8
callq 0x1dfcf2
movq 0x60(%rsp), %r13
incq %r13
cmpq 0x40(%rsp), %r13
jne 0x1e000f
testq %r15, %r15
je 0x1e04e5
movq %r14, %rdi
callq 0xa0a7d
jmp 0x1e04e8
xorl %r15d, %r15d
movq %r15, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x638e6(%rip), %rdi     # 0x243de7
leaq 0x5f753(%rip), %rsi     # 0x23fc5b
leaq 0x6387b(%rip), %rcx     # 0x243d8a
movl $0x4b74, %edx           # imm = 0x4B74
callq 0x2110
leaq 0x63825(%rip), %rdi     # 0x243d45
leaq 0x5f734(%rip), %rsi     # 0x23fc5b
leaq 0x6385c(%rip), %rcx     # 0x243d8a
movl $0x4b6b, %edx           # imm = 0x4B6B
callq 0x2110
leaq 0x638af(%rip), %rdi     # 0x243dee
jmp 0x1e0548
leaq 0x638b5(%rip), %rdi     # 0x243dfd
leaq 0x5f70c(%rip), %rsi     # 0x23fc5b
leaq 0x63834(%rip), %rcx     # 0x243d8a
movl $0x4b92, %edx           # imm = 0x4B92
callq 0x2110