// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movsd %xmm0, 0x40(%rsp)
movq %r8, %r14
movq %rsi, %r8
movq %rdi, 0x18(%rsp)
movq 0xa0(%rsp), %rax
testq %rax, %rax
je 0x1619d7
andq $0x0, (%rax)
cmpl $0x0, 0x1c(%r8)
movq %r9, 0x10(%rsp)
movq %rcx, 0x38(%rsp)
movq %r8, 0x8(%rsp)
movq %rdx, 0x50(%rsp)
je 0x161c30
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movl 0x10(%r8), %esi
movl 0x18(%r8), %ebp
imulq $0x28, %rsi, %rsi
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl (%rax,%rsi), %eax
andl %edi, %eax
notl %ebp
addl %eax, %ebp
subl 0x14(%r8), %eax
movl %eax, 0x20(%rsp)
movq %rcx, %r15
addq $-0x1, %r15
setb %al
testq %rdx, %rdx
setne %cl
andb %al, %cl
movb %cl, 0x7(%rsp)
leaq 0x10(%rdx), %rax
movq %rax, 0x48(%rsp)
xorl %r13d, %r13d
leaq 0xf27e3(%rip), %rdi     # 0x254230
xorl %r12d, %r12d
movq $0x0, 0x30(%rsp)
movl 0x10(%r14), %eax
cmpq %rax, %r13
jae 0x161e36
movq 0x18(%r14), %rax
movq (%rax,%r13,8), %rbx
movq %rbx, %rax
shrq $0x20, %rax
movl %eax, 0x24(%rsp)
movl %r12d, 0x28(%rsp)
movq 0x18(%rdi), %rax
movq %rbx, %rcx
shrq $0x21, %rcx
movl (%rax,%rcx,8), %eax
addl %r12d, %eax
movl %eax, 0x2c(%rsp)
cmpl %ebp, %eax
jbe 0x161b44
cmpl %r12d, 0x20(%rsp)
jbe 0x161e36
testq %r15, %r15
setns %cl
andb 0x7(%rsp), %cl
cmpb $0x1, %cl
jne 0x161b03
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
movl 0x10(%r8), %edx
imulq $0x28, %rdx, %rdx
movl (%rcx,%rdx), %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %ecx
imulq $0x18, %r15, %rdx
addq 0x48(%rsp), %rdx
movl (%rdx), %esi
notl %esi
addl %ecx, %esi
cmpl %r12d, %esi
jb 0x161aeb
cmpl %eax, %esi
jb 0x161b4f
cmpl %eax, %esi
jae 0x161b03
leaq -0x1(%r15), %rsi
addq $-0x18, %rdx
testq %r15, %r15
movq %rsi, %r15
jg 0x161adc
pushq $-0x1
popq %r15
movq 0x18(%rsp), %rdi
leaq 0x24(%rsp), %rsi
movq %r8, %rdx
movq %r9, %rcx
movsd 0x40(%rsp), %xmm0
movq 0x90(%rsp), %r8
movq 0xa8(%rsp), %r9
callq 0x161004
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r9
addq %rax, 0x30(%rsp)
leaq 0xf26ec(%rip), %rdi     # 0x254230
addq %rbx, %r12
incq %r13
jmp 0x161a59
cmpq $0x0, 0x98(%rsp)
je 0x161b44
movq 0x18(%rsp), %rdi
leaq 0x24(%rsp), %rsi
movq %r8, %rdx
movq %r9, %rcx
movsd 0x40(%rsp), %xmm0
movq 0x90(%rsp), %r8
movq 0xa8(%rsp), %r9
callq 0x161004
testq %rax, %rax
je 0x161c21
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
imulq $0x1c, 0x8(%rcx), %rcx
andl $0xbfffffff, -0x4(%rax,%rcx) # imm = 0xBFFFFFFF
movq 0xa0(%rsp), %rax
testq %rax, %rax
movq 0x38(%rsp), %rsi
je 0x161bb8
incq (%rax)
incq 0x30(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x8(%rsp), %rdx
movl 0x10(%rdx), %ecx
movl 0x1c(%rdx), %r9d
imulq $0x28, %rcx, %rcx
movl (%rax,%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x50(%rsp), %rdi
movq %r15, %rdx
leaq 0x24(%rsp), %r8
callq 0x16146a
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r9
movq (%r9), %rcx
imulq $0x1c, 0x8(%r9), %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)
jmp 0x161b3d
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
jmp 0x161b3d
movl 0x14(%r8), %r13d
movl 0x18(%r8), %eax
incl %eax
movl %eax, 0x20(%rsp)
testq %rdx, %rdx
setne %al
testq %rcx, %rcx
setne %sil
andb %al, %sil
movb %sil, 0x7(%rsp)
leaq 0x10(%rdx), %rax
movq %rax, 0x48(%rsp)
xorl %r12d, %r12d
leaq 0xf25ca(%rip), %r10     # 0x254230
xorl %ebp, %ebp
xorl %r15d, %r15d
movq $0x0, 0x30(%rsp)
movl 0x10(%r14), %eax
cmpq %rax, %r12
jae 0x161e36
movq 0x18(%r14), %rax
movq (%rax,%r12,8), %rbx
movq %rbx, %rax
shrq $0x20, %rax
movl %eax, 0x24(%rsp)
movl %ebp, 0x28(%rsp)
movq 0x18(%r10), %rax
movq %rbx, %rdx
shrq $0x21, %rdx
movl (%rax,%rdx,8), %eax
addl %ebp, %eax
movl %eax, 0x2c(%rsp)
cmpl %r13d, %eax
jbe 0x161d3e
cmpl %ebp, 0x20(%rsp)
jbe 0x161e36
cmpb $0x0, 0x7(%rsp)
je 0x161cf8
cmpq %rcx, %r15
movq %rcx, %rdi
cmovgq %r15, %rdi
imulq $0x18, %r15, %rdx
addq 0x48(%rsp), %rdx
cmpq %rcx, %r15
jge 0x161cf5
movl (%rdx), %esi
cmpl %ebp, %esi
jb 0x161ce8
cmpl %eax, %esi
jb 0x161d49
cmpl %eax, %esi
jae 0x161cf8
incq %r15
addq $0x18, %rdx
jmp 0x161cd9
movq %rdi, %r15
movq 0x18(%rsp), %rdi
leaq 0x24(%rsp), %rsi
movq %r8, %rdx
movq %r9, %rcx
movsd 0x40(%rsp), %xmm0
movq 0x90(%rsp), %r8
movq 0xa8(%rsp), %r9
callq 0x161004
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r9
addq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rcx
leaq 0xf24f2(%rip), %r10     # 0x254230
addq %rbx, %rbp
incq %r12
jmp 0x161c74
cmpq $0x0, 0x98(%rsp)
je 0x161d3e
movq 0x18(%rsp), %rdi
leaq 0x24(%rsp), %rsi
movq %r8, %rdx
movq %r9, %rcx
movsd 0x40(%rsp), %xmm0
movq 0x90(%rsp), %r8
movq 0xa8(%rsp), %r9
callq 0x161004
testq %rax, %rax
je 0x161e22
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
imulq $0x1c, 0x8(%rcx), %rcx
andl $0xbfffffff, -0x4(%rax,%rcx) # imm = 0xBFFFFFFF
movq 0xa0(%rsp), %rax
testq %rax, %rax
movq 0x38(%rsp), %rsi
je 0x161db2
incq (%rax)
incq 0x30(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x8(%rsp), %rdx
movl 0x10(%rdx), %ecx
movl 0x1c(%rdx), %r9d
imulq $0x28, %rcx, %rcx
movl (%rax,%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x50(%rsp), %rdi
movq %r15, %rdx
leaq 0x24(%rsp), %r8
callq 0x16146a
movq 0x8(%rsp), %r8
movq 0x38(%rsp), %rcx
movq 0x10(%rsp), %r9
movq (%r9), %rdi
imulq $0x1c, 0x8(%r9), %rdx
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movl -0x4(%rdi,%rdx), %esi
movl $0xc0000000, %r10d      # imm = 0xC0000000
andl %r10d, %esi
orl %eax, %esi
movl %esi, -0x4(%rdi,%rdx)
jmp 0x161d37
movq 0x10(%rsp), %r9
movq 0x38(%rsp), %rcx
movq 0x8(%rsp), %r8
jmp 0x161d37
movq 0x30(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq