// File: /workspace/github_cpp_makefile_star2_O1/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, 0x78(%rsp)
movq %r9, 0x18(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, 0x70(%rsp)
movq %r8, 0x8(%rsp)
movq %rcx, 0x10(%rsp)
cmpq %r8, %rcx
jge 0x1c7ea8
movq 0xd0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x20(%rsp)
movq 0xd8(%rsp), %rax
movq (%rax), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq $-0x1, %rsi
movq 0x10(%rsp), %rdi
leaq (%rdi,%rdi,4), %r8
movq 0x18(%rsp), %rax
movslq (%rax,%r8,8), %r8
leaq (%r8,%r8,8), %r9
leaq (%r9,%r9,2), %r9
addq %r8, %r9
movq 0x20(%rsp), %rax
leaq (%rax,%r9), %r8
movl (%rax,%r9), %r14d
leaq (%r14,%r14,8), %r9
leaq (%r9,%r9,2), %r11
addq %r14, %r11
movl 0x4(%rcx,%r11), %ebx
movl 0x8(%rcx,%r11), %r9d
movl 0x10(%rcx,%r11), %r10d
movl 0x14(%rcx,%r11), %r11d
movq %rbx, %r15
movq %r9, %r12
movq %r10, %r13
movq %r11, %rbp
leaq (%r14,%r14,8), %r9
leaq (%r9,%r9,2), %rax
addq %r14, %rax
movl 0x4(%rcx,%rax), %r14d
movl 0x8(%rcx,%rax), %r9d
cmpq %r14, %rbx
movq %r14, %rbx
cmovbq %r15, %rbx
movl 0x10(%rcx,%rax), %r10d
cmpq %r10, %r13
cmovbq %r13, %r10
cmpq %r9, %r12
cmovaq %r12, %r9
movl 0x14(%rcx,%rax), %r11d
cmpq %r11, %rbp
cmovaq %rbp, %r11
orl $0x80000000, %r14d      # imm = 0x80000000
movl %r14d, 0x4(%rcx,%rax)
movl 0xc(%rcx,%rax), %r14d
cmpq %rdx, %r14
cmoveq %rsi, %r14
jne 0x1c7e02
movl 0x4(%r8), %eax
cmpq %rax, %rbx
jne 0x1c8302
movl 0x8(%r8), %eax
cmpq %rax, %r9
jne 0x1c8302
movl 0x10(%r8), %eax
cmpq %rax, %r10
jne 0x1c8302
movl 0x14(%r8), %eax
cmpq %rax, %r11
jne 0x1c8302
incq %rdi
cmpq 0x8(%rsp), %rdi
jne 0x1c7dbe
cmpq $0x0, 0x30(%rsp)
movq 0x28(%rsp), %rdx
jle 0x1c81a2
xorl %edi, %edi
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq $-0x1, %r15
xorl %esi, %esi
xorl %ecx, %ecx
movq %rsi, 0x68(%rsp)
movq (%rdx,%rsi,8), %rax
movl %eax, %eax
leaq (%rax,%rcx), %rdx
movq %rcx, 0x38(%rsp)
xorq 0x10(%rsp), %rcx
movq %rdx, 0x80(%rsp)
xorq 0x8(%rsp), %rdx
orq %rcx, %rdx
je 0x1c8164
movl $0x0, %r8d
testq %rax, %rax
je 0x1c8167
xorl %r8d, %r8d
movq 0x38(%rsp), %rdx
movq %r8, 0x90(%rsp)
movq 0xd0(%rsp), %rax
movq (%rax), %rcx
movq %rdx, 0x88(%rsp)
leaq (%rdx,%rdx,4), %rax
movq 0x18(%rsp), %rdx
leaq (%rdx,%rax,8), %rax
movq %rax, 0x20(%rsp)
movslq (%rax), %rax
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
leaq (%rcx,%rdx), %rax
movl (%rcx,%rdx), %r8d
movq 0xd8(%rsp), %rcx
movq (%rcx), %rsi
leaq (%r8,%r8,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r8, %rcx
movl 0x4(%rsi,%rcx), %r10d
movl 0x8(%rsi,%rcx), %r13d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movl 0x10(%rsi,%rcx), %r12d
movl 0x14(%rsi,%rcx), %edx
xorl %ecx, %ecx
movq %r10, %rbp
movq %r13, %rdi
movq %r12, %r9
leaq (%r8,%r8,8), %r10
leaq (%r10,%r10,2), %r11
addq %r8, %r11
movl 0x4(%rsi,%r11), %r14d
movl 0x8(%rsi,%r11), %r13d
movl %r14d, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
cmpq %r10, %rbp
cmovbq %rbp, %r10
movl 0x10(%rsi,%r11), %r12d
cmpq %r12, %r9
cmovbq %r9, %r12
cmpq %r13, %rdi
cmovaq %rdi, %r13
movq %rdx, %rdi
movl 0x14(%rsi,%r11), %edx
cmpq %rdx, %rdi
cmovaq %rdi, %rdx
testl %r14d, %r14d
js 0x1c7feb
movq 0xe0(%rsp), %rdi
movq %r8, (%rdi,%rcx,8)
incq %rcx
movl 0xc(%rsi,%r11), %r8d
cmpq %rbx, %r8
cmoveq %r15, %r8
jne 0x1c7f86
movl 0x4(%rax), %edi
cmpq %rdi, %r10
jne 0x1c82c4
movl 0x8(%rax), %edi
cmpq %rdi, %r13
jne 0x1c82c4
movl 0x10(%rax), %edi
cmpq %rdi, %r12
jne 0x1c82c4
movl 0x14(%rax), %edi
cmpq %rdi, %rdx
jne 0x1c82c4
movl 0xc(%rax), %r8d
leaq 0x40(%rsp), %rax
movq %rax, (%rsp)
movq 0x70(%rsp), %r14
movq %r14, %rdi
movq 0xe0(%rsp), %rdx
movq 0xe8(%rsp), %r9
movsd 0x78(%rsp), %xmm0
callq 0x1c73c8
movq 0xd0(%rsp), %rax
movq (%rax), %rcx
movq 0x20(%rsp), %r12
movslq (%r12), %rax
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
movl 0xc(%rcx,%rdx), %esi
movl 0x18(%rcx,%rdx), %eax
shrdl $0x1f, %esi, %eax
movl %eax, 0x48(%rsp)
movq (%r14), %rsi
movq 0x18(%rsi), %rsi
movq 0x20(%rsi), %rsi
movl 0xc(%rcx,%rdx), %ecx
movl (%rsi,%rcx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movslq 0x54(%rsp), %rdx
movslq 0x4c(%rsp), %rsi
movslq 0x50(%rsp), %r8
movq %rcx, %rdi
subq %r8, %rdi
movq %rcx, %r9
subq %rsi, %r9
testb $0x1, %al
cmoveq %rsi, %rdi
cmoveq %r8, %r9
movslq 0x58(%rsp), %r8
movl %edi, %esi
subl %edx, %esi
movq 0xf0(%rsp), %r14
movq %r14, %r10
subq %r8, %r10
movq %rcx, %r11
subq %r9, %r11
addl %r10d, %r9d
addl %r11d, %r8d
movl %edx, %ebp
subl %edi, %ebp
cmpq %r11, %r10
cmovgl %ecx, %r9d
cmovlel %r14d, %r8d
cmpq %rdx, %rdi
movl $0x0, %edx
cmovll %edx, %esi
movl $0x0, %edi
cmovgel %edx, %ebp
movl %ecx, %edx
subl %esi, %edx
testb $0x1, %al
cmovel %r9d, %edx
subl %r9d, %ecx
testb $0x1, %al
movl %ebp, 0x54(%rsp)
movl %r8d, 0x58(%rsp)
cmovel %esi, %ecx
movl %ecx, 0x4c(%rsp)
movl %edx, 0x50(%rsp)
movl 0x1c(%r12), %eax
subl 0x5c(%rsp), %eax
jl 0x1c82e3
cltq
movq 0x90(%rsp), %r8
addq %rax, %r8
movq 0x88(%rsp), %rdx
incq %rdx
cmpq 0x80(%rsp), %rdx
jb 0x1c7f11
jmp 0x1c8167
xorl %r8d, %r8d
movq 0x28(%rsp), %rdx
movq 0x68(%rsp), %rsi
movq (%rdx,%rsi,8), %rax
movl %eax, %ecx
shrq $0x20, %rax
subq %r8, %rax
testq %rax, %rax
cmovleq %rdi, %rax
movl %eax, 0x4(%rdx,%rsi,8)
movq 0x38(%rsp), %rax
addq %rcx, %rax
movq %rax, %rcx
incq %rsi
cmpq 0x30(%rsp), %rsi
jne 0x1c7ecb
movq 0x10(%rsp), %r15
cmpq 0x8(%rsp), %r15
jge 0x1c82b2
movq 0xd0(%rsp), %rax
movq (%rax), %rax
movq 0xd8(%rsp), %rcx
movq (%rcx), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq $-0x1, %rsi
leaq (%r15,%r15,4), %rdi
movq 0x18(%rsp), %r8
movslq (%r8,%rdi,8), %rdi
leaq (%rdi,%rdi,8), %r8
leaq (%r8,%r8,2), %r8
addq %rdi, %r8
leaq (%rax,%r8), %rdi
movl (%rax,%r8), %ebx
leaq (%rbx,%rbx,8), %r8
leaq (%r8,%r8,2), %r8
addq %rbx, %r8
movl 0x4(%rcx,%r8), %r11d
movl 0x8(%rcx,%r8), %r9d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movl 0x10(%rcx,%r8), %r10d
movl 0x14(%rcx,%r8), %r8d
leaq (%rbx,%rbx,8), %r14
leaq (%r14,%r14,2), %r14
addq %rbx, %r14
leaq (%rcx,%r14), %rbx
addq $0x4, %rbx
movl (%rbx), %ebp
testl %ebp, %ebp
jns 0x1c823b
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movl %ebp, (%rbx)
movl (%rbx), %ebx
cmpq %rbx, %r11
cmovaeq %rbx, %r11
movl 0x10(%rcx,%r14), %ebx
cmpq %rbx, %r10
cmovaeq %rbx, %r10
movl 0x8(%rcx,%r14), %ebx
cmpq %rbx, %r9
cmovbeq %rbx, %r9
movl 0x14(%rcx,%r14), %ebx
cmpq %rbx, %r8
cmovbeq %rbx, %r8
movl 0xc(%rcx,%r14), %ebx
cmpq %rdx, %rbx
cmoveq %rsi, %rbx
jne 0x1c821a
movl 0x4(%rdi), %ebx
cmpq %rbx, %r11
jne 0x1c8321
movl 0x8(%rdi), %r11d
cmpq %r11, %r9
jne 0x1c8321
movl 0x10(%rdi), %r9d
cmpq %r9, %r10
jne 0x1c8321
movl 0x14(%rdi), %edi
cmpq %rdi, %r8
jne 0x1c8321
incq %r15
cmpq 0x8(%rsp), %r15
jne 0x1c81d4
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6e229(%rip), %rdi     # 0x2364f4
leaq 0x6b6b9(%rip), %rsi     # 0x23398b
leaq 0x6e4b6(%rip), %rcx     # 0x23678f
movl $0x3a3f, %edx           # imm = 0x3A3F
callq 0x2110
leaq 0x6e547(%rip), %rdi     # 0x236831
leaq 0x6b69a(%rip), %rsi     # 0x23398b
leaq 0x6e497(%rip), %rcx     # 0x23678f
movl $0x3a45, %edx           # imm = 0x3A45
callq 0x2110
leaq 0x6e1eb(%rip), %rdi     # 0x2364f4
leaq 0x6b67b(%rip), %rsi     # 0x23398b
leaq 0x6e478(%rip), %rcx     # 0x23678f
movl $0x3a25, %edx           # imm = 0x3A25
callq 0x2110
leaq 0x6e1cc(%rip), %rdi     # 0x2364f4
leaq 0x6b65c(%rip), %rsi     # 0x23398b
leaq 0x6e459(%rip), %rcx     # 0x23678f
movl $0x3a63, %edx           # imm = 0x3A63
callq 0x2110