// File: /workspace/github_cpp_makefile_star2_O1/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 0x1a00a1
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 0x1a00ab
movl 0x4(%r14), %edx
movl 0x8(%r14), %eax
decl %eax
movl 0x14(%rbx), %r12d
movl 0x18(%rbx), %r13d
cmpl %r12d, %edx
cmoval %edx, %r12d
cmpl %r13d, %eax
cmovbl %eax, %r13d
cmpl %r13d, %r12d
jbe 0x1a00cd
xorl %eax, %eax
jmp 0x1a025f
movq %rcx, 0x28(%rsp)
movq %r8, 0x18(%rsp)
movsd %xmm0, 0x20(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x19fc4d
movq %rax, %r15
movq %rbx, %rdi
movq %r13, %rsi
callq 0x19fc4d
movq %rax, %r8
incq %rax
movq %rax, 0x10(%rsp)
movq %r15, 0x8(%rsp)
cmpq %r8, %r15
jg 0x1a026e
cmpl $0x0, 0x1c(%rbx)
je 0x1a013b
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 %r12, %rbp
movq %r13, %r12
jmp 0x1a0141
incq %r13
movq %r13, %rbp
movq 0x18(%rsp), %rsi
movl 0x4(%r14), %r15d
movl 0x8(%r14), %r13d
movq %r13, %rax
subq %rbp, %rax
movq %r15, %rcx
subq %r12, %rcx
subq %r15, %r12
subq %r13, %rbp
testb $0x1, (%r14)
cmovneq %rax, %r12
cmovneq %rcx, %rbp
testq %rsi, %rsi
setle %al
xorpd %xmm0, %xmm0
movsd 0x20(%rsp), %xmm1
ucomisd %xmm0, %xmm1
setbe %cl
orb %al, %cl
jne 0x1a01a3
movq %rbx, %rdi
movq 0x8(%rsp), %rdx
movq %r8, %rcx
callq 0x19fda5
ucomisd 0x20(%rsp), %xmm0
ja 0x1a00c6
subl %r15d, %r13d
movq 0x28(%rsp), %r15
movq 0x10(%r15), %rax
cmpq %rax, 0x8(%r15)
jne 0x1a01e4
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmovneq %rcx, %rax
movq %rax, 0x10(%r15)
movq (%r15), %rsi
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rdx
addq %rax, %rdx
movq 0x30(%rsp), %rdi
callq 0x1e3800
movq %rax, (%r15)
movq (%r15), %rax
movq 0x8(%r15), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, 0x8(%r15)
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 $0x80000000, %edx       # imm = 0x80000000
movl %edx, 0xc(%rax,%rcx)
movl (%r14), %esi
shrl %esi
orl %edx, %esi
movl %esi, 0xc(%rax,%rcx)
movl %r12d, 0x10(%rax,%rcx)
addl %ebp, %r13d
movl %r13d, 0x14(%rax,%rcx)
andl 0x18(%rax,%rcx), %edx
movl $0x40000000, %esi       # imm = 0x40000000
orl %esi, %edx
movl %edx, 0x18(%rax,%rcx)
movl (%r14), %edx
andl $0x1, %edx
cmpl %edx, 0x1c(%rbx)
movl $0xc0000000, %edx       # imm = 0xC0000000
cmovel %esi, %edx
movl %edx, 0x18(%rax,%rcx)
movl $0x1, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xe9d6b(%rip), %rax     # 0x289fe0
movq (%rax), %rdi
leaq 0x93f63(%rip), %rsi     # 0x2341e2
movq %r12, %rdx
movq %r13, %rcx
movq %r8, %r15
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r9
xorl %eax, %eax
callq 0x22b0
movq %r15, %r8
jmp 0x1a010f