// File: /workspace/github_cpp_makefile_star2_O3/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 %r8, 0x50(%rsp)
movq %rcx, 0x80(%rsp)
movq 0x100(%rsp), %rbx
movq 0xf8(%rsp), %r14
movq $0x0, 0x8(%rdx)
movq $-0x1, %rax
movq %rax, (%rbx)
movq %rax, (%r14)
pxor %xmm0, %xmm0
movdqu %xmm0, 0xa4(%rsp)
testq %rsi, %rsi
je 0x1ddc95
movq %rdx, %r12
movq %rsi, %r13
xorl %r11d, %r11d
testb $0x40, 0x1b(%rdi)
movq %rdi, %rax
cmoveq %r11, %rax
movq %rax, 0x40(%rsp)
cmpq $0x1, %rsi
je 0x1dd906
leaq (,%r13,8), %rax
addq %r13, %rax
leaq (%rax,%rax,2), %rax
addq %r13, %rax
xorl %r11d, %r11d
addq %rdi, %rax
addq $-0x1c, %rax
testb $0x40, 0x1b(%rax)
cmovneq %rax, %r11
xorl %r8d, %r8d
movq %rdi, 0x58(%rsp)
movq %r11, 0x38(%rsp)
movq %r12, 0x60(%rsp)
movq %r13, 0x78(%rsp)
movq 0x80(%rsp), %rax
movq 0x8(%rax), %rax
leaq (%r8,%r8,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %r8, %rsi
leaq (%rdi,%rsi), %rcx
movq %rcx, 0x68(%rsp)
movl 0x18(%rdi,%rsi), %ecx
movl %ecx, %edx
shrl $0x1a, %edx
andb $0x20, %dl
movb %dl, 0xb0(%rsp)
movdqu 0x4(%rdi,%rsi), %xmm0
movl 0xc(%rdi,%rsi), %edx
leaq (%rdx,%rdx,2), %rdx
pshufd $0xd2, %xmm0, %xmm0     # xmm0 = xmm0[2,0,1,3]
movdqa %xmm0, 0x90(%rsp)
movl 0x14(%rdi,%rsi), %esi
movl %esi, 0xa0(%rsp)
movl 0xc(%rax,%rdx,8), %r15d
testl %r15d, %r15d
movq %r8, 0x48(%rsp)
je 0x1ddb99
leaq (%rax,%rdx,8), %rax
movq %rax, 0x70(%rsp)
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x50(%rsp), %rbx
movq 0x40(%rsp), %rcx
movq 0x20(%rbx), %r14
movq 0x70(%rsp), %rax
movq (%rax), %rdi
movl 0xc(%rdi,%r12), %eax
cmpl $0x0, (%r14,%rax,8)
js 0x1ddb5a
testq %rcx, %rcx
je 0x1dd9cd
cmpl %eax, 0xc(%rcx)
je 0x1ddb5a
testq %r11, %r11
je 0x1dd9dc
cmpl %eax, 0xc(%r11)
je 0x1ddb5a
addq %r12, %rdi
leaq 0x90(%rsp), %rsi
movq %rbx, %rdx
leaq 0x10(%rsp), %rcx
xorl %r8d, %r8d
callq 0x1dac2c
testl %eax, %eax
je 0x1ddb50
movl 0x1c(%rsp), %eax
movl (%r14,%rax,8), %ebp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl 0x14(%rsp), %r9d
movl 0x18(%rsp), %r8d
movl 0x20(%rsp), %esi
movl 0x24(%rsp), %edi
movl 0x28(%rsp), %edx
movq %rbp, %r10
subq %rdi, %r10
movq %rbp, %r11
subq %rsi, %r11
testl %edx, %edx
cmovnsq %rsi, %r10
cmovnsq %rdi, %r11
movq 0x88(%rsp), %rbx
movq %rbx, %r14
subq %r8, %r14
movq %rbp, %rcx
subq %r11, %rcx
addq %rcx, %r8
cmpq %rcx, %r14
cmovleq %rbx, %r8
subq %r10, %r9
movl $0x0, %ecx
cmovleq %rcx, %r9
movq 0x40(%rsp), %rcx
testq %rcx, %rcx
je 0x1dda8a
movl 0x8(%rcx), %r10d
cmpq %r10, %r9
jg 0x1dda8a
cmpq %r10, %r8
jg 0x1dda8a
movq 0x50(%rsp), %rbx
movq 0x38(%rsp), %r11
jmp 0x1ddb5a
movq 0x38(%rsp), %r11
testq %r11, %r11
movq 0x50(%rsp), %rbx
je 0x1ddaab
movl 0x4(%r11), %r10d
cmpq %r10, %r9
jl 0x1ddaab
cmpq %r10, %r8
jge 0x1ddb5a
testl %edx, %edx
jns 0x1ddabd
movl %ebp, %ecx
subl %edi, %ecx
subl %esi, %ebp
movl %ecx, 0x20(%rsp)
movl %ebp, 0x24(%rsp)
shldl $0x1, %edx, %eax
andl $0xbfffffff, %edx       # imm = 0xBFFFFFFF
movl %edx, 0x28(%rsp)
movl %eax, 0x1c(%rsp)
movq 0x48(%rsp), %rax
movl %eax, 0x10(%rsp)
movq 0x60(%rsp), %r14
movq (%r14), %rax
movq 0x8(%r14), %rcx
cmpq 0x10(%r14), %rcx
jne 0x1ddb1d
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%r14)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x38(%rsp), %r11
movq %rax, (%r14)
movq 0x8(%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r14)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movdqu 0x10(%rsp), %xmm0
movups 0x1c(%rsp), %xmm1
movups %xmm1, 0xc(%rax,%rdx)
movdqu %xmm0, (%rax,%rdx)
movq 0x70(%rsp), %rax
movl 0xc(%rax), %r15d
jmp 0x1ddb55
movq 0x38(%rsp), %r11
movq 0x40(%rsp), %rcx
incq %r13
movl %r15d, %eax
addq $0x28, %r12
cmpq %rax, %r13
jb 0x1dd9a3
movq 0x68(%rsp), %rax
movl 0x18(%rax), %ecx
movq 0x100(%rsp), %rbx
movq 0xf8(%rsp), %r14
movq 0x60(%rsp), %r12
movq 0x78(%rsp), %r13
movq 0x58(%rsp), %rdi
movq 0x48(%rsp), %r8
btl $0x1e, %ecx
movq 0x68(%rsp), %r15
jae 0x1ddc43
movups (%r15), %xmm0
movups 0xc(%r15), %xmm1
movups %xmm1, 0x1c(%rsp)
movaps %xmm0, 0x10(%rsp)
movl 0x28(%rsp), %eax
shldl $0x1, %eax, 0x1c(%rsp)
movl %r8d, 0x10(%rsp)
movq (%r12), %rax
movq 0x8(%r12), %rcx
cmpq 0x10(%r12), %rcx
jne 0x1ddc1a
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%r12)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x48(%rsp), %r8
movq 0x38(%rsp), %r11
movq 0x58(%rsp), %rdi
movq %rax, (%r12)
movq 0x8(%r12), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r12)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movups 0x1c(%rsp), %xmm0
movups %xmm0, 0xc(%rax,%rdx)
movdqa 0x10(%rsp), %xmm0
movdqu %xmm0, (%rax,%rdx)
movq (%r14), %rcx
movl 0x4(%r15), %eax
cmpq $-0x1, %rcx
je 0x1ddc55
cmpq %rax, %rcx
jbe 0x1ddc58
movq %rax, (%r14)
movq (%rbx), %rcx
movl 0x8(%r15), %eax
cmpq $-0x1, %rcx
je 0x1ddc6a
cmpq %rax, %rcx
jae 0x1ddc6d
movq %rax, (%rbx)
incq %r8
cmpq %r13, %r8
jne 0x1dd91d
movq 0x40(%rsp), %rax
testq %rax, %rax
je 0x1ddc89
movl 0x4(%rax), %eax
movq %rax, (%r14)
testq %r11, %r11
je 0x1ddc95
movl 0x8(%r11), %eax
movq %rax, (%rbx)
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq