// File: /workspace/github_cpp_makefile_star2_O2/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, %ebp
subl %esi, %ebp
jbe 0x190c8e
movq %r8, %rbx
movq %rcx, %r13
movq %rdi, %r14
movq %rdx, 0x68(%rsp)
movsd %xmm0, 0x58(%rsp)
movq 0xe8(%rsp), %r9
movq 0xc8(%rsp), %r8
movq 0xc0(%rsp), %rcx
movq (%rbx), %r15
movl %esi, %eax
movq %rax, 0x60(%rsp)
imulq $0x1c, %rax, %r12
movq (%rdi), %rdi
addq %r12, %rdi
leaq 0x78(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x80(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rbp, %rsi
movq %r13, %rdx
callq 0x18fd2c
movq %r13, 0x48(%rsp)
cmpq $0x0, 0x8(%r13)
je 0x190c8e
leaq (%r15,%rbp), %rax
cmpq %rax, 0x8(%rbx)
jae 0x1909db
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 0xe0(%rsp), %r13
movl 0x68(%rsp), %edi
leaq 0x18(%r12), %rax
movq 0x60(%rsp), %rcx
cmpq %rdi, %rcx
jae 0x190a18
movq (%r14), %rdx
testb $0x40, 0x3(%rdx,%rax)
jne 0x190a0f
movl -0xc(%rdx,%rax), %edx
movq 0x10(%rbx), %rsi
movq %rdx, (%rsi,%r15,8)
incq %r15
incq %rcx
addq $0x1c, %rax
jmp 0x1909f1
movq %rdi, 0x50(%rsp)
cmpq 0x8(%rbx), %r15
ja 0x190eda
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq (%rcx,%rax,8), %rdi
leaq (%rcx,%r15,8), %rsi
callq 0x156b0d
movq (%rbx), %rax
leaq (,%rax,8), %rcx
addq 0x10(%rbx), %rcx
subq %rax, %r15
movq %r15, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq %r13, (%rsp)
movq 0x48(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq 0xc8(%rsp), %rdx
movq 0xd0(%rsp), %r8
movsd 0x58(%rsp), %xmm0
callq 0x190713
movq 0x8(%r15), %rsi
testq %rsi, %rsi
je 0x190c8e
movq (%r14), %rax
pushq $-0x1
popq %r15
testb $0x40, 0x1b(%rax,%r12)
movq %r15, %r8
je 0x190aa8
leaq (%rax,%r12), %rcx
movl 0xc(%rcx), %r8d
movq %r13, %r9
cmpl $0x2, %ebp
movsd 0x58(%rsp), %xmm0
jb 0x190ad6
movq 0x68(%rsp), %rcx
decl %ecx
imulq $0x1c, %rcx, %rcx
testb $0x40, 0x1b(%rax,%rcx)
movq 0x48(%rsp), %r13
je 0x190adb
addq %rcx, %rax
movl 0xc(%rax), %r15d
jmp 0x190adb
movq 0x48(%rsp), %r13
movq (%r13), %rdi
movq 0x80(%rsp), %rax
movq 0x78(%rsp), %rcx
movq %r15, 0x38(%rsp)
movq %r8, 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, %rbp
movq 0xd0(%rsp), %r8
callq 0x190111
movq %rax, 0x8(%r13)
testq %rax, %rax
movq 0x50(%rsp), %rdi
je 0x190c8e
movq %rbp, %r8
cmpq $-0x1, %rbp
je 0x190b73
movq 0x48(%rsp), %rcx
movq (%rcx), %rcx
movl 0xc(%rcx), %edx
cmpq %rdx, %r8
jne 0x190ef9
testb $0x40, 0x1b(%rcx)
je 0x190ef9
cmpq $-0x1, %r15
je 0x190baf
movq 0x48(%rsp), %rcx
movq (%rcx), %rcx
imulq $0x1c, %rax, %rdx
movl -0x10(%rcx,%rdx), %esi
cmpq %rsi, %r15
jne 0x190f18
addq %rdx, %rcx
testb $0x40, -0x1(%rcx)
je 0x190f18
cmpq $-0x1, %r8
je 0x190baf
cmpq $0x1, %rax
je 0x190c8e
movq (%rbx), %rbp
movq 0x60(%rsp), %r15
movq 0x48(%rsp), %r13
cmpq %rdi, %r15
jae 0x190c1f
movq (%r14), %rdx
testb $0x40, 0x1b(%rdx,%r12)
jne 0x190c16
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x190c06
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x50(%rsp), %rdi
movq %rax, 0x10(%rbx)
movq (%r14), %rdx
movq (%rbx), %rcx
movl 0xc(%rdx,%r12), %edx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %rdx, (%rax,%rcx,8)
incq %r15
addq $0x1c, %r12
jmp 0x190bbc
xorl %r15d, %r15d
xorl %r12d, %r12d
cmpq 0x8(%r13), %r12
jae 0x190c86
movq (%r13), %rdx
testb $0x40, 0x1b(%rdx,%r15)
jne 0x190c7d
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x190c6d
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%r13), %rdx
movq (%rbx), %rcx
movl 0xc(%rdx,%r15), %edx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %rdx, (%rax,%rcx,8)
incq %r12
addq $0x1c, %r15
jmp 0x190c25
movq (%rbx), %rax
cmpq %rbp, %rax
jne 0x190ca0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rbx), %rcx
leaq (%rcx,%rbp,8), %rdi
leaq (%rcx,%rax,8), %rsi
callq 0x156b0d
movq (%rbx), %rax
movq %rax, %rcx
negq %rcx
pushq $0x1
popq %r11
movq %rbp, 0x58(%rsp)
movq %rbp, %rsi
movq 0x48(%rsp), %r13
movq 0x60(%rsp), %rbp
movq %rsi, %rdi
leaq (%rcx,%rsi), %r8
leaq (,%rsi,8), %r9
movq %r11, %rdx
leaq (%rdi,%r11), %rsi
cmpq %rax, %rsi
ja 0x190d33
movq %r11, %r10
movq %r8, %r11
addq %r10, %r11
je 0x190d0b
movq 0x10(%rbx), %r15
movq (%r15,%rdi,8), %r12
addq %r9, %r15
leaq 0x1(%r10), %r11
cmpq (%r15,%r10,8), %r12
je 0x190ce2
cmpq $0x2, %r10
movq %rdx, %r11
jb 0x190cd0
leaq 0x64ce4(%rip), %rdi     # 0x1f59ff
leaq 0x60e01(%rip), %rsi     # 0x1f1b23
leaq 0x64b88(%rip), %rcx     # 0x1f58b1
movl $0x4969, %edx           # imm = 0x4969
callq 0x2110
movq 0x58(%rsp), %rdx
movq %rdx, (%rbx)
movl 0xd8(%rsp), %ecx
cmpl $-0x1, %ecx
jne 0x190d53
movq (%r13), %rax
movl $0x80000000, %ecx       # imm = 0x80000000
orl 0xc(%rax), %ecx
shlq $0x20, %rcx
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
orq %rcx, %r15
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rdx
jne 0x190d8f
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, (%rbx)
movq %r15, (%rax,%rdx,8)
pushq $0x18
popq %r15
xorl %r12d, %r12d
movq %r14, 0x70(%rsp)
cmpq 0x8(%r13), %r12
jae 0x190e22
movq %rbp, %r14
movq (%r13), %rcx
movl -0xc(%rcx,%r15), %ebp
movl (%rcx,%r15), %r13d
movq (%rbx), %rcx
cmpq 0x8(%rbx), %rcx
jne 0x190dea
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
shldl $0x1, %r13d, %ebp
andl $0x40000000, %r13d      # imm = 0x40000000
leaq (,%r13,4), %rdx
addq %rbp, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %rdx, (%rax,%rcx,8)
incq %r12
addq $0x1c, %r15
movq %r14, %rbp
movq 0x70(%rsp), %r14
movq 0x48(%rsp), %r13
jmp 0x190da6
movq (%rbx), %rcx
cmpq 0x8(%rbx), %rcx
jne 0x190e50
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
movq 0x50(%rsp), %rdi
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
orq $-0x1, (%rax,%rcx,8)
imulq $0x1c, %rbp, %r15
addq $0x18, %r15
cmpq %rdi, %rbp
jae 0x190c8e
movq %rbp, %r13
movq (%r14), %rcx
movl -0xc(%rcx,%r15), %r12d
movl (%rcx,%r15), %ebp
movq (%rbx), %rcx
cmpq 0x8(%rbx), %rcx
jne 0x190eb4
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x50(%rsp), %rdi
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
shldl $0x1, %ebp, %r12d
andl $0x40000000, %ebp       # imm = 0x40000000
leaq (%r12,%rbp,4), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rbx)
movq %rdx, (%rax,%rcx,8)
movq %r13, %rbp
incq %rbp
addq $0x1c, %r15
jmp 0x190e69
leaq 0x649c2(%rip), %rdi     # 0x1f58a3
leaq 0x60c3b(%rip), %rsi     # 0x1f1b23
leaq 0x649c2(%rip), %rcx     # 0x1f58b1
movl $0x4945, %edx           # imm = 0x4945
callq 0x2110
leaq 0x64a6f(%rip), %rdi     # 0x1f596f
leaq 0x60c1c(%rip), %rsi     # 0x1f1b23
leaq 0x649a3(%rip), %rcx     # 0x1f58b1
movl $0x4954, %edx           # imm = 0x4954
callq 0x2110
leaq 0x64a91(%rip), %rdi     # 0x1f59b0
leaq 0x60bfd(%rip), %rsi     # 0x1f1b23
leaq 0x64984(%rip), %rcx     # 0x1f58b1
movl $0x4955, %edx           # imm = 0x4955
callq 0x2110