// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: gl_chain_refine(overlap_region_alloc*, Correct_dumy*, haplotype_evdience_alloc*, glchain_t*, ul_idx_t const*, double, long, long, ug_opt_t const*, void*)
int64_t gl_chain_refine(overlap_region_alloc* olist, Correct_dumy* dumy, haplotype_evdience_alloc *hap, glchain_t *ll, const ul_idx_t *uref, double diff_ec_ul, int64_t winLen, int64_t qlen, const ug_opt_t *uopt, void *km)
{
	ll->tk.n = ll->lo.n = 0;
	kv_ul_ov_t *idx = &(ll->lo);
	ul_contain *ct = uref->ct;
	gl_chain_gen(olist, uref, idx, 0, hap, km);
	if(idx->n == 0) return 0;
	kv_resize_km(km, ul_ov_t, ll->tk, idx->n);
	kv_resize_km(km, uint64_t, ll->srt.a, idx->n);
	kv_resize_km(km, uint64_t, hap->snp_srt, idx->n);
 	if(gl_exact_chain(idx, &(ll->tk), uref, G_CHAIN_BW, diff_ec_ul, qlen, dumy->overlapID, ll->srt.a.a, hap->snp_srt.a, km)) {
		kv_ul_ov_t *chains = &(ll->tk); ul_ov_t *p = NULL; 
		uint64_t k, z, ff, s, e, an, cn, sft = 50, si = 0, ei = 0, resc = 0, chains_pl = chains->n;
		radix_sort_ul_ov_srt_qs(idx->a, idx->a + idx->n);
		for (k = 0; k < olist->length; k++) {
			if(olist->list[k].is_match!=2) continue;

			s = olist->list[k].x_pos_s; e = olist->list[k].x_pos_e+1;
			for (z = ff = 0; z < idx->n; z++) {
				if((s+sft) >= idx->a[z].qs && e <= (idx->a[z].qe+sft)) {
					ff = 1;
					break;
				}
				if(idx->a[z].qs >= (e+sft)) break;
			}
			cn = ((uint32_t)(ct->idx.a[olist->list[k].y_id]));
			if(ff && cn==0) continue;
			an = update_ava_het_site(hap, k, &si, &ei, cn);
			// if(an != get_het_site(hap, k)) fprintf(stderr, "an->%lu, get_het_site->%lu\n", an, get_het_site(hap, k));
			
			if(cn > 0 && an > 0) {
				resc += rescue_contain_ul_chains(uref, &(olist->list[k]), hap->list+si, an, 
				ct->rids.a + ((ct->idx.a[olist->list[k].y_id])>>32), cn, chains, diff_ec_ul, winLen, 0, km);
			}

			if(ff == 0) {
				kv_pushp_km(km, ul_ov_t, *chains, &p);
				p->qn = k/**olist->list[k].x_id**/; p->qs = olist->list[k].x_pos_s; p->qe = olist->list[k].x_pos_e+1;
				p->tn = olist->list[k].y_id; p->el = 0; p->sec = (an&((uint64_t)0x3FFFFFFF))/**get_het_site(hap, k)**/; 
				p->rev = olist->list[k].y_pos_strand;
				if(p->rev) {
					p->ts = uref->ug->u.a[p->tn].len - (olist->list[k].y_pos_e+1); 
					p->te = uref->ug->u.a[p->tn].len - olist->list[k].y_pos_s;
				} else {
					p->ts = olist->list[k].y_pos_s; 
					p->te = olist->list[k].y_pos_e+1;
				}
				
			}
			si = ei;
		}

		if(resc > 0) {
			radix_sort_ul_ov_srt_tn(chains->a + chains_pl, chains->a + chains->n);
			ff = dedup_sort_ul_ov_t(chains->a + chains->n - resc, resc);
			chains->n = chains->n - resc + ff;
		}
	}

	if(ll->tk.n > 0) infer_read_ovlp(uref, olist, &(ll->tk), &(ll->lo), diff_ec_ul, winLen, uopt, ct, km);
	else ll->lo.n = 0;
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x50(%rsp)
movsd %xmm0, 0x38(%rsp)
movq %rcx, %rbx
movq %rdx, %rbp
movq %rsi, %r15
movq %rdi, %r12
movq 0xb0(%rsp), %r13
xorl %r14d, %r14d
movq %r14, 0x8(%rcx)
movq %r14, 0x20(%rcx)
movq 0x20(%r8), %rax
movq %rax, 0x20(%rsp)
movq %r8, 0x8(%rsp)
movq %r8, %rsi
movq %rcx, %rdx
xorl %ecx, %ecx
movq %rbp, %r8
movq %r13, %r9
callq 0x19f426
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x1a21a8
cmpq %rax, 0x28(%rbx)
jae 0x1a1d19
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x28(%rbx)
movq 0x18(%rbx), %rsi
leaq (%rax,%rax,8), %rax
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax), %rdx
incq %rdx
movq %r13, %rdi
callq 0x1e3800
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rax
cmpq %rax, 0x50(%rbx)
jae 0x1a1d77
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x50(%rbx)
movq 0x58(%rbx), %rsi
leaq 0x8(,%rcx,8), %rdx
movq %r13, %rdi
callq 0x1e3800
movq %rax, 0x58(%rbx)
movq 0xa0(%rsp), %r14
leaq 0x18(%rbx), %rax
movq %rax, 0x18(%rsp)
movq 0x8(%rbx), %rax
cmpq %rax, 0x240(%rbp)
jae 0x1a1df2
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x240(%rbp)
movq 0x248(%rbp), %rsi
leaq 0x8(,%rcx,8), %rdx
movq %r13, %rdi
callq 0x1e3800
movq %rax, 0x248(%rbp)
movq 0x28(%r15), %r9
subq $0x10, %rsp
movl $0x10, %ecx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdx
movsd 0x48(%rsp), %xmm0
movq %r14, %r8
pushq 0x248(%rbp)
pushq 0x58(%rbx)
callq 0x19f800
addq $0x20, %rsp
testq %rax, %rax
je 0x1a216a
movq %rbp, 0x60(%rsp)
xorl %r14d, %r14d
movq %r14, 0x28(%rsp)
movq %r14, 0x40(%rsp)
movq 0x20(%rbx), %rax
movq %rax, 0x48(%rsp)
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
leaq (%rsi,%rsi,8), %rax
leaq (%rax,%rax,2), %rax
addq %rdi, %rsi
addq %rax, %rsi
callq 0x1957bd
movq $0x0, 0x10(%rsp)
cmpq %r14, 0x10(%r12)
je 0x1a20f4
movq $0x0, 0x10(%rsp)
xorl %r13d, %r13d
movq %rbx, 0x58(%rsp)
movq (%r12), %rax
imulq $0xa8, %r13, %rbp
cmpb $0x2, 0x2c(%rax,%rbp)
jne 0x1a20e6
movq 0x8(%rbx), %rcx
movb $0x1, %r14b
testq %rcx, %rcx
je 0x1a1efe
leaq (%rax,%rbp), %rsi
movl 0x8(%rsi), %edx
incl %edx
movl 0x4(%rsi), %esi
addq $0x32, %rsi
movq (%rbx), %r8
leaq 0x32(%rdx), %r9
addq $0x4, %r8
movl $0x1, %r10d
movl (%r8), %edi
cmpq %rdi, %rsi
jb 0x1a1ee2
movl 0x4(%r8), %edi
addq $0x32, %rdi
cmpq %rdx, %rdi
jae 0x1a1f02
movl (%r8), %r11d
xorl %edi, %edi
cmpq %r11, %r9
jbe 0x1a1f08
leaq 0x1(%r10), %r11
addq $0x1c, %r8
cmpq %rcx, %r10
movq %r11, %r10
jb 0x1a1ecd
jmp 0x1a1f08
xorl %edi, %edi
jmp 0x1a1f08
movb $0x1, %dil
xorl %r14d, %r14d
movq 0x20(%rsp), %rcx
movq 0x10(%rcx), %rcx
movl 0x10(%rax,%rbp), %eax
movl (%rcx,%rax,8), %r15d
testb %dil, %dil
je 0x1a1f27
testq %r15, %r15
je 0x1a20e6
movl %r14d, 0x34(%rsp)
movq %r12, %rbx
movq 0x60(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
leaq 0x28(%rsp), %rdx
leaq 0x40(%rsp), %rcx
movq %r15, %r8
callq 0x19f226
movq %rax, %r14
testq %r15, %r15
sete %al
testq %r14, %r14
sete %cl
orb %al, %cl
jne 0x1a1fc3
movq (%rbx), %rsi
movq 0x28(%rsp), %rax
leaq (%rax,%rax,2), %rdx
shlq $0x3, %rdx
addq (%r12), %rdx
movq 0x20(%rsp), %rdi
movq 0x10(%rdi), %rax
movl 0x10(%rsi,%rbp), %ecx
addq %rbp, %rsi
movl 0x4(%rax,%rcx,8), %eax
leaq (%rax,%rax,2), %r8
shlq $0x2, %r8
addq 0x28(%rdi), %r8
movq 0x8(%rsp), %rdi
movq %r14, %rcx
movq %r15, %r9
movsd 0x38(%rsp), %xmm0
pushq 0xb0(%rsp)
pushq $0x0
pushq 0x60(%rsp)
pushq 0x30(%rsp)
callq 0x1a0658
addq $0x20, %rsp
addq %rax, 0x10(%rsp)
cmpb $0x0, 0x34(%rsp)
movq %rbx, %r12
movq 0x58(%rsp), %rbx
je 0x1a20dc
movq 0x28(%rbx), %rax
cmpq %rax, 0x20(%rbx)
jne 0x1a2014
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x28(%rbx)
movq 0x18(%rbx), %rsi
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rdx
addq %rcx, %rdx
movq 0xb0(%rsp), %rdi
callq 0x1e3800
movq %rax, 0x18(%rbx)
movq 0x18(%rbx), %rcx
movq 0x20(%rbx), %rax
leaq 0x1(%rax), %rdx
movq %rdx, 0x20(%rbx)
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
leaq (%rcx,%rdx), %rax
movl %r13d, (%rcx,%rdx)
movq (%r12), %rsi
movl 0x4(%rsi,%rbp), %edi
movl %edi, 0x4(%rcx,%rdx)
movl 0x8(%rsi,%rbp), %edi
incl %edi
movl %edi, 0x8(%rcx,%rdx)
movl 0x10(%rsi,%rbp), %esi
movl %esi, 0xc(%rcx,%rdx)
andl $0x3fffffff, %r14d      # imm = 0x3FFFFFFF
movl 0x18(%rcx,%rdx), %edi
movl $0x80000000, %r8d       # imm = 0x80000000
andl %r8d, %edi
orl %r14d, %edi
movl %edi, 0x18(%rcx,%rdx)
movq (%r12), %rdi
movl 0x1c(%rdi,%rbp), %edi
movl %edi, %r8d
shll $0x1f, %r8d
orl %r14d, %r8d
movl %r8d, 0x18(%rcx,%rdx)
testb $0x1, %dil
jne 0x1a20a0
movq (%r12), %rcx
movl 0x14(%rcx,%rbp), %edx
movl %edx, 0x10(%rax)
movl 0x18(%rcx,%rbp), %ecx
incl %ecx
jmp 0x1a20d9
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
leaq (%rsi,%rsi,4), %rdx
movl (%rcx,%rdx,8), %esi
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
andl %r9d, %esi
movq (%r12), %rdi
movl 0x18(%rdi,%rbp), %r8d
notl %r8d
addl %esi, %r8d
movl %r8d, 0x10(%rax)
movl (%rcx,%rdx,8), %ecx
andl %r9d, %ecx
subl 0x14(%rdi,%rbp), %ecx
movl %ecx, 0x14(%rax)
movq 0x40(%rsp), %rax
movq %rax, 0x28(%rsp)
incq %r13
cmpq 0x10(%r12), %r13
jb 0x1a1e8a
movq 0x10(%rsp), %r15
testq %r15, %r15
je 0x1a2162
movq %r15, %r14
negq %r14
movq 0x48(%rsp), %rcx
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rdi
addq %rcx, %rdi
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rax
addq %rsi, %rdi
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rax, %rsi
addq %rcx, %rsi
callq 0x195abc
movq 0x20(%rbx), %rax
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rax, %rcx
addq 0x18(%rbx), %rcx
leaq (%r14,%r14,8), %rax
leaq (%rax,%rax,2), %rdi
subq %r15, %rdi
addq %rcx, %rdi
movq %r15, %rsi
callq 0x1a1016
subq %r15, %rax
addq %rax, 0x20(%rbx)
movq 0xb0(%rsp), %r13
cmpq $0x0, 0x20(%rbx)
je 0x1a219a
movq 0xa8(%rsp), %r9
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movq 0x18(%rsp), %rdx
movq %rbx, %rcx
pushq %r13
pushq 0x28(%rsp)
callq 0x1a16b3
addq $0x10, %rsp
jmp 0x1a21a2
movq $0x0, 0x8(%rbx)
movl $0x1, %r14d
movq %r14, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq