// File: /workspace/github_cpp_makefile_star2_O1/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, 0xa8(%rsp)
movq %r8, %r15
movq %rcx, 0x98(%rsp)
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r12
movq 0x100(%rsp), %rcx
movq 0xf8(%rsp), %rdx
movq $0x0, 0x8(%rbx)
movq $-0x1, %rax
movq %rax, (%rcx)
movq %rax, (%rdx)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movl $0x0, 0x80(%rsp)
xorl %r11d, %r11d
testq %rsi, %rsi
je 0x1d385c
testb $0x40, 0x1b(%r12)
cmovneq %r12, %r11
cmpq $0x2, %r14
jb 0x1d3885
leaq (%r14,%r14,8), %rax
leaq (%rax,%rax,2), %rax
addq %r14, %rax
xorl %ecx, %ecx
addq %r12, %rax
addq $-0x1c, %rax
testb $0x40, 0x1b(%rax)
cmovneq %rax, %rcx
movq %rcx, 0x18(%rsp)
jmp 0x1d388e
movq $0x0, 0x18(%rsp)
testq %r14, %r14
je 0x1d3c20
leaq 0x60(%rsp), %rax
movb 0x20(%rax), %al
andb $-0x21, %al
movb %al, 0x17(%rsp)
xorl %r13d, %r13d
movq %r11, 0x48(%rsp)
movq %rbx, 0x50(%rsp)
movq %r14, 0x90(%rsp)
movq %r12, 0x88(%rsp)
movq 0x98(%rsp), %rax
movq 0x8(%rax), %rax
leaq (,%r13,8), %rcx
addq %r13, %rcx
leaq (%rcx,%rcx,2), %rsi
movq %r13, 0x58(%rsp)
addq %r13, %rsi
movl 0xc(%r12,%rsi), %edx
leaq (%rdx,%rdx,2), %rcx
movl %edx, 0x60(%rsp)
movl 0x18(%r12,%rsi), %edx
shrl $0x1a, %edx
andb $0x20, %dl
orb 0x17(%rsp), %dl
movb %dl, 0x80(%rsp)
movl 0x4(%r12,%rsi), %edx
movl %edx, 0x64(%rsp)
movl 0x8(%r12,%rsi), %edx
movl %edx, 0x68(%rsp)
movl 0x10(%r12,%rsi), %edx
movl %edx, 0x6c(%rsp)
movq %rsi, 0xa0(%rsp)
movl 0x14(%r12,%rsi), %edx
movl %edx, 0x70(%rsp)
cmpl $0x0, 0xc(%rax,%rcx,8)
je 0x1d3b10
leaq (%rax,%rcx,8), %rbp
xorl %r12d, %r12d
xorl %ebx, %ebx
movq 0x20(%r15), %r13
movq (%rbp), %rdi
movl 0xc(%rdi,%r12), %eax
cmpl $0x0, (%r13,%rax,8)
js 0x1d3afd
testq %r11, %r11
je 0x1d3970
cmpl %eax, 0xc(%r11)
je 0x1d3afd
cmpq $0x0, 0x18(%rsp)
je 0x1d3986
movq 0x18(%rsp), %rcx
cmpl %eax, 0xc(%rcx)
je 0x1d3afd
addq %r12, %rdi
leaq 0x60(%rsp), %rsi
movq %r15, %rdx
leaq 0x20(%rsp), %rcx
xorl %r8d, %r8d
callq 0x1d0b1b
movq 0x48(%rsp), %r11
testl %eax, %eax
je 0x1d3afd
movl 0x2c(%rsp), %eax
movq %rax, 0xb0(%rsp)
movl (%r13,%rax,8), %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movl 0x24(%rsp), %r9d
movl 0x28(%rsp), %r8d
movl 0x30(%rsp), %esi
movl 0x34(%rsp), %edi
movl 0x38(%rsp), %edx
movq %r14, %r10
subq %rdi, %r10
movq %r14, %r11
subq %rsi, %r11
testl %edx, %edx
cmovnsq %rsi, %r10
cmovnsq %rdi, %r11
movq %r15, %rax
movq 0xa8(%rsp), %r15
movq %r15, %r13
subq %r8, %r13
movq %r14, %rcx
subq %r11, %rcx
movq 0x48(%rsp), %r11
addq %rcx, %r8
cmpq %rcx, %r13
cmovleq %r15, %r8
movq %rax, %r15
subq %r10, %r9
movl $0x0, %eax
cmovleq %rax, %r9
testq %r11, %r11
je 0x1d3a3b
movl 0x8(%r11), %r10d
cmpq %r10, %r9
jg 0x1d3a3b
cmpq %r10, %r8
jle 0x1d3afd
cmpq $0x0, 0x18(%rsp)
je 0x1d3a5a
movq 0x18(%rsp), %rax
movl 0x4(%rax), %r10d
cmpq %r10, %r9
jl 0x1d3a5a
cmpq %r10, %r8
jge 0x1d3afd
testl %edx, %edx
jns 0x1d3a6f
movl %r14d, %ecx
subl %edi, %ecx
subl %esi, %r14d
movl %ecx, 0x30(%rsp)
movl %r14d, 0x34(%rsp)
movq 0xb0(%rsp), %rax
shldl $0x1, %edx, %eax
andl $0xbfffffff, %edx       # imm = 0xBFFFFFFF
movl %edx, 0x38(%rsp)
movl %eax, 0x2c(%rsp)
movq 0x58(%rsp), %rax
movl %eax, 0x20(%rsp)
movq 0x50(%rsp), %r14
movq 0x10(%r14), %rax
cmpq %rax, 0x8(%r14)
jne 0x1d3ad0
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%r14)
movq (%r14), %rdi
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rcx, %rsi
callq 0x2370
movq 0x48(%rsp), %r11
movq %rax, (%r14)
movq (%r14), %rax
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
movups 0x20(%rsp), %xmm0
movups 0x2c(%rsp), %xmm1
movups %xmm1, 0xc(%rax,%rdx)
movups %xmm0, (%rax,%rdx)
incq %rbx
movl 0xc(%rbp), %eax
addq $0x28, %r12
cmpq %rax, %rbx
jb 0x1d3948
movq 0x88(%rsp), %r12
movq 0xa0(%rsp), %rbp
addq %r12, %rbp
testb $0x40, 0x1b(%rbp)
movq 0x50(%rsp), %rbx
movq 0x90(%rsp), %r14
movq 0x58(%rsp), %r13
je 0x1d3bc6
movups (%rbp), %xmm0
movups 0xc(%rbp), %xmm1
movups %xmm1, 0x2c(%rsp)
movaps %xmm0, 0x20(%rsp)
movl 0x38(%rsp), %eax
shldl $0x1, %eax, 0x2c(%rsp)
movl %r13d, 0x20(%rsp)
movq 0x10(%rbx), %rax
cmpq %rax, 0x8(%rbx)
jne 0x1d3b99
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%rbx)
movq (%rbx), %rdi
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rcx, %rsi
callq 0x2370
movq 0x48(%rsp), %r11
movq %rax, (%rbx)
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbx)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movups 0x2c(%rsp), %xmm0
movups %xmm0, 0xc(%rax,%rdx)
movaps 0x20(%rsp), %xmm0
movups %xmm0, (%rax,%rdx)
movq 0xf8(%rsp), %rax
movq (%rax), %rax
cmpq $-0x1, %rax
je 0x1d3bdf
movl 0x4(%rbp), %ecx
cmpq %rcx, %rax
jbe 0x1d3bed
movl 0x4(%rbp), %eax
movq 0xf8(%rsp), %rcx
movq %rax, (%rcx)
movq 0x100(%rsp), %rax
movq (%rax), %rax
cmpq $-0x1, %rax
je 0x1d3c06
movl 0x8(%rbp), %ecx
cmpq %rcx, %rax
jae 0x1d3c14
movl 0x8(%rbp), %eax
movq 0x100(%rsp), %rcx
movq %rax, (%rcx)
incq %r13
cmpq %r14, %r13
jne 0x1d38c2
testq %r11, %r11
je 0x1d3c34
movl 0x4(%r11), %eax
movq 0xf8(%rsp), %rcx
movq %rax, (%rcx)
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0x1d3c4c
movl 0x8(%rax), %eax
movq 0x100(%rsp), %rcx
movq %rax, (%rcx)
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq