// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movsd %xmm0, 0x40(%rsp)
movq %r8, %r14
movq %rcx, %r15
movq %rdx, 0x38(%rsp)
movq %rsi, %rdx
movq %rdi, 0x18(%rsp)
movq 0x90(%rsp), %rcx
cmpl $0x0, 0x1c(%rsi)
movq %r9, 0x10(%rsp)
movq %rsi, 0x30(%rsp)
je 0x1a08ef
testq %r9, %r9
jle 0x1a0ad3
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movl 0x10(%rdx), %esi
movl 0x18(%rdx), %edi
leaq (%rsi,%rsi,4), %rsi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movl (%rax,%rsi,8), %eax
andl %r8d, %eax
notl %edi
addl %eax, %edi
subl 0x14(%rdx), %eax
movl %eax, 0x2c(%rsp)
leaq -0x1(%r15), %rbp
movq 0x38(%rsp), %rsi
testq %rsi, %rsi
setne %al
testq %r15, %r15
setg %r8b
andb %al, %r8b
movb %r8b, 0xb(%rsp)
leaq 0x10(%rsi), %rax
movq %rax, 0x48(%rsp)
xorl %r13d, %r13d
xorl %ebx, %ebx
movl %edi, 0xc(%rsp)
movq %r14, 0x50(%rsp)
leaq (%rbx,%rbx,2), %rsi
movl 0x8(%r14,%rsi,4), %eax
cmpl %edi, %eax
jbe 0x1a08c7
leaq (%r14,%rsi,4), %r12
movl 0x4(%r12), %r10d
cmpl 0x2c(%rsp), %r10d
jae 0x1a0ad6
testq %rbp, %rbp
setns %sil
andb 0xb(%rsp), %sil
cmpb $0x1, %sil
jne 0x1a07a8
movq 0x18(%rsp), %rsi
movq (%rsi), %rsi
movq 0x10(%rsi), %rdi
movl 0x10(%rdx), %esi
leaq (%rsi,%rsi,4), %rsi
movl (%rdi,%rsi,8), %r9d
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %r9d
leaq (,%rbp,2), %rsi
addq %rbp, %rsi
movq 0x48(%rsp), %rdi
leaq (%rdi,%rsi,8), %rdi
movl (%rdi), %r8d
notl %r8d
addl %r9d, %r8d
cmpl %r10d, %r8d
setae %sil
cmpl %eax, %r8d
setb %r8b
andb %r8b, %sil
cmpb %r8b, %sil
je 0x1a07aa
leaq -0x1(%rbp), %r8
addq $-0x18, %rdi
testq %rbp, %rbp
movq %r8, %rbp
jg 0x1a0770
movq $-0x1, %rbp
jmp 0x1a07aa
xorl %esi, %esi
testb %sil, %sil
je 0x1a088b
cmpq $0x0, 0xa0(%rsp)
je 0x1a08be
movq %r13, 0x20(%rsp)
movq 0x18(%rsp), %rdi
movq %r12, %rsi
movsd 0x40(%rsp), %xmm0
movq 0x98(%rsp), %r8
movq 0xa8(%rsp), %r9
movq %rdx, %r13
movq %rcx, %r14
callq 0x1a005a
testq %rax, %rax
je 0x1a08d8
movq (%r14), %rax
movq 0x8(%r14), %rcx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
andl $0xbfffffff, -0x4(%rax,%rdx) # imm = 0xBFFFFFFF
incq 0x20(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movl 0x10(%r13), %ecx
movl 0x1c(%r13), %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 %r12, %r8
callq 0x1a052d
movq (%r14), %rcx
movq 0x8(%r14), %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 0x10(%rsp), %r9
movq %r13, %rdx
movq 0x20(%rsp), %r13
movq %r14, %rcx
movq 0x50(%rsp), %r14
jmp 0x1a08c3
movq 0x18(%rsp), %rdi
movq %r12, %rsi
movsd 0x40(%rsp), %xmm0
movq 0x98(%rsp), %r8
movq 0xa8(%rsp), %r9
callq 0x1a005a
movq 0x90(%rsp), %rcx
movq 0x30(%rsp), %rdx
addq %rax, %r13
movq 0x10(%rsp), %r9
movl 0xc(%rsp), %edi
incq %rbx
cmpq %r9, %rbx
jne 0x1a0706
jmp 0x1a0ad6
movq 0x10(%rsp), %r9
movq %r13, %rdx
movq %r14, %rcx
movq 0x50(%rsp), %r14
movq 0x20(%rsp), %r13
jmp 0x1a08c3
testq %r9, %r9
jle 0x1a0ad3
movl 0x14(%rdx), %edi
movl 0x18(%rdx), %eax
incl %eax
movl %eax, 0x2c(%rsp)
movq 0x38(%rsp), %rsi
testq %rsi, %rsi
setne %al
testq %r15, %r15
setg %r8b
andb %al, %r8b
movb %r8b, 0xb(%rsp)
leaq 0x10(%rsi), %rax
movq %rax, 0x48(%rsp)
xorl %r13d, %r13d
xorl %ebx, %ebx
xorl %ebp, %ebp
movl %edi, 0xc(%rsp)
leaq (%rbx,%rbx,2), %rsi
movl 0x8(%r14,%rsi,4), %eax
cmpl %edi, %eax
jbe 0x1a0ac5
leaq (%r14,%rsi,4), %r12
movl 0x4(%r12), %r9d
cmpl 0x2c(%rsp), %r9d
jae 0x1a0ad6
cmpq %r15, %rbp
setl %sil
andb 0xb(%rsp), %sil
cmpb $0x1, %sil
jne 0x1a09a5
leaq (,%rbp,2), %rsi
addq %rbp, %rsi
movq 0x48(%rsp), %rdi
leaq (%rdi,%rsi,8), %rsi
movl (%rsi), %edi
cmpl %r9d, %edi
setae %r8b
cmpl %eax, %edi
setb %dil
andb %dil, %r8b
cmpb %dil, %r8b
je 0x1a09a8
incq %rbp
addq $0x18, %rsi
cmpq %rbp, %r15
jne 0x1a097d
movq %r15, %rbp
jmp 0x1a09a8
xorl %r8d, %r8d
testb %r8b, %r8b
je 0x1a0a89
cmpq $0x0, 0xa0(%rsp)
je 0x1a0abc
movq %r13, 0x20(%rsp)
movq 0x18(%rsp), %rdi
movq %r12, %rsi
movsd 0x40(%rsp), %xmm0
movq 0x98(%rsp), %r8
movq 0xa8(%rsp), %r9
movq %rcx, %r13
callq 0x1a005a
testq %rax, %rax
je 0x1a0a75
movq (%r13), %rax
movq 0x8(%r13), %rcx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
andl $0xbfffffff, -0x4(%rax,%rdx) # imm = 0xBFFFFFFF
incq 0x20(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x30(%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 %r12, %r8
callq 0x1a052d
movq (%r13), %rcx
movq 0x8(%r13), %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 0x10(%rsp), %r9
movq 0x30(%rsp), %rdx
movq %r13, %rcx
movq 0x20(%rsp), %r13
jmp 0x1a0ac1
movq 0x18(%rsp), %rdi
movq %r12, %rsi
movsd 0x40(%rsp), %xmm0
movq 0x98(%rsp), %r8
movq 0xa8(%rsp), %r9
callq 0x1a005a
movq 0x90(%rsp), %rcx
movq 0x30(%rsp), %rdx
addq %rax, %r13
movq 0x10(%rsp), %r9
movl 0xc(%rsp), %edi
incq %rbx
cmpq %r9, %rbx
jne 0x1a0932
jmp 0x1a0ad6
xorl %r13d, %r13d
movq %r13, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq