// File: chhylp123[P]hifiasm/inter.cpp
// Function: gen_commen_win(All_reads*, ul_idx_t const*, overlap_region_alloc*, unsigned long*, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long*, unsigned long, char*, char*, kv_ul_ov_t*)
uint64_t gen_commen_win(All_reads *rref, const ul_idx_t *uref, overlap_region_alloc* ol, uint64_t *id_a, uint64_t id_n, uint64_t s, uint64_t e, uint64_t ql, uint64_t wl,
uint64_t *buf, uint64_t dp, char *str0, char *str1, kv_ul_ov_t *aln)///[s, e)
{
	if(!id_n) return id_n;
	uint64_t i, m, k, rm_n = 0, buf_n = 0, qs, qe, wid; char *qstring, *tstring;
	overlap_region *z; uint64_t ws, we; int64_t r_y[2], r_err, p_y[2], p_err;
	///shrink [qs, qe)
	qs = (s/wl)*wl; if(qs < s) qs += wl; if(qs >= ql) return id_n;
	qe = (e/wl)*wl; if(qe >= ql) qe = ql;
	if(qs >= qe) return id_n;
	//idx_a[] is sorted by aln[].qs
	for (k = 0; k < id_n; k++) {
		if(aln->a[id_a[k]].qs<=qs && aln->a[id_a[k]].qe>=qe) {
			buf[buf_n++] = id_a[k];
		}
		if(aln->a[id_a[k]].qe < e) rm_n++;
	}
	assert(buf_n == dp && buf_n > 1);

	if(buf_n > 0) {
		///fs = fe = (uint64_t)-1;
		for (k = qs; k < qe; k += wl) {
			ws = k; we = ws + wl; if(we > qe) we = qe;//[ws, we)
			// fprintf(stderr, ">>>[M::%s::] w[%lu, %lu), buf_n::%lu\n", __func__, ws, we, buf_n);
			///first overlap
			z = &(ol->list[aln->a[buf[0]].qn]);
			wid = get_win_id_by_s(z, ws, wl, NULL);
			if(!get_win_info(z, wid, &(r_y[0]), &(r_y[1]), &r_err)) continue;
			// fprintf(stderr, "###[M::%s::] y[%ld, %ld), off::%ld, y_err::%ld\n", __func__, r_y[0], r_y[1], r_y[1]-r_y[0], r_err);
			qstring = tstring = NULL;
			for (i = 1; i < buf_n; i++) {
				z = &(ol->list[aln->a[buf[i]].qn]);
				wid = get_win_id_by_s(z, ws, wl, NULL);
				if(!get_win_info(z, wid, &(p_y[0]), &(p_y[1]), &p_err)) break;
				// fprintf(stderr, "###[M::%s::] y[%ld, %ld), off::%ld, y_err::%ld\n", __func__, p_y[0], p_y[1], p_y[1]-p_y[0], p_err);
				if(((r_y[1]-r_y[0]) != (p_y[1]-p_y[0])) || (r_err != p_err)) break;
				if(r_err == 0) continue;
				if(r_err != -1) {///if this window ar all overlaps is unmapped
					if(!qstring) {
						qstring = retrive_str_piece(rref, uref, str0, r_y[0], r_y[1]+1-r_y[0], 
													ol->list[aln->a[buf[0]].qn].y_pos_strand, ol->list[aln->a[buf[0]].qn].y_id);
					}
					tstring = retrive_str_piece(rref, uref, str1, p_y[0], p_y[1]+1-p_y[0], z->y_pos_strand, z->y_id);
					if(memcmp(str0, str1, (we-ws))) break;
				}
			}
			if(i < buf_n) continue;
			for (i = 0; i < buf_n; i++) {
				z = &(ol->list[aln->a[buf[i]].qn]);
				wid = get_win_id_by_s(z, ws, wl, NULL);
				z->w_list.a[wid].extra_end = -1;
			}
			// if(fs == (uint64_t)-1) {
			// 	fs = ws; fe = we;
			// } else if(ws >= fs && ws <= fe) {
			// 	if(fe < we) fe = we;
			// } else {
			// 	kv_pushp(ul_ov_t, *aln, &p); p->el = 0; p->qs = fs; p->qe = fe;
			// 	fs = ws; fe = we;
			// }
		}
		// if(fs != (uint64_t)-1) {
		// 	kv_pushp(ul_ov_t, *aln, &p); p->el = 0; p->qs = fs; p->qe = fe;
		// }
	}
	if(rm_n) {
		for (i = m = 0; i < id_n; i++) {
			if(aln->a[id_a[i]].qe < e) continue;
			id_a[m++] = id_a[i];
		}
		id_n = m;
	}
	return id_n;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x2af9d7
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2aff8d
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x38(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0x20(%rbp)
imulq 0x20(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0x2afa12
movq 0x20(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq 0x18(%rbp), %rax
jb 0x2afa29
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2aff8d
movq 0x10(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0x20(%rbp)
imulq 0x20(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq 0x18(%rbp), %rax
jb 0x2afa50
movq 0x18(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
jb 0x2afa67
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2aff8d
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x2afb1f
movq 0x48(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movq -0x50(%rbp), %rdx
imulq $0x1c, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
cmpq -0x68(%rbp), %rax
ja 0x2afae0
movq 0x48(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movq -0x50(%rbp), %rdx
imulq $0x1c, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpq -0x70(%rbp), %rax
jb 0x2afae0
movq -0x28(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq 0x28(%rbp), %rax
movq -0x60(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x60(%rbp)
movq %rdx, (%rax,%rcx,8)
movq 0x48(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movq -0x50(%rbp), %rdx
imulq $0x1c, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpq 0x10(%rbp), %rax
jae 0x2afb0c
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x2afb0e
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x2afa6f
movq -0x60(%rbp), %rcx
xorl %eax, %eax
cmpq 0x30(%rbp), %rcx
movb %al, -0xd9(%rbp)
jne 0x2afb3f
cmpq $0x1, -0x60(%rbp)
seta %al
movb %al, -0xd9(%rbp)
movb -0xd9(%rbp), %al
testb $0x1, %al
jne 0x2afb4b
jmp 0x2afb4d
jmp 0x2afb6c
leaq 0xd5d17(%rip), %rdi     # 0x38586b
leaq 0xe0cd4(%rip), %rsi     # 0x39082f
movl $0x1caf, %edx           # imm = 0x1CAF
leaq 0xe28a4(%rip), %rcx     # 0x39240b
callq 0x2130
cmpq $0x0, -0x60(%rbp)
jbe 0x2aff09
movq -0x68(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x70(%rbp), %rax
jae 0x2aff07
movq -0x50(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
addq 0x20(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
cmpq -0x70(%rbp), %rax
jbe 0x2afbc2
movq -0x70(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x48(%rbp), %rcx
movq (%rcx), %rcx
movq 0x28(%rbp), %rdx
imulq $0x1c, (%rdx), %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq 0x20(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x9e2e0
movq %rax, -0x78(%rbp)
movq -0x90(%rbp), %rdi
movq -0x78(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
leaq -0xb0(%rbp), %rcx
addq $0x8, %rcx
leaq -0xb8(%rbp), %r8
callq 0x2af770
cmpq $0x0, %rax
jne 0x2afc41
jmp 0x2afef6
movq $0x0, -0x88(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x1, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x60(%rbp), %rax
jae 0x2afe56
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x48(%rbp), %rcx
movq (%rcx), %rcx
movq 0x28(%rbp), %rdx
movq -0x40(%rbp), %rsi
imulq $0x1c, (%rdx,%rsi,8), %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq 0x20(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x9e2e0
movq %rax, -0x78(%rbp)
movq -0x90(%rbp), %rdi
movq -0x78(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
leaq -0xd0(%rbp), %rcx
addq $0x8, %rcx
leaq -0xd8(%rbp), %r8
callq 0x2af770
cmpq $0x0, %rax
jne 0x2afcee
jmp 0x2afe56
movq -0xa8(%rbp), %rax
subq -0xb0(%rbp), %rax
movq -0xc8(%rbp), %rcx
subq -0xd0(%rbp), %rcx
cmpq %rcx, %rax
jne 0x2afd1f
movq -0xb8(%rbp), %rax
cmpq -0xd8(%rbp), %rax
je 0x2afd24
jmp 0x2afe56
cmpq $0x0, -0xb8(%rbp)
jne 0x2afd33
jmp 0x2afe45
cmpq $-0x1, -0xb8(%rbp)
je 0x2afe43
cmpq $0x0, -0x80(%rbp)
jne 0x2afdd3
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq 0x38(%rbp), %rdx
movq -0xb0(%rbp), %rcx
movq -0xa8(%rbp), %r8
addq $0x1, %r8
subq -0xb0(%rbp), %r8
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x48(%rbp), %r9
movq (%r9), %r9
movq 0x28(%rbp), %r10
imulq $0x1c, (%r10), %r10
addq %r10, %r9
movl (%r9), %r9d
imulq $0xa8, %r9, %r9
addq %r9, %rax
movl 0x1c(%rax), %eax
movl %eax, %r9d
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x48(%rbp), %r10
movq (%r10), %r10
movq 0x28(%rbp), %r11
imulq $0x1c, (%r11), %r11
addq %r11, %r10
movl (%r10), %r10d
imulq $0xa8, %r10, %r10
addq %r10, %rax
movl 0x10(%rax), %eax
movq %rax, (%rsp)
callq 0x2af8d0
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq 0x40(%rbp), %rdx
movq -0xd0(%rbp), %rcx
movq -0xc8(%rbp), %r8
addq $0x1, %r8
subq -0xd0(%rbp), %r8
movq -0x90(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, %r9d
movq -0x90(%rbp), %rax
movl 0x10(%rax), %eax
movq %rax, (%rsp)
callq 0x2af8d0
movq %rax, -0x88(%rbp)
movq 0x38(%rbp), %rdi
movq 0x40(%rbp), %rsi
movq -0xa0(%rbp), %rdx
subq -0x98(%rbp), %rdx
callq 0x20b0
cmpl $0x0, %eax
je 0x2afe41
jmp 0x2afe56
jmp 0x2afe43
jmp 0x2afe45
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x2afc5c
movq -0x40(%rbp), %rax
cmpq -0x60(%rbp), %rax
jae 0x2afe65
jmp 0x2afef6
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x60(%rbp), %rax
jae 0x2afef4
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x48(%rbp), %rcx
movq (%rcx), %rcx
movq 0x28(%rbp), %rdx
movq -0x40(%rbp), %rsi
imulq $0x1c, (%rdx,%rsi,8), %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq 0x20(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x9e2e0
movq %rax, -0x78(%rbp)
movq -0x90(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x78(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movw $0xffff, 0x12(%rax)     # imm = 0xFFFF
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x2afe6d
jmp 0x2afef6
movq 0x20(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0x2afb7f
jmp 0x2aff09
cmpq $0x0, -0x58(%rbp)
je 0x2aff85
movq $0x0, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0x2aff7d
movq 0x48(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movq -0x40(%rbp), %rdx
imulq $0x1c, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpq 0x10(%rbp), %rax
jae 0x2aff4c
jmp 0x2aff6f
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x28(%rbp), %rax
movq -0x48(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x48(%rbp)
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x2aff20
movq -0x48(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xf0, %rsp
popq %rbp
retq
nopw (%rax,%rax)