// File: /workspace/github_cpp_makefile_star2_O3/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 $0x98, %rsp
movsd %xmm0, 0x80(%rsp)
movq %r9, 0x18(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, 0x78(%rsp)
movq %rcx, 0x10(%rsp)
cmpq %r8, %rcx
jge 0x1d1e30
movq 0xd0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x20(%rsp)
movq 0xd8(%rsp), %rax
movq (%rax), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq 0x10(%rsp), %rsi
leaq (%rsi,%rsi,4), %rdi
movq 0x18(%rsp), %rax
movslq (%rax,%rdi,8), %rdi
leaq (%rdi,%rdi,8), %rax
leaq (%rax,%rax,2), %rax
addq %rdi, %rax
movq 0x20(%rsp), %r9
leaq (%r9,%rax), %rdi
movl (%r9,%rax), %ebx
leaq (%rbx,%rbx,8), %rax
leaq (%rax,%rax,2), %r10
addq %rbx, %r10
movl 0x4(%rcx,%r10), %r11d
movl 0x8(%rcx,%r10), %ebp
movl 0x10(%rcx,%r10), %r9d
movl 0x14(%rcx,%r10), %r10d
movq %r11, %r14
movq %rbp, %r15
movq %r9, %r12
movq %r10, %r13
leaq (%rbx,%rbx,8), %rax
leaq (%rax,%rax,2), %rax
addq %rbx, %rax
movl 0x4(%rcx,%rax), %ebx
movl 0x8(%rcx,%rax), %ebp
cmpq %rbx, %r11
movq %rbx, %r11
cmovbq %r14, %r11
movl 0x10(%rcx,%rax), %r9d
cmpq %r9, %r12
cmovbq %r12, %r9
cmpq %rbp, %r15
cmovaq %r15, %rbp
movl 0x14(%rcx,%rax), %r10d
cmpq %r10, %r13
cmovaq %r13, %r10
orl $0x80000000, %ebx       # imm = 0x80000000
movl %ebx, 0x4(%rcx,%rax)
movl 0xc(%rcx,%rax), %ebx
cmpq %rdx, %rbx
jne 0x1d1d99
movl 0x4(%rdi), %eax
cmpq %rax, %r11
jne 0x1d2289
movl 0x8(%rdi), %eax
cmpq %rax, %rbp
jne 0x1d2289
movl 0x10(%rdi), %eax
cmpq %rax, %r9
jne 0x1d2289
movl 0x14(%rdi), %eax
cmpq %rax, %r10
jne 0x1d2289
incq %rsi
cmpq %r8, %rsi
jne 0x1d1d55
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
movq 0x10(%rsp), %r13
movq 0x28(%rsp), %r9
jle 0x1d2130
xorl %r11d, %r11d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
xorl %r10d, %r10d
xorl %r15d, %r15d
movq %r8, 0x60(%rsp)
movq (%r9,%r10,8), %rax
movl %eax, %ecx
leaq (%rcx,%r15), %rbx
movq %r15, %rdx
xorq %r13, %rdx
movq %rbx, %rsi
xorq %r8, %rsi
orq %rdx, %rsi
je 0x1d2106
testq %rcx, %rcx
je 0x1d2106
movq %r10, 0x70(%rsp)
movq 0xd0(%rsp), %rax
movq (%rax), %rax
xorl %edx, %edx
movq %r15, 0x68(%rsp)
movq %rbx, 0x88(%rsp)
movq %rdx, 0x90(%rsp)
leaq (%r15,%r15,4), %rcx
movq 0x18(%rsp), %rdx
leaq (%rdx,%rcx,8), %rcx
movq %rcx, 0x20(%rsp)
movslq (%rcx), %rcx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rsi
addq %rcx, %rsi
leaq (%rax,%rsi), %rdx
movl (%rax,%rsi), %r8d
movq 0xd8(%rsp), %rax
movq (%rax), %rsi
leaq (%r8,%r8,8), %rax
leaq (%rax,%rax,2), %rax
addq %r8, %rax
movl 0x4(%rsi,%rax), %r10d
movl 0x8(%rsi,%rax), %ebx
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movl 0x10(%rsi,%rax), %r13d
movl 0x14(%rsi,%rax), %eax
xorl %ecx, %ecx
movq %r10, %rbp
movq %rbx, %rdi
movq %r13, %r9
leaq (%r8,%r8,8), %r10
leaq (%r10,%r10,2), %r11
addq %r8, %r11
movl 0x4(%rsi,%r11), %r14d
movl 0x8(%rsi,%r11), %ebx
movl %r14d, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
cmpq %r10, %rbp
cmovbq %rbp, %r10
movl 0x10(%rsi,%r11), %r13d
cmpq %r13, %r9
cmovbq %r9, %r13
cmpq %rbx, %rdi
cmovaq %rdi, %rbx
movq %rax, %rdi
movl 0x14(%rsi,%r11), %eax
cmpq %rax, %rdi
cmovaq %rdi, %rax
testl %r14d, %r14d
js 0x1d1f69
movq 0xe0(%rsp), %rdi
movq %r8, (%rdi,%rcx,8)
incq %rcx
movl 0xc(%rsi,%r11), %r8d
cmpq %r12, %r8
jne 0x1d1f04
movl 0x4(%rdx), %edi
cmpq %rdi, %r10
jne 0x1d224b
movl 0x8(%rdx), %edi
cmpq %rdi, %rbx
jne 0x1d224b
movl 0x10(%rdx), %edi
cmpq %rdi, %r13
jne 0x1d224b
movl 0x14(%rdx), %edi
cmpq %rdi, %rax
jne 0x1d224b
movl 0xc(%rdx), %r8d
leaq 0x38(%rsp), %rax
movq %rax, (%rsp)
movq 0x78(%rsp), %rbx
movq %rbx, %rdi
movq 0xe0(%rsp), %rdx
movq 0xe8(%rsp), %r9
movsd 0x80(%rsp), %xmm0
callq 0x1d13a4
movq 0xd0(%rsp), %rax
movq (%rax), %rax
movq 0x20(%rsp), %r13
movslq (%r13), %rcx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movl 0xc(%rax,%rdx), %esi
movl 0x18(%rax,%rdx), %ecx
shrdl $0x1f, %esi, %ecx
movl %ecx, 0x40(%rsp)
movq (%rbx), %rdx
movq 0x18(%rdx), %rdx
movq 0x20(%rdx), %rdx
movl (%rdx,%rsi,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movslq 0x4c(%rsp), %rsi
movslq 0x44(%rsp), %rdi
movslq 0x48(%rsp), %r9
movq %rdx, %r8
subq %r9, %r8
movq %rdx, %r10
subq %rdi, %r10
testb $0x1, %cl
cmoveq %rdi, %r8
cmoveq %r9, %r10
movslq 0x50(%rsp), %r9
movl %r8d, %edi
subl %esi, %edi
movq 0xf0(%rsp), %r14
movq %r14, %r11
subq %r9, %r11
movq %rdx, %rbx
subq %r10, %rbx
addl %r11d, %r10d
movl %esi, %ebp
subl %r8d, %ebp
cmpq %rbx, %r11
leal (%rbx,%r9), %r9d
cmovgl %edx, %r10d
cmovlel %r14d, %r9d
cmpq %rsi, %r8
movl $0x0, %esi
cmovll %esi, %edi
movl $0x0, %r11d
cmovgel %esi, %ebp
movl %edx, %esi
subl %edi, %esi
testb $0x1, %cl
cmovel %r10d, %esi
subl %r10d, %edx
testb $0x1, %cl
movl %ebp, 0x4c(%rsp)
movl %r9d, 0x50(%rsp)
cmovel %edi, %edx
movl %edx, 0x44(%rsp)
movl %esi, 0x48(%rsp)
movl 0x1c(%r13), %ecx
subl 0x54(%rsp), %ecx
jl 0x1d226a
movslq %ecx, %rcx
movq 0x90(%rsp), %rdx
addq %rcx, %rdx
incq %r15
movq 0x88(%rsp), %rbx
cmpq %rbx, %r15
jb 0x1d1ea3
movq 0x28(%rsp), %r9
movq 0x70(%rsp), %r10
movq (%r9,%r10,8), %rax
movl %eax, %ecx
movq 0x68(%rsp), %rbx
addq %rcx, %rbx
movq 0x60(%rsp), %r8
movq 0x10(%rsp), %r13
movq 0x30(%rsp), %rdi
jmp 0x1d2108
xorl %edx, %edx
shrq $0x20, %rax
subq %rdx, %rax
testq %rax, %rax
cmovleq %r11, %rax
shlq $0x20, %rax
orq %rcx, %rax
movq %rax, (%r9,%r10,8)
incq %r10
movq %rbx, %r15
cmpq %rdi, %r10
jne 0x1d1e5c
cmpq %r8, %r13
jge 0x1d2239
movq 0xd0(%rsp), %rax
movq (%rax), %rax
movq 0xd8(%rsp), %rcx
movq (%rcx), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
leaq (,%r13,4), %rsi
addq %r13, %rsi
movq 0x18(%rsp), %rdi
movslq (%rdi,%rsi,8), %rsi
leaq (%rsi,%rsi,8), %rdi
leaq (%rdi,%rdi,2), %rdi
addq %rsi, %rdi
leaq (%rax,%rdi), %rsi
movl (%rax,%rdi), %r11d
leaq (%r11,%r11,8), %rdi
leaq (%rdi,%rdi,2), %rdi
addq %r11, %rdi
movl 0x4(%rcx,%rdi), %r10d
movl 0x8(%rcx,%rdi), %r15d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movl 0x10(%rcx,%rdi), %r9d
movl 0x14(%rcx,%rdi), %edi
leaq (%r11,%r11,8), %rbx
leaq (%rbx,%rbx,2), %rbx
addq %r11, %rbx
movl 0x4(%rcx,%rbx), %r11d
testl %r11d, %r11d
jns 0x1d21c7
leaq (%rcx,%rbx), %r14
addq $0x4, %r14
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movl %r11d, (%r14)
movl %r11d, %r11d
cmpq %r11, %r10
cmovaeq %r11, %r10
movl 0x10(%rcx,%rbx), %r11d
cmpq %r11, %r9
cmovaeq %r11, %r9
movl 0x8(%rcx,%rbx), %r11d
cmpq %r11, %r15
cmovbeq %r11, %r15
movl 0xc(%rcx,%rbx), %r11d
movl 0x14(%rcx,%rbx), %ebx
cmpq %rbx, %rdi
cmovbeq %rbx, %rdi
cmpq %rdx, %r11
jne 0x1d21a0
movl 0x4(%rsi), %r11d
cmpq %r11, %r10
jne 0x1d22a8
movl 0x8(%rsi), %r10d
cmpq %r10, %r15
jne 0x1d22a8
movl 0x10(%rsi), %r10d
cmpq %r10, %r9
jne 0x1d22a8
movl 0x14(%rsi), %esi
cmpq %rsi, %rdi
jne 0x1d22a8
incq %r13
cmpq %r8, %r13
jne 0x1d2154
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x70572(%rip), %rdi     # 0x2427c4
leaq 0x6da02(%rip), %rsi     # 0x23fc5b
leaq 0x707ff(%rip), %rcx     # 0x242a5f
movl $0x3a3f, %edx           # imm = 0x3A3F
callq 0x2110
leaq 0x70890(%rip), %rdi     # 0x242b01
leaq 0x6d9e3(%rip), %rsi     # 0x23fc5b
leaq 0x707e0(%rip), %rcx     # 0x242a5f
movl $0x3a45, %edx           # imm = 0x3A45
callq 0x2110
leaq 0x70534(%rip), %rdi     # 0x2427c4
leaq 0x6d9c4(%rip), %rsi     # 0x23fc5b
leaq 0x707c1(%rip), %rcx     # 0x242a5f
movl $0x3a25, %edx           # imm = 0x3A25
callq 0x2110
leaq 0x70515(%rip), %rdi     # 0x2427c4
leaq 0x6d9a5(%rip), %rsi     # 0x23fc5b
leaq 0x707a2(%rip), %rcx     # 0x242a5f
movl $0x3a63, %edx           # imm = 0x3A63
callq 0x2110