// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movsd %xmm0, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movq %r9, -0x40(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
je 0x28e2c1
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
movl 0x10(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq -0x18(%rbp), %rcx
subl 0x8(%rcx), %eax
movl %eax, %eax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
movl 0x10(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq -0x18(%rbp), %rcx
subl 0x4(%rcx), %eax
subl $0x1, %eax
movl %eax, %eax
movq %rax, -0x50(%rbp)
jmp 0x28e2dc
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
subl $0x1, %eax
movl %eax, %eax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
movq -0x20(%rbp), %rcx
movl 0x14(%rcx), %ecx
cmpq %rcx, %rax
jl 0x28e2f9
movq -0x48(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x28e307
movq -0x20(%rbp), %rax
movl 0x14(%rax), %eax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x50(%rbp), %rax
movq -0x20(%rbp), %rcx
movl 0x18(%rcx), %ecx
cmpq %rcx, %rax
jg 0x28e32f
movq -0x50(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0x28e33d
movq -0x20(%rbp), %rax
movl 0x18(%rax), %eax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
jle 0x28e35f
movq $0x0, -0x8(%rbp)
jmp 0x28e67e
movq -0x20(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x28d9c0
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x28d9c0
addq $0x1, %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
jl 0x28e3b7
movq 0x135c4a(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movq -0x48(%rbp), %rdx
movq -0x50(%rbp), %rcx
movq -0x68(%rbp), %r8
movq -0x70(%rbp), %r9
leaq 0x102cd6(%rip), %rsi    # 0x391086
movb $0x0, %al
callq 0x22a0
movq -0x20(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
je 0x28e41e
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
movl 0x10(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq -0x50(%rbp), %rcx
addq $0x1, %rcx
subq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
movl 0x10(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
subq -0x48(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x28e432
movq -0x48(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x18(%rbp), %rcx
subl 0x4(%rcx), %eax
movl %eax, %eax
movq %rax, -0x80(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x28e48a
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
subq -0x60(%rbp), %rax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x58(%rbp), %rcx
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
subq %rax, %rcx
movq -0x80(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x80(%rbp)
jmp 0x28e4b8
movq -0x58(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x4(%rcx), %ecx
subq %rcx, %rax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %ecx
subq -0x60(%rbp), %rcx
movq -0x80(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x38(%rbp)
jle 0x28e4fa
movsd -0x30(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x28e4fa
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x68(%rbp), %rdx
movq -0x70(%rbp), %rcx
subq $0x1, %rcx
callq 0x28dc40
ucomisd -0x30(%rbp), %xmm0
jbe 0x28e4fa
movq $0x0, -0x8(%rbp)
jmp 0x28e67e
movq $0x0, -0x88(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x28e576
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x28e536
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
shlq %rax
movq %rax, -0xa0(%rbp)
jmp 0x28e544
movl $0x2, %eax
movq %rax, -0xa0(%rbp)
jmp 0x28e544
movq -0xa0(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x40(%rbp), %rdi
movq -0x28(%rbp), %rax
movq (%rax), %rsi
movq -0x28(%rbp), %rax
imulq $0x1c, 0x10(%rax), %rdx
callq 0x30e740
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rdx
movq 0x8(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x8(%rdx)
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %ecx
movq -0x88(%rbp), %rax
movl %ecx, (%rax)
movq -0x68(%rbp), %rax
movl %eax, %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x70(%rbp), %rax
movl %eax, %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x88(%rbp), %rax
movl $0x80000000, 0xc(%rax)  # imm = 0x80000000
movq -0x18(%rbp), %rax
movl (%rax), %ecx
shrl %ecx
movq -0x88(%rbp), %rax
orl 0xc(%rax), %ecx
movl %ecx, 0xc(%rax)
movq -0x78(%rbp), %rax
movl %eax, %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x80(%rbp), %rax
movl %eax, %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x88(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0xbfffffff, %ecx       # imm = 0xBFFFFFFF
orl $0x40000000, %ecx       # imm = 0x40000000
movl %ecx, 0x18(%rax)
movq -0x88(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0xc0000000, %ecx       # imm = 0xC0000000
orl $0x0, %ecx
movl %ecx, 0x18(%rax)
movq -0x20(%rbp), %rax
movl 0x1c(%rax), %ecx
movq -0x18(%rbp), %rax
movl (%rax), %esi
andl $0x1, %esi
movl $0x1, %edx
xorl %eax, %eax
cmpl %esi, %ecx
cmovel %eax, %edx
movq -0x88(%rbp), %rax
movl 0x18(%rax), %ecx
andl $0x1, %edx
shll $0x1f, %edx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl %edx, %ecx
movl %ecx, 0x18(%rax)
movq $0x1, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)