// File: /workspace/github_cpp_makefile_star2_O2/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 0x16104b
movq (%rbp), %rax
movq 0x10(%rax), %rax
movl 0x10(%rbx), %edx
imulq $0x28, %rdx, %rsi
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl (%rax,%rsi), %edx
movl 0x4(%r14), %eax
notl %eax
addl %edx, %eax
subl 0x8(%r14), %edx
jmp 0x161055
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 0x161077
xorl %eax, %eax
jmp 0x1611fa
movq %rcx, 0x28(%rsp)
movq %r8, 0x18(%rsp)
movsd %xmm0, 0x20(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x160c1b
movq %rax, %r12
movq %rbx, %rdi
movq %r13, %rsi
callq 0x160c1b
movq %rax, %r8
incq %rax
movq %rax, 0x10(%rsp)
movq %r12, 0x8(%rsp)
cmpq %r8, %r12
jg 0x161209
cmpl $0x0, 0x1c(%rbx)
je 0x1610e5
movq (%rbp), %rax
movq 0x10(%rax), %rax
movl 0x10(%rbx), %ecx
imulq $0x28, %rcx, %rcx
movl (%rax,%rcx), %ebp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
notq %r13
addq %rbp, %r13
subq %r15, %rbp
movq %r13, %r15
jmp 0x1610eb
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 0x16114d
movq %rbx, %rdi
movq 0x8(%rsp), %rdx
movq %r8, %rcx
callq 0x160d96
ucomisd 0x20(%rsp), %xmm0
ja 0x161070
subl %r12d, %r13d
movq 0x28(%rsp), %r12
movq (%r12), %rax
movq 0x8(%r12), %rcx
cmpq 0x10(%r12), %rcx
jne 0x161192
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmovneq %rdx, %rcx
movq %rcx, 0x10(%r12)
imulq $0x1c, %rcx, %rdx
movq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x1a3acc
movq %rax, (%r12)
movq 0x8(%r12), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r12)
imulq $0x1c, %rcx, %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)
pushq $0x1
popq %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xf1dd0(%rip), %rax     # 0x252fe0
movq (%rax), %rdi
leaq 0x91160(%rip), %rsi     # 0x1f237a
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 0x1610b9