// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: dedup_second_chain(ul_idx_t const*, unsigned long*, long, long, long, mg_lchain_t*, kv_ul_ov_t*, kv_ul_ov_t*, unsigned long*, long, double, long)
void dedup_second_chain(const ul_idx_t *uref, uint64_t *ix, int64_t ix_n, int64_t p_sidx, int64_t p_eidx, mg_lchain_t *chain_a,
kv_ul_ov_t *raw_idx, kv_ul_ov_t *raw_chn, uint64_t *b, int64_t bw, double diff_ec_ul, int64_t qlen)
{
	int64_t k, i, z, a_n, ss, ee, b_n; uint64_t qs, qe, ts, te; uint32_t mk = 0x80000000; mg_lchain_t nn;
	int64_t iqs, iqe, its, ite, tsc;
	
	for (k = p_sidx; k < p_eidx; k++) {
		i = raw_idx->a[chain_a[k].off].qn; 
		qs = raw_chn->a[i].qs; qe = raw_chn->a[i].qe;
		ts = raw_chn->a[i].ts; te = raw_chn->a[i].te;
		// fprintf(stderr, "++++++++++++[M::%s::idx:%ld]\n", __func__, i);
		for (;i>=0;) {
			// fprintf(stderr, "--[M::%s::i->%ld]\n", __func__, i);
			if(raw_chn->a[i].qs < qs) qs = raw_chn->a[i].qs;
        	if(raw_chn->a[i].ts < ts) ts = raw_chn->a[i].ts;
        	if(raw_chn->a[i].qe > qe) qe = raw_chn->a[i].qe;
        	if(raw_chn->a[i].te > te) te = raw_chn->a[i].te;
			raw_chn->a[i].qs |= mk;

			if(raw_chn->a[i].tn == (uint32_t)-1) i = -1;
			else i = raw_chn->a[i].tn;
		}
		assert(raw_idx->a[chain_a[k].off].qs == qs && raw_idx->a[chain_a[k].off].qe == qe && 
				raw_idx->a[chain_a[k].off].ts == ts && raw_idx->a[chain_a[k].off].te == te);
	}


	///dedup
	for (z = a_n = 0; z < ix_n; ++z) {
		ss = a_n; ee = a_n + ((uint32_t)ix[z]); tsc = 0;
		if(ss != p_sidx || ee != p_eidx) {
			for (k = ss; k < ee; k++) {
				i = raw_idx->a[chain_a[k].off].qn; b_n = 0;
				qs = ((raw_chn->a[i].qs<<1)>>1); qe = raw_chn->a[i].qe;
				ts = raw_chn->a[i].ts; te = raw_chn->a[i].te;
				// fprintf(stderr, "++++++++++++[M::%s::idx:%ld]\n", __func__, i);
				for (;i>=0;) {
					// fprintf(stderr, "--[M::%s::i->%ld]\n", __func__, i);
					if(((raw_chn->a[i].qs<<1)>>1) < qs) qs = ((raw_chn->a[i].qs<<1)>>1);
					if(raw_chn->a[i].ts < ts) ts = raw_chn->a[i].ts;
					if(raw_chn->a[i].qe > qe) qe = raw_chn->a[i].qe;
					if(raw_chn->a[i].te > te) te = raw_chn->a[i].te;
					// raw_chn->a[i].qs |= mk;
					//update here!!!!!!!
					if(!(raw_chn->a[i].qs&mk)) b[b_n++] = i;
					if(raw_chn->a[i].tn == (uint32_t)-1) i = -1;
					else i = raw_chn->a[i].tn;
				}
				assert(raw_idx->a[chain_a[k].off].qs == qs && raw_idx->a[chain_a[k].off].qe == qe && 
						raw_idx->a[chain_a[k].off].ts == ts && raw_idx->a[chain_a[k].off].te == te);
				
				update_exist_chain(uref, raw_chn->a, b, b_n, raw_idx->a[chain_a[k].off].tn, bw, diff_ec_ul, &nn);
				nn.v = (raw_idx->a[chain_a[k].off].tn<<1)|raw_idx->a[chain_a[k].off].rev;
				extend_end_coord(&nn, NULL, qlen, uref->ug->g->seq[raw_idx->a[chain_a[k].off].tn].len, &iqs, &iqe, &its, &ite);
				nn.qs = iqs; nn.qe = iqe; nn.rs = its; nn.re = ite;
				assert(chain_a[k].score >= nn.score);
				tsc += (chain_a[k].score - nn.score);
			}
		}
		///TODO: also update qs, qe
		tsc = ((int64_t)(ix[z]>>32)) - tsc; if(tsc < 0) tsc = 0;
		ix[z] <<= 32; ix[z] >>= 32; ix[z] |= ((uint64_t)tsc)<<32;

		a_n += ((uint32_t)ix[z]);
	}



	for (k = p_sidx; k < p_eidx; k++) {
		i = raw_idx->a[chain_a[k].off].qn; 
		qs = ((raw_chn->a[i].qs<<1)>>1); qe = raw_chn->a[i].qe;
		ts = raw_chn->a[i].ts; te = raw_chn->a[i].te;
		for (;i>=0;) {
			// fprintf(stderr, "--[M::%s::i->%ld]\n", __func__, i);
			if(raw_chn->a[i].qs&mk) raw_chn->a[i].qs -= mk;
			if(raw_chn->a[i].qs < qs) qs = raw_chn->a[i].qs;
        	if(raw_chn->a[i].ts < ts) ts = raw_chn->a[i].ts;
        	if(raw_chn->a[i].qe > qe) qe = raw_chn->a[i].qe;
        	if(raw_chn->a[i].te > te) te = raw_chn->a[i].te;
			

			if(raw_chn->a[i].tn == (uint32_t)-1) i = -1;
			else i = raw_chn->a[i].tn;
		}
		assert(raw_idx->a[chain_a[k].off].qs == qs && raw_idx->a[chain_a[k].off].qe == qe && 
				raw_idx->a[chain_a[k].off].ts == ts && raw_idx->a[chain_a[k].off].te == te);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movsd %xmm0, 0x48(%rsp)
movq %r9, 0x20(%rsp)
movq %r8, 0x18(%rsp)
movq %rdx, %rbp
movq %rsi, 0x10(%rsp)
movq %rdi, 0x40(%rsp)
movq 0x100(%rsp), %r13
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
pushq $-0x1
popq %r14
movq %rcx, 0x28(%rsp)
cmpq 0x18(%rsp), %rcx
jge 0x185cae
movq 0xf0(%rsp), %rdx
movq (%rdx), %rsi
imulq $0x28, %rcx, %rdx
movq 0x20(%rsp), %rdi
movslq (%rdi,%rdx), %rdx
imulq $0x1c, %rdx, %rdi
leaq (%rsi,%rdi), %rdx
movl (%rsi,%rdi), %r11d
movq 0xf8(%rsp), %rsi
movq (%rsi), %r10
imulq $0x1c, %r11, %rsi
movl 0x4(%r10,%rsi), %r9d
movl 0x8(%r10,%rsi), %r8d
movl 0x10(%r10,%rsi), %edi
movl 0x14(%r10,%rsi), %esi
testq %r11, %r11
js 0x185c65
imulq $0x1c, %r11, %r11
movl 0x4(%r10,%r11), %ebx
movl 0x8(%r10,%r11), %r15d
cmpq %rbx, %r9
cmovaeq %rbx, %r9
movl 0x10(%r10,%r11), %r12d
cmpq %r12, %rdi
cmovaeq %r12, %rdi
cmpq %r15, %r8
cmovbeq %r15, %r8
movl 0x14(%r10,%r11), %r15d
cmpq %r15, %rsi
cmovbeq %r15, %rsi
orl $0x80000000, %ebx       # imm = 0x80000000
movl %ebx, 0x4(%r10,%r11)
movl 0xc(%r10,%r11), %r11d
cmpq %rax, %r11
cmoveq %r14, %r11
jmp 0x185c13
movl 0x4(%rdx), %r10d
cmpq %r10, %r9
jne 0x185c8f
movl 0x8(%rdx), %r9d
cmpq %r9, %r8
jne 0x185c8f
movl 0x10(%rdx), %r8d
cmpq %r8, %rdi
jne 0x185c8f
movl 0x14(%rdx), %edx
incq %rcx
cmpq %rdx, %rsi
je 0x185bc1
leaq 0x6e9f6(%rip), %rdi     # 0x1f468c
leaq 0x6be86(%rip), %rsi     # 0x1f1b23
leaq 0x6ec83(%rip), %rcx     # 0x1f4927
movl $0x3a25, %edx           # imm = 0x3A25
callq 0x2110
xorl %eax, %eax
testq %rbp, %rbp
cmovleq %rax, %rbp
movq %rbp, 0x38(%rsp)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq $0x0, 0x8(%rsp)
xorl %edi, %edi
movq 0x10(%rsp), %rdx
cmpq 0x38(%rsp), %rdi
je 0x185f57
movq %rdi, 0x30(%rsp)
movq (%rdx,%rdi,8), %rax
movl %eax, %ecx
movq 0x8(%rsp), %rdx
leaq (%rcx,%rdx), %rdi
cmpq 0x28(%rsp), %rdx
jne 0x185d15
cmpq 0x18(%rsp), %rdi
jne 0x185d15
xorl %esi, %esi
movq %rdi, 0x8(%rsp)
movq 0x10(%rsp), %rdx
movq 0x30(%rsp), %rdi
jmp 0x185f31
movq 0x8(%rsp), %rdx
xorl %esi, %esi
movq %rdi, 0x50(%rsp)
cmpq %rdi, %rdx
jae 0x185f1c
movq %rsi, 0x60(%rsp)
movq 0xf0(%rsp), %rax
movq (%rax), %rcx
movq %rdx, 0x58(%rsp)
imulq $0x28, %rdx, %rax
movq 0x20(%rsp), %rdx
leaq (%rdx,%rax), %rsi
movq %rsi, 0x68(%rsp)
movslq (%rdx,%rax), %rax
imulq $0x1c, %rax, %rdx
leaq (%rcx,%rdx), %rax
movl (%rcx,%rdx), %r10d
movq 0xf8(%rsp), %rcx
movq (%rcx), %rsi
imulq $0x1c, %r10, %rcx
movl 0x4(%rsi,%rcx), %r9d
movl 0x8(%rsi,%rcx), %edi
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movl 0x10(%rsi,%rcx), %r8d
movl 0x14(%rsi,%rcx), %edx
xorl %ecx, %ecx
testq %r10, %r10
js 0x185de8
imulq $0x1c, %r10, %r11
movl 0x4(%rsi,%r11), %ebp
movl 0x8(%rsi,%r11), %ebx
movl %ebp, %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
cmpq %r15, %r9
cmovaeq %r15, %r9
movl 0x10(%rsi,%r11), %r15d
cmpq %r15, %r8
cmovaeq %r15, %r8
cmpq %rbx, %rdi
cmovbeq %rbx, %rdi
movl 0x14(%rsi,%r11), %ebx
cmpq %rbx, %rdx
cmovbeq %rbx, %rdx
testl %ebp, %ebp
js 0x185dda
movq %r10, (%r13,%rcx,8)
incq %rcx
movl 0xc(%rsi,%r11), %r10d
cmpq %r12, %r10
cmoveq %r14, %r10
jmp 0x185d8b
movl 0x4(%rax), %r10d
cmpq %r10, %r9
jne 0x186075
movl 0x8(%rax), %r9d
cmpq %r9, %rdi
jne 0x186075
movl 0x10(%rax), %edi
cmpq %rdi, %r8
jne 0x186075
movl 0x14(%rax), %edi
cmpq %rdi, %rdx
jne 0x186075
movl 0xc(%rax), %r8d
subq $0x8, %rsp
movq 0x48(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rdx
movq 0x110(%rsp), %r9
movsd 0x50(%rsp), %xmm0
leaq 0x98(%rsp), %r15
pushq %r15
callq 0x1852e6
popq %rax
popq %rcx
movq 0xf0(%rsp), %rax
movq (%rax), %rax
movq 0x68(%rsp), %rbp
movslq (%rbp), %rcx
imulq $0x1c, %rcx, %rcx
movl 0xc(%rax,%rcx), %edx
movl 0x18(%rax,%rcx), %eax
shrdl $0x1f, %edx, %eax
movl %eax, 0x98(%rsp)
movq (%rbx), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl (%rax,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq %r15, %rdi
xorl %esi, %esi
movq 0x110(%rsp), %rdx
leaq 0x88(%rsp), %r8
leaq 0x80(%rsp), %r9
leaq 0x70(%rsp), %rax
pushq %rax
leaq 0x80(%rsp), %rax
pushq %rax
callq 0x184214
popq %rax
popq %rcx
movl 0x88(%rsp), %eax
movl %eax, 0xa4(%rsp)
movl 0x80(%rsp), %eax
movl %eax, 0xa8(%rsp)
movl 0x78(%rsp), %eax
movl %eax, 0x9c(%rsp)
movl 0x70(%rsp), %eax
movl %eax, 0xa0(%rsp)
movl 0x1c(%rbp), %eax
subl 0xac(%rsp), %eax
jl 0x186094
cltq
movq 0x60(%rsp), %rsi
addq %rax, %rsi
movq 0x58(%rsp), %rdx
incq %rdx
movq 0x50(%rsp), %rdi
jmp 0x185d21
movq 0x10(%rsp), %rdx
movq 0x30(%rsp), %rdi
movq (%rdx,%rdi,8), %rax
movl %eax, %ecx
addq %rcx, 0x8(%rsp)
shrq $0x20, %rax
subq %rsi, %rax
testq %rax, %rax
movl $0x0, %esi
cmovleq %rsi, %rax
shlq $0x20, %rax
orq %rcx, %rax
movq %rax, (%rdx,%rdi,8)
incq %rdi
jmp 0x185cd2
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq 0x28(%rsp), %r15
cmpq 0x18(%rsp), %r15
jge 0x186063
movq 0xf0(%rsp), %rcx
movq (%rcx), %rdx
imulq $0x28, %r15, %rcx
movq 0x20(%rsp), %rsi
movslq (%rsi,%rcx), %rcx
imulq $0x1c, %rcx, %rsi
leaq (%rdx,%rsi), %rcx
movl (%rdx,%rsi), %r10d
movq 0xf8(%rsp), %rdx
movq (%rdx), %r9
imulq $0x1c, %r10, %rdx
movl 0x4(%r9,%rdx), %r8d
movl 0x8(%r9,%rdx), %esi
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movl 0x10(%r9,%rdx), %edi
movl 0x14(%r9,%rdx), %edx
testq %r10, %r10
js 0x18601b
imulq $0x1c, %r10, %r10
movl 0x4(%r9,%r10), %r11d
testl %r11d, %r11d
jns 0x185fdf
leaq (%r9,%r10), %rbx
addq $0x4, %rbx
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movl %r11d, (%rbx)
movl %r11d, %r11d
cmpq %r11, %r8
cmovaeq %r11, %r8
movl 0x10(%r9,%r10), %r11d
cmpq %r11, %rdi
cmovaeq %r11, %rdi
movl 0x8(%r9,%r10), %r11d
cmpq %r11, %rsi
cmovbeq %r11, %rsi
movl 0x14(%r9,%r10), %r11d
cmpq %r11, %rdx
cmovbeq %r11, %rdx
movl 0xc(%r9,%r10), %r10d
cmpq %rax, %r10
cmoveq %r14, %r10
jmp 0x185fba
movl 0x4(%rcx), %r9d
cmpq %r9, %r8
jne 0x186044
movl 0x8(%rcx), %r8d
cmpq %r8, %rsi
jne 0x186044
movl 0x10(%rcx), %esi
cmpq %rsi, %rdi
jne 0x186044
movl 0x14(%rcx), %ecx
incq %r15
cmpq %rcx, %rdx
je 0x185f61
leaq 0x6e641(%rip), %rdi     # 0x1f468c
leaq 0x6bad1(%rip), %rsi     # 0x1f1b23
leaq 0x6e8ce(%rip), %rcx     # 0x1f4927
movl $0x3a63, %edx           # imm = 0x3A63
callq 0x2110
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6e610(%rip), %rdi     # 0x1f468c
leaq 0x6baa0(%rip), %rsi     # 0x1f1b23
leaq 0x6e89d(%rip), %rcx     # 0x1f4927
movl $0x3a3f, %edx           # imm = 0x3A3F
callq 0x2110
leaq 0x6e92e(%rip), %rdi     # 0x1f49c9
leaq 0x6ba81(%rip), %rsi     # 0x1f1b23
leaq 0x6e87e(%rip), %rcx     # 0x1f4927
movl $0x3a45, %edx           # imm = 0x3A45
callq 0x2110