// File: /workspace/github_cpp_makefile_star2_O3/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, 0x58(%rsp)
movsd %xmm0, 0x40(%rsp)
movq %r8, %r12
movq %rcx, %rbx
movq %rdx, %r8
movq %rsi, %r15
movq 0xb0(%rsp), %rbp
xorl %r14d, %r14d
movq %r14, 0x8(%rcx)
movq %r14, 0x20(%rcx)
movq 0x20(%r12), %rax
movq %rax, 0x28(%rsp)
movq %rdi, 0x8(%rsp)
movq %r12, %rsi
movq %rcx, %rdx
xorl %ecx, %ecx
movq %r8, 0x20(%rsp)
movq %rbp, %r9
callq 0x1a8167
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x1ab122
cmpq %rax, 0x28(%rbx)
jae 0x1aac7d
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 %rbp, %rdi
callq 0x1ee21d
movq %rax, 0x18(%rbx)
movq 0x8(%rbx), %rax
cmpq %rax, 0x50(%rbx)
jae 0x1aacdb
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 %rbp, %rdi
callq 0x1ee21d
movq %rax, 0x58(%rbx)
movq 0x8(%rbx), %rax
movq 0x20(%rsp), %rdx
movq 0xa0(%rsp), %r13
leaq 0x18(%rbx), %rcx
movq %rcx, 0x18(%rsp)
cmpq %rax, 0x240(%rdx)
jae 0x1aad5c
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(%rdx)
movq 0x248(%rdx), %rsi
movq %rdx, %r14
leaq 0x8(,%rcx,8), %rdx
movq %rbp, %rdi
callq 0x1ee21d
movq %rax, 0x248(%r14)
jmp 0x1aad63
movq 0x248(%rdx), %rax
movq 0x28(%r15), %r9
subq $0x10, %rsp
movl $0x10, %ecx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r12, %rdx
movsd 0x50(%rsp), %xmm0
movq %r13, %r8
pushq %rax
pushq 0x58(%rbx)
callq 0x1a853c
addq $0x20, %rsp
movq 0x20(%rbx), %r14
testq %rax, %rax
je 0x1ab0db
xorl %eax, %eax
movq %rax, 0x30(%rsp)
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 0x19e55c
movq 0x8(%rsp), %r15
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x1ab0e2
movq %r14, 0x50(%rsp)
movq %r12, 0x10(%rsp)
movq $0x0, 0x38(%rsp)
xorl %r13d, %r13d
movq %rbx, 0x60(%rsp)
movq (%r15), %rcx
imulq $0xa8, %r13, %rbp
cmpb $0x2, 0x2c(%rcx,%rbp)
jne 0x1ab04e
movq 0x8(%rbx), %rdx
movb $0x1, %r12b
testq %rdx, %rdx
je 0x1aae62
leaq (%rcx,%rbp), %rdi
movl 0x8(%rdi), %esi
incl %esi
movl 0x4(%rdi), %edi
addq $0x32, %rdi
movq (%rbx), %r9
leaq 0x32(%rsi), %r10
addq $0x8, %r9
movl $0x1, %r11d
movl -0x4(%r9), %r14d
cmpq %r14, %rdi
jb 0x1aae48
movl (%r9), %r8d
addq $0x32, %r8
cmpq %rsi, %r8
jae 0x1aae67
xorl %r8d, %r8d
cmpq %r14, %r10
jbe 0x1aae6d
leaq 0x1(%r11), %r14
addq $0x1c, %r9
cmpq %rdx, %r11
movq %r14, %r11
jb 0x1aae33
jmp 0x1aae6d
xorl %r8d, %r8d
jmp 0x1aae6d
movb $0x1, %r8b
xorl %r12d, %r12d
movq 0x28(%rsp), %rdx
movq 0x10(%rdx), %rdx
movl 0x10(%rcx,%rbp), %ecx
movl (%rdx,%rcx,8), %r14d
testb %r8b, %r8b
je 0x1aae8c
testq %r14, %r14
je 0x1ab04e
movl %r12d, %ebx
movq 0x20(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
leaq 0x30(%rsp), %rdx
leaq 0x48(%rsp), %rcx
movq %r14, %r8
callq 0x1a7f4d
movq %rax, %r15
testq %r14, %r14
sete %al
testq %r15, %r15
sete %cl
orb %al, %cl
jne 0x1aaf28
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq 0x30(%rsp), %rax
leaq (%rax,%rax,2), %rdx
shlq $0x3, %rdx
addq (%r12), %rdx
movq 0x28(%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 0x10(%rsp), %rdi
movq %r15, %rcx
movq %r14, %r9
movsd 0x40(%rsp), %xmm0
pushq 0xb0(%rsp)
pushq $0x0
pushq 0x68(%rsp)
pushq 0x30(%rsp)
callq 0x1a93d9
addq $0x20, %rsp
addq %rax, 0x38(%rsp)
testb %bl, %bl
movq 0x60(%rsp), %rbx
je 0x1ab03b
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
cmpq 0x28(%rbx), %rcx
jne 0x1aaf7a
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x28(%rbx)
leaq (%rsi,%rsi,8), %rcx
leaq (%rcx,%rcx,2), %rdx
addq %rsi, %rdx
movq 0xb0(%rsp), %rdi
movq %rax, %rsi
callq 0x1ee21d
movq %rax, 0x18(%rbx)
movq 0x20(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%rbx)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
leaq (%rax,%rdx), %rcx
movl %r13d, (%rax,%rdx)
movq 0x8(%rsp), %r9
movq (%r9), %rsi
movl 0x4(%rsi,%rbp), %edi
movl %edi, 0x4(%rax,%rdx)
movl 0x8(%rsi,%rbp), %edi
incl %edi
movl %edi, 0x8(%rax,%rdx)
movl 0x10(%rsi,%rbp), %esi
movl %esi, 0xc(%rax,%rdx)
andl $0x3fffffff, %r15d      # imm = 0x3FFFFFFF
movl 0x18(%rax,%rdx), %edi
movl $0x80000000, %r8d       # imm = 0x80000000
andl %r8d, %edi
orl %r15d, %edi
movl %edi, 0x18(%rax,%rdx)
movq (%r9), %rdi
movl 0x1c(%rdi,%rbp), %edi
movl %edi, %r8d
shll $0x1f, %r8d
orl %r15d, %r8d
movl %r8d, 0x18(%rax,%rdx)
testb $0x1, %dil
jne 0x1ab000
movq (%r9), %rax
movl 0x14(%rax,%rbp), %edx
movl %edx, 0x10(%rcx)
movl 0x18(%rax,%rbp), %eax
incl %eax
jmp 0x1ab038
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
leaq (%rsi,%rsi,4), %rdx
movl (%rax,%rdx,8), %esi
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
andl %r10d, %esi
movq (%r9), %rdi
movl 0x18(%rdi,%rbp), %r8d
notl %r8d
addl %esi, %r8d
movl %r8d, 0x10(%rcx)
movl (%rax,%rdx,8), %eax
andl %r10d, %eax
subl 0x14(%rdi,%rbp), %eax
movl %eax, 0x14(%rcx)
movq 0x48(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x8(%rsp), %r15
movq 0x10(%r15), %rax
incq %r13
cmpq %rax, %r13
jb 0x1aadf1
movq 0x20(%rbx), %rax
movq 0x38(%rsp), %r12
testq %r12, %r12
je 0x1ab0cc
movq %r12, %r14
negq %r14
movq 0x18(%rbx), %rsi
movq 0x50(%rsp), %rdx
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rdi
addq %rdx, %rdi
addq %rsi, %rdi
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rax, %rsi
addq %rcx, %rsi
callq 0x19e855
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 %r12, %rdi
addq %rcx, %rdi
movq %r12, %rsi
callq 0x1a9d8a
subq %r12, %rax
addq 0x20(%rbx), %rax
movq %rax, 0x20(%rbx)
movq 0x10(%rsp), %r12
movq 0xb0(%rsp), %rbp
jmp 0x1ab0e6
movq 0x8(%rsp), %r15
jmp 0x1ab0e9
movq 0x20(%rbx), %rax
movq %rax, %r14
testq %r14, %r14
je 0x1ab114
movq 0xa8(%rsp), %r9
movq %r12, %rdi
movq %r15, %rsi
movq 0x18(%rsp), %rdx
movq %rbx, %rcx
pushq %rbp
pushq 0x30(%rsp)
callq 0x1aa5f8
addq $0x10, %rsp
jmp 0x1ab11c
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