// File: /workspace/github_cpp_makefile_star2_O1/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, 0x58(%rsp)
movq %r8, %r14
movq %rcx, %rbx
movq %rdx, 0x48(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, 0x28(%rsp)
movq 0xb0(%rsp), %rax
testq %rax, %rax
je 0x1a0b25
movq $0x0, (%rax)
movq 0x10(%rsp), %rax
cmpl $0x0, 0x1c(%rax)
movq %r9, 0x50(%rsp)
je 0x1a0db7
cmpl $0x0, 0x10(%r14)
je 0x1a0ff9
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x10(%rsp), %rdx
movl 0x10(%rdx), %ecx
movl 0x18(%rdx), %esi
leaq (%rcx,%rcx,4), %rcx
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl (%rax,%rcx,8), %eax
andl %edi, %eax
notl %esi
addl %eax, %esi
subl 0x14(%rdx), %eax
movl %eax, 0x30(%rsp)
movq %rbx, %r15
addq $-0x1, %r15
setb %al
movq 0x48(%rsp), %rcx
testq %rcx, %rcx
setne %dl
andb %al, %dl
movb %dl, 0xf(%rsp)
leaq 0x10(%rcx), %rax
movq %rax, 0x60(%rsp)
movq $0x0, 0x20(%rsp)
leaq 0xea688(%rip), %rdx     # 0x28b230
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %rsi, 0x18(%rsp)
movq %rbx, 0x40(%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(%rdx), %rax
movq %rbp, %rcx
shrq $0x21, %rcx
movl (%rax,%rcx,8), %eax
addl %r12d, %eax
movl %eax, 0x3c(%rsp)
cmpl %esi, %eax
jbe 0x1a0d9a
cmpl %r12d, 0x30(%rsp)
jbe 0x1a1002
testq %r15, %r15
setns %cl
andb 0xf(%rsp), %cl
cmpb $0x1, %cl
jne 0x1a0c5c
movq 0x28(%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 %r12d, %esi
jae 0x1a0c60
cmpl %eax, %esi
jae 0x1a0c60
leaq -0x1(%r15), %rsi
addq $-0x18, %rdx
testq %r15, %r15
movq %rsi, %r15
jg 0x1a0c36
movq $-0x1, %r15
xorl %eax, %eax
jmp 0x1a0c6d
cmpl %eax, %esi
setb %cl
cmpl %r12d, %esi
setae %al
andb %cl, %al
testb %al, %al
je 0x1a0d57
cmpq $0x0, 0xa8(%rsp)
je 0x1a0d8e
movq 0x28(%rsp), %rdi
leaq 0x34(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r9, %rcx
movsd 0x58(%rsp), %xmm0
movq 0xa0(%rsp), %r8
movq %r9, %rbx
movq 0xb8(%rsp), %r9
callq 0x1a005a
testq %rax, %rax
je 0x1a0db2
movq (%rbx), %rax
movq 0x8(%rbx), %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
je 0x1a0ce7
incq (%rax)
incq 0x20(%rsp)
movq 0x28(%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 0x48(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq %r15, %rdx
leaq 0x34(%rsp), %r8
callq 0x1a052d
movq 0x50(%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, %edi       # imm = 0xC0000000
andl %edi, %edx
orl %eax, %edx
movl %edx, -0x4(%rcx,%rsi)
jmp 0x1a0d8e
movq 0x28(%rsp), %rdi
leaq 0x34(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r9, %rcx
movsd 0x58(%rsp), %xmm0
movq 0xa0(%rsp), %r8
movq 0xb8(%rsp), %r9
callq 0x1a005a
movq 0x50(%rsp), %r9
addq %rax, 0x20(%rsp)
movq 0x18(%rsp), %rsi
leaq 0xea496(%rip), %rdx     # 0x28b230
addq %rbp, %r12
incq %r13
movl 0x10(%r14), %eax
cmpq %rax, %r13
jb 0x1a0bb8
jmp 0x1a1002
movq %rbx, %r9
jmp 0x1a0d8e
cmpl $0x0, 0x10(%r14)
je 0x1a0ff9
movq 0x10(%rsp), %rax
movl 0x14(%rax), %edx
movl 0x18(%rax), %eax
incl %eax
movl %eax, 0x30(%rsp)
movq 0x48(%rsp), %rcx
testq %rcx, %rcx
setne %al
testq %rbx, %rbx
setne %sil
andb %al, %sil
movb %sil, 0xf(%rsp)
leaq 0x10(%rcx), %rax
movq %rax, 0x60(%rsp)
movq $0x0, 0x20(%rsp)
leaq 0xea42a(%rip), %rsi     # 0x28b230
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %rdx, 0x18(%rsp)
movq %rbx, 0x40(%rsp)
movq 0x18(%r14), %rax
movq (%rax,%r15,8), %r12
movq %r12, %rax
shrq $0x20, %rax
movl %eax, 0x34(%rsp)
movl %r13d, 0x38(%rsp)
movq 0x18(%rsi), %rax
movq %r12, %rcx
shrq $0x21, %rcx
movl (%rax,%rcx,8), %eax
addl %r13d, %eax
movl %eax, 0x3c(%rsp)
cmpl %edx, %eax
jbe 0x1a0fda
cmpl %r13d, 0x30(%rsp)
jbe 0x1a1002
cmpq %rbx, %rbp
setl %cl
andb 0xf(%rsp), %cl
cmpb $0x1, %cl
jne 0x1a0e99
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
movq 0x60(%rsp), %rdx
leaq (%rdx,%rcx,8), %rcx
movl (%rcx), %edx
cmpl %r13d, %edx
jae 0x1a0e9d
cmpl %eax, %edx
jae 0x1a0e9d
incq %rbp
addq $0x18, %rcx
cmpq %rbp, %rbx
jne 0x1a0e7b
xorl %eax, %eax
movq %rbx, %rbp
jmp 0x1a0eaa
xorl %eax, %eax
jmp 0x1a0eaa
cmpl %eax, %edx
setb %cl
cmpl %r13d, %edx
setae %al
andb %cl, %al
testb %al, %al
je 0x1a0f97
cmpq $0x0, 0xa8(%rsp)
je 0x1a0fce
movq 0x28(%rsp), %rdi
leaq 0x34(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r9, %rcx
movsd 0x58(%rsp), %xmm0
movq 0xa0(%rsp), %r8
movq %r9, %rbx
movq 0xb8(%rsp), %r9
callq 0x1a005a
testq %rax, %rax
je 0x1a0fef
movq (%rbx), %rax
movq 0x8(%rbx), %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
je 0x1a0f24
incq (%rax)
incq 0x20(%rsp)
movq 0x28(%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 0x48(%rsp), %rdi
movq 0x40(%rsp), %rbx
movq %rbx, %rsi
movq %rbp, %rdx
leaq 0x34(%rsp), %r8
callq 0x1a052d
movq 0x50(%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, %edi       # imm = 0xC0000000
andl %edi, %edx
orl %eax, %edx
movl %edx, -0x4(%rcx,%rsi)
jmp 0x1a0fce
movq 0x28(%rsp), %rdi
leaq 0x34(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r9, %rcx
movsd 0x58(%rsp), %xmm0
movq 0xa0(%rsp), %r8
movq 0xb8(%rsp), %r9
callq 0x1a005a
movq 0x50(%rsp), %r9
addq %rax, 0x20(%rsp)
movq 0x18(%rsp), %rdx
leaq 0xea256(%rip), %rsi     # 0x28b230
addq %r12, %r13
incq %r15
movl 0x10(%r14), %eax
cmpq %rax, %r15
jb 0x1a0e18
jmp 0x1a1002
movq %rbx, %r9
movq 0x40(%rsp), %rbx
jmp 0x1a0fce
movq $0x0, 0x20(%rsp)
movq 0x20(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq