// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: gen_shared_trace(overlap_region_alloc*, unsigned long*, unsigned long, unsigned long, unsigned long, ul_idx_t const*, ug_opt_t const*, long, double, long*, kv_ul_ov_t*)
uint64_t gen_shared_trace(overlap_region_alloc* ol, uint64_t *id_a, uint64_t id_n, uint64_t s, uint64_t e, 
const ul_idx_t *uref, const ug_opt_t *uopt, int64_t bw, double diff_ec_ul, int64_t *is_srt, kv_ul_ov_t *res)///[s, e]
{
	if(!id_n) return 0;
	uint64_t i, m, k, os, oe, ovlp, rm_n = 0; ul_ov_t p, q, *li, *lj, *t; 
	for (i = m = 0; i < id_n; i++) {
		if(ol->list[id_a[i]].x_pos_e < s) continue;
		id_a[m++] = id_a[i];
	}
	id_n = m;
	if(!id_n) return 0;
	//idx_a[] is sorted by ol->list[].x_pos_s
	for (k = 0; k < id_n; k++) {
		convert_ul_ov_t(&p, &(ol->list[id_a[k]]), uref->ug); p.qn = id_a[k];
		if(ol->list[id_a[k]].x_pos_e <= e) rm_n++;
		
		for (i = 0; i < id_n && ol->list[id_a[i]].x_pos_s <= ol->list[id_a[k]].x_pos_e; i++) {
			if(i == k) continue;
			convert_ul_ov_t(&q, &(ol->list[id_a[i]]), uref->ug); q.qn = id_a[i];
			if(p.qe > q.qe) li = &p, lj = &q;
			else if(p.qe == q.qe && p.qs >= q.qs) li = &p, lj = &q;
			else lj = &p, li = &q;
			os = MAX(li->qs, lj->qs), oe = MIN(li->qe, lj->qe);
			ovlp = ((oe > os)? (oe - os):0);
			if(!ovlp) continue;//no overlap
			t = NULL;
			if(res->n > 0 && res->a[res->n-1].qn == p.qn) {
				if(res->a[res->n-1].qs<=os && res->a[res->n-1].qe>=oe) continue;
				t = &(res->a[res->n-1]);
			}
			// fprintf(stderr, "\nk::%lu::utg%.6dl[M::%s::utg%.6dl->utg%.6dl]\n",
			// 		k, (int32_t)ol->list[id_a[k]].y_id+1,  __func__, (int32_t)li->tn+1, (int32_t)lj->tn+1);
			//not contain; graph does not has contained overlaps
			//but at boundary, migh be lj->qs == li->qs
			if(lj->qs <= li->qs+G_CHAIN_INDEL) {
				if(govlp_check(uref, uopt, bw, diff_ec_ul, li, lj)) continue;
			} else if((lj->qe+G_CHAIN_INDEL>=li->qe) && (lj->qs+G_CHAIN_INDEL>=li->qs)) {
				if(govlp_check(uref, uopt, bw, diff_ec_ul, lj, li)) continue;
			}
			if(t && t->qs<=os && t->qe >= os) {
				///assert(t->qs<=os && t->qe<oe); 
				if(oe > t->qe) t->qe = oe;
			} else {
				kv_pushp(ul_ov_t, *res, &t);
				t->qn = p.qn; t->qs = os; t->qe = oe;
			}
		}
		if(res->n > 0 && res->a[res->n-1].qn == p.qn) {
			ol->list[p.qn].align_length++;
			if(ol->list[p.qn].align_length > 1) (*is_srt) = 0;
		}
	}

	if(rm_n) {
		for (i = m = 0; i < id_n; i++) {
			if(ol->list[id_a[i]].x_pos_e <= e) continue;
			id_a[m++] = id_a[i];
		}
		id_n = m;
	}
	return id_n;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movsd %xmm0, 0x28(%rsp)
movq %r9, 0x18(%rsp)
movq %r8, 0x20(%rsp)
testq %rdx, %rdx
je 0x1715ee
movq %rsi, %r10
movq %rdi, %rbx
movq (%rdi), %rax
xorl %r11d, %r11d
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x171302
movq (%r10,%rsi,8), %rdi
imulq $0xa8, %rdi, %r8
movl 0x8(%rax,%r8), %r8d
cmpq %rcx, %r8
jb 0x1712fd
movq %rdi, (%r10,%r11,8)
incq %r11
incq %rsi
jmp 0x1712dc
testq %r11, %r11
je 0x1715ee
xorl %ebp, %ebp
xorl %r14d, %r14d
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
cmpq %r11, %rbp
je 0x1715f3
imulq $0xa8, (%r10,%rbp,8), %rsi
addq (%rbx), %rsi
movq 0x18(%rsp), %rax
movq (%rax), %rdx
leaq 0x34(%rsp), %rdi
callq 0x3d508
movq 0x10(%rsp), %r10
movq 0x8(%rsp), %r11
movq (%r10,%rbp,8), %rax
movl %eax, 0x34(%rsp)
movq (%rbx), %rcx
imulq $0xa8, %rax, %rax
movl 0x8(%rcx,%rax), %eax
cmpq %rax, 0x20(%rsp)
sbbq $-0x1, %r14
movq %r14, 0x50(%rsp)
xorl %r14d, %r14d
cmpq %r14, %r11
je 0x17159a
movq (%rbx), %rsi
imulq $0xa8, (%r10,%r14,8), %rax
movl 0x4(%rsi,%rax), %ecx
imulq $0xa8, (%r10,%rbp,8), %rdx
cmpl 0x8(%rsi,%rdx), %ecx
ja 0x17159a
cmpq %r14, %rbp
je 0x171580
addq %rax, %rsi
movq 0x18(%rsp), %rax
movq (%rax), %rdx
leaq 0x5c(%rsp), %r15
movq %r15, %rdi
callq 0x3d508
movq 0x10(%rsp), %r10
movl (%r10,%r14,8), %eax
movl %eax, 0x5c(%rsp)
movl 0x3c(%rsp), %eax
movl 0x64(%rsp), %ecx
cmpl %ecx, %eax
jbe 0x1713e3
movl %ecx, %edx
movl %eax, %ecx
leaq 0x34(%rsp), %rax
movq %r15, %r8
jmp 0x1713fd
jne 0x1713f3
movl 0x38(%rsp), %edx
cmpl 0x60(%rsp), %edx
jae 0x171588
movl %eax, %edx
movq %r15, %rax
leaq 0x34(%rsp), %r8
movq 0x8(%rsp), %r11
movl 0x4(%rax), %esi
movl 0x4(%r8), %edi
cmpl %edi, %esi
movl %edi, %r13d
cmoval %esi, %r13d
cmpl %edx, %ecx
movl %edx, %r12d
cmovbl %ecx, %r12d
cmpl %r13d, %r12d
jbe 0x171580
movq 0xc8(%rsp), %r9
movq 0x8(%r9), %r10
testq %r10, %r10
je 0x17146f
movq (%r9), %r9
imulq $0x1c, %r10, %r10
movl -0x1c(%r9,%r10), %r11d
cmpl 0x34(%rsp), %r11d
jne 0x171474
leaq (%r9,%r10), %r11
leaq (%r9,%r10), %r15
addq $-0x1c, %r15
cmpl %r13d, -0x18(%r11)
ja 0x171477
cmpl %r12d, -0x14(%r11)
movq 0x8(%rsp), %r11
movq 0x10(%rsp), %r10
jb 0x171481
jmp 0x171580
xorl %r15d, %r15d
jmp 0x17147c
xorl %r15d, %r15d
movq 0x8(%rsp), %r11
movq 0x10(%rsp), %r10
leal 0x80(%rsi), %r9d
cmpl %r9d, %edi
jbe 0x1714c8
movl $0x80, %r9d
addl %r9d, %edi
cmpl %esi, %edi
jb 0x1714fd
movl $0x80, %esi
addl %esi, %edx
cmpl %ecx, %edx
jb 0x1714fd
movq 0x18(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
movsd 0x28(%rsp), %xmm0
movq %r8, %rcx
movq %rax, %r8
jmp 0x1714e6
movq 0x18(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
movsd 0x28(%rsp), %xmm0
movq %rax, %rcx
callq 0x3d8a8
movq 0x10(%rsp), %r10
movq 0x8(%rsp), %r11
testl %eax, %eax
jne 0x171580
testq %r15, %r15
je 0x171511
cmpl %r13d, 0x4(%r15)
ja 0x171511
movl 0x8(%r15), %eax
cmpl %r13d, %eax
jae 0x17158f
movq 0xc8(%rsp), %rsi
movq (%rsi), %rax
movq 0x8(%rsi), %rcx
cmpq 0x10(%rsi), %rcx
jne 0x17155d
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rsi)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0xc8(%rsp), %rsi
movq 0x10(%rsp), %r10
movq 0x8(%rsp), %r11
movq %rax, (%rsi)
movq 0x8(%rsi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rsi)
imulq $0x1c, %rcx, %rcx
movl 0x34(%rsp), %edx
leaq (%rax,%rcx), %r15
addq $0x8, %r15
movl %edx, -0x8(%r15)
movl %r13d, -0x4(%r15)
movl %r12d, (%r15)
incq %r14
jmp 0x171371
movl %eax, %edx
jmp 0x1713d7
cmpl %eax, %r12d
jbe 0x171580
addq $0x8, %r15
jmp 0x17157d
movq 0xc8(%rsp), %rcx
movq 0x8(%rcx), %rax
testq %rax, %rax
je 0x1715e1
movq (%rcx), %rcx
imulq $0x1c, %rax, %rax
movl -0x1c(%rcx,%rax), %eax
cmpl 0x34(%rsp), %eax
jne 0x1715e1
movq (%rbx), %rcx
imulq $0xa8, %rax, %rax
movl 0x28(%rcx,%rax), %edx
incl %edx
movl %edx, 0x28(%rcx,%rax)
cmpl $0x2, %edx
jb 0x1715e1
movq 0xc0(%rsp), %rax
andq $0x0, (%rax)
incq %rbp
movq 0x50(%rsp), %r14
jmp 0x17131a
xorl %r11d, %r11d
jmp 0x171629
testq %r14, %r14
je 0x171629
movq (%rbx), %rcx
xorl %eax, %eax
xorl %edx, %edx
cmpq %rdx, %r11
je 0x171626
movq (%r10,%rdx,8), %rsi
imulq $0xa8, %rsi, %rdi
movl 0x8(%rcx,%rdi), %edi
cmpq 0x20(%rsp), %rdi
jbe 0x171621
movq %rsi, (%r10,%rax,8)
incq %rax
incq %rdx
jmp 0x1715ff
movq %rax, %r11
movq %r11, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq