// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: gen_contain_chain(ul_idx_t const*, utg_ct_t*, overlap_region*, kv_ul_ov_t*, double, long, void*)
int64_t gen_contain_chain(const ul_idx_t *uref, utg_ct_t *p, overlap_region* o, kv_ul_ov_t *chains, double diff_ec_ul, int64_t winLen, void *km)
{
	int64_t y_s, y_e, y_bs, y_be, x_s, x_e, q_s, q_e;
	if(o->y_pos_strand) {
		y_s = uref->ug->u.a[o->y_id].len - p->e;
		y_e = uref->ug->u.a[o->y_id].len - p->s - 1;
	} else {
		y_s = p->s; y_e = p->e - 1;
	}


	y_s = MAX(y_s, (int64_t)o->y_pos_s); y_e = MIN(y_e, (int64_t)o->y_pos_e);
	if(y_s > y_e) return 0;
	x_s = get_chain_x(o, y_s); x_e = get_chain_x(o, y_e) + 1;
	if(x_s >= x_e) fprintf(stderr, "+++y_s->%ld, y_e->%ld, x_s->%ld, x_e->%ld\n", y_s, y_e, x_s, x_e);
	if(o->y_pos_strand) {
		y_bs = uref->ug->u.a[o->y_id].len - (y_e+1);
		y_be = uref->ug->u.a[o->y_id].len - y_s;
	} else {
		y_bs = y_s; y_be = y_e + 1;
	}

	q_s = 0; q_e = p->e - p->s;
	if(p->x&1) {
		q_s += (p->e - y_be);
		q_e -= (y_bs - p->s);
	} else {
		q_s += (y_bs - p->s);
		q_e -= (p->e - y_be); 
	}
	// if(q_s < 0 || q_e < 0 || q_s >= (int64_t)(p->e - p->s) || q_e > (int64_t)(p->e - p->s)) fprintf(stderr, "ERROR\n");
	if(winLen > 0 && diff_ec_ul > 0 && es_win_err(o, winLen, x_s, x_e-1) > diff_ec_ul) return 0;

	ul_ov_t *x = NULL;
	kv_pushp_km(km, ul_ov_t, *chains, &x);
	x->qn = o->x_id; x->qs = x_s; x->qe = x_e;

	/**x->tn = p->x>>1;**/x->tn = (uint32_t)(0x80000000); x->tn |= (p->x>>1);
	x->ts = q_s; x->te = q_e; x->el = 1;x->sec = 0; x->rev = ((o->y_pos_strand == (p->x&1))?0:1);
	// if(((x->tn<<1)>>1) == 23113) fprintf(stderr, "x->tn:%u, o->y_id:%u\n", (x->tn<<1)>>1, o->y_id);
	// if(x->qn == 0 /**&& ((x->tn<<1)>>1) == 302**/) {
	// 	/**if(o->x_id == 0 && (o->y_id == 46 || o->y_id == 48))**/ {
	// 		// fprintf(stderr, "\nUL[%u]\t%u\t%u\t%c\tUTG[%u]\t%u\t%u\n", o->x_id, o->x_pos_s, o->x_pos_e,
	// 		// 				"+-"[o->y_pos_strand], o->y_id, o->y_pos_s, o->y_pos_e);
	// 		// fprintf(stderr, "Contain[%u]\t%c\ts[%u]\te[%u]\n", p->x>>1, "+-"[p->x&1], p->s, p->e);				
	// 		fprintf(stderr, "U[%u]\t%u\t%u\t%c\tR[%u]\t%u\t%u\tUid[%u]\n", x->qn, x->qs, x->qe, 
	// 											"+-"[x->rev], ((x->tn<<1)>>1), x->ts, x->te, o->y_id);
	// 	}									
	// }
	
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, 0x30(%rsp)
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %rbp
cmpl $0x0, 0x1c(%rdx)
je 0x1a8dfd
movq (%rbp), %rax
movq 0x10(%rax), %rax
movl 0x10(%rbx), %edx
leaq (%rdx,%rdx,4), %rsi
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl (%rax,%rsi,8), %edx
movl 0x4(%r14), %eax
notl %eax
addl %edx, %eax
subl 0x8(%r14), %edx
jmp 0x1a8e07
movl 0x4(%r14), %edx
movl 0x8(%r14), %eax
decl %eax
movl 0x14(%rbx), %r15d
movl 0x18(%rbx), %r13d
cmpl %r15d, %edx
cmoval %edx, %r15d
cmpl %r13d, %eax
cmovbl %eax, %r13d
cmpl %r13d, %r15d
jbe 0x1a8e29
xorl %eax, %eax
jmp 0x1a8fbe
movq %rcx, 0x28(%rsp)
movq %r8, 0x18(%rsp)
movsd %xmm0, 0x20(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1a8993
movq %rax, %r12
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1a8993
movq %rax, %r8
incq %rax
movq %rax, 0x10(%rsp)
movq %r12, 0x8(%rsp)
cmpq %r8, %r12
jg 0x1a8fcd
cmpl $0x0, 0x1c(%rbx)
je 0x1a8e97
movq (%rbp), %rax
movq 0x10(%rax), %rax
movl 0x10(%rbx), %ecx
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %ebp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
notq %r13
addq %rbp, %r13
subq %r15, %rbp
movq %r13, %r15
jmp 0x1a8e9d
incq %r13
movq %r13, %rbp
movq 0x18(%rsp), %rsi
movl 0x4(%r14), %r12d
movl 0x8(%r14), %r13d
movq %r13, %rax
subq %rbp, %rax
movq %r12, %rcx
subq %r15, %rcx
subq %r12, %r15
subq %r13, %rbp
testb $0x1, (%r14)
cmovneq %rax, %r15
cmovneq %rcx, %rbp
testq %rsi, %rsi
setle %al
xorpd %xmm0, %xmm0
movsd 0x20(%rsp), %xmm1
ucomisd %xmm0, %xmm1
setbe %cl
orb %al, %cl
jne 0x1a8eff
movq %rbx, %rdi
movq 0x8(%rsp), %rdx
movq %r8, %rcx
callq 0x1a8b27
ucomisd 0x20(%rsp), %xmm0
ja 0x1a8e22
subl %r12d, %r13d
movq 0x28(%rsp), %r12
movq (%r12), %rax
movq 0x8(%r12), %rdx
cmpq 0x10(%r12), %rdx
jne 0x1a8f4d
leaq (%rdx,%rdx), %rcx
testq %rdx, %rdx
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x10(%r12)
leaq (%rsi,%rsi,8), %rcx
leaq (%rcx,%rcx,2), %rdx
addq %rsi, %rdx
movq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x1ee21d
movq %rax, (%r12)
movq 0x8(%r12), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, 0x8(%r12)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdx, %rcx
movl (%rbx), %edx
movl %edx, (%rax,%rcx)
movq 0x8(%rsp), %rdx
movl %edx, 0x4(%rax,%rcx)
movq 0x10(%rsp), %rdx
movl %edx, 0x8(%rax,%rcx)
movl (%r14), %edx
shrl %edx
movl $0x80000000, %esi       # imm = 0x80000000
orl %esi, %edx
movl %edx, 0xc(%rax,%rcx)
movl %r15d, 0x10(%rax,%rcx)
addl %ebp, %r13d
movl %r13d, 0x14(%rax,%rcx)
andl 0x18(%rax,%rcx), %esi
movl $0x40000000, %edx       # imm = 0x40000000
orl %edx, %esi
movl %esi, 0x18(%rax,%rcx)
movl (%r14), %esi
andl $0x1, %esi
cmpl %esi, 0x1c(%rbx)
movl $0xc0000000, %esi       # imm = 0xC0000000
cmovel %edx, %esi
movl %esi, 0x18(%rax,%rcx)
movl $0x1, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xec00c(%rip), %rax     # 0x294fe0
movq (%rax), %rdi
leaq 0x974d4(%rip), %rsi     # 0x2404b2
movq %r15, %rdx
movq %r13, %rcx
movq %r8, %r12
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r9
xorl %eax, %eax
callq 0x22b0
movq %r12, %r8
jmp 0x1a8e6b