// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movsd %xmm0, 0x30(%rsp)
movq %r8, %r14
movq %rcx, %r15
movq %rsi, %r8
movq 0x80(%rsp), %rcx
cmpl $0x0, 0x1c(%rsi)
movq %r9, 0x28(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
je 0x1a9654
testq %r9, %r9
jle 0x1a9846
movq (%rdi), %rax
movq 0x10(%rax), %rax
movl 0x10(%r8), %esi
movl 0x18(%r8), %r12d
leaq (%rsi,%rsi,4), %rsi
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movl (%rax,%rsi,8), %eax
andl %r10d, %eax
notl %r12d
addl %eax, %r12d
subl 0x14(%r8), %eax
movl %eax, 0x24(%rsp)
leaq -0x1(%r15), %rbp
testq %rdx, %rdx
setne %al
testq %r15, %r15
setg %sil
andb %al, %sil
movb %sil, 0x7(%rsp)
movq %rdx, 0x38(%rsp)
leaq 0x10(%rdx), %rax
movq %rax, 0x40(%rsp)
movq $0x0, 0x18(%rsp)
xorl %ebx, %ebx
leaq (%rbx,%rbx,2), %rdx
movl 0x8(%r14,%rdx,4), %eax
cmpl %r12d, %eax
jbe 0x1a954c
leaq (%r14,%rdx,4), %r13
movl 0x4(%r13), %r11d
cmpl 0x24(%rsp), %r11d
jae 0x1a984f
testq %rbp, %rbp
setns %dl
andb 0x7(%rsp), %dl
cmpb $0x1, %dl
jne 0x1a950f
movq (%rdi), %rdx
movq 0x10(%rdx), %rdx
movl 0x10(%r8), %esi
leaq (%rsi,%rsi,4), %rsi
movl (%rdx,%rsi,8), %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %edx
leaq (,%rbp,2), %rsi
addq %rbp, %rsi
movq 0x40(%rsp), %r10
leaq (%r10,%rsi,8), %rsi
movl (%rsi), %r10d
notl %r10d
addl %edx, %r10d
cmpl %r11d, %r10d
jb 0x1a94f3
cmpl %eax, %r10d
jb 0x1a955d
cmpl %eax, %r10d
jae 0x1a950f
leaq -0x1(%rbp), %r10
addq $-0x18, %rsi
testq %rbp, %rbp
movq %r10, %rbp
jg 0x1a94e0
movq $-0x1, %rbp
movq %r13, %rsi
movq %r8, %rdx
movsd 0x30(%rsp), %xmm0
movq 0x88(%rsp), %r8
movq 0x98(%rsp), %r9
callq 0x1a8db6
movq 0x80(%rsp), %rcx
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %r8
addq %rax, 0x18(%rsp)
movq 0x28(%rsp), %r9
incq %rbx
cmpq %r9, %rbx
jne 0x1a947f
jmp 0x1a984f
cmpq $0x0, 0x90(%rsp)
je 0x1a954c
movq %r13, %rsi
movq %r8, %rdx
movsd 0x30(%rsp), %xmm0
movq 0x88(%rsp), %r8
movq 0x98(%rsp), %r9
callq 0x1a8db6
testq %rax, %rax
je 0x1a9638
movq 0x80(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
andl $0xbfffffff, -0x4(%rax,%rdx) # imm = 0xBFFFFFFF
incq 0x18(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x10(%rsp), %rdx
movl 0x10(%rdx), %ecx
movl 0x1c(%rdx), %r9d
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x38(%rsp), %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r13, %r8
callq 0x1a92ad
movq 0x80(%rsp), %r10
movq (%r10), %rcx
movq 0x8(%r10), %rdx
leaq (%rdx,%rdx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdx, %rsi
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movl -0x4(%rcx,%rsi), %edx
movl $0xc0000000, %edi       # imm = 0xC0000000
andl %edi, %edx
orl %eax, %edx
movl %edx, -0x4(%rcx,%rsi)
movq 0x28(%rsp), %r9
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rdi
movq %r10, %rcx
jmp 0x1a954c
movq 0x28(%rsp), %r9
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rdi
movq 0x80(%rsp), %rcx
jmp 0x1a954c
testq %r9, %r9
jle 0x1a9846
movl 0x14(%r8), %r12d
movl 0x18(%r8), %eax
incl %eax
movl %eax, 0x24(%rsp)
testq %rdx, %rdx
setne %al
testq %r15, %r15
setg %sil
andb %al, %sil
movb %sil, 0x7(%rsp)
movq %rdx, 0x38(%rsp)
leaq 0x10(%rdx), %rax
movq %rax, 0x40(%rsp)
movq $0x0, 0x18(%rsp)
xorl %ebx, %ebx
xorl %ebp, %ebp
leaq (%rbx,%rbx,2), %rdx
movl 0x8(%r14,%rdx,4), %eax
cmpl %r12d, %eax
jbe 0x1a973e
leaq (%r14,%rdx,4), %r13
movl 0x4(%r13), %r10d
cmpl 0x24(%rsp), %r10d
jae 0x1a984f
cmpq %r15, %rbp
setl %dl
andb 0x7(%rsp), %dl
cmpb $0x1, %dl
jne 0x1a9701
leaq (,%rbp,2), %rdx
addq %rbp, %rdx
movq 0x40(%rsp), %rsi
leaq (%rsi,%rdx,8), %rdx
movl (%rdx), %esi
cmpl %r10d, %esi
jb 0x1a96ee
cmpl %eax, %esi
jb 0x1a974f
cmpl %eax, %esi
jae 0x1a9701
incq %rbp
addq $0x18, %rdx
cmpq %rbp, %r15
jne 0x1a96e3
movq %r15, %rbp
movq %r13, %rsi
movq %r8, %rdx
movsd 0x30(%rsp), %xmm0
movq 0x88(%rsp), %r8
movq 0x98(%rsp), %r9
callq 0x1a8db6
movq 0x80(%rsp), %rcx
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %r8
addq %rax, 0x18(%rsp)
movq 0x28(%rsp), %r9
incq %rbx
cmpq %r9, %rbx
jne 0x1a969b
jmp 0x1a984f
cmpq $0x0, 0x90(%rsp)
je 0x1a973e
movq %r13, %rsi
movq %r8, %rdx
movsd 0x30(%rsp), %xmm0
movq 0x88(%rsp), %r8
movq 0x98(%rsp), %r9
callq 0x1a8db6
testq %rax, %rax
je 0x1a982a
movq 0x80(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
andl $0xbfffffff, -0x4(%rax,%rdx) # imm = 0xBFFFFFFF
incq 0x18(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x10(%rsp), %rdx
movl 0x10(%rdx), %ecx
movl 0x1c(%rdx), %r9d
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x38(%rsp), %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r13, %r8
callq 0x1a92ad
movq 0x80(%rsp), %r10
movq (%r10), %rcx
movq 0x8(%r10), %rdx
leaq (%rdx,%rdx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdx, %rsi
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movl -0x4(%rcx,%rsi), %edx
movl $0xc0000000, %edi       # imm = 0xC0000000
andl %edi, %edx
orl %eax, %edx
movl %edx, -0x4(%rcx,%rsi)
movq 0x28(%rsp), %r9
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rdi
movq %r10, %rcx
jmp 0x1a973e
movq 0x28(%rsp), %r9
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rdi
movq 0x80(%rsp), %rcx
jmp 0x1a973e
movq $0x0, 0x18(%rsp)
movq 0x18(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq