// File: /workspace/github_cpp_makefile_star2_O2/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, 0x30(%rsp)
movq %r8, 0x88(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, 0x78(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
pushq $0x1
popq %r12
pushq $0x48
popq %rsi
movq %r12, %rdi
callq 0x2120
movq %rax, %r15
movq %r14, 0x40(%rsp)
movq (%r14), %r13
movq %r13, 0x8(%rax)
movq %r13, (%rax)
pushq $0x8
popq %rsi
movq %r13, %rdi
callq 0x2120
movq %rax, 0x20(%rsp)
movq %rax, 0x10(%r15)
movq %rbx, 0x18(%rsp)
movl 0x14(%rbx), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq %rdi, 0x30(%r15)
movq %r12, %rsi
callq 0x2120
movq %r15, 0x8(%rsp)
movq %rax, 0x70(%rsp)
movq %rax, 0x40(%r15)
xorl %r12d, %r12d
movq $0x0, (%rsp)
xorl %esi, %esi
cmpq %r13, %rsi
jae 0x197b7f
imulq $0x28, %rsi, %rdx
movq 0x40(%rsp), %rax
addq 0x10(%rax), %rdx
movq (%rsp), %rax
shlq $0x20, %rax
movq 0x20(%rsp), %rcx
movq %rsi, 0x48(%rsp)
movq %rax, (%rcx,%rsi,8)
movq $0x0, 0x38(%rsp)
xorl %r10d, %r10d
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %r8
movq 0x18(%rsp), %rdi
movq 0xe0(%rsp), %r9
movq %rdx, 0x60(%rsp)
movl 0x10(%rdx), %eax
cmpq %rax, %r10
jae 0x1979f6
movq 0x18(%rdx), %rax
movq %r10, 0x68(%rsp)
movq (%rax,%r10,8), %rcx
shrq $0x21, %rcx
movq %rcx, 0x80(%rsp)
imulq $0x18, %rcx, %r15
addq 0x78(%rsp), %r15
xorl %ebp, %ebp
xorl %r14d, %r14d
movl 0xc(%r15), %eax
cmpq %rax, %r14
jae 0x1979d2
movq (%r15), %rbx
testq %rsi, %rsi
je 0x1977e8
cmpb $0x0, 0x20(%rbx,%rbp)
je 0x1978d6
testq %r9, %r9
je 0x1977f9
cmpq $0x0, 0x18(%rbx,%rbp)
jns 0x1978d6
movl 0xc(%rbx,%rbp), %r13d
movq 0x20(%rdi), %rax
movl (%rax,%r13,8), %edx
testl %edx, %edx
jns 0x1978d6
movq %r12, 0x10(%rsp)
movq (%rbx,%rbp), %r12
movl 0x8(%rbx,%rbp), %ecx
subl %r12d, %ecx
cmpq %r8, %rcx
jl 0x1978d1
movl 0x14(%rbx,%rbp), %ecx
subl 0x10(%rbx,%rbp), %ecx
cmpq %r8, %rcx
jl 0x1978d1
leaq (%rbx,%rbp), %rdi
shrq $0x20, %r12
movl (%rax,%r12,8), %esi
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %esi
andl %eax, %edx
leaq 0xbbf04(%rip), %rax     # 0x253758
movss 0x158(%rax), %xmm0
movq 0x88(%rsp), %rcx
leaq 0x90(%rsp), %r9
callq 0x15d560
cmpl $-0x3, %eax
jne 0x1978ba
movq 0x70(%rsp), %rax
movb $0x1, (%rax,%r12)
movq 0x60(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x68(%rsp), %rcx
movq (%rax,%rcx,8), %r11
btq $0x20, %r11
movq 0x18(%rsp), %rdi
movq 0xe0(%rsp), %r9
jb 0x1978e2
movq (%rbx,%rbp), %rax
movq %rax, 0x58(%rsp)
movl 0x8(%rbx,%rbp), %eax
movq %rax, 0x50(%rsp)
jmp 0x197911
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %r8
movq 0x18(%rsp), %rdi
movq 0xe0(%rsp), %r9
movq 0x10(%rsp), %r12
incq %r14
addq $0x28, %rbp
jmp 0x1977c8
leaq 0xbc947(%rip), %rax     # 0x254230
movq 0x18(%rax), %rax
movq 0x80(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
movl 0x8(%rbx,%rbp), %eax
movq %rdx, %rcx
subq %rax, %rcx
movq %rcx, 0x58(%rsp)
subq (%rbx,%rbp), %rdx
movq %rdx, 0x50(%rsp)
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %r12
movq (%rsp), %rcx
cmpq 0x20(%rdx), %rcx
jne 0x197969
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rax
movq %rax, 0x20(%rdx)
imulq $0xc, %rax, %rsi
movq %r12, %rdi
movq %r11, %r12
callq 0x2370
movq %r12, %r11
movq (%rsp), %rcx
movq 0x8(%rsp), %rdx
movq 0xe0(%rsp), %r9
movq 0x18(%rsp), %rdi
movq %rax, %r12
movq %rax, 0x28(%rdx)
movq (%r15), %rbx
shrq $0x20, %r11
andl $0x1, %r11d
movq 0x38(%rsp), %rax
movq 0x58(%rsp), %r10
addq %rax, %r10
movq 0x50(%rsp), %r8
addq %rax, %r8
imulq $0xc, %rcx, %rax
incq %rcx
movq %rcx, 0x18(%rdx)
leal (,%r13,2), %edx
movl %edx, (%r12,%rax)
movl 0x18(%rbx,%rbp), %edx
shrl $0x1f, %edx
xorl %esi, %esi
cmpl %r11d, %edx
setne %sil
leal (%rsi,%r13,2), %edx
movl %edx, (%r12,%rax)
movl %r10d, 0x4(%r12,%rax)
movl %r8d, 0x8(%r12,%rax)
movq 0x28(%rsp), %r8
movq %rcx, (%rsp)
movq 0x30(%rsp), %rsi
jmp 0x1978d6
movq 0x60(%rsp), %rdx
movq 0x18(%rdx), %rax
movq 0x38(%rsp), %rcx
movq 0x68(%rsp), %r10
addq (%rax,%r10,8), %rcx
movq %rcx, 0x38(%rsp)
incq %r10
jmp 0x197795
movq 0x20(%rsp), %rbx
movq 0x48(%rsp), %r14
movl 0x4(%rbx,%r14,8), %eax
imulq $0xc, %rax, %rdi
addq %r12, %rdi
imulq $0xc, (%rsp), %rsi
addq %r12, %rsi
callq 0x157976
movsd 0x4bac7(%rip), %xmm4    # 0x1e34e8
movsd 0x5a02f(%rip), %xmm3    # 0x1f1a58
movl 0x4(%rbx,%r14,8), %r14d
leaq 0xc(%r12), %rax
movq %r14, %rcx
imulq $0xc, %rcx, %rsi
leaq (%r12,%rsi), %rdi
leaq 0x1(%rcx), %r8
addq %rax, %rsi
cmpq (%rsp), %r8
ja 0x197b25
movq %r8, %rdx
je 0x197b14
movl (%rsi), %r9d
leaq 0x1(%rdx), %r8
addq $0xc, %rsi
cmpl (%rdi), %r9d
je 0x197a45
jmp 0x197b14
imulq $0xc, %rcx, %rdi
leaq (%r12,%rdi), %rsi
addq %r12, %rdi
addq $0x4, %rdi
movl %r14d, %r8d
decq %r8
testl %r8d, %r8d
js 0x197afa
movl %r8d, %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
imulq $0xc, %r9, %rbx
movl (%r12,%rbx), %r9d
cmpl (%rsi), %r9d
jne 0x197afa
leaq (%r12,%rbx), %r9
movl 0x8(%rsi), %r10d
movl 0x8(%r9), %r9d
cmpl %r9d, %r10d
movl %r9d, %r11d
cmovbl %r10d, %r11d
movl (%rdi), %ebp
movl 0x4(%r12,%rbx), %ebx
cmpl %ebx, %ebp
movl %ebx, %r15d
cmoval %ebp, %r15d
subl %r15d, %r11d
jbe 0x197a7f
subl %ebp, %r10d
subl %ebx, %r9d
cmpl %r9d, %r10d
cmovbl %r10d, %r9d
cvtsi2sd %r11, %xmm0
cvtsi2sd %r9, %xmm1
movapd %xmm1, %xmm2
mulsd %xmm3, %xmm2
ucomisd %xmm0, %xmm2
mulsd %xmm4, %xmm1
jb 0x197a7f
ucomisd %xmm1, %xmm0
jb 0x197a7f
jmp 0x197b11
imulq $0xc, %r14, %rdi
incq %r14
movl 0x8(%rsi), %r8d
movl %r8d, 0x8(%r12,%rdi)
movq (%rsi), %rsi
movq %rsi, (%r12,%rdi)
incq %rcx
cmpq %rdx, %rcx
jb 0x197a6d
movq %rdx, %rcx
jmp 0x197a36
movq 0x8(%rsp), %rax
movq %r14, 0x18(%rax)
movq 0x20(%rsp), %rbx
movq 0x48(%rsp), %r15
movl 0x4(%rbx,%r15,8), %eax
imulq $0xc, %rax, %rdi
addq %r12, %rdi
imulq $0xc, %r14, %rsi
addq %r12, %rsi
callq 0x157bcb
movq %r15, %rsi
movq (%rbx,%r15,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
movq %r14, %rdx
subq %rcx, %rdx
orq %rax, %rdx
movq %rdx, (%rbx,%r15,8)
incq %rsi
movq 0x40(%rsp), %rax
movq (%rax), %r13
movq %r14, (%rsp)
jmp 0x197741
movq 0x8(%rsp), %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq