// File: /workspace/github_cpp_makefile_star2_O1/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 0x1b232b
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 0x1b1ff4
movq 0x10(%rsp), %r8
movq %r9, (%r11,%r8,8)
incq %r8
movq %r8, 0x10(%rsp)
incq %rsi
cmpq %rsi, %rdx
jne 0x1b1fce
cmpq $0x0, 0x10(%rsp)
je 0x1b232b
xorl %ebx, %ebx
xorl %r13d, %r13d
movq %rdi, 0x8(%rsp)
movq %r11, (%rsp)
imulq $0xa8, (%r11,%r13,8), %rsi
addq (%rdi), %rsi
movq 0x18(%rsp), %rax
movq (%rax), %rdx
leaq 0x34(%rsp), %rdi
callq 0x6f306
movq (%rsp), %r11
movq 0x8(%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 %r15d, %r15d
movq (%rdi), %rsi
imulq $0xa8, (%r11,%r15,8), %rax
movl 0x4(%rsi,%rax), %ecx
imulq $0xa8, (%r11,%r13,8), %rdx
cmpl 0x8(%rsi,%rdx), %ecx
ja 0x1b2292
cmpq %r15, %r13
je 0x1b2277
addq %rax, %rsi
movq 0x18(%rsp), %rax
movq (%rax), %rdx
leaq 0x5c(%rsp), %rbx
movq %rbx, %rdi
callq 0x6f306
movq (%rsp), %r11
movl (%r11,%r15,8), %eax
movl %eax, 0x5c(%rsp)
movl 0x64(%rsp), %ecx
leaq 0x34(%rsp), %rax
movq %rbx, %r8
cmpl %ecx, 0x3c(%rsp)
ja 0x1b20de
jne 0x1b20d4
movl 0x38(%rsp), %ecx
leaq 0x5c(%rsp), %r8
cmpl 0x60(%rsp), %ecx
jae 0x1b20de
leaq 0x5c(%rsp), %rax
leaq 0x34(%rsp), %r8
movl 0x4(%rax), %ecx
movl 0x8(%rax), %esi
movl 0x4(%r8), %edx
movl 0x8(%r8), %ebp
cmpl %edx, %ecx
movl %edx, %r12d
cmoval %ecx, %r12d
cmpl %ebp, %esi
movl %ebp, %r14d
cmovbl %esi, %r14d
cmpl %r12d, %r14d
movq 0x8(%rsp), %rdi
jbe 0x1b2277
movq 0xc8(%rsp), %r9
movq 0x8(%r9), %r10
testq %r10, %r10
je 0x1b215d
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 0x1b2161
leaq (%r9,%r11), %r10
leaq (%r9,%r11), %rbx
addq $-0x1c, %rbx
cmpl %r12d, -0x18(%r10)
ja 0x1b2163
cmpl %r14d, -0x14(%r10)
movq 0x8(%rsp), %rdi
movq (%rsp), %r11
jb 0x1b216c
jmp 0x1b2277
xorl %ebx, %ebx
jmp 0x1b216c
xorl %ebx, %ebx
movq 0x8(%rsp), %rdi
movq (%rsp), %r11
leal 0x80(%rcx), %r9d
cmpl %r9d, %edx
jbe 0x1b21b7
movl $0x80, %r9d
addl %r9d, %ebp
cmpl %esi, %ebp
setb %sil
addl %r9d, %edx
cmpl %ecx, %edx
setb %cl
orb %sil, %cl
jne 0x1b21eb
movq 0x18(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
movsd 0x28(%rsp), %xmm0
movq %r8, %rcx
movq %rax, %r8
jmp 0x1b21d5
movq 0x18(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
movsd 0x28(%rsp), %xmm0
movq %rax, %rcx
callq 0x6f757
movq (%rsp), %r11
movq 0x8(%rsp), %rdi
testl %eax, %eax
jne 0x1b2277
testq %rbx, %rbx
je 0x1b2202
cmpl %r12d, 0x4(%rbx)
ja 0x1b2202
movl 0x8(%rbx), %eax
cmpl %r12d, %eax
jae 0x1b2287
movq 0xc8(%rsp), %rbx
movq 0x10(%rbx), %rax
cmpq %rax, 0x8(%rbx)
jne 0x1b2247
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 (%rsp), %r11
movq 0x8(%rsp), %rdi
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
movl 0x34(%rsp), %ecx
leaq (%rax,%rdx), %rbx
addq $0x8, %rbx
movl %ecx, -0x8(%rbx)
movl %r12d, -0x4(%rbx)
movl %r14d, (%rbx)
incq %r15
cmpq %r15, 0x10(%rsp)
jne 0x1b2063
jmp 0x1b2292
cmpl %eax, %r14d
jbe 0x1b2277
addq $0x8, %rbx
jmp 0x1b2274
movq 0xc8(%rsp), %rcx
movq 0x8(%rcx), %rax
testq %rax, %rax
je 0x1b22e3
movq (%rcx), %rcx
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
movl 0x34(%rsp), %eax
cmpl %eax, -0x1c(%rcx,%rdx)
jne 0x1b22e3
movq (%rdi), %rcx
imulq $0xa8, %rax, %rax
movl 0x28(%rcx,%rax), %edx
incl %edx
movl %edx, 0x28(%rcx,%rax)
cmpl $0x2, %edx
jb 0x1b22e3
movq 0xc0(%rsp), %rax
movq $0x0, (%rax)
incq %r13
cmpq 0x10(%rsp), %r13
movq 0x50(%rsp), %rbx
jne 0x1b2016
testq %rbx, %rbx
je 0x1b233c
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 0x1b231f
movq %rsi, (%r11,%rax,8)
incq %rax
incq %rdx
cmpq %rdx, 0x10(%rsp)
jne 0x1b2302
jmp 0x1b232d
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rax
jmp 0x1b232d