// File: /workspace/github_cpp_makefile_star2_O3/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 0x1bbb36
movq %rsi, %r11
movq (%rdi), %rax
xorl %esi, %esi
movq $0x0, 0x10(%rsp)
movq (%r11,%rsi,8), %r9
imulq $0xa8, %r9, %r8
movl 0x8(%rax,%r8), %r8d
cmpq %rcx, %r8
jb 0x1bb7fb
movq 0x10(%rsp), %r8
movq %r9, (%r11,%r8,8)
incq %r8
movq %r8, 0x10(%rsp)
incq %rsi
cmpq %rsi, %rdx
jne 0x1bb7d5
cmpq $0x0, 0x10(%rsp)
je 0x1bbb36
xorl %ebx, %ebx
xorl %r13d, %r13d
movq %rdi, (%rsp)
movq %r11, 0x8(%rsp)
imulq $0xa8, (%r11,%r13,8), %rsi
addq (%rdi), %rsi
movq 0x18(%rsp), %rax
movq (%rax), %rdx
leaq 0x34(%rsp), %rdi
callq 0x6ecd8
movq 0x8(%rsp), %r11
movq (%rsp), %rdi
movq (%r11,%r13,8), %rax
movl %eax, 0x34(%rsp)
movq (%rdi), %rcx
imulq $0xa8, %rax, %rax
movl 0x8(%rcx,%rax), %eax
cmpq %rax, 0x20(%rsp)
sbbq $-0x1, %rbx
movq %rbx, 0x50(%rsp)
xorl %ebp, %ebp
movq (%rdi), %rsi
imulq $0xa8, (%r11,%rbp,8), %rax
movl 0x4(%rsi,%rax), %ecx
imulq $0xa8, (%r11,%r13,8), %rdx
cmpl 0x8(%rsi,%rdx), %ecx
ja 0x1bba9d
cmpq %rbp, %r13
je 0x1bba7b
addq %rax, %rsi
movq 0x18(%rsp), %rax
movq (%rax), %rdx
leaq 0x5c(%rsp), %rbx
movq %rbx, %rdi
callq 0x6ecd8
movq 0x8(%rsp), %r11
movl (%r11,%rbp,8), %eax
movl %eax, 0x5c(%rsp)
movl 0x3c(%rsp), %eax
movl 0x64(%rsp), %ecx
cmpl %ecx, %eax
jbe 0x1bb8d2
movl %ecx, %edx
movl %eax, %ecx
leaq 0x34(%rsp), %rax
movq %rbx, %r8
jmp 0x1bb8ec
jne 0x1bb8e2
movl 0x38(%rsp), %edx
cmpl 0x60(%rsp), %edx
jae 0x1bba8b
movl %eax, %edx
movq %rbx, %rax
leaq 0x34(%rsp), %r8
movq (%rsp), %rdi
movl 0x4(%rax), %esi
movl 0x4(%r8), %r12d
cmpl %r12d, %esi
movl %r12d, %r14d
cmoval %esi, %r14d
cmpl %edx, %ecx
movl %edx, %r15d
cmovbl %ecx, %r15d
cmpl %r14d, %r15d
jbe 0x1bba7b
movq 0xc8(%rsp), %r9
movq 0x8(%r9), %r10
testq %r10, %r10
je 0x1bb964
movq (%r9), %r9
leaq (%r10,%r10,8), %r11
leaq (%r11,%r11,2), %r11
addq %r10, %r11
movl -0x1c(%r9,%r11), %r10d
cmpl 0x34(%rsp), %r10d
jne 0x1bb968
leaq (%r9,%r11), %r10
leaq (%r9,%r11), %rbx
addq $-0x1c, %rbx
cmpl %r14d, -0x18(%r10)
ja 0x1bb96a
cmpl %r15d, -0x14(%r10)
movq (%rsp), %rdi
movq 0x8(%rsp), %r11
jb 0x1bb973
jmp 0x1bba7b
xorl %ebx, %ebx
jmp 0x1bb973
xorl %ebx, %ebx
movq (%rsp), %rdi
movq 0x8(%rsp), %r11
leal 0x80(%rsi), %r9d
cmpl %r9d, %r12d
jbe 0x1bb9bb
movl $0x80, %r9d
addl %r9d, %r12d
cmpl %esi, %r12d
jb 0x1bb9ef
movl $0x80, %esi
addl %esi, %edx
cmpl %ecx, %edx
jb 0x1bb9ef
movq 0x18(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
movsd 0x28(%rsp), %xmm0
movq %r8, %rcx
movq %rax, %r8
jmp 0x1bb9d9
movq 0x18(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
movsd 0x28(%rsp), %xmm0
movq %rax, %rcx
callq 0x6f0a2
movq 0x8(%rsp), %r11
movq (%rsp), %rdi
testl %eax, %eax
jne 0x1bba7b
testq %rbx, %rbx
je 0x1bba06
cmpl %r14d, 0x4(%rbx)
ja 0x1bba06
movl 0x8(%rbx), %eax
cmpl %r14d, %eax
jae 0x1bba92
movq 0xc8(%rsp), %rbx
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
cmpq 0x10(%rbx), %rcx
jne 0x1bba52
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%rbx)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r11
movq (%rsp), %rdi
movq %rax, (%rbx)
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
movl 0x34(%rsp), %ecx
leaq (%rax,%rdx), %rbx
addq $0x8, %rbx
movl %ecx, -0x8(%rbx)
movl %r14d, -0x4(%rbx)
movl %r15d, (%rbx)
incq %rbp
cmpq %rbp, 0x10(%rsp)
jne 0x1bb869
jmp 0x1bba9d
movl %eax, %edx
jmp 0x1bb8c6
cmpl %eax, %r15d
jbe 0x1bba7b
addq $0x8, %rbx
jmp 0x1bba78
movq 0xc8(%rsp), %rcx
movq 0x8(%rcx), %rax
testq %rax, %rax
je 0x1bbaee
movq (%rcx), %rcx
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
movl -0x1c(%rcx,%rdx), %eax
cmpl 0x34(%rsp), %eax
jne 0x1bbaee
movq (%rdi), %rcx
imulq $0xa8, %rax, %rax
movl 0x28(%rcx,%rax), %edx
incl %edx
movl %edx, 0x28(%rcx,%rax)
cmpl $0x2, %edx
jb 0x1bbaee
movq 0xc0(%rsp), %rax
movq $0x0, (%rax)
incq %r13
cmpq 0x10(%rsp), %r13
movq 0x50(%rsp), %rbx
jne 0x1bb81d
testq %rbx, %rbx
je 0x1bbb47
movq (%rdi), %rcx
xorl %edx, %edx
xorl %eax, %eax
movq (%r11,%rdx,8), %rsi
imulq $0xa8, %rsi, %rdi
movl 0x8(%rcx,%rdi), %edi
cmpq 0x20(%rsp), %rdi
jbe 0x1bbb2a
movq %rsi, (%r11,%rax,8)
incq %rax
incq %rdx
cmpq %rdx, 0x10(%rsp)
jne 0x1bbb0d
jmp 0x1bbb38
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rax
jmp 0x1bbb38