// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: renew_consensus_chain(kv_ul_ov_t*, unsigned int, unsigned int, kv_ul_ov_t*, asg64_v*, R_to_U*, ug_opt_t const*, asg_t const*, long, double, unsigned int, Chain_Data*, unsigned long, unsigned int)
void renew_consensus_chain(kv_ul_ov_t *in, uint32_t in_s, uint32_t in_e, kv_ul_ov_t *idx, asg64_v *ou, R_to_U *ri,
const ug_opt_t *uopt, const asg_t *rg, int64_t bw, double diff_ec_ul, uint32_t arc_id, Chain_Data* dp, uint64_t rlen, uint32_t ulid)
{	
	if(in_e <= in_s) return;
	uint64_t mqs, mqe, k, l, ou_n = ou->n, stn, etn;
	collect_pp_ovlps(in->a + in_s, in_e - in_s, idx, uopt, rg, rlen, ulid, &mqs, &mqe);
	// if(ulid == 7723 && in_e-in_s == 2 && in->a[in_s].tn == (7723>>1) && in->a[in_e-1].tn == (7829>>1)) {
	// 	fprintf(stderr, "-0-[M::%s] ulid::%u, idx->n::%u\n", __func__, ulid, (uint32_t)idx->n);
	// }
	if(!idx->n) return;///it is possible
	kv_resize(uint64_t, *ou, (ou_n+(in_e-in_s)));
	for (k = in_s; k < in_e; k++) {
		if(in->a[k].el) continue;
		ou->a[ou_n++] = in->a[k].tn;
	}
	assert(ou_n <= ou->m);
	radix_sort_gfa64(ou->a+ou->n, ou->a+ou_n);
	gen_linear_rchains_dedup(idx, in, rg, uopt, bw, diff_ec_ul, rlen, dp, ulid, ou->a+ou->n, ou_n-ou->n);
	// if(ulid == 7723 && in_e-in_s == 2 && in->a[in_s].tn == (7723>>1) && in->a[in_e-1].tn == (7829>>1)) {
	// 	fprintf(stderr, "-1-[M::%s] ulid::%u, idx->n::%u\n", __func__, ulid, (uint32_t)idx->n);
	// }
	if(!idx->n) return;///it is possible
	stn = etn = (uint64_t)-1;
	if(in_e > in_s && in->a[in_s].el) stn = in->a[in_s].tn;
	if(in_e-in_s>1 && in->a[in_e-1].el) etn = in->a[in_e-1].tn;
	idx->n = gen_cns_chain_linear_hard(idx->a, idx->n, rg, rlen, bw, diff_ec_ul, dp, UG_SKIP_N, UG_ITER_N, UG_DIS_N, mqs, mqe, ulid, stn, etn);
	// if(ulid == 7723 && in_e-in_s == 2 && in->a[in_s].tn == (7723>>1) && in->a[in_e-1].tn == (7829>>1)) {
	// 	fprintf(stderr, "-2-[M::%s] ulid::%u, idx->n::%u\n", __func__, ulid, (uint32_t)idx->n);
	// }
	if(!idx->n) return;
	assert((stn==((uint64_t)-1))||((idx->a[0].tn == stn) && (idx->a[0].el)));
	assert((etn==((uint64_t)-1))||((idx->a[idx->n-1].tn == etn) && (idx->a[idx->n-1].el)));
	if((stn != ((uint64_t)-1)) && (etn != ((uint64_t)-1)) && idx->n < 2) return;///could happen for circle
	if(((stn != ((uint64_t)-1)) || (etn != ((uint64_t)-1))) && idx->n < 1) return;///not sure if it will happen
	ou_n = ou->n;
	for (k = in_s; k < in_e; k++) {
		if(in->a[k].el) continue;
		kv_push(uint64_t, *ou, in->a[k].tn);
	}
	for (k = 0; k < idx->n; k++) {
		if(idx->a[k].el) continue;
		kv_push(uint64_t, *ou, idx->a[k].tn);
	}
	if(ou->n == ou_n) return;
	radix_sort_gfa64(ou->a+ou_n, ou->a+ou->n);
	for (k = ou_n+1, l = ou_n; k <= ou->n; k++) {
		if(k == ou->n || ou->a[l] != ou->a[k]) {
			if(k-l > 1) break;
			l = k;
		}
	}
	assert(k > ou->n);
	ou->n = ou_n;
	l = arc_id; 
	if(arc_id == (uint32_t)-1) {///this is an isloated node
		l = idx->a[0].tn; l |= ((uint64_t)0x80000000);
	}
	l <<= 32; l |= ((uint64_t)((uint32_t)-1));
	kv_push(uint64_t, *ou, l);
	for (k = 0; k < idx->n; k++) {
		l = ((uint64_t)(idx->a[k].tn<<1))|((uint64_t)idx->a[k].rev);
		if(idx->a[k].el) l += ((uint64_t)0x100000000);
		kv_push(uint64_t, *ou, l);
	}
	kv_push(uint64_t, *ou, ((uint64_t)-1));
	for (k = in_s; k < in_e; k++) {
		l = ((uint64_t)(in->a[k].tn<<1))|((uint64_t)in->a[k].rev);
		if(in->a[k].el) l += ((uint64_t)0x100000000);
		kv_push(uint64_t, *ou, l);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %edx, %r15d
subl %esi, %r15d
jbe 0x1d4c5b
movq %r8, %rbx
movq %rcx, %r12
movq %rdi, %r14
movsd %xmm0, 0x48(%rsp)
movq %rdx, 0x50(%rsp)
movq 0xe8(%rsp), %r9
movq 0xc8(%rsp), %r8
movq 0xc0(%rsp), %rcx
movq (%rbx), %r13
movl %esi, 0x44(%rsp)
movl %esi, %edx
leaq (%rdx,%rdx,8), %rax
leaq (%rax,%rax,2), %rsi
movq %rdx, 0x58(%rsp)
movq %rsi, 0x68(%rsp)
leaq (%rsi,%rdx), %rbp
movq (%rdi), %rdi
addq %rbp, %rdi
leaq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x80(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r15, %rsi
movq %r12, %rdx
callq 0x1d37e0
cmpq $0x0, 0x8(%r12)
je 0x1d4c5b
leaq (%r15,%r13), %rax
cmpq %rax, 0x8(%rbx)
jae 0x1d4748
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, 0x8(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq 0x50(%rsp), %rax
movl %eax, %edx
cmpl 0x44(%rsp), %eax
movq %rdx, 0x60(%rsp)
jbe 0x1d4794
movq (%r14), %rcx
movq %rdx, %rax
movq 0x58(%rsp), %rdx
subq %rdx, %rax
movq 0x68(%rsp), %rsi
addq %rsi, %rdx
addq %rdx, %rcx
addq $0x18, %rcx
testb $0x40, 0x3(%rcx)
jne 0x1d478b
movl -0xc(%rcx), %edx
movq 0x10(%rbx), %rsi
movq %rdx, (%rsi,%r13,8)
incq %r13
addq $0x1c, %rcx
decq %rax
jne 0x1d4777
cmpq 0x8(%rbx), %r13
ja 0x1d4c6d
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq (%rcx,%rax,8), %rdi
leaq (%rcx,%r13,8), %rsi
callq 0x194fef
movq (%rbx), %rax
leaq (,%rax,8), %rcx
addq 0x10(%rbx), %rcx
subq %rax, %r13
movq %r13, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movq %r14, %rsi
movq 0xc8(%rsp), %rdx
movq 0xd0(%rsp), %r8
movsd 0x48(%rsp), %xmm0
callq 0x1d43b1
movq 0x8(%r12), %rsi
testq %rsi, %rsi
je 0x1d4c5b
movq (%r14), %rax
movq $-0x1, %r8
testb $0x40, 0x1b(%rax,%rbp)
movq $-0x1, %r13
je 0x1d4828
addq %rbp, %rax
movl 0xc(%rax), %r13d
cmpl $0x2, %r15d
movsd 0x48(%rsp), %xmm0
jb 0x1d4858
movq (%r14), %rax
movq 0x50(%rsp), %rcx
leal -0x1(%rcx), %edx
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rdx, %rcx
testb $0x40, 0x1b(%rax,%rcx)
je 0x1d4858
addq %rcx, %rax
movl 0xc(%rax), %r8d
movq (%r12), %rdi
movq 0x80(%rsp), %rax
movq 0x78(%rsp), %rcx
movq %r8, 0x38(%rsp)
movq %r13, 0x30(%rsp)
movq %rcx, 0x20(%rsp)
movq %rax, 0x18(%rsp)
movq $0xc350, 0x10(%rsp)     # imm = 0xC350
movq $0x1388, 0x8(%rsp)      # imm = 0x1388
movq $0x64, (%rsp)
movq 0xc8(%rsp), %rdx
movq 0xe8(%rsp), %rcx
movq %r8, %r15
movq 0xd0(%rsp), %r8
movq 0xe0(%rsp), %r9
callq 0x1d3c5e
movq %rax, 0x8(%r12)
testq %rax, %rax
je 0x1d4c5b
movq %r15, %rdi
cmpq $-0x1, %r13
je 0x1d48f0
movq (%r12), %rcx
movl 0xc(%rcx), %edx
cmpq %rdx, %r13
jne 0x1d4c8c
testb $0x40, 0x1b(%rcx)
je 0x1d4c8c
cmpq $-0x1, %rdi
je 0x1d491f
movq (%r12), %rcx
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
movl -0x10(%rcx,%rdx), %esi
cmpq %rsi, %rdi
jne 0x1d4cab
addq %rdx, %rcx
testb $0x40, -0x1(%rcx)
je 0x1d4cab
cmpq $-0x1, %r13
je 0x1d4935
cmpq $-0x1, %rdi
je 0x1d4935
cmpq $0x1, %rax
je 0x1d4c5b
movq (%rbx), %rax
movq %rax, 0x70(%rsp)
movl 0x44(%rsp), %eax
cmpl %eax, 0x50(%rsp)
movq 0x60(%rsp), %r13
jbe 0x1d49ac
subq 0x58(%rsp), %r13
movl $0x2, %r15d
movq (%r14), %rax
testb $0x40, 0x1b(%rax,%rbp)
jne 0x1d49a3
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x1d498a
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r15, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%r14), %rax
movl 0xc(%rax,%rbp), %eax
movq (%rbx), %rcx
movq 0x10(%rbx), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %rax, (%rdx,%rcx,8)
addq $0x1c, %rbp
decq %r13
jne 0x1d4957
cmpq $0x0, 0x8(%r12)
je 0x1d4a1d
xorl %r13d, %r13d
movl $0x2, %ebp
xorl %r15d, %r15d
movq (%r12), %rax
testb $0x40, 0x1b(%rax,%r13)
jne 0x1d4a0f
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x1d49f4
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %rbp, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%r12), %rax
movl 0xc(%rax,%r13), %eax
movq (%rbx), %rcx
movq 0x10(%rbx), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %rax, (%rdx,%rcx,8)
incq %r15
addq $0x1c, %r13
cmpq 0x8(%r12), %r15
jb 0x1d49bf
movq (%rbx), %rax
movq 0x70(%rsp), %r15
cmpq %r15, %rax
je 0x1d4c5b
movq 0x10(%rbx), %rcx
leaq (%rcx,%r15,8), %rdi
leaq (%rcx,%rax,8), %rsi
callq 0x194fef
movq (%rbx), %rax
leaq 0x1(%r15), %rcx
cmpq %rax, %rcx
seta %dl
ja 0x1d4a89
movq %r15, %rsi
cmpq %rcx, %rax
je 0x1d4a64
movq 0x10(%rbx), %rdi
movq (%rdi,%rsi,8), %r8
cmpq (%rdi,%rcx,8), %r8
je 0x1d4a73
movq %rcx, %rdi
subq %rsi, %rdi
movq %rcx, %rsi
cmpq $0x1, %rdi
ja 0x1d4a80
incq %rcx
cmpq %rax, %rcx
seta %dl
jbe 0x1d4a51
jmp 0x1d4a89
testb $0x1, %dl
je 0x1d4cca
movq %r14, 0x48(%rsp)
movl 0xd8(%rsp), %eax
movq %r15, (%rbx)
cmpl $-0x1, %eax
jne 0x1d4aa9
movq (%r12), %rcx
movl $0x80000000, %eax       # imm = 0x80000000
orl 0xc(%rcx), %eax
shlq $0x20, %rax
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
orq %rax, %r14
movq 0x8(%rbx), %rax
cmpq %rax, %r15
jne 0x1d4ae4
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movq %r14, (%rcx,%rax,8)
cmpq $0x0, 0x8(%r12)
je 0x1d4b79
movl $0x18, %r15d
xorl %r13d, %r13d
movq (%r12), %rax
movl -0xc(%rax,%r15), %ebp
movl (%rax,%r15), %r14d
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x1d4b42
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
shldl $0x1, %r14d, %ebp
andl $0x40000000, %r14d      # imm = 0x40000000
leaq (,%r14,4), %rax
addq %rbp, %rax
movq (%rbx), %rcx
movq 0x10(%rbx), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %rax, (%rdx,%rcx,8)
incq %r13
addq $0x1c, %r15
cmpq 0x8(%r12), %r13
jb 0x1d4b07
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x1d4ba7
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movq $-0x1, (%rcx,%rax,8)
movl 0x44(%rsp), %eax
cmpl %eax, 0x50(%rsp)
movq 0x48(%rsp), %rax
jbe 0x1d4c5b
movq 0x58(%rsp), %rcx
subq %rcx, 0x60(%rsp)
movq 0x68(%rsp), %rdx
leaq (%rcx,%rdx), %r15
addq $0x18, %r15
movl $0x2, %r12d
movq (%rax), %rax
movl -0xc(%rax,%r15), %r13d
movl (%rax,%r15), %r14d
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x1d4c22
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r12, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
shldl $0x1, %r14d, %r13d
andl $0x40000000, %r14d      # imm = 0x40000000
leaq (,%r14,4), %rax
addq %r13, %rax
movq (%rbx), %rcx
movq 0x10(%rbx), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %rax, (%rdx,%rcx,8)
addq $0x1c, %r15
decq 0x60(%rsp)
movq 0x48(%rsp), %rax
jne 0x1d4bed
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x62a97(%rip), %rdi     # 0x23770b
leaq 0x5ed10(%rip), %rsi     # 0x23398b
leaq 0x62a97(%rip), %rcx     # 0x237719
movl $0x4945, %edx           # imm = 0x4945
callq 0x2110
leaq 0x62b44(%rip), %rdi     # 0x2377d7
leaq 0x5ecf1(%rip), %rsi     # 0x23398b
leaq 0x62a78(%rip), %rcx     # 0x237719
movl $0x4954, %edx           # imm = 0x4954
callq 0x2110
leaq 0x62b66(%rip), %rdi     # 0x237818
leaq 0x5ecd2(%rip), %rsi     # 0x23398b
leaq 0x62a59(%rip), %rcx     # 0x237719
movl $0x4955, %edx           # imm = 0x4955
callq 0x2110
leaq 0x62b96(%rip), %rdi     # 0x237867
leaq 0x5ecb3(%rip), %rsi     # 0x23398b
leaq 0x62a3a(%rip), %rcx     # 0x237719
movl $0x4969, %edx           # imm = 0x4969
callq 0x2110