// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: rescue_trans_ul_chains(ul_idx_t const*, overlap_region*, haplotype_evdience*, long, ma_utg_t*, kv_ul_ov_t*, double, long, long, unsigned long*, void*)
int64_t rescue_trans_ul_chains(const ul_idx_t *uref, overlap_region* o, haplotype_evdience *he_a, int64_t he_n, ma_utg_t *u, 
kv_ul_ov_t *chains, double diff_ec_ul, int64_t winLen, int64_t rescue_trans, uint64_t *cis_occ, void *km)
{
	uint64_t ys, ye, i, l;
	int64_t k, ff, ss, t0 = 0;
	utg_ct_t p; 
	if(cis_occ) (*cis_occ) = 0;

	if(o->y_pos_strand == 0) {
		ys = o->y_pos_s; ye = o->y_pos_e + 1;
		for (i = k = l = 0; i < u->n; i++) {
			p.x = u->a[i]>>32; p.s = l; p.e = l + Get_READ_LENGTH(R_INF, (u->a[i]>>33));
			l += (uint32_t)u->a[i];
			if(p.e <= ys) continue; 
			if(p.s >= ye) break;
			ff = 1;
			if(he_a && he_n) {
				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, 0, 0, 0, &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; if(cis_occ) (*cis_occ)++;
					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 = l = 0, k = he_n - 1; i < u->n; i++) {
            p.x = u->a[i]>>32; p.s = l; p.e = l + Get_READ_LENGTH(R_INF, (u->a[i]>>33));
			l += (uint32_t)u->a[i];
            if(p.e <= ys) continue; 
            if(p.s >= ye) break;
			ff = 1;
			if(he_a && he_n) {
				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, 0, 0, 0, &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; if(cis_occ) (*cis_occ)++;
					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, NULL, 0, u, he_a, he_n)!=t0) fprintf(stderr, "ERROR\n");
	// fprintf(stderr, "t0->%ld\n", t0);
    return t0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movsd %xmm0, 0x50(%rsp)
movq %r8, %r14
movq %rsi, 0x10(%rsp)
movq %rdi, 0x20(%rsp)
movq 0xb0(%rsp), %rax
testq %rax, %rax
je 0x1a9898
movq $0x0, (%rax)
movq 0x10(%rsp), %rax
cmpl $0x0, 0x1c(%rax)
movq %r9, 0x18(%rsp)
movq %rcx, 0x40(%rsp)
je 0x1a9b34
cmpl $0x0, 0x10(%r14)
je 0x1a9d76
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x10(%rsp), %rdi
movl 0x10(%rdi), %esi
movl 0x18(%rdi), %ebx
leaq (%rsi,%rsi,4), %rsi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movl (%rax,%rsi,8), %eax
andl %r8d, %eax
notl %ebx
addl %eax, %ebx
subl 0x14(%rdi), %eax
movl %eax, 0x30(%rsp)
movq %rcx, %r15
addq $-0x1, %r15
setb %al
testq %rdx, %rdx
setne %cl
andb %al, %cl
movb %cl, 0xf(%rsp)
movq %rdx, 0x58(%rsp)
leaq 0x10(%rdx), %rax
movq %rax, 0x60(%rsp)
xorl %edi, %edi
leaq 0xec915(%rip), %r8      # 0x296230
xorl %r13d, %r13d
xorl %r12d, %r12d
movq %rbx, 0x48(%rsp)
movq 0x18(%r14), %rax
movq (%rax,%r12,8), %rbp
movq %rbp, %rax
shrq $0x20, %rax
movl %eax, 0x34(%rsp)
movl %r13d, 0x38(%rsp)
movq 0x18(%r8), %rax
movq %rbp, %rcx
shrq $0x21, %rcx
movl (%rax,%rcx,8), %eax
addl %r13d, %eax
movl %eax, 0x3c(%rsp)
cmpl %ebx, %eax
jbe 0x1a9a1a
cmpl %r13d, 0x30(%rsp)
jbe 0x1a9d78
testq %r15, %r15
setns %cl
andb 0xf(%rsp), %cl
cmpb $0x1, %cl
jne 0x1a99d3
movq 0x20(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
movq 0x10(%rsp), %rdx
movl 0x10(%rdx), %edx
leaq (%rdx,%rdx,4), %rdx
movl (%rcx,%rdx,8), %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %ecx
leaq (%r15,%r15,2), %rdx
movq 0x60(%rsp), %rsi
leaq (%rsi,%rdx,8), %rdx
movl (%rdx), %esi
notl %esi
addl %ecx, %esi
cmpl %r13d, %esi
jb 0x1a99b3
cmpl %eax, %esi
jb 0x1a9a32
cmpl %eax, %esi
jae 0x1a99d3
leaq -0x1(%r15), %rsi
addq $-0x18, %rdx
testq %r15, %r15
movq %rsi, %r15
jg 0x1a99a4
movq %rdi, %rbx
movq $-0x1, %r15
jmp 0x1a99d6
movq %rdi, %rbx
movq 0x20(%rsp), %rdi
leaq 0x34(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r9, %rcx
movsd 0x50(%rsp), %xmm0
movq 0xa0(%rsp), %r8
movq 0xb8(%rsp), %r9
callq 0x1a8db6
movq 0x18(%rsp), %r9
movq %rbx, %rdi
addq %rax, %rdi
movq 0x48(%rsp), %rbx
leaq 0xec816(%rip), %r8      # 0x296230
addq %rbp, %r13
incq %r12
movl 0x10(%r14), %eax
cmpq %rax, %r12
jb 0x1a9926
jmp 0x1a9d78
cmpq $0x0, 0xa8(%rsp)
je 0x1a9a1a
movq %rdi, 0x28(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x34(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r9, %rcx
movsd 0x50(%rsp), %xmm0
movq 0xa0(%rsp), %r8
movq 0xb8(%rsp), %r9
callq 0x1a8db6
testq %rax, %rax
je 0x1a9b25
movq 0x18(%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
movq 0xb0(%rsp), %rax
testq %rax, %rax
movq 0x40(%rsp), %rsi
movq 0x28(%rsp), %rdi
je 0x1a9ab1
incq (%rax)
incq %rdi
movq 0x20(%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 %rdi, %rbx
movq 0x58(%rsp), %rdi
movq %r15, %rdx
leaq 0x34(%rsp), %r8
callq 0x1a92ad
movq %rbx, %rdi
movq 0x18(%rsp), %r9
movq (%r9), %rcx
movq 0x8(%r9), %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, %r8d       # imm = 0xC0000000
andl %r8d, %edx
orl %eax, %edx
movl %edx, -0x4(%rcx,%rsi)
jmp 0x1a9a0e
movq 0x18(%rsp), %r9
movq 0x28(%rsp), %rdi
jmp 0x1a9a13
cmpl $0x0, 0x10(%r14)
je 0x1a9d76
movq 0x10(%rsp), %rax
movl 0x14(%rax), %r15d
movl 0x18(%rax), %eax
incl %eax
movl %eax, 0x30(%rsp)
testq %rdx, %rdx
setne %al
testq %rcx, %rcx
setne %sil
andb %al, %sil
movb %sil, 0xf(%rsp)
movq %rdx, 0x58(%rsp)
leaq 0x10(%rdx), %rax
movq %rax, 0x60(%rsp)
xorl %edi, %edi
leaq 0xec6b3(%rip), %r8      # 0x296230
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %r15, 0x48(%rsp)
movq 0x18(%r14), %rax
movq (%rax,%r13,8), %rbp
movq %rbp, %rax
shrq $0x20, %rax
movl %eax, 0x34(%rsp)
movl %r12d, 0x38(%rsp)
movq 0x18(%r8), %rax
movq %rbp, %rdx
shrq $0x21, %rdx
movl (%rax,%rdx,8), %eax
addl %r12d, %eax
movl %eax, 0x3c(%rsp)
cmpl %r15d, %eax
jbe 0x1a9c56
cmpl %r12d, 0x30(%rsp)
jbe 0x1a9d78
cmpq %rcx, %rbx
setl %dl
andb 0xf(%rsp), %dl
cmpb $0x1, %dl
jne 0x1a9c0a
leaq (%rbx,%rbx,2), %rdx
movq 0x60(%rsp), %rsi
leaq (%rsi,%rdx,8), %rsi
movl (%rsi), %edx
cmpl %r12d, %edx
jb 0x1a9bf2
cmpl %eax, %edx
jb 0x1a9c6e
cmpl %eax, %edx
jae 0x1a9c0a
incq %rbx
addq $0x18, %rsi
cmpq %rbx, %rcx
jne 0x1a9be7
movq %rdi, %r15
movq %rcx, %rbx
jmp 0x1a9c0d
movq %rdi, %r15
movq 0x20(%rsp), %rdi
leaq 0x34(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r9, %rcx
movsd 0x50(%rsp), %xmm0
movq 0xa0(%rsp), %r8
movq 0xb8(%rsp), %r9
callq 0x1a8db6
movq 0x18(%rsp), %r9
movq %r15, %rdi
addq %rax, %rdi
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %r15
leaq 0xec5da(%rip), %r8      # 0x296230
addq %rbp, %r12
incq %r13
movl 0x10(%r14), %eax
cmpq %rax, %r13
jb 0x1a9b8a
jmp 0x1a9d78
cmpq $0x0, 0xa8(%rsp)
je 0x1a9c56
movq %rdi, 0x28(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x34(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r9, %rcx
movsd 0x50(%rsp), %xmm0
movq 0xa0(%rsp), %r8
movq 0xb8(%rsp), %r9
callq 0x1a8db6
testq %rax, %rax
je 0x1a9d62
movq 0x18(%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
movq 0xb0(%rsp), %rax
testq %rax, %rax
movq 0x40(%rsp), %rsi
movq 0x28(%rsp), %rcx
je 0x1a9ced
incq (%rax)
incq %rcx
movq %rcx, 0x28(%rsp)
movq 0x20(%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 0x58(%rsp), %rdi
movq %rbx, %rdx
leaq 0x34(%rsp), %r8
callq 0x1a92ad
movq 0x40(%rsp), %rcx
movq 0x18(%rsp), %r9
movq (%r9), %rdi
movq 0x8(%r9), %rdx
leaq (%rdx,%rdx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdx, %rsi
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movl -0x4(%rdi,%rsi), %edx
movl $0xc0000000, %r8d       # imm = 0xC0000000
andl %r8d, %edx
orl %eax, %edx
movl %edx, -0x4(%rdi,%rsi)
jmp 0x1a9d6c
movq 0x18(%rsp), %r9
movq 0x40(%rsp), %rcx
movq 0x28(%rsp), %rdi
jmp 0x1a9c4f
xorl %edi, %edi
movq %rdi, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq