// File: 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
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x2ef996
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movq -0x10(%rbp), %rcx
movq -0x20(%rbp), %rdx
movq -0x38(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movl %edx, %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq 0x58(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movq -0x10(%rbp), %rcx
movq -0x20(%rbp), %rdx
movq -0x38(%rbp), %rsi
movq (%rdx,%rsi,8), %rdx
movl %edx, %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq 0x48(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq -0x20(%rbp), %rcx
movq -0x38(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0x58(%rbp)
movq -0x48(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %ecx
xorl %eax, %eax
cmpl $-0x1, %ecx
movb %al, -0xb9(%rbp)
jne 0x2ef444
movq -0x48(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
setne %al
movb %al, -0xb9(%rbp)
movb -0xb9(%rbp), %al
testb $0x1, %al
jne 0x2ef450
jmp 0x2ef452
jmp 0x2ef471
leaq 0xa54c0(%rip), %rdi     # 0x394919
leaq 0xa13cf(%rip), %rsi     # 0x39082f
movl $0x4b6b, %edx           # imm = 0x4B6B
leaq 0xa54f2(%rip), %rcx     # 0x39495e
callq 0x2130
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x50(%rbp), %rax
jae 0x2ef4c3
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
cmpq $-0x1, (%rax,%rcx,8)
jne 0x2ef4b3
jmp 0x2ef4c3
jmp 0x2ef4b5
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x2ef498
movq -0x40(%rbp), %rax
cmpq -0x50(%rbp), %rax
jae 0x2ef4cf
jmp 0x2ef4ee
leaq 0xa54e5(%rip), %rdi     # 0x3949bb
leaq 0xa1352(%rip), %rsi     # 0x39082f
movl $0x4b74, %edx           # imm = 0x4B74
leaq 0xa5475(%rip), %rcx     # 0x39495e
callq 0x2130
movq -0x40(%rbp), %rax
subq -0x58(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x50(%rbp), %rax
jae 0x2ef55d
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
cmpq $-0x1, (%rax,%rcx,8)
je 0x2ef54d
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpl $-0x1, %eax
jne 0x2ef54d
jmp 0x2ef55d
jmp 0x2ef54f
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x2ef521
movq -0x40(%rbp), %rax
subq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movq $-0x1, -0xb8(%rbp)
movq $-0x1, -0xb0(%rbp)
movq $-0x1, -0xa8(%rbp)
movq $-0x1, -0xa0(%rbp)
cmpq $0x0, -0x78(%rbp)
jbe 0x2ef5bd
movq -0x70(%rbp), %rax
movq (%rax), %rax
shrq $0x20, %rax
cmpq $0x0, %rax
je 0x2ef5bd
movq -0x70(%rbp), %rax
movq (%rax), %rax
movl %eax, %eax
movq %rax, -0xa0(%rbp)
cmpq $0x1, -0x78(%rbp)
jbe 0x2ef5f7
movq -0x70(%rbp), %rax
movq -0x78(%rbp), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
cmpq $0x0, %rax
je 0x2ef5f7
movq -0x70(%rbp), %rax
movq -0x78(%rbp), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0xa8(%rbp)
cmpq $0x0, -0x68(%rbp)
jbe 0x2ef61f
movq -0x60(%rbp), %rax
movq (%rax), %rax
shrq $0x20, %rax
cmpq $0x0, %rax
je 0x2ef61f
movq -0x60(%rbp), %rax
movq (%rax), %rax
movl %eax, %eax
movq %rax, -0xb0(%rbp)
cmpq $0x1, -0x68(%rbp)
jbe 0x2ef659
movq -0x60(%rbp), %rax
movq -0x68(%rbp), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
cmpq $0x0, %rax
je 0x2ef659
movq -0x60(%rbp), %rax
movq -0x68(%rbp), %rcx
subq $0x1, %rcx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
movq %rax, -0xb8(%rbp)
movq -0xa0(%rbp), %rax
cmpq -0xb0(%rbp), %rax
jne 0x2ef66b
jmp 0x2ef68a
leaq 0xa5350(%rip), %rdi     # 0x3949c2
leaq 0xa11b6(%rip), %rsi     # 0x39082f
movl $0x4b92, %edx           # imm = 0x4B92
leaq 0xa52d9(%rip), %rcx     # 0x39495e
callq 0x2130
movq -0xa8(%rbp), %rax
cmpq -0xb8(%rbp), %rax
jne 0x2ef69c
jmp 0x2ef6bb
leaq 0xa532e(%rip), %rdi     # 0x3949d1
leaq 0xa1185(%rip), %rsi     # 0x39082f
movl $0x4b92, %edx           # imm = 0x4B92
leaq 0xa52a8(%rip), %rcx     # 0x39495e
callq 0x2130
cmpq $-0x1, -0xa0(%rbp)
je 0x2ef6dd
movq -0x70(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x70(%rbp)
movq -0x78(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x78(%rbp)
cmpq $-0x1, -0xa8(%rbp)
je 0x2ef6f3
movq -0x78(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x78(%rbp)
cmpq $-0x1, -0xb0(%rbp)
je 0x2ef715
movq -0x60(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x60(%rbp)
movq -0x68(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x68(%rbp)
cmpq $-0x1, -0xb8(%rbp)
je 0x2ef72b
movq -0x68(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0x2ef7cb
cmpq $0x0, -0x68(%rbp)
jbe 0x2ef7cb
movq -0x70(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq $0x0, -0x98(%rbp)
cmpq $-0x1, -0xa0(%rbp)
je 0x2ef802
movq -0x8(%rbp), %rdi
movq -0xa0(%rbp), %rax
movl %eax, %esi
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x311ba0
cmpl $0x1, %eax
ja 0x2ef802
jmp 0x2ef985
cmpq $-0x1, -0xa8(%rbp)
je 0x2ef832
movq -0x8(%rbp), %rdi
movq -0xa8(%rbp), %rax
xorq $0x1, %rax
movl %eax, %esi
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x311ba0
cmpl $0x1, %eax
ja 0x2ef832
jmp 0x2ef985
movq -0x8(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
movq -0x70(%rbp), %rcx
movq -0x78(%rbp), %r8
xorl %eax, %eax
movl %eax, %r9d
movq $0x0, (%rsp)
callq 0x2ee450
cmpq $0x0, %rax
jne 0x2ef869
jmp 0x2ef985
movq -0x8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
movq -0xb8(%rbp), %rdx
movq -0x60(%rbp), %rcx
movq -0x68(%rbp), %r8
movl $0x1, %r9d
leaq -0x98(%rbp), %rax
movq %rax, (%rsp)
callq 0x2ee450
cmpq $0x0, %rax
jne 0x2ef8a4
jmp 0x2ef985
cmpq $0x0, -0x68(%rbp)
jbe 0x2ef8f9
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x68(%rbp), %rax
jae 0x2ef8e5
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
shrq %rax
movl %eax, %esi
callq 0x2ef9c0
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x2ef8b3
movq -0x68(%rbp), %rax
addq -0x90(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x2ef953
movq -0x8(%rbp), %rdi
movq -0xb0(%rbp), %rax
movl %eax, %esi
movq -0xb8(%rbp), %rax
movl %eax, %edx
movl $0x1, %ecx
callq 0x2efac0
movq -0x8(%rbp), %rdi
movq -0xb8(%rbp), %rax
xorq $0x1, %rax
movl %eax, %esi
movq -0xb0(%rbp), %rax
xorq $0x1, %rax
movl %eax, %edx
movl $0x1, %ecx
callq 0x2efac0
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x98(%rbp)
jbe 0x2ef983
movq -0x8(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
movq -0x70(%rbp), %rcx
movq -0x78(%rbp), %r8
movq -0x98(%rbp), %r9
callq 0x2eedf0
jmp 0x2ef985
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x2ef376
cmpq $0x0, -0x90(%rbp)
je 0x2ef9a9
movq -0x8(%rbp), %rdi
callq 0xcdd10
movq -0x90(%rbp), %rax
addq $0xd0, %rsp
popq %rbp
retq
nopl (%rax)