// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: ul_contain_gen(ma_ug_t*, asg_t*, ma_hit_t_alloc*, long, long, unsigned long, unsigned long)
ul_contain *ul_contain_gen(ma_ug_t *ug, asg_t *rg, ma_hit_t_alloc* src, int64_t min_ovlp, int64_t max_hang, uint64_t is_el, uint64_t is_del)
{
	uint64_t k, l, i, z, t, qn, tn, ori, qs, qe, ovlp, o_z, o_r, o_o;
	ul_contain *p = NULL; ma_utg_t *u = NULL;
	utg_ct_t *m = NULL;
	int32_t r; asg_arc_t e;
	CALLOC(p, 1);
	p->idx.n = p->idx.m = ug->u.n; CALLOC(p->idx.a, p->idx.n);
	p->is_c.n = rg->n_seq; CALLOC(p->is_c.a, p->is_c.n);
	for (t = 0; t < ug->u.n; t++) {
		u = &(ug->u.a[t]);
		p->idx.a[t] = p->rids.n; p->idx.a[t] <<= 32;

		for (k = l = 0; k < u->n; k++) {
			i = u->a[k]>>33;///rid

			for (z = 0; z < src[i].length; z++) {
				if(is_el && (!src[i].buffer[z].el)) continue;
				if(is_del && (!src[i].buffer[z].del)) continue;
				qn = Get_qn(src[i].buffer[z]); tn = Get_tn(src[i].buffer[z]);
				if(!rg->seq[tn].del) continue;
				if((Get_qe(src[i].buffer[z]) - Get_qs(src[i].buffer[z])) < min_ovlp) continue;
				if((Get_te(src[i].buffer[z]) - Get_ts(src[i].buffer[z])) < min_ovlp) continue;
				r = ma_hit2arc(&(src[i].buffer[z]), rg->seq[qn].len, rg->seq[tn].len, max_hang, asm_opt.max_hang_rate, min_ovlp, &e);
            	if(r != MA_HT_TCONT) continue;///tn is contained
				p->is_c.a[qn] = 1;
				ori = (u->a[k]>>32)&1;
				if(ori == 0) {
                	qs = Get_qs(src[i].buffer[z]); qe = Get_qe(src[i].buffer[z]);
				} else {
					qs = (Get_READ_LENGTH(R_INF, i)) - Get_qe(src[i].buffer[z]);
					qe = (Get_READ_LENGTH(R_INF, i)) - Get_qs(src[i].buffer[z]);
				}
				qs += l; qe += l;
				kv_pushp(utg_ct_t, p->rids, &m);
				m->x = tn; m->x <<= 1; m->x |= (src[i].buffer[z].rev == ori?0:1);
				m->s = qs; m->e = qe/** + 1**/;
			}

			l += (uint32_t)u->a[k];
		}

		radix_sort_utg_ct_t_x_srt(p->rids.a + (p->idx.a[t]>>32), p->rids.a + p->rids.n);
		/**
		for (k = (p->idx.a[t]>>32) + 1, l = i = (p->idx.a[t]>>32); k <= p->rids.n; ++k) { 
			if (k == p->rids.n || (p->rids.a[k].x>>1) != (p->rids.a[l].x>>1)) {
				p->rids.a[i] = p->rids.a[l]; i++;
				l = k;
			}
		}
		**/
		for (k = (p->idx.a[t]>>32) + 1, l = i = (p->idx.a[t]>>32); k <= p->rids.n; ++k) { 
			if (k == p->rids.n || p->rids.a[k].x != p->rids.a[l].x) {
				for (z = l; z < k; z++) {
					for (r = (int64_t)i-1; r >= 0 && p->rids.a[r].x == p->rids.a[z].x; r--) {
						ovlp = ((MIN(p->rids.a[z].e, p->rids.a[r].e) > MAX(p->rids.a[z].s, p->rids.a[r].s))?
									(MIN(p->rids.a[z].e, p->rids.a[r].e) - MAX(p->rids.a[z].s, p->rids.a[r].s)):0);
						if(ovlp) {
							o_z = p->rids.a[z].e - p->rids.a[z].s; 
							o_r = p->rids.a[r].e - p->rids.a[r].s;
							o_o = MIN(o_z, o_r);
							if((ovlp <= o_o*1.05) && (ovlp >= o_o*0.95)) break;
						}
						// if(p->rids.a[z].s == p->rids.a[r].s && p->rids.a[z].e == p->rids.a[r].e) break;
					}
					if(r >= 0 && p->rids.a[r].x == p->rids.a[z].x) continue;
					p->rids.a[i++] = p->rids.a[z];
				}
				l = k;
			}
		}
		p->rids.n = i;

		radix_sort_utg_ct_t_s_srt(p->rids.a + (p->idx.a[t]>>32), p->rids.a + p->rids.n);
		p->idx.a[t] |= (p->rids.n - (p->idx.a[t]>>32));
	}
	
	// fprintf(stderr, "p->rids.n:%u, p->idx.n:%u\n", (uint32_t)p->rids.n, (uint32_t)p->idx.n);

	return p;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, %r13
movq %r8, 0xa0(%rsp)
movq %rcx, 0x8(%rsp)
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movl $0x1, %edi
movl $0x48, %esi
callq 0x2120
movq %rax, %r12
movq %r15, 0x58(%rsp)
movq (%r15), %rbp
movq %rbp, 0x8(%rax)
movq %rbp, (%rax)
movl $0x8, %esi
movq %rbp, %rdi
callq 0x2120
movq %rax, 0x30(%rsp)
movq %rax, 0x10(%r12)
movq %r14, 0x28(%rsp)
movl 0x14(%r14), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq %rdi, 0x30(%r12)
movl $0x1, %esi
callq 0x2120
movq %rax, 0x80(%rsp)
movq %rax, 0x40(%r12)
testq %rbp, %rbp
je 0x1e43c5
movq %r12, 0x50(%rsp)
movq 0xe0(%rsp), %r12
xorl %ebp, %ebp
xorl %r8d, %r8d
xorl %r15d, %r15d
movq %r13, 0x18(%rsp)
movq %rbx, 0x68(%rsp)
movq 0x58(%rsp), %rax
movq 0x30(%rsp), %r14
movq 0x10(%rax), %rdx
leaq (%r15,%r15,4), %rsi
movq %r8, %rax
shlq $0x20, %rax
movq %r15, 0x38(%rsp)
movq %rax, (%r14,%r15,8)
movl 0x10(%rdx,%rsi,8), %ecx
testl %ecx, %ecx
je 0x1e41d0
movq %r8, 0x20(%rsp)
leaq (%rdx,%rsi,8), %rax
movq %rax, 0x60(%rsp)
movq 0x18(%rax), %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
movq 0x8(%rsp), %r10
movq 0x28(%rsp), %r11
movq (%rdx,%r8,8), %rsi
movq %rsi, %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rdi
movl 0xc(%rbx,%rdi,8), %r14d
testl %r14d, %r14d
je 0x1e41aa
movl %r14d, %r15d
movq %rax, 0x90(%rsp)
movq %r9, 0x70(%rsp)
movq %r8, 0x78(%rsp)
leaq (%rbx,%rdi,8), %rax
movq %rax, 0x88(%rsp)
movq (%rax), %rcx
movl $0x18, %r14d
xorl %r13d, %r13d
movq 0x18(%rsp), %rbx
movl %r15d, %eax
testq %rbx, %rbx
je 0x1e3e6b
cmpb $0x0, 0x8(%rcx,%r14)
je 0x1e3f8e
testq %r12, %r12
je 0x1e3e7b
cmpq $0x0, (%rcx,%r14)
jns 0x1e3f8e
movl -0xc(%rcx,%r14), %r15d
movq 0x20(%r11), %r8
movl (%r8,%r15,8), %edx
testl %edx, %edx
jns 0x1e3f8e
movq -0x18(%rcx,%r14), %rsi
movl -0x10(%rcx,%r14), %edi
movl %edi, %r9d
subl %esi, %r9d
cmpq %r10, %r9
jl 0x1e3f8e
movl %eax, 0x14(%rsp)
movq %rbx, %rax
movl -0x8(%rcx,%r14), %ebx
movl -0x4(%rcx,%r14), %r11d
movl %r11d, %r12d
subl %ebx, %r12d
cmpq %r10, %r12
jge 0x1e3ee3
movq 0x8(%rsp), %r10
movq 0x28(%rsp), %r11
movq 0xe0(%rsp), %r12
movq %rax, %rbx
movl 0x14(%rsp), %eax
jmp 0x1e3f8e
movq %rsi, %rax
shrq $0x20, %rax
movl (%r8,%rax,8), %r10d
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %r10d
andl %r8d, %edx
subl %r11d, %edx
testb $-0x80, 0x3(%rcx,%r14)
movl %ebx, %r11d
cmovel %edx, %r11d
cmovel %ebx, %edx
cmpl %esi, %edx
movl %esi, %r8d
cmovll %edx, %r8d
movl %r10d, 0x40(%rsp)
subl %edi, %r10d
cmpl %r11d, %r10d
movl %r11d, 0x48(%rsp)
movl %r11d, %ebx
cmovll %r10d, %ebx
movq 0xa0(%rsp), %r11
cmpl %r11d, %r8d
jg 0x1e3fa5
movq %rax, 0x98(%rsp)
cmpl %r11d, %ebx
movl 0x14(%rsp), %eax
jg 0x1e3f77
movl %r9d, %r11d
cvtsi2ss %r11, %xmm1
addl %r8d, %ebx
addl %ebx, %r9d
cvtsi2ss %r9, %xmm2
leaq 0xb17f3(%rip), %r8      # 0x295758
movss 0x158(%r8), %xmm0
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
jbe 0x1e3fc1
movq 0x18(%rsp), %rbx
movq 0x8(%rsp), %r10
movq 0x28(%rsp), %r11
movq 0xe0(%rsp), %r12
incq %r13
movl %eax, %edx
addq $0x28, %r14
cmpq %rdx, %r13
jb 0x1e3e5a
jmp 0x1e418b
movq 0x18(%rsp), %rbx
movq 0x8(%rsp), %r10
movq 0x28(%rsp), %r11
movq 0xe0(%rsp), %r12
jmp 0x1e3eda
movl %r12d, %r8d
xorps %xmm1, %xmm1
cvtsi2ss %r8, %xmm1
addl %r12d, %ebx
xorps %xmm2, %xmm2
cvtsi2ss %rbx, %xmm2
mulss %xmm2, %xmm0
ucomiss %xmm1, %xmm0
movq 0x28(%rsp), %r11
jbe 0x1e3ff1
movq 0x18(%rsp), %rbx
movq 0x8(%rsp), %r10
jmp 0x1e3f86
cmpl %edx, %esi
movq 0xe0(%rsp), %r12
movl 0x48(%rsp), %r8d
jg 0x1e4016
cmpl %r8d, %r10d
jg 0x1e4016
movq 0x18(%rsp), %rbx
movq 0x8(%rsp), %r10
jmp 0x1e3f8e
cmpl %edx, %esi
jl 0x1e4065
cmpl %r8d, %r10d
jl 0x1e4065
movq 0x80(%rsp), %rdx
movq 0x98(%rsp), %rax
movb $0x1, (%rdx,%rax)
movq 0x60(%rsp), %rax
movq 0x18(%rax), %rdx
movq 0x78(%rsp), %rax
movq (%rdx,%rax,8), %rax
movq %rax, %r8
btq $0x20, %rax
jb 0x1e4098
movq -0x18(%rcx,%r14), %rax
movq %rax, 0x40(%rsp)
movl -0x10(%rcx,%r14), %eax
movq %rax, 0x48(%rsp)
jmp 0x1e40c9
movq 0x8(%rsp), %r10
cmpl %r10d, %r9d
jl 0x1e4181
cmpl %r10d, %ebx
jl 0x1e4181
addl %r8d, %edi
movl 0x40(%rsp), %r8d
subl %edi, %r8d
subl %edx, %esi
cmovlel %r8d, %esi
cmpl $-0x3, %esi
jne 0x1e4007
jmp 0x1e401f
leaq 0xb2191(%rip), %rdx     # 0x296230
movq 0x18(%rdx), %rdx
movq 0x90(%rsp), %rax
movq (%rdx,%rax,8), %rsi
movl -0x10(%rcx,%r14), %edx
movq %rsi, %rax
subq %rdx, %rax
movq %rax, 0x40(%rsp)
subq -0x18(%rcx,%r14), %rsi
movq %rsi, 0x48(%rsp)
movq 0x50(%rsp), %rdx
movq 0x20(%rsp), %rbx
cmpq 0x20(%rdx), %rbx
jne 0x1e411d
leaq (%rbx,%rbx), %rax
testq %rbx, %rbx
movl $0x2, %ecx
cmoveq %rcx, %rax
movq %rax, 0x20(%rdx)
shlq $0x2, %rax
leaq (%rax,%rax,2), %rsi
movq %rbp, %rdi
movq %r8, %rbp
callq 0x2370
movq %rbp, %r8
movq 0x28(%rsp), %r11
movq %rax, %rbp
movq 0x88(%rsp), %rax
movq (%rax), %rcx
movl 0xc(%rax), %eax
movl %eax, 0x14(%rsp)
movq 0x8(%rsp), %r10
shrq $0x20, %r8
andl $0x1, %r8d
movq 0x70(%rsp), %rax
movq 0x40(%rsp), %r9
addq %rax, %r9
movq 0x48(%rsp), %rdx
addq %rax, %rdx
movq %rdx, %rax
leaq (%rbx,%rbx,2), %rdx
incq %rbx
movq %rbx, 0x20(%rsp)
leal (%r15,%r15), %esi
movl %esi, (%rbp,%rdx,4)
movl (%rcx,%r14), %esi
shrl $0x1f, %esi
xorl %edi, %edi
cmpl %r8d, %esi
setne %dil
leal (%rdi,%r15,2), %esi
movl %esi, (%rbp,%rdx,4)
movl %r9d, 0x4(%rbp,%rdx,4)
movl %eax, 0x8(%rbp,%rdx,4)
movq 0x18(%rsp), %rbx
jmp 0x1e3eda
movq 0x18(%rsp), %rbx
jmp 0x1e3f8e
movq 0x60(%rsp), %rax
movq 0x18(%rax), %rdx
movl 0x10(%rax), %ecx
movq 0x78(%rsp), %r8
movq (%rdx,%r8,8), %rsi
movq 0x68(%rsp), %rbx
movq 0x70(%rsp), %r9
addq %rsi, %r9
incq %r8
movl %ecx, %eax
cmpq %rax, %r8
jb 0x1e3e08
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %r15
movq (%r14,%r15,8), %rax
movq 0x20(%rsp), %r8
jmp 0x1e41da
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %r15
movq %r8, 0x20(%rsp)
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (,%rax,4), %rdi
addq %rbp, %rdi
leaq (%r8,%r8,2), %rax
leaq (,%rax,4), %rsi
addq %rbp, %rsi
callq 0x19ecee
movq 0x20(%rsp), %r8
movq (%r14,%r15,8), %rax
shrq $0x20, %rax
cmpq %r8, %rax
jae 0x1e4353
leaq 0x1(%rax), %rdx
movq %rax, %rcx
movsd 0x5b95d(%rip), %xmm3    # 0x23fb88
movsd 0x4d365(%rip), %xmm4    # 0x231598
cmpq %r8, %rdx
je 0x1e424e
leaq (%rdx,%rdx,2), %rsi
movl (%rbp,%rsi,4), %esi
leaq (%rax,%rax,2), %rdi
cmpl (%rbp,%rdi,4), %esi
je 0x1e4333
cmpq %rdx, %rax
jae 0x1e4330
movl %ecx, %esi
decl %esi
leaq (%rax,%rax,2), %rsi
js 0x1e42f2
leaq (,%rsi,4), %rdi
addq %rbp, %rdi
movl (%rdi), %r8d
movl %ecx, %r9d
decl %r9d
leaq (%r9,%r9,2), %r10
cmpl %r8d, (%rbp,%r10,4)
jne 0x1e42f2
leaq (,%r10,4), %r14
addq %rbp, %r14
movl 0x8(%rdi), %r11d
movl 0x8(%r14), %r10d
cmpl %r10d, %r11d
movl %r10d, %ebx
cmovbl %r11d, %ebx
movl 0x4(%rdi), %r15d
movl 0x4(%r14), %r14d
cmpl %r14d, %r15d
movl %r14d, %r12d
cmoval %r15d, %r12d
subl %r12d, %ebx
jbe 0x1e42ed
subl %r15d, %r11d
subl %r14d, %r10d
cmpl %r10d, %r11d
cmovbl %r11d, %r10d
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r10, %xmm1
movapd %xmm1, %xmm2
mulsd %xmm3, %xmm2
ucomisd %xmm0, %xmm2
mulsd %xmm4, %xmm1
jb 0x1e42ed
ucomisd %xmm1, %xmm0
jae 0x1e430d
testl %r9d, %r9d
jg 0x1e4276
leaq (%rcx,%rcx,2), %rdi
incq %rcx
movl 0x8(%rbp,%rsi,4), %r8d
movl %r8d, 0x8(%rbp,%rdi,4)
movq (%rbp,%rsi,4), %rsi
movq %rsi, (%rbp,%rdi,4)
incq %rax
cmpq %rdx, %rax
jb 0x1e4257
movq %rdx, %rax
movq 0x68(%rsp), %rbx
movq 0xe0(%rsp), %r12
movq 0x20(%rsp), %r8
jmp 0x1e4333
movq %rdx, %rax
incq %rdx
cmpq %r8, %rdx
jbe 0x1e4233
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %r15
movl 0x4(%r14,%r15,8), %eax
movq %rcx, %r13
jmp 0x1e4360
movq %rax, %r13
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %r15
leaq (%rax,%rax,2), %rax
leaq (,%rax,4), %rdi
addq %rbp, %rdi
leaq (,%r13,2), %rax
addq %r13, %rax
leaq (,%rax,4), %rsi
addq %rbp, %rsi
callq 0x19ef39
movq %r13, %r8
movq (%r14,%r15,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
movq %r13, %rdx
subq %rcx, %rdx
orq %rax, %rdx
movq %rdx, (%r14,%r15,8)
incq %r15
movq 0x58(%rsp), %rax
cmpq (%rax), %r15
jb 0x1e3dc2
movq 0x50(%rsp), %r12
movq %r8, 0x18(%r12)
movq %rbp, 0x28(%r12)
movq %r12, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq