// File: chhylp123[P]hifiasm/inter.cpp
// Function: rescue_contain_ul_chains(ul_idx_t const*, overlap_region*, haplotype_evdience*, long, utg_ct_t*, long, kv_ul_ov_t*, double, long, long, void*)
int64_t rescue_contain_ul_chains(const ul_idx_t *uref, overlap_region* o, haplotype_evdience *he_a, int64_t he_n, utg_ct_t *ct_a, int64_t ct_n, 
kv_ul_ov_t *chains, double diff_ec_ul, int64_t winLen, int64_t rescue_trans, void *km)
{
	int64_t i, k, ss, ff, t0 = 0;
	uint64_t ys, ye;
	utg_ct_t *p = NULL; 
	// if(o->x_id == 0) {
	// 	fprintf(stderr, "\no->y_id->%u\n", o->y_id);
	// 	for (i = 0; i < ct_n; i++) {
	// 		p = &(ct_a[i]);
	// 		fprintf(stderr, "***rid->%u, rev->%u, s->%u, e->%u\n", p->x>>1, p->x&1, p->s, p->e);
	// 	}
	// }

	if(o->y_pos_strand == 0){
		ys = o->y_pos_s; ye = o->y_pos_e + 1;
		for (i = k = 0; i < ct_n; i++) {
			p = &(ct_a[i]);
			if(p->e <= ys) continue; 
			if(p->s >= ye) break;
			ff = 1;
			if(he_a && he_n > 0) {
				for (; k < he_n; k++) {
					if(he_a[k].cov >= p->s && he_a[k].cov < p->e) {
						ff = 0;
						break;
					}
					if(he_a[k].cov >= p->e) break;
				}
			}
			// if(ff == debug_utg_ct_t(uref, o, p, he_a, he_n)) fprintf(stderr, "ERROR\n");
			if(ff) {
				///push ovlp
				t0 += gen_contain_chain(uref, p, o, chains, diff_ec_ul, winLen, km);
			} else if(rescue_trans) {
                if(gen_contain_chain(uref, p, o, chains, diff_ec_ul, winLen, km)){
                    t0++; chains->a[chains->n-1].el = 0;
					chains->a[chains->n-1].sec = get_het_occ(he_a, he_n, k, uref->ug->u.a[o->y_id].len, p, o->y_pos_strand);
                }
            }
			// if(!ff)	t0++;	
		}
		
	} else {
		ys = uref->ug->u.a[o->y_id].len - (o->y_pos_e+1); 
		ye = uref->ug->u.a[o->y_id].len - o->y_pos_s;
		for (i = 0, k = he_n - 1; i < ct_n; i++) {
			p = &(ct_a[i]); 
			if(p->e <= ys) continue; 
			if(p->s >= ye) break;
			ff = 1;
			if(he_a && he_n > 0) {
				for (; k >= 0; k--) {
					ss = uref->ug->u.a[o->y_id].len - he_a[k].cov - 1;
					if(ss >= p->s && ss < p->e) {
						ff = 0;
						break;
					}
					if(ss >= p->e) break;
				}
			}
			// if(ff == debug_utg_ct_t(uref, o, p, he_a, he_n)) fprintf(stderr, "ERROR\n");
			if(ff) {
				///push ovlp
				t0 += gen_contain_chain(uref, p, o, chains, diff_ec_ul, winLen, km);
			} else if(rescue_trans) {
                if(gen_contain_chain(uref, p, o, chains, diff_ec_ul, winLen, km)){
                    t0++; chains->a[chains->n-1].el = 0;
					chains->a[chains->n-1].sec = get_het_occ(he_a, he_n, k, uref->ug->u.a[o->y_id].len, p, o->y_pos_strand);
                }
            }
			// if(!ff)	t0++;
		}
	}
	// if(debug_utg_ct_t(uref, o, ct_a, ct_n, he_a, he_n)!=t0) fprintf(stderr, "ERROR\n");
	// fprintf(stderr, "***[M::%s] o->y_id:%u, chains->n:%u\n", __func__, o->y_id, (uint32_t)chains->n);
	return t0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0x28ee8c
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
addl $0x1, %eax
movl %eax, %eax
movq %rax, -0x70(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x30(%rbp), %rax
jge 0x28ee87
movq -0x28(%rbp), %rax
imulq $0xc, -0x40(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movl 0x8(%rax), %eax
cmpq -0x68(%rbp), %rax
ja 0x28ecc1
jmp 0x28ee76
movq -0x78(%rbp), %rax
movl 0x4(%rax), %eax
cmpq -0x70(%rbp), %rax
jb 0x28ecd3
jmp 0x28ee87
movq $0x1, -0x58(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x28ed5b
cmpq $0x0, -0x20(%rbp)
jle 0x28ed5b
jmp 0x28eceb
movq -0x48(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x28ed59
movq -0x18(%rbp), %rax
imulq $0x18, -0x48(%rbp), %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movq -0x78(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jb 0x28ed2f
movq -0x18(%rbp), %rax
imulq $0x18, -0x48(%rbp), %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movq -0x78(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jae 0x28ed2f
movq $0x0, -0x58(%rbp)
jmp 0x28ed59
movq -0x18(%rbp), %rax
imulq $0x18, -0x48(%rbp), %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movq -0x78(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jb 0x28ed49
jmp 0x28ed59
jmp 0x28ed4b
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x28eceb
jmp 0x28ed5b
cmpq $0x0, -0x58(%rbp)
je 0x28ed91
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq 0x10(%rbp), %rcx
movsd -0x38(%rbp), %xmm0
movq 0x18(%rbp), %r8
movq 0x28(%rbp), %r9
callq 0x28e230
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x28ee74
cmpq $0x0, 0x20(%rbp)
je 0x28ee72
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq 0x10(%rbp), %rcx
movsd -0x38(%rbp), %xmm0
movq 0x18(%rbp), %r8
movq 0x28(%rbp), %r9
callq 0x28e230
cmpq $0x0, %rax
je 0x28ee70
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
subq $0x1, %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %ecx
andl $0xbfffffff, %ecx       # imm = 0xBFFFFFFF
orl $0x0, %ecx
movl %ecx, 0x18(%rax)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x10(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %ecx
movq -0x78(%rbp), %r8
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, %r9d
callq 0x28e9f0
movl %eax, %edx
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
subq $0x1, %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %ecx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
andl $0xc0000000, %ecx       # imm = 0xC0000000
orl %edx, %ecx
movl %ecx, 0x18(%rax)
jmp 0x28ee72
jmp 0x28ee74
jmp 0x28ee76
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x28ec91
jmp 0x28f123
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x10(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq -0x10(%rbp), %rcx
movl 0x18(%rcx), %ecx
addl $0x1, %ecx
subl %ecx, %eax
movl %eax, %eax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x10(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq -0x10(%rbp), %rcx
subl 0x14(%rcx), %eax
movl %eax, %eax
movq %rax, -0x70(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x20(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x30(%rbp), %rax
jge 0x28f121
movq -0x28(%rbp), %rax
imulq $0xc, -0x40(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movl 0x8(%rax), %eax
cmpq -0x68(%rbp), %rax
ja 0x28ef2f
jmp 0x28f110
movq -0x78(%rbp), %rax
movl 0x4(%rax), %eax
cmpq -0x70(%rbp), %rax
jb 0x28ef41
jmp 0x28f121
movq $0x1, -0x58(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x28eff5
cmpq $0x0, -0x20(%rbp)
jle 0x28eff5
jmp 0x28ef61
cmpq $0x0, -0x48(%rbp)
jl 0x28eff3
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq -0x10(%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
imulq $0x18, -0x48(%rbp), %rdx
addq %rdx, %rcx
subl 0x10(%rcx), %eax
subl $0x1, %eax
movl %eax, %eax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x78(%rbp), %rcx
movl 0x4(%rcx), %ecx
cmpq %rcx, %rax
jl 0x28efce
movq -0x50(%rbp), %rax
movq -0x78(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
jge 0x28efce
movq $0x0, -0x58(%rbp)
jmp 0x28eff3
movq -0x50(%rbp), %rax
movq -0x78(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
jl 0x28efe0
jmp 0x28eff3
jmp 0x28efe2
movq -0x48(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x28ef61
jmp 0x28eff5
cmpq $0x0, -0x58(%rbp)
je 0x28f02b
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq 0x10(%rbp), %rcx
movsd -0x38(%rbp), %xmm0
movq 0x18(%rbp), %r8
movq 0x28(%rbp), %r9
callq 0x28e230
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x28f10e
cmpq $0x0, 0x20(%rbp)
je 0x28f10c
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq 0x10(%rbp), %rcx
movsd -0x38(%rbp), %xmm0
movq 0x18(%rbp), %r8
movq 0x28(%rbp), %r9
callq 0x28e230
cmpq $0x0, %rax
je 0x28f10a
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
subq $0x1, %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %ecx
andl $0xbfffffff, %ecx       # imm = 0xBFFFFFFF
orl $0x0, %ecx
movl %ecx, 0x18(%rax)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x10(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %ecx
movq -0x78(%rbp), %r8
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, %r9d
callq 0x28e9f0
movl %eax, %edx
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
subq $0x1, %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %ecx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
andl $0xc0000000, %ecx       # imm = 0xC0000000
orl %edx, %ecx
movl %ecx, 0x18(%rax)
jmp 0x28f10c
jmp 0x28f10e
jmp 0x28f110
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x28eeff
jmp 0x28f123
movq -0x60(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq