// File: /workspace/github_cpp_makefile_star2_O1/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 %rdi, %rbx
movq %r8, 0x40(%rsp)
testq %r8, %r8
je 0x1d65f8
xorl %r13d, %r13d
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
xorl %r12d, %r12d
movq 0x58(%rsp), %rax
movq 0x88(%rax), %rcx
movq 0x50(%rsp), %rax
movl (%rax,%r12,8), %eax
movq 0x48(%rsp), %rdx
movq (%rdx,%rax,8), %rdx
movl %edx, %eax
shrq $0x20, %rdx
imulq $0x68, %rdx, %rdx
movq 0x58(%rcx,%rdx), %r8
movq (%r8,%rax,8), %rsi
movabsq $-0x100000001, %rdi     # imm = 0xFFFFFFFEFFFFFFFF
cmpq %rdi, %rsi
ja 0x1d6639
movl %esi, %esi
cmpl $-0x1, %esi
jne 0x1d6639
movq 0x48(%rcx,%rdx), %r9
leaq 0x1(%rax), %rcx
cmpq %r9, %rcx
jae 0x1d661a
leaq (%r8,%rax,8), %rdi
addq $0x8, %rdi
movq %rax, %rdx
subq %r9, %rdx
incq %rdx
xorl %esi, %esi
xorl %ecx, %ecx
cmpq $-0x1, (%rdi,%rcx,8)
je 0x1d618d
incq %rcx
addq $-0x8, %rsi
movq %rdx, %r10
addq %rcx, %r10
jne 0x1d6172
jmp 0x1d661a
leaq (%rax,%rcx), %rdx
addq $0x2, %rdx
cmpq %r9, %rdx
movq %r13, 0x8(%rsp)
jae 0x1d61c3
movq (%r8,%rdx,8), %r10
cmpq $-0x1, %r10
setne %r11b
cmpl $-0x1, %r10d
sete %r10b
testb %r10b, %r11b
jne 0x1d61c3
incq %rdx
cmpq %r9, %rdx
jb 0x1d619f
movq %r9, %rdx
movq $-0x1, %r13
movq $-0x1, %rbp
testq %rcx, %rcx
je 0x1d61ef
movq (%rdi), %r9
movl %r9d, %r10d
shrq $0x20, %r9
cmpq $0x1, %r9
movl $0x0, %ebp
sbbq %rbp, %rbp
orq %r10, %rbp
leaq (%r8,%rax,8), %r11
cmpq $0x2, %rcx
jb 0x1d6214
movq (%r11,%rcx,8), %r9
movl %r9d, %r10d
shrq $0x20, %r9
cmpq $0x1, %r9
movl $0x0, %r13d
sbbq %r13, %r13
orq %r10, %r13
movq %rax, %r9
subq %rdx, %r9
leaq (%r9,%rcx), %r15
addq $0x2, %r15
movq $-0x1, %r9
movq $-0x1, %r10
testq %r15, %r15
je 0x1d6251
movq 0x8(%rdi,%rcx,8), %r10
movl %r10d, %r15d
shrq $0x20, %r10
cmpq $0x1, %r10
movl $0x0, %r10d
sbbq %r10, %r10
orq %r15, %r10
movq %rdx, %r15
subq %rax, %r15
subq %rcx, %r15
addq $-0x2, %r15
cmpq $0x2, %r15
jb 0x1d6280
movq -0x8(%r8,%rdx,8), %r8
movl %r8d, %r15d
shrq $0x20, %r8
cmpq $0x1, %r8
movl $0x0, %r9d
sbbq %r9, %r9
orq %r15, %r9
cmpq %r10, %rbp
jne 0x1d6658
movq %r12, 0x60(%rsp)
cmpq %r9, %r13
jne 0x1d6661
xorl %r15d, %r15d
cmpq $-0x1, %rbp
setne %r15b
leaq (%rdi,%r15,8), %rdi
xorl %r8d, %r8d
cmpq $-0x1, %r13
setne %r8b
addq %r15, %r8
negq %r8
xorl %r15d, %r15d
cmpq $-0x1, %r10
setne %r15b
leaq (%r11,%r15,8), %r11
subq %rsi, %r11
addq $0x10, %r11
movq %r11, 0x10(%rsp)
xorl %esi, %esi
cmpq $-0x1, %r9
setne %sil
subq %r15, %rdx
subq %rsi, %rdx
movq %rdx, %r15
subq %rax, %r15
subq %rcx, %r15
addq $-0x2, %r15
addq %rcx, %r8
jne 0x1d6318
subq %rdx, %rax
addq %rcx, %rax
addq $0x2, %rax
testq %rax, %rax
je 0x1d6318
movq %r15, 0x18(%rsp)
movq %rdi, 0x20(%rsp)
xorl %r15d, %r15d
movq %r10, %rax
movq %r9, %r12
jmp 0x1d6338
movq 0x10(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %rdi, 0x10(%rsp)
movq %r8, 0x18(%rsp)
movq %rbp, %rax
movq %r13, %r12
movq %r10, %rbp
movq %r9, %r13
movq $0x0, 0x30(%rsp)
cmpq $-0x1, %rax
movq %r13, 0x28(%rsp)
je 0x1d6380
movq %rbx, %rdi
movl %eax, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r12, %r13
movq %rbp, %r12
movq %r15, %rbp
movq %rax, %r15
callq 0x1e50d4
cmpl $0x2, %eax
movq %r15, %rax
movq %rbp, %r15
movq %r12, %rbp
movq %r13, %r12
movq 0x28(%rsp), %r13
jb 0x1d6506
cmpq $-0x1, %r12
je 0x1d63be
movl %r12d, %esi
xorl $0x1, %esi
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r12, %r13
movq %rbp, %r12
movq %r15, %rbp
movq %rax, %r15
callq 0x1e50d4
cmpl $0x2, %eax
movq %r15, %rax
movq %rbp, %r15
movq %r12, %rbp
movq %r13, %r12
movq 0x28(%rsp), %r13
jb 0x1d6506
movq $0x0, (%rsp)
movq %rbx, %rdi
movq %rax, 0x38(%rsp)
movq %rax, %rsi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %r8
xorl %r9d, %r9d
callq 0x1d57d1
testq %rax, %rax
je 0x1d6506
leaq 0x30(%rsp), %rax
movq %rax, (%rsp)
movl $0x1, %r9d
movq %rbx, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq 0x20(%rsp), %rcx
movq %r15, %r8
callq 0x1d57d1
testq %rax, %rax
je 0x1d6506
testq %r15, %r15
je 0x1d6526
xorl %eax, %eax
movq 0x20(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
movq 0x20(%rbx), %rdx
movq %rcx, %rsi
movabsq $0x1fffffffe, %rdi      # imm = 0x1FFFFFFFE
andq %rdi, %rsi
orl $0x80000000, (%rdx,%rsi,4) # imm = 0x80000000
andl $-0x2, %ecx
xorl %edx, %edx
leaq (%rdx,%rcx), %rsi
movq 0x28(%rbx), %rdi
movq (%rdi,%rsi,8), %r8
movq %r8, %rdi
andq %r14, %rdi
je 0x1d64e0
shrq $0x20, %r8
leaq (%r8,%r8,2), %r8
shlq $0x3, %r8
addq 0x8(%rbx), %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(%rbx), %r11
movq (%r11,%r10,8), %r11
movq %r11, %r10
andq %r14, %r10
je 0x1d64d8
movq 0x8(%rbx), %r13
shrq $0x20, %r11
shlq $0x3, %r10
leaq (%r10,%r10,2), %r10
leaq (%r11,%r11,2), %r11
leaq 0xc(,%r11,8), %r11
addq %r13, %r11
xorl %r13d, %r13d
movl -0x4(%r11,%r13), %ebp
xorl %esi, %ebp
cmpl $0x1, %ebp
jne 0x1d64cf
orb $-0x80, 0x3(%r11,%r13)
addq $0x18, %r13
cmpq %r13, %r10
jne 0x1d64bd
incq %r9
cmpq %rdi, %r9
jne 0x1d6478
leaq 0x1(%rdx), %rsi
testq %rdx, %rdx
movq %rsi, %rdx
je 0x1d6451
incq %rax
cmpq %r15, %rax
jne 0x1d6428
movq 0x8(%rsp), %rdi
jmp 0x1d65c8
movq 0x8(%rsp), %r15
movq 0x60(%rsp), %r12
incq %r12
movq %r15, %r13
cmpq 0x40(%rsp), %r12
jne 0x1d60fc
jmp 0x1d65fb
movq 0x28(%rbx), %rax
movl %ebp, %ecx
movq (%rax,%rcx,8), %rcx
movq %rcx, %rax
andq %r14, %rax
je 0x1d656b
movq 0x8(%rbx), %rdx
shrq $0x20, %rcx
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %rcx
addq $0xc, %rcx
xorl %edx, %edx
cmpl %r13d, -0x4(%rcx,%rdx)
jne 0x1d6562
orb $-0x80, 0x3(%rcx,%rdx)
addq $0x18, %rdx
cmpq %rdx, %rax
jne 0x1d6556
movq 0x28(%rbx), %rax
movl %r13d, %ecx
xorq $0x1, %rcx
movq (%rax,%rcx,8), %rcx
movq %rcx, %rax
movl $0x1, %r15d
andq %r14, %rax
je 0x1d64fc
movq 0x8(%rbx), %rdx
shrq $0x20, %rcx
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %rcx
addq $0xc, %rcx
xorl %edx, %edx
movq 0x8(%rsp), %rdi
movl -0x4(%rcx,%rdx), %esi
xorl %ebp, %esi
cmpl $0x1, %esi
jne 0x1d65bf
orb $-0x80, 0x3(%rcx,%rdx)
addq $0x18, %rdx
cmpq %rdx, %rax
jne 0x1d65af
addq %rdi, %r15
movq 0x30(%rsp), %r9
testq %r9, %r9
movq 0x38(%rsp), %rsi
je 0x1d650b
movq %rbx, %rdi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %r8
callq 0x1d5ddf
jmp 0x1d650b
xorl %r15d, %r15d
testq %r15, %r15
je 0x1d6608
movq %rbx, %rdi
callq 0xa02fa
movq %r15, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x614f6(%rip), %rdi     # 0x237b17
leaq 0x5d363(%rip), %rsi     # 0x23398b
leaq 0x6148b(%rip), %rcx     # 0x237aba
movl $0x4b74, %edx           # imm = 0x4B74
callq 0x2110
leaq 0x61435(%rip), %rdi     # 0x237a75
leaq 0x5d344(%rip), %rsi     # 0x23398b
leaq 0x6146c(%rip), %rcx     # 0x237aba
movl $0x4b6b, %edx           # imm = 0x4B6B
callq 0x2110
leaq 0x614bf(%rip), %rdi     # 0x237b1e
jmp 0x1d6668
leaq 0x614c5(%rip), %rdi     # 0x237b2d
leaq 0x5d31c(%rip), %rsi     # 0x23398b
leaq 0x61444(%rip), %rcx     # 0x237aba
movl $0x4b92, %edx           # imm = 0x4B92
callq 0x2110