// File: /workspace/github_cpp_makefile_star2_O2/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 %rsi, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
movq %rdx, 0x40(%rsp)
je 0x1617bc
movq (%rdi), %rax
movq 0x10(%rax), %rax
movl 0x10(%r8), %esi
movl 0x18(%r8), %r12d
imulq $0x28, %rsi, %rsi
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movl (%rax,%rsi), %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)
xorl %ebx, %ebx
testq %r9, %r9
cmovleq %rbx, %r9
leaq 0x10(%rdx), %rax
movq %rax, 0x38(%rsp)
movq $0x0, 0x28(%rsp)
movq %r9, 0x18(%rsp)
cmpq %r9, %rbx
je 0x161993
imulq $0xc, %rbx, %r13
movl 0x8(%r14,%r13), %eax
cmpl %r12d, %eax
jbe 0x1616d1
addq %r14, %r13
movl 0x4(%r13), %r11d
cmpl 0x24(%rsp), %r11d
jae 0x161993
testq %rbp, %rbp
setns %dl
andb 0x7(%rsp), %dl
cmpb $0x1, %dl
jne 0x161694
movq (%rdi), %rdx
movq 0x10(%rdx), %rdx
movl 0x10(%r8), %esi
imulq $0x28, %rsi, %rsi
movl (%rdx,%rsi), %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %edx
imulq $0x18, %rbp, %rsi
addq 0x38(%rsp), %rsi
movl (%rsi), %r10d
notl %r10d
addl %edx, %r10d
cmpl %r11d, %r10d
jb 0x16167c
cmpl %eax, %r10d
jb 0x1616d9
cmpl %eax, %r10d
jae 0x161694
leaq -0x1(%rbp), %r10
addq $-0x18, %rsi
testq %rbp, %rbp
movq %r10, %rbp
jg 0x161669
pushq $-0x1
popq %rbp
movq %r13, %rsi
movq %r8, %rdx
movsd 0x30(%rsp), %xmm0
movq 0x88(%rsp), %r8
movq 0x98(%rsp), %r9
callq 0x161004
movq 0x80(%rsp), %rcx
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %r8
addq %rax, 0x28(%rsp)
movq 0x18(%rsp), %r9
incq %rbx
jmp 0x16160b
cmpq $0x0, 0x90(%rsp)
je 0x1616d1
movq %r13, %rsi
movq %r8, %rdx
movsd 0x30(%rsp), %xmm0
movq 0x88(%rsp), %r8
movq 0x98(%rsp), %r9
callq 0x161004
testq %rax, %rax
je 0x1617a0
movq 0x80(%rsp), %rcx
imulq $0x1c, 0x8(%rcx), %rax
movq (%rcx), %rcx
andl $0xbfffffff, -0x4(%rcx,%rax) # imm = 0xBFFFFFFF
incq 0x28(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x10(%rsp), %rdx
movl 0x10(%rdx), %ecx
movl 0x1c(%rdx), %r9d
imulq $0x28, %rcx, %rcx
movl (%rax,%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x40(%rsp), %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r13, %r8
callq 0x16146a
movq 0x80(%rsp), %r10
movq (%r10), %rcx
imulq $0x1c, 0x8(%r10), %rdx
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movl -0x4(%rcx,%rdx), %esi
movl $0xc0000000, %edi       # imm = 0xC0000000
andl %edi, %esi
orl %eax, %esi
movl %esi, -0x4(%rcx,%rdx)
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rdi
movq %r10, %rcx
jmp 0x1616d1
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rdi
movq 0x80(%rsp), %rcx
jmp 0x1616d1
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)
xorl %ebp, %ebp
testq %r9, %r9
cmovleq %rbp, %r9
leaq 0x10(%rdx), %rax
movq %rax, 0x38(%rsp)
xorl %ebx, %ebx
movq $0x0, 0x28(%rsp)
movq %r9, 0x18(%rsp)
cmpq %r9, %rbx
je 0x161993
imulq $0xc, %rbx, %r13
movl 0x8(%r14,%r13), %eax
cmpl %r12d, %eax
jbe 0x1618a8
addq %r14, %r13
movl 0x4(%r13), %r11d
cmpl 0x24(%rsp), %r11d
jae 0x161993
cmpb $0x0, 0x7(%rsp)
je 0x16186b
cmpq %r15, %rbp
movq %r15, %rdx
cmovgq %rbp, %rdx
imulq $0x18, %rbp, %rsi
addq 0x38(%rsp), %rsi
cmpq %r15, %rbp
jge 0x161868
movl (%rsi), %r10d
cmpl %r11d, %r10d
jb 0x16185a
cmpl %eax, %r10d
jb 0x1618b0
cmpl %eax, %r10d
jae 0x16186b
incq %rbp
addq $0x18, %rsi
jmp 0x161848
movq %rdx, %rbp
movq %r13, %rsi
movq %r8, %rdx
movsd 0x30(%rsp), %xmm0
movq 0x88(%rsp), %r8
movq 0x98(%rsp), %r9
callq 0x161004
movq 0x80(%rsp), %rcx
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %r8
addq %rax, 0x28(%rsp)
movq 0x18(%rsp), %r9
incq %rbx
jmp 0x161801
cmpq $0x0, 0x90(%rsp)
je 0x1618a8
movq %r13, %rsi
movq %r8, %rdx
movsd 0x30(%rsp), %xmm0
movq 0x88(%rsp), %r8
movq 0x98(%rsp), %r9
callq 0x161004
testq %rax, %rax
je 0x161977
movq 0x80(%rsp), %rcx
imulq $0x1c, 0x8(%rcx), %rax
movq (%rcx), %rcx
andl $0xbfffffff, -0x4(%rcx,%rax) # imm = 0xBFFFFFFF
incq 0x28(%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x10(%rsp), %rdx
movl 0x10(%rdx), %ecx
movl 0x1c(%rdx), %r9d
imulq $0x28, %rcx, %rcx
movl (%rax,%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x40(%rsp), %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %r13, %r8
callq 0x16146a
movq 0x80(%rsp), %r10
movq (%r10), %rcx
imulq $0x1c, 0x8(%r10), %rdx
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movl -0x4(%rcx,%rdx), %esi
movl $0xc0000000, %edi       # imm = 0xC0000000
andl %edi, %esi
orl %eax, %esi
movl %esi, -0x4(%rcx,%rdx)
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rdi
movq %r10, %rcx
jmp 0x1618a8
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rdi
movq 0x80(%rsp), %rcx
jmp 0x1618a8
movq 0x28(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq