// File: /workspace/github_cpp_makefile_star2_O1/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, 0x18(%rsp)
movq %r8, 0x88(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
movl $0x1, %edi
movl $0x48, %esi
callq 0x2120
movq %rax, %r15
movq (%r14), %rdi
movq %rdi, 0x8(%rax)
movq %rdi, (%rax)
movl $0x8, %esi
callq 0x2120
movq %rax, %r12
movq %rax, 0x10(%r15)
movq %rbx, 0x8(%rsp)
movl 0x14(%rbx), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq %rdi, 0x30(%r15)
movl $0x1, %esi
callq 0x2120
movq %r15, %rcx
movq %rax, 0x70(%rsp)
movq %rax, 0x40(%r15)
cmpq $0x0, (%r14)
je 0x1da163
movq 0xe0(%rsp), %r10
movq 0x18(%rcx), %r15
movq %rcx, 0x50(%rsp)
movq 0x28(%rcx), %rbp
xorl %ebx, %ebx
movq %r14, 0x68(%rsp)
movq %r12, 0x40(%rsp)
movq 0x10(%r14), %rax
leaq (%rbx,%rbx,4), %rcx
movq %r15, %rdx
shlq $0x20, %rdx
movq %rbx, 0x48(%rsp)
movq %rdx, (%r12,%rbx,8)
cmpl $0x0, 0x10(%rax,%rcx,8)
je 0x1d9f57
leaq (%rax,%rcx,8), %rax
movq %rax, 0x30(%rsp)
movq %r15, (%rsp)
xorl %ecx, %ecx
movq $0x0, 0x28(%rsp)
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %r8
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %r9
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rax
movq %rcx, 0x60(%rsp)
movq (%rax,%rcx,8), %rcx
shrq $0x21, %rcx
leaq (%rcx,%rcx,2), %rax
cmpl $0x0, 0xc(%rdi,%rax,8)
je 0x1d9f29
movq %rcx, 0x78(%rsp)
leaq (%rdi,%rax,8), %r13
xorl %r14d, %r14d
xorl %r12d, %r12d
testq %rsi, %rsi
je 0x1d9d46
movq (%r13), %rax
cmpb $0x0, 0x20(%rax,%r14)
je 0x1d9f15
testq %r10, %r10
je 0x1d9d5b
movq (%r13), %rax
cmpq $0x0, 0x18(%rax,%r14)
jns 0x1d9f15
movq (%r13), %rbx
movl 0xc(%rbx,%r14), %ecx
movq 0x20(%r9), %rax
movl (%rax,%rcx,8), %edx
testl %edx, %edx
jns 0x1d9f15
movq %rcx, 0x38(%rsp)
movq (%rbx,%r14), %r15
movl 0x8(%rbx,%r14), %ecx
subl %r15d, %ecx
cmpq %r8, %rcx
jl 0x1d9f15
movl 0x14(%rbx,%r14), %ecx
subl 0x10(%rbx,%r14), %ecx
cmpq %r8, %rcx
jl 0x1d9f15
leaq (%rbx,%r14), %rdi
shrq $0x20, %r15
movl (%rax,%r15,8), %esi
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %esi
andl %eax, %edx
leaq 0xb099c(%rip), %rax     # 0x28a758
movss 0x158(%rax), %xmm0
movq 0x88(%rsp), %rcx
leaq 0x90(%rsp), %r9
callq 0x19c0d3
cmpl $-0x3, %eax
jne 0x1d9e1e
movq 0x70(%rsp), %rax
movb $0x1, (%rax,%r15)
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x60(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x80(%rsp)
btq $0x20, %rax
movq 0x50(%rsp), %rdx
jb 0x1d9e3f
movq (%rbx,%r14), %rax
movq %rax, 0x58(%rsp)
movl 0x8(%rbx,%r14), %r15d
jmp 0x1d9e67
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %r8
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %r9
movq 0xe0(%rsp), %r10
jmp 0x1d9f15
leaq 0xb13ea(%rip), %rax     # 0x28b230
movq 0x18(%rax), %rax
movq 0x78(%rsp), %rcx
movq (%rax,%rcx,8), %r15
movl 0x8(%rbx,%r14), %eax
movq %r15, %rcx
subq %rax, %rcx
movq %rcx, 0x58(%rsp)
subq (%rbx,%r14), %r15
movq (%rsp), %rcx
movq 0x20(%rdx), %rax
cmpq %rax, %rcx
jne 0x1d9e9b
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x20(%rdx)
shlq $0x2, %rcx
leaq (%rcx,%rcx,2), %rsi
movq %rbp, %rdi
callq 0x2370
movq %rax, %rbp
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %r9
movq 0xe0(%rsp), %r10
movq 0x80(%rsp), %rdi
shrq $0x20, %rdi
andl $0x1, %edi
movq 0x28(%rsp), %rax
movq 0x58(%rsp), %r11
addq %rax, %r11
addq %rax, %r15
movq (%rsp), %rcx
leaq (%rcx,%rcx,2), %rax
incq %rcx
movq %rcx, (%rsp)
movq 0x38(%rsp), %rbx
leal (%rbx,%rbx), %ecx
movl %ecx, (%rbp,%rax,4)
movq (%r13), %rcx
movl 0x18(%rcx,%r14), %ecx
shrl $0x1f, %ecx
xorl %edx, %edx
cmpl %edi, %ecx
setne %dl
leal (%rdx,%rbx,2), %ecx
movl %ecx, (%rbp,%rax,4)
movl %r11d, 0x4(%rbp,%rax,4)
movl %r15d, 0x8(%rbp,%rax,4)
movq 0x20(%rsp), %rdi
incq %r12
movl 0xc(%r13), %eax
addq $0x28, %r14
cmpq %rax, %r12
jb 0x1d9d31
movq 0x30(%rsp), %r11
movq 0x18(%r11), %rax
movq 0x60(%rsp), %rcx
movq 0x28(%rsp), %rdx
addq (%rax,%rcx,8), %rdx
movq %rdx, 0x28(%rsp)
incq %rcx
movl 0x10(%r11), %eax
cmpq %rax, %rcx
jb 0x1d9cfd
jmp 0x1d9f5b
movq %r15, (%rsp)
movq 0x40(%rsp), %rbx
movq 0x48(%rsp), %r14
movl 0x4(%rbx,%r14,8), %eax
leaq (%rax,%rax,2), %rax
leaq (,%rax,4), %rdi
addq %rbp, %rdi
movq (%rsp), %r15
leaq (%r15,%r15,2), %rax
leaq (,%rax,4), %rsi
addq %rbp, %rsi
callq 0x195f57
movq (%rbx,%r14,8), %rax
shrq $0x20, %rax
cmpq %r15, %rax
jae 0x1da0f0
movq %r15, %rcx
leaq 0x1(%rax), %rdi
movq %rax, %r15
movsd 0x59904(%rip), %xmm3    # 0x2338b8
movsd 0x4b4dc(%rip), %xmm4    # 0x225498
cmpq %rcx, %rdi
je 0x1d9fd7
leaq (%rdi,%rdi,2), %rdx
movl (%rbp,%rdx,4), %edx
leaq (%rax,%rax,2), %rsi
cmpl (%rbp,%rsi,4), %edx
je 0x1da0e2
cmpq %rdi, %rax
jae 0x1da0df
movq %rdi, 0x38(%rsp)
movl %r15d, %edx
decl %edx
leaq (%rax,%rax,2), %rdx
js 0x1da0ac
leaq (,%rdx,4), %rdi
addq %rbp, %rdi
movl (%rdi), %esi
movl %r15d, %r8d
decl %r8d
leaq (%r8,%r8,2), %r14
movl (%rbp,%r14,4), %r9d
cmpl %esi, %r9d
jne 0x1da0ac
leaq (,%r14,4), %rbx
addq %rbp, %rbx
movl 0x8(%rdi), %r11d
movl 0x8(%rbx), %r10d
cmpl %r10d, %r11d
movl %r10d, %r13d
cmovbl %r11d, %r13d
movl 0x4(%rdi), %ecx
movl 0x4(%rbx), %r12d
cmpl %r12d, %ecx
cmoval %ecx, %r12d
cmpl %r12d, %r13d
jbe 0x1da05d
movl 0x8(%rdi), %ecx
movl 0x8(%rbp,%r14,4), %r14d
cmpl %r14d, %ecx
cmovbl %ecx, %r14d
subl %r12d, %r14d
jmp 0x1da060
xorl %r14d, %r14d
testq %r14, %r14
je 0x1da09c
subl 0x4(%rdi), %r11d
subl 0x4(%rbx), %r10d
cmpl %r10d, %r11d
cmovbl %r11d, %r10d
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r10, %xmm1
movapd %xmm1, %xmm2
mulsd %xmm3, %xmm2
ucomisd %xmm0, %xmm2
mulsd %xmm4, %xmm1
jb 0x1da09c
ucomisd %xmm1, %xmm0
jae 0x1da0a7
testl %r8d, %r8d
jg 0x1da004
jmp 0x1da0ac
cmpl %esi, %r9d
je 0x1da0c5
leaq (%r15,%r15,2), %rcx
incq %r15
movl 0x8(%rbp,%rdx,4), %esi
movl %esi, 0x8(%rbp,%rcx,4)
movq (%rbp,%rdx,4), %rdx
movq %rdx, (%rbp,%rcx,4)
incq %rax
movq 0x38(%rsp), %rdi
cmpq %rdi, %rax
jb 0x1d9fe5
movq %rdi, %rax
movq (%rsp), %rcx
jmp 0x1da0e2
movq %rdi, %rax
incq %rdi
cmpq %rcx, %rdi
jbe 0x1d9fbc
jmp 0x1da0f3
movq %rax, %r15
movq 0x40(%rsp), %r12
movq 0x48(%rsp), %rbx
movl 0x4(%r12,%rbx,8), %eax
leaq (%rax,%rax,2), %rax
leaq (,%rax,4), %rdi
addq %rbp, %rdi
leaq (%r15,%r15,2), %rax
leaq (,%rax,4), %rsi
addq %rbp, %rsi
callq 0x1961a2
movq (%r12,%rbx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
movq %r15, %rdx
subq %rcx, %rdx
orq %rax, %rdx
movq %rdx, (%r12,%rbx,8)
incq %rbx
movq 0x68(%rsp), %r14
cmpq (%r14), %rbx
movq 0xe0(%rsp), %r10
jb 0x1d9cae
movq 0x50(%rsp), %rcx
movq %r15, 0x18(%rcx)
movq %rbp, 0x28(%rcx)
movq %rcx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq