// File: /workspace/github_cpp_makefile_star2_O2/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, %r12
movq %rsi, %r15
movq 0xb0(%rsp), %r9
andq $0x0, 0x8(%rcx)
andq $0x0, 0x20(%rcx)
movq 0x20(%r8), %rax
movq %rax, 0x20(%rsp)
xorl %r14d, %r14d
movq %rdi, 0x8(%rsp)
movq %r8, 0x10(%rsp)
movq %r8, %rsi
movq %rcx, %rdx
xorl %ecx, %ecx
movq %r12, %r8
callq 0x1604c4
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x162ebc
cmpq %rax, 0x28(%rbx)
jae 0x162a70
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
incq %rcx
movq %rcx, 0x28(%rbx)
movq 0x18(%rbx), %rsi
imulq $0x1c, %rcx, %rdx
movq 0xb0(%rsp), %rdi
callq 0x1a3acc
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rax
cmpq %rax, 0x50(%rbx)
jae 0x162ad3
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 0xb0(%rsp), %rdi
callq 0x1a3acc
movq %rax, 0x58(%rbx)
movq 0x8(%rbx), %rax
movq 0xa0(%rsp), %r14
leaq 0x18(%rbx), %rcx
movq %rcx, 0x18(%rsp)
cmpq %rax, 0x240(%r12)
jae 0x162b55
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(%r12)
movq 0x248(%r12), %rsi
leaq 0x8(,%rcx,8), %rdx
movq 0xb0(%rsp), %rdi
callq 0x1a3acc
movq %rax, 0x248(%r12)
jmp 0x162b5d
movq 0x248(%r12), %rax
movq 0x28(%r15), %r9
subq $0x10, %rsp
pushq $0x10
popq %rcx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
movsd 0x48(%rsp), %xmm0
movq %r14, %r8
pushq %rax
pushq 0x58(%rbx)
callq 0x16087f
addq $0x20, %rsp
movq 0x20(%rbx), %rcx
testq %rax, %rax
je 0x162e80
movq %rcx, 0x48(%rsp)
movq %r12, 0x58(%rsp)
leaq 0x28(%rsp), %rax
andq $0x0, (%rax)
leaq 0x40(%rsp), %rax
andq $0x0, (%rax)
movq (%rbx), %rdi
imulq $0x1c, 0x8(%rbx), %rsi
addq %rdi, %rsi
callq 0x1571fa
xorl %r13d, %r13d
pushq $0x8
popq %r14
movq $0x0, 0x30(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsi), %r13
jae 0x162e26
movq (%rsi), %rax
imulq $0xa8, %r13, %rbp
cmpb $0x2, 0x2c(%rax,%rbp)
jne 0x162e1e
leaq (%rax,%rbp), %rdx
movl 0x4(%rdx), %ecx
movl 0x8(%rdx), %edx
incl %edx
movq 0x8(%rbx), %r11
addq $0x32, %rcx
leaq 0x32(%rdx), %rdi
incq %r11
movq %r14, %r8
movb $0x1, %r12b
decq %r11
je 0x162c45
movq (%rbx), %r10
movl -0x4(%r10,%r8), %r9d
cmpq %r9, %rcx
jb 0x162c3c
movl (%r10,%r8), %r10d
addq $0x32, %r10
cmpq %rdx, %r10
jae 0x162dce
addq $0x1c, %r8
cmpq %r9, %rdi
ja 0x162c16
xorl %ecx, %ecx
movq 0x20(%rsp), %rdx
movq 0x10(%rdx), %rdx
movl 0x10(%rax,%rbp), %eax
movl (%rdx,%rax,8), %r15d
testb %cl, %cl
je 0x162c65
testq %r15, %r15
je 0x162e1e
movq 0x58(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
leaq 0x28(%rsp), %rdx
leaq 0x40(%rsp), %rcx
movq %r15, %r8
callq 0x160309
movq %rax, %r9
testq %r15, %r15
sete %al
testq %r9, %r9
sete %cl
orb %al, %cl
movq %r9, 0x60(%rsp)
jne 0x162cf7
imulq $0x18, 0x28(%rsp), %rdx
addq (%r14), %rdx
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq 0x20(%rsp), %rdi
movq 0x10(%rdi), %rax
movl 0x10(%rsi,%rbp), %ecx
addq %rbp, %rsi
movl 0x4(%rax,%rcx,8), %eax
imulq $0xc, %rax, %r8
addq 0x28(%rdi), %r8
movq 0x10(%rsp), %rdi
movq %r9, %rcx
movq %r15, %r9
movsd 0x38(%rsp), %xmm0
pushq 0xb0(%rsp)
pushq $0x0
pushq 0x60(%rsp)
pushq 0x30(%rsp)
callq 0x161567
addq $0x20, %rsp
addq %rax, 0x30(%rsp)
testb %r12b, %r12b
movq 0x8(%rsp), %rsi
pushq $0x8
popq %r14
je 0x162e14
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
cmpq 0x28(%rbx), %rcx
jne 0x162d4a
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x28(%rbx)
imulq $0x1c, %rdx, %rdx
movq 0xb0(%rsp), %rdi
movq %rax, %rsi
callq 0x1a3acc
movq 0x8(%rsp), %rsi
movq %rax, 0x18(%rbx)
movq 0x20(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%rbx)
imulq $0x1c, %rcx, %r9
leaq (%rax,%r9), %rcx
movl %r13d, (%rax,%r9)
movq (%rsi), %rdx
movl 0x4(%rdx,%rbp), %edi
movl %edi, 0x4(%rax,%r9)
movl 0x8(%rdx,%rbp), %edi
incl %edi
movl %edi, 0x8(%rax,%r9)
movl 0x10(%rdx,%rbp), %edx
movl %edx, 0xc(%rax,%r9)
movq 0x60(%rsp), %r10
andl $0x3fffffff, %r10d      # imm = 0x3FFFFFFF
movl 0x18(%rax,%r9), %edi
movl $0x80000000, %r8d       # imm = 0x80000000
andl %r8d, %edi
orl %r10d, %edi
movl %edi, 0x18(%rax,%r9)
movq (%rsi), %rdi
movl 0x1c(%rdi,%rbp), %edi
movl %edi, %r8d
shll $0x1f, %r8d
orl %r10d, %r8d
movl %r8d, 0x18(%rax,%r9)
testb $0x1, %dil
jne 0x162dd8
movq (%rsi), %rax
movl 0x14(%rax,%rbp), %edx
movl %edx, 0x10(%rcx)
movl 0x18(%rax,%rbp), %eax
incl %eax
jmp 0x162e11
movb $0x1, %cl
xorl %r12d, %r12d
jmp 0x162c47
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
imulq $0x28, %rdx, %rdx
movl (%rax,%rdx), %r9d
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
andl %r10d, %r9d
movq (%rsi), %rdi
movl 0x18(%rdi,%rbp), %r8d
notl %r8d
addl %r9d, %r8d
movl %r8d, 0x10(%rcx)
movl (%rax,%rdx), %eax
andl %r10d, %eax
subl 0x14(%rdi,%rbp), %eax
movl %eax, 0x14(%rcx)
movq 0x40(%rsp), %rax
movq %rax, 0x28(%rsp)
incq %r13
jmp 0x162bd9
movq 0x20(%rbx), %rax
movq 0x30(%rsp), %r15
testq %r15, %r15
je 0x162e7b
movq %r15, %r14
negq %r14
movq 0x18(%rbx), %rcx
imulq $0x1c, 0x48(%rsp), %rdi
addq %rcx, %rdi
imulq $0x1c, %rax, %rsi
addq %rcx, %rsi
callq 0x1574e1
imulq $0x1c, 0x20(%rbx), %rax
addq 0x18(%rbx), %rax
imulq $0x1c, %r14, %rdi
addq %rax, %rdi
movq %r15, %rsi
callq 0x161e4a
movq 0x8(%rsp), %rsi
subq %r15, %rax
addq 0x20(%rbx), %rax
movq %rax, 0x20(%rbx)
movq %rax, %rcx
jmp 0x162e85
movq 0x8(%rsp), %rsi
testq %rcx, %rcx
je 0x162eb3
movq 0xa8(%rsp), %r9
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
movq %rbx, %rcx
pushq 0xb0(%rsp)
pushq 0x28(%rsp)
callq 0x162471
popq %rcx
popq %rdx
jmp 0x162eb8
andq $0x0, 0x8(%rbx)
pushq $0x1
popq %r14
movq %r14, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq