// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r9, 0x88(%rsp)
movq %rcx, 0x78(%rsp)
movq %rdx, %rbx
andq $0x0, 0x8(%rdx)
movq 0x100(%rsp), %rax
orq $-0x1, (%rax)
movq 0xf8(%rsp), %rax
orq $-0x1, (%rax)
movq %rdi, %r14
pxor %xmm0, %xmm0
movdqu %xmm0, 0xa4(%rsp)
testq %rsi, %rsi
je 0x18fdac
xorl %r9d, %r9d
testb $0x40, 0x1b(%r14)
movq %r14, %r15
cmoveq %r9, %r15
cmpq $0x1, %rsi
je 0x18fdb2
imulq $0x1c, %rsi, %rax
xorl %r9d, %r9d
addq %r14, %rax
addq $-0x1c, %rax
testb $0x40, 0x1b(%rax)
cmovneq %rax, %r9
jmp 0x18fdb2
xorl %r15d, %r15d
xorl %r9d, %r9d
xorl %r12d, %r12d
movq %r8, 0x40(%rsp)
movq %r9, 0x38(%rsp)
movq %rbx, 0x58(%rsp)
movq %rsi, 0x70(%rsp)
movq %r14, 0x68(%rsp)
cmpq %rsi, %r12
je 0x1900d7
movq 0x78(%rsp), %rax
movq 0x8(%rax), %rax
movq %r12, 0x60(%rsp)
imulq $0x1c, %r12, %rcx
leaq (%r14,%rcx), %rdx
movq %rdx, 0x80(%rsp)
movl 0x18(%r14,%rcx), %edx
shrl $0x1a, %edx
andb $0x20, %dl
movb %dl, 0xb0(%rsp)
movdqu 0x4(%r14,%rcx), %xmm0
movl 0xc(%r14,%rcx), %edx
imulq $0x18, %rdx, %rbx
addq %rax, %rbx
pshufd $0xd2, %xmm0, %xmm0     # xmm0 = xmm0[2,0,1,3]
movdqa %xmm0, 0x90(%rsp)
movl 0x14(%r14,%rcx), %eax
movl %eax, 0xa0(%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
movl 0xc(%rbx), %eax
cmpq %rax, %r12
jae 0x18ffde
movq 0x20(%r8), %rbp
movq (%rbx), %rdi
movl 0xc(%rdi,%r13), %eax
cmpl $0x0, (%rbp,%rax,8)
js 0x18ffd2
testq %r15, %r15
je 0x18fe6c
cmpl %eax, 0xc(%r15)
je 0x18ffd2
testq %r9, %r9
je 0x18fe7b
cmpl %eax, 0xc(%r9)
je 0x18ffd2
addq %r13, %rdi
leaq 0x90(%rsp), %rsi
movq %r8, %rdx
leaq 0x10(%rsp), %rcx
xorl %r8d, %r8d
callq 0x18d7f8
movq 0x38(%rsp), %r9
movq 0x40(%rsp), %r8
testl %eax, %eax
je 0x18ffd2
movl 0x1c(%rsp), %r14d
movl (%rbp,%r14,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
pxor %xmm0, %xmm0
movdqu %xmm0, (%rsp)
xorl %edi, %edi
leaq 0x10(%rsp), %rsi
movq 0x88(%rsp), %rdx
leaq 0x50(%rsp), %r8
leaq 0x48(%rsp), %r9
callq 0x184214
movq 0x38(%rsp), %r9
movq 0x40(%rsp), %r8
testq %r15, %r15
je 0x18ff04
movl 0x8(%r15), %eax
cmpq %rax, 0x50(%rsp)
jg 0x18ff04
cmpq %rax, 0x48(%rsp)
jle 0x18ffd2
testq %r9, %r9
je 0x18ff1f
movl 0x4(%r9), %eax
cmpq %rax, 0x50(%rsp)
jl 0x18ff1f
cmpq %rax, 0x48(%rsp)
jge 0x18ffd2
movl 0x28(%rsp), %eax
testl %eax, %eax
jns 0x18ff54
movl (%rbp,%r14,8), %ecx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %ecx
subl 0x24(%rsp), %ecx
movq %rcx, 0x50(%rsp)
movl (%rbp,%r14,8), %edx
andl %esi, %edx
subl 0x20(%rsp), %edx
movq %rdx, 0x48(%rsp)
movl %ecx, 0x20(%rsp)
movl %edx, 0x24(%rsp)
shldl $0x1, %eax, %r14d
andl $0xbfffffff, %eax       # imm = 0xBFFFFFFF
movl %eax, 0x28(%rsp)
movl %r14d, 0x1c(%rsp)
movq 0x60(%rsp), %rax
movl %eax, 0x10(%rsp)
movq 0x58(%rsp), %r14
movq (%r14), %rax
movq 0x8(%r14), %rcx
cmpq 0x10(%r14), %rcx
jne 0x18ffb1
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%r14)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x38(%rsp), %r9
movq 0x40(%rsp), %r8
movq %rax, (%r14)
movq 0x8(%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r14)
imulq $0x1c, %rcx, %rcx
movups 0x1c(%rsp), %xmm0
movups %xmm0, 0xc(%rax,%rcx)
movdqa 0x10(%rsp), %xmm0
movdqu %xmm0, (%rax,%rcx)
incq %r12
addq $0x28, %r13
jmp 0x18fe3a
movq 0x80(%rsp), %r13
testb $0x40, 0x1b(%r13)
movq 0x58(%rsp), %rbx
movq 0x68(%rsp), %r14
movq 0x60(%rsp), %r12
je 0x190080
movups (%r13), %xmm0
movups 0xc(%r13), %xmm1
movups %xmm1, 0x1c(%rsp)
movaps %xmm0, 0x10(%rsp)
movl 0x28(%rsp), %eax
shldl $0x1, %eax, 0x1c(%rsp)
movl %r12d, 0x10(%rsp)
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
cmpq 0x10(%rbx), %rcx
jne 0x19005f
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbx)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x38(%rsp), %r9
movq 0x40(%rsp), %r8
movq %rax, (%rbx)
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
imulq $0x1c, %rcx, %rcx
movups 0x1c(%rsp), %xmm0
movups %xmm0, 0xc(%rax,%rcx)
movdqa 0x10(%rsp), %xmm0
movdqu %xmm0, (%rax,%rcx)
movq 0xf8(%rsp), %rax
movq (%rax), %rcx
movl 0x4(%r13), %eax
cmpq $-0x1, %rcx
je 0x19009a
cmpq %rax, %rcx
jbe 0x1900a5
movq 0xf8(%rsp), %rcx
movq %rax, (%rcx)
movq 0x100(%rsp), %rax
movq (%rax), %rcx
movl 0x8(%r13), %eax
cmpq $-0x1, %rcx
je 0x1900bf
cmpq %rax, %rcx
jae 0x1900ca
movq 0x100(%rsp), %rcx
movq %rax, (%rcx)
incq %r12
movq 0x70(%rsp), %rsi
jmp 0x18fdce
testq %r15, %r15
je 0x1900eb
movl 0x4(%r15), %eax
movq 0xf8(%rsp), %rcx
movq %rax, (%rcx)
testq %r9, %r9
je 0x1900ff
movl 0x8(%r9), %eax
movq 0x100(%rsp), %rcx
movq %rax, (%rcx)
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq