// File: chhylp123[P]hifiasm/inter.cpp
// Function: collect_pp_ovlps(ul_ov_t*, unsigned long, kv_ul_ov_t*, ug_opt_t const*, asg_t const*, unsigned long, long, unsigned long*, unsigned long*)
void collect_pp_ovlps(ul_ov_t *a, uint64_t a_n, kv_ul_ov_t *res, const ug_opt_t *uopt, const asg_t *rg, uint64_t rlen, int64_t ulid, uint64_t *mqs, uint64_t *mqe)
{
	uint64_t i, k; ma_hit_t_alloc* src; uc_block_t rch; ul_ov_t p, *st, *et;
    int64_t min_ovlp = uopt->min_ovlp, max_hang = uopt->max_hang, iqs, iqe; st = et = NULL;
    res->n = 0; (*mqs) = (*mqe) = (uint64_t)-1; memset(&rch, 0, sizeof(rch));
	if(a_n && a[0].el) st = &(a[0]);
	if(a_n > 1 && a[a_n-1].el) et = &(a[a_n-1]);
	// if(ulid == 7723 && a_n == 2 && a[0].tn == (7723>>1) && a[a_n-1].tn == (7829>>1)) {
	// 	fprintf(stderr, "[M::%s] ulid::%ld, st::%ld, st_q::[%ld, %ld), et::%ld, et_q::[%ld, %ld)\n", 
	// 	__func__, ulid, st?(int64_t)st->tn:-1, st?(int64_t)st->qs:-1, st?(int64_t)st->qe:-1, 
	// 	et?(int64_t)et->tn:-1, et?(int64_t)et->qs:-1, et?(int64_t)et->qe:-1);
	// }
	for (i = 0; i < a_n; i++) {
		src = &(uopt->sources[a[i].tn]);
		rch.hid = a[i].tn; rch.rev = a[i].rev;
		rch.qs = a[i].qs; rch.qe = a[i].qe;
		rch.ts = a[i].ts; rch.te = a[i].te;
		for (k = 0; k < src->length; k++) {
            if(rg->seq[Get_tn(src->buffer[k])].del) continue;///tn must exist
			if(st && st->tn == Get_tn(src->buffer[k])) continue;//not useful
			if(et && et->tn == Get_tn(src->buffer[k])) continue;//not useful
			if(!extract_nccov(&(src->buffer[k]), &rch, rg, &p, 0, min_ovlp, max_hang)) continue;
			
			extend_end_coord(NULL, &p, rlen, rg->seq[p.tn].len, &iqs, &iqe, NULL, NULL);
			if(st && iqs <= (int64_t)st->qe && iqe <= (int64_t)st->qe) continue;//not useful
			if(et && iqs >= (int64_t)et->qs && iqe >= (int64_t)et->qs) continue;//not useful
			
			if(p.rev) {
				iqs = rg->seq[p.tn].len - p.te;
				iqe = rg->seq[p.tn].len - p.ts;
				p.ts = iqs; p.te = iqe;
			}
			// if(ulid == 7723 && a_n == 2 && a[0].tn == (7723>>1) && a[a_n-1].tn == (7829>>1)) {
			// 	fprintf(stderr, "[M::%s] ulid::%ld, p.tn::%u, pq::[%u, %u), p.el::%u\n", 
			// 	__func__, ulid, p.tn, p.qs, p.qe, p.el);
			// }
			p.el = 0; p.tn <<= 1; p.tn |= p.rev; p.qn = i;//for linear chain
            kv_push(ul_ov_t, *res, p);
		}

		///it is unnecessary to push any non-end reads; 
		///since the read graph will remove any old read; each read/node is unique within the read graph
		if(a[i].el) {
			///push itself into the chain
			p = a[i]; 
			// if(ulid == 7723 && a_n == 2 && a[0].tn == (7723>>1) && a[a_n-1].tn == (7829>>1)) {
			// 	fprintf(stderr, "[M::%s] ulid::%ld, p.tn::%u, pq::[%u, %u), p.el::%u\n", 
			// 	__func__, ulid, p.tn, p.qs, p.qe, p.el);
			// }
			p.tn <<= 1; p.tn |= p.rev; p.qn = i;//for linear chain
			kv_push(ul_ov_t, *res, p);
		}
        if(((*mqs) == ((uint64_t)-1)) || ((*mqs) > a[i].qs)) (*mqs) = a[i].qs;
        if(((*mqe) == ((uint64_t)-1)) || ((*mqe) < a[i].qe)) (*mqe) = a[i].qe;
		
	}
	if(st) (*mqs) = st->qs; if(et) (*mqe) = et->qe;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
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 -0x20(%rbp), %rax
movslq 0x44(%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x20(%rbp), %rax
movslq 0x40(%rax), %rax
movq %rax, -0xa8(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0x90(%rbp)
movq -0x18(%rbp), %rax
movq $0x0, 0x8(%rax)
movq 0x20(%rbp), %rax
movq $-0x1, (%rax)
movq 0x18(%rbp), %rax
movq $-0x1, (%rax)
leaq -0x6c(%rbp), %rdi
xorl %esi, %esi
movl $0x24, %edx
callq 0x2100
cmpq $0x0, -0x10(%rbp)
je 0x2e9d39
movq -0x8(%rbp), %rax
movl 0x18(%rax), %eax
shrl $0x1e, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x2e9d39
movq -0x8(%rbp), %rax
movq %rax, -0x90(%rbp)
cmpq $0x1, -0x10(%rbp)
jbe 0x2e9d7b
movq -0x8(%rbp), %rax
movq -0x10(%rbp), %rcx
subq $0x1, %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
shrl $0x1e, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x2e9d7b
movq -0x8(%rbp), %rax
movq -0x10(%rbp), %rcx
subq $0x1, %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x2ea2c2
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
imulq $0x1c, -0x38(%rbp), %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x6c(%rbp)
movq -0x8(%rbp), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
shrl $0x1f, %eax
movb %al, %cl
movb -0x4c(%rbp), %al
andb $0x1, %cl
shlb $0x5, %cl
andb $-0x21, %al
orb %cl, %al
movb %al, -0x4c(%rbp)
movq -0x8(%rbp), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x68(%rbp)
movq -0x8(%rbp), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x64(%rbp)
movq -0x8(%rbp), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movl %eax, -0x60(%rbp)
movq -0x8(%rbp), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movl %eax, -0x5c(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x48(%rbp), %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jae 0x2ea11d
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x48(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x28, -0x40(%rbp), %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x2e9e77
jmp 0x2ea10c
cmpq $0x0, -0x90(%rbp)
je 0x2e9ea4
movq -0x90(%rbp), %rax
movl 0xc(%rax), %eax
movq -0x48(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x28, -0x40(%rbp), %rdx
addq %rdx, %rcx
cmpl 0xc(%rcx), %eax
jne 0x2e9ea4
jmp 0x2ea10c
cmpq $0x0, -0x98(%rbp)
je 0x2e9ed1
movq -0x98(%rbp), %rax
movl 0xc(%rax), %eax
movq -0x48(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x28, -0x40(%rbp), %rdx
addq %rdx, %rcx
cmpl 0xc(%rcx), %eax
jne 0x2e9ed1
jmp 0x2ea10c
movq -0x48(%rbp), %rax
movq (%rax), %rdi
imulq $0x28, -0x40(%rbp), %rax
addq %rax, %rdi
movq -0x28(%rbp), %rdx
movq -0xa0(%rbp), %r9
movq -0xa8(%rbp), %rax
leaq -0x6c(%rbp), %rsi
leaq -0x88(%rbp), %rcx
xorl %r8d, %r8d
movq %rax, (%rsp)
callq 0x2e55e0
cmpl $0x0, %eax
jne 0x2e9f13
jmp 0x2ea10c
movq -0x30(%rbp), %rdx
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x7c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %ecx
xorl %eax, %eax
movl %eax, %edi
leaq -0x88(%rbp), %rsi
leaq -0xb0(%rbp), %r8
leaq -0xb8(%rbp), %r9
movq $0x0, (%rsp)
movq $0x0, 0x8(%rsp)
callq 0x2d2ba0
cmpq $0x0, -0x90(%rbp)
je 0x2e9f98
movq -0xb0(%rbp), %rax
movq -0x90(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
jg 0x2e9f98
movq -0xb8(%rbp), %rax
movq -0x90(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
jg 0x2e9f98
jmp 0x2ea10c
cmpq $0x0, -0x98(%rbp)
je 0x2e9fd3
movq -0xb0(%rbp), %rax
movq -0x98(%rbp), %rcx
movl 0x4(%rcx), %ecx
cmpq %rcx, %rax
jl 0x2e9fd3
movq -0xb8(%rbp), %rax
movq -0x98(%rbp), %rcx
movl 0x4(%rcx), %ecx
cmpq %rcx, %rax
jl 0x2e9fd3
jmp 0x2ea10c
movl -0x70(%rbp), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x2ea030
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x7c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl -0x74(%rbp), %eax
movl %eax, %eax
movq %rax, -0xb0(%rbp)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x7c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl -0x78(%rbp), %eax
movl %eax, %eax
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rax
movl %eax, -0x78(%rbp)
movq -0xb8(%rbp), %rax
movl %eax, -0x74(%rbp)
movl -0x70(%rbp), %eax
andl $0xbfffffff, %eax       # imm = 0xBFFFFFFF
orl $0x0, %eax
movl %eax, -0x70(%rbp)
movl -0x7c(%rbp), %eax
shll %eax
movl %eax, -0x7c(%rbp)
movl -0x70(%rbp), %eax
shrl $0x1f, %eax
orl -0x7c(%rbp), %eax
movl %eax, -0x7c(%rbp)
movq -0x38(%rbp), %rax
movl %eax, -0x88(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x2ea0c9
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x2ea08d
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
shlq %rax
movq %rax, -0xc0(%rbp)
jmp 0x2ea09b
movl $0x2, %eax
movq %rax, -0xc0(%rbp)
jmp 0x2ea09b
movq -0xc0(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
imulq $0x1c, 0x10(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rdx
movq 0x8(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x8(%rdx)
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq -0x88(%rbp), %rcx
movq %rcx, (%rax)
movq -0x80(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x78(%rbp), %rcx
movq %rcx, 0x10(%rax)
movl -0x70(%rbp), %ecx
movl %ecx, 0x18(%rax)
jmp 0x2ea10c
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x2e9e39
movq -0x8(%rbp), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
shrl $0x1e, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x2ea235
movq -0x8(%rbp), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x88(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x80(%rbp)
movq 0x10(%rax), %rcx
movq %rcx, -0x78(%rbp)
movl 0x18(%rax), %eax
movl %eax, -0x70(%rbp)
movl -0x7c(%rbp), %eax
shll %eax
movl %eax, -0x7c(%rbp)
movl -0x70(%rbp), %eax
shrl $0x1f, %eax
orl -0x7c(%rbp), %eax
movl %eax, -0x7c(%rbp)
movq -0x38(%rbp), %rax
movl %eax, -0x88(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x2ea1f2
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x2ea1b6
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
shlq %rax
movq %rax, -0xc8(%rbp)
jmp 0x2ea1c4
movl $0x2, %eax
movq %rax, -0xc8(%rbp)
jmp 0x2ea1c4
movq -0xc8(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
imulq $0x1c, 0x10(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rdx
movq 0x8(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x8(%rdx)
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq -0x88(%rbp), %rcx
movq %rcx, (%rax)
movq -0x80(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x78(%rbp), %rcx
movq %rcx, 0x10(%rax)
movl -0x70(%rbp), %ecx
movl %ecx, 0x18(%rax)
jmp 0x2ea235
movq 0x18(%rbp), %rax
cmpq $-0x1, (%rax)
je 0x2ea25a
movq 0x18(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rbp), %rcx
imulq $0x1c, -0x38(%rbp), %rdx
addq %rdx, %rcx
movl 0x4(%rcx), %ecx
cmpq %rcx, %rax
jbe 0x2ea272
movq -0x8(%rbp), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, %ecx
movq 0x18(%rbp), %rax
movq %rcx, (%rax)
movq 0x20(%rbp), %rax
cmpq $-0x1, (%rax)
je 0x2ea297
movq 0x20(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rbp), %rcx
imulq $0x1c, -0x38(%rbp), %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
jae 0x2ea2af
movq -0x8(%rbp), %rax
imulq $0x1c, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, %ecx
movq 0x20(%rbp), %rax
movq %rcx, (%rax)
jmp 0x2ea2b1
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x2e9d83
cmpq $0x0, -0x90(%rbp)
je 0x2ea2df
movq -0x90(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, %ecx
movq 0x18(%rbp), %rax
movq %rcx, (%rax)
cmpq $0x0, -0x98(%rbp)
je 0x2ea2fc
movq -0x98(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %ecx
movq 0x20(%rbp), %rax
movq %rcx, (%rax)
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)