// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %r9, 0x40(%rsp)
movq %r8, 0x48(%rsp)
movq %rcx, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdi, %r12
xorl %r13d, %r13d
pushq $-0x1
popq %rbp
xorl %r14d, %r14d
movq %rdi, 0x8(%rsp)
cmpq 0x48(%rsp), %r13
je 0x192ffd
movq 0x40(%rsp), %rax
movq 0x88(%rax), %rcx
movq 0x38(%rsp), %rax
movl (%rax,%r13,8), %eax
movq 0x30(%rsp), %rdx
movq (%rdx,%rax,8), %rdx
movl %edx, %eax
shrq $0x20, %rdx
imulq $0x68, %rdx, %rdx
movq 0x58(%rcx,%rdx), %rsi
movq (%rsi,%rax,8), %rdi
movabsq $-0x100000001, %r8      # imm = 0xFFFFFFFEFFFFFFFF
cmpq %r8, %rdi
ja 0x19303b
movl %edi, %edi
cmpl $-0x1, %edi
jne 0x19303b
movq 0x48(%rcx,%rdx), %rdi
leaq (%rsi,%rax,8), %rcx
addq $0x8, %rcx
xorl %r8d, %r8d
xorl %edx, %edx
leaq (%rax,%rdx), %r9
incq %r9
cmpq %rdi, %r9
jae 0x19301c
cmpq $-0x1, (%rcx,%rdx,8)
je 0x192d2a
incq %rdx
addq $-0x8, %r8
jmp 0x192d0a
leaq (%rax,%rdx), %r11
addq $0x2, %r11
movq %r11, %r9
movq %r14, 0x10(%rsp)
cmpq %rdi, %r9
jae 0x192d5c
movq (%rsi,%r9,8), %r10
cmpq $-0x1, %r10
setne %bl
cmpl $-0x1, %r10d
sete %r10b
testb %r10b, %bl
jne 0x192d61
incq %r9
jmp 0x192d3a
movq %rdi, %r10
jmp 0x192d64
movq %r9, %r10
cmpq %r9, %rdi
movq %r9, %rbx
cmovbq %rdi, %rbx
movq %rbp, %r14
movq %rbp, %r15
testq %rdx, %rdx
je 0x192dbd
movq (%rcx), %r14
movl %r14d, %r15d
shrq $0x20, %r14
cmpq $0x1, %r14
movl $0x0, %r14d
sbbq %r14, %r14
orq %r15, %r14
cmpq $0x2, %rdx
jb 0x192dba
leaq (%rsi,%rax,8), %r15
movq (%r15,%rdx,8), %r15
movl %r15d, %r12d
shrq $0x20, %r15
cmpq $0x1, %r15
movl $0x0, %r15d
sbbq %r15, %r15
orq %r12, %r15
jmp 0x192dbd
movq %rbp, %r15
movq %r15, 0x18(%rsp)
movq %rbp, %r15
movq %rbp, %r12
cmpq %r10, %r11
je 0x192e1a
subq %rax, %rbx
subq %rdx, %rbx
addq $-0x2, %rbx
movq 0x8(%rcx,%rdx,8), %r11
movl %r11d, %r12d
shrq $0x20, %r11
cmpq $0x1, %r11
movl $0x0, %r15d
sbbq %r15, %r15
orq %r12, %r15
cmpq $0x2, %rbx
jb 0x192e17
movq -0x8(%rsi,%r10,8), %r10
movl %r10d, %r11d
shrq $0x20, %r10
cmpq $0x1, %r10
movl $0x0, %r12d
sbbq %r12, %r12
orq %r11, %r12
jmp 0x192e1a
movq %rbp, %r12
cmpq %r15, %r14
jne 0x193063
movq %r13, 0x50(%rsp)
movq 0x18(%rsp), %r15
cmpq %r12, %r15
jne 0x19305a
xorl %r10d, %r10d
cmpq $-0x1, %r14
setne %r10b
leal (,%r10,8), %ebx
addq %rbx, %rcx
xorl %r11d, %r11d
cmpq $-0x1, %r15
setne %r11b
addq %r11, %r10
movq %r10, %r11
negq %r11
leaq (%rsi,%rax,8), %r13
addq %rbx, %r13
subq %r8, %r13
addq $0x10, %r13
cmpq %r9, %rdi
cmovbq %rdi, %r9
movq %r9, %r15
subq %r10, %r15
subq %rax, %r15
subq %rdx, %r15
addq $-0x2, %r15
addq %rdx, %r11
movq 0x8(%rsp), %r12
jne 0x192ea8
subq %r9, %rax
addq %r10, %rax
addq %rdx, %rax
addq $0x2, %rax
testq %rax, %rax
je 0x192ea8
movq %rcx, %rbx
xorl %ebp, %ebp
jmp 0x192eb4
movq %r13, %rbx
movq %r15, %rbp
movq %rcx, %r13
movq %r11, %r15
andq $0x0, 0x20(%rsp)
cmpq $-0x1, %r14
je 0x192ede
movq %r12, %rdi
movl %r14d, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1a5422
cmpl $0x2, %eax
jae 0x192ede
movq 0x10(%rsp), %rbp
jmp 0x192fea
movq 0x18(%rsp), %r12
cmpq $-0x1, %r12
je 0x192f02
movl %r12d, %esi
xorl $0x1, %esi
movq 0x8(%rsp), %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1a5422
cmpl $0x2, %eax
jb 0x192f7b
andq $0x0, (%rsp)
movq %r14, %rsi
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rsi, 0x28(%rsp)
movq %r12, %rdx
movq %r13, %rcx
movq %r15, %r8
xorl %r9d, %r9d
callq 0x1924b4
testq %rax, %rax
je 0x192f87
leaq 0x20(%rsp), %rax
movq %rax, (%rsp)
movq %r14, %rdi
movq 0x28(%rsp), %r14
movq %r14, %rsi
movq %r12, %rdx
movq %rbx, %rcx
movq %rbp, %r8
pushq $0x1
popq %r9
callq 0x1924b4
testq %rax, %rax
je 0x192f7b
testq %rbp, %rbp
je 0x192f98
xorl %r12d, %r12d
cmpq %r12, %rbp
je 0x192f91
movq (%rbx,%r12,8), %rsi
shrq %rsi
movq 0x8(%rsp), %rdi
callq 0x193082
incq %r12
jmp 0x192f60
movq 0x10(%rsp), %rbp
movq 0x8(%rsp), %r12
jmp 0x192fea
movq 0x10(%rsp), %rbp
movq %r14, %r12
jmp 0x192fea
movq 0x8(%rsp), %r12
jmp 0x192fc5
movq 0x8(%rsp), %r12
movq %r12, %rdi
movl %r14d, %esi
movq 0x18(%rsp), %rbx
movl %ebx, %edx
callq 0x193111
movl %ebx, %esi
xorl $0x1, %esi
movl %r14d, %edx
xorl $0x1, %edx
movq %r12, %rdi
callq 0x193111
pushq $0x1
popq %rbp
addq 0x10(%rsp), %rbp
movq 0x20(%rsp), %r9
testq %r9, %r9
je 0x192fea
movq %r12, %rdi
movq %r14, %rsi
movq 0x18(%rsp), %rdx
movq %r13, %rcx
movq %r15, %r8
callq 0x1929c9
movq 0x50(%rsp), %r13
incq %r13
movq %rbp, %r14
pushq $-0x1
popq %rbp
jmp 0x192c9e
testq %r14, %r14
je 0x19300a
movq %r12, %rdi
callq 0x694eb
movq %r14, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x62c8c(%rip), %rdi     # 0x1f5caf
leaq 0x5eaf9(%rip), %rsi     # 0x1f1b23
leaq 0x62c21(%rip), %rcx     # 0x1f5c52
movl $0x4b74, %edx           # imm = 0x4B74
callq 0x2110
leaq 0x62bcb(%rip), %rdi     # 0x1f5c0d
leaq 0x5eada(%rip), %rsi     # 0x1f1b23
leaq 0x62c02(%rip), %rcx     # 0x1f5c52
movl $0x4b6b, %edx           # imm = 0x4B6B
callq 0x2110
leaq 0x62c64(%rip), %rdi     # 0x1f5cc5
jmp 0x19306a
leaq 0x62c4c(%rip), %rdi     # 0x1f5cb6
leaq 0x5eab2(%rip), %rsi     # 0x1f1b23
leaq 0x62bda(%rip), %rcx     # 0x1f5c52
movl $0x4b92, %edx           # imm = 0x4B92
callq 0x2110