// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: simple_g_chain_dp(kv_ul_ov_t*, ul_ov_t*, ul_idx_t const*, ug_opt_t const*, long, double, long, long, unsigned long*, unsigned long*, unsigned long*)
int64_t simple_g_chain_dp(kv_ul_ov_t *in, ul_ov_t *buf, const ul_idx_t *uref, const ug_opt_t *uopt, int64_t bw, 
double diff_ec_ul, int64_t qlen, int64_t max_skip, uint64_t *srt, uint64_t *idx, uint64_t *track)
{
	if(in->n == 0) return 0;
	uint32_t ai_v, aj_v, rev_n; ma_ug_t *ug = uref->ug;
    int64_t mm_ovlp, x, i, j, k, sc, csc, mm_sc, mm_idx, qo, share, in_n = in->n;
	int64_t iqs, iqe, its, ite, i_end, j_end;
    ul_ov_t *ai, *aj, *e_ai, *e_aj, rev_t;
	for (i = 0; i < in_n; i++) {
		gen_end_coord(&(in->a[i]), qlen, ug->u.a[in->a[i].tn].len, NULL, &iqe, NULL, NULL);
		srt[i] = iqe; srt[i] <<= 32; srt[i] |= (uint64_t)i;
	}
	radix_sort_gfa64(srt, srt+in_n); 
	for (i = 0; i < in_n; i++) buf[i] = in->a[(uint32_t)srt[i]];
	memcpy(in->a, buf, in_n *sizeof((*buf)));///all alignments have been sorted by the real end-qe

	for (i = 0; i < in_n; ++i) {
		ai = &(in->a[i]); ai_v = (ai->tn<<1)|ai->rev; e_ai = &(buf[i]); i_end = 0;
		gen_end_coord(ai, qlen, ug->u.a[ai->tn].len, &iqs, &iqe, &its, &ite);
		e_ai->qs = iqs; e_ai->qe = iqe; e_ai->ts = its; e_ai->te = ite;
		mm_ovlp = max_ovlp(uref->ug->g, ai_v^1);
		x = (e_ai->qs + mm_ovlp)*diff_ec_ul; 
        if(x < bw) x = bw; 
        x += e_ai->qs + mm_ovlp;
        if (x > qlen+1) x = qlen+1;
		x = find_ul_ov_max(i, buf, x+G_CHAIN_INDEL); 
		i_end = is_end_check(ai_v^1, ai, uref->ug->g);
		csc = mm_sc = e_ai->sec; mm_idx = -1; 
		for (j = x; j >= 0; --j) { // collect potential destination vertices
            aj = &(in->a[j]); aj_v = (aj->tn<<1)|aj->rev; e_aj = &(buf[i]); j_end = 0;
			if(e_aj->qe+G_CHAIN_INDEL <= e_ai->qs) break;//even this pair has a overlap, its length will be very small; just ignore
            if(e_aj->qs >= e_ai->qs+G_CHAIN_INDEL) continue; // lj is contained in li on the query coordinate; 128 for indel offset
			qo = infer_rovlp(e_ai, e_aj, NULL, NULL, NULL, ug); ///overlap length in query (UL read)
			if(ai_v != aj_v && get_ecov_adv(uref, uopt, ai_v^1, aj_v^1, bw, diff_ec_ul, qo, 0, &share)) {
				sc = csc + pop_sc(track[j]); j_end = is_end_check(aj_v, aj, uref->ug->g);
				if(i_end && j_end) sc -= (share>=csc?csc:share);
				if(sc > mm_sc) mm_sc = sc, mm_idx = j;
			}
		}
		track[i] = push_sc_pre(mm_sc, mm_idx);
        srt[i] = track[i]>>32; srt[i] <<= 32; srt[i] |= i;
	}

	int64_t n_v, n_u, n_v0; 
    radix_sort_gfa64(srt, srt+in_n); 
	for (k = in_n-1, n_v = n_u = 0; k >= 0; --k) {
		n_v0 = n_v;
		for (i = (uint32_t)srt[k]; i >= 0 && (track[i]&((uint64_t)0x80000000)) == 0;){
			buf[n_v] = in->a[i]; 
			gen_end_coord(&(buf[n_v]), qlen, ug->u.a[buf[n_v].tn].len, &iqs, &iqe, &its, &ite);
			buf[n_v].qs = iqs; buf[n_v].qe = iqe; buf[n_v].ts = its; buf[n_v].te = ite;

			track[i] |= ((uint64_t)0x80000000);
            i = pop_pre(track[i]);
			n_v++;
		}
		if(n_v0 == n_v) continue;
		sc = (i<0?(pop_sc(srt[k])):(pop_sc(srt[k])-pop_sc(track[i])));
		idx[n_u++] = ((uint64_t)sc<<32)|(n_v-n_v0); 
	}

	for (k = 0, n_v = n_v0 = 0; k < n_u; k++) {
        n_v0 = n_v; n_v += (uint32_t)idx[k];
        in->a[k].qn = idx[k]>>32;//score
        in->a[k].ts = n_v0; in->a[k].te = n_v;///idx
        
        rev_n = ((uint32_t)idx[k])>>1; 
        ///we need to consider contained reads; so determining qs is not such easy
        in->a[k].qs = (uint32_t)-1; in->a[k].qe = buf[n_v0].qe;
        for (i = 0; i < rev_n; i++) {
            rev_t = buf[n_v0+i]; buf[n_v0+i] = buf[n_v-i-1]; buf[n_v-i-1] = rev_t;
            
            if(in->a[k].qs > buf[n_v0+i].qs) in->a[k].qs = buf[n_v0+i].qs;
            if(in->a[k].qs > buf[n_v-i-1].qs) in->a[k].qs = buf[n_v-i-1].qs;
        }
        if(((uint32_t)idx[k])&1) {
            if(in->a[k].qs > buf[n_v0+i].qs) in->a[k].qs = buf[n_v0+i].qs;
        }
        // fprintf(stderr, "[M::%s] k:%ld, qs:%u, qe:%u, chain_occ:%u, chain_score:%u\n", __func__, k, 
        //                  res->a[k].qs, res->a[k].qe, res->a[k].te - res->a[k].ts, res->a[k].qn);
    }

	in->n = n_u;
    radix_sort_ul_ov_srt_qn(in->a, in->a + in->n);//sort by score
	return n_v;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %r9, 0x40(%rsp)
movsd %xmm0, 0xa0(%rsp)
movq %r8, 0x98(%rsp)
movq %rcx, 0xc0(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, 0x28(%rsp)
movq 0x8(%rdi), %r13
testq %r13, %r13
je 0x1841fc
movq 0x138(%rsp), %rbp
movq %rdx, 0x58(%rsp)
movq (%rdx), %rax
movq %rax, 0x48(%rsp)
xorl %ebx, %ebx
testq %r13, %r13
movl $0x0, %eax
cmovgq %r13, %rax
movq %rax, 0x50(%rsp)
leaq 0x38(%rsp), %r14
xorl %r15d, %r15d
cmpq %r15, 0x50(%rsp)
je 0x183b42
movq 0x28(%rsp), %rax
movq (%rax), %rdi
movq 0x48(%rsp), %rax
movq 0x10(%rax), %rax
movl 0xc(%rdi,%rbx), %ecx
addq %rbx, %rdi
imulq $0x28, %rcx, %rcx
movl (%rax,%rcx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andq $0x0, (%rsp)
movq 0x40(%rsp), %rsi
xorl %ecx, %ecx
movq %r14, %r8
xorl %r9d, %r9d
callq 0x18399e
movq 0x38(%rsp), %rax
shlq $0x20, %rax
orq %r15, %rax
movq %rax, (%rbp,%r15,8)
incq %r15
addq $0x1c, %rbx
jmp 0x183ae5
leaq (,%r13,8), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
movq %rsi, 0xa8(%rsp)
callq 0x156b0d
xorl %eax, %eax
movq 0x18(%rsp), %rcx
movq 0x28(%rsp), %rdx
movq (%rdx), %rdi
cmpq %rax, 0x50(%rsp)
je 0x183b96
movl (%rbp,%rax,8), %edx
imulq $0x1c, %rdx, %rdx
movupd (%rdi,%rdx), %xmm0
movups 0xc(%rdi,%rdx), %xmm1
movups %xmm1, 0xc(%rcx)
movupd %xmm0, (%rcx)
incq %rax
addq $0x1c, %rcx
jmp 0x183b64
movq %r13, 0x80(%rsp)
imulq $0x1c, %r13, %rdx
movq 0x18(%rsp), %rsi
callq 0x2160
movq 0x40(%rsp), %rax
incq %rax
movq %rax, 0xb0(%rsp)
xorl %r13d, %r13d
cmpq 0x50(%rsp), %r13
je 0x183ec9
movq 0x28(%rsp), %rax
movq (%rax), %rax
imulq $0x1c, %r13, %rbx
leaq (%rax,%rbx), %rdi
movq %rdi, 0x10(%rsp)
movl 0xc(%rax,%rbx), %ecx
movl 0x18(%rax,%rbx), %ebp
shrdl $0x1f, %ecx, %ebp
movq 0x18(%rsp), %r14
leaq (%r14,%rbx), %rax
movq %rax, 0x88(%rsp)
movq 0x48(%rsp), %rax
movq 0x10(%rax), %rax
imulq $0x28, %rcx, %rcx
movl (%rax,%rcx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leaq 0x60(%rsp), %rax
movq %rax, (%rsp)
movq 0x40(%rsp), %rsi
leaq 0x70(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x68(%rsp), %r9
callq 0x18399e
movq 0x70(%rsp), %rax
movl %eax, %r15d
movl %eax, 0x4(%r14,%rbx)
movl 0x38(%rsp), %eax
movl %eax, 0x8(%r14,%rbx)
movl 0x68(%rsp), %eax
movl %eax, 0x10(%r14,%rbx)
movl 0x60(%rsp), %eax
movl %eax, 0x14(%r14,%rbx)
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rdi
movl %ebp, 0x20(%rsp)
movq 0x88(%rsp), %r12
xorl $0x1, %ebp
movl %ebp, %esi
callq 0x15d450
addq %rax, %r15
cvtsi2sd %r15, %xmm0
mulsd 0xa0(%rsp), %xmm0
cvttsd2si %xmm0, %rdx
movq 0x98(%rsp), %rax
cmpq %rax, %rdx
cmovleq %rax, %rdx
addq %r15, %rdx
movq 0xb0(%rsp), %rax
cmpq %rax, %rdx
cmovgeq %rax, %rdx
subl $-0x80, %edx
movq %r13, 0xb8(%rsp)
movl %r13d, %edi
movq %r14, %rsi
callq 0x160717
movslq %eax, %r13
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rdx
movl %ebp, 0x7c(%rsp)
movl %ebp, %edi
movq 0x10(%rsp), %rsi
callq 0x183a49
movl %eax, 0x78(%rsp)
movl 0x18(%r14,%rbx), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
imulq $0x1c, %r13, %rbx
movq %rax, 0x90(%rsp)
movq %rax, 0x30(%rsp)
pushq $-0x1
popq %rax
movq %rax, 0x10(%rsp)
movq 0x148(%rsp), %rdx
testq %r13, %r13
js 0x183e76
movl 0x4(%r12), %eax
movl 0x8(%r12), %ecx
subl $-0x80, %ecx
cmpl %eax, %ecx
jbe 0x183e76
cmpl $0xffffff7f, %eax       # imm = 0xFFFFFF7F
ja 0x183e6a
movq 0x28(%rsp), %rax
movq (%rax), %r14
movl 0xc(%r14,%rbx), %eax
movl 0x18(%r14,%rbx), %r15d
shrdl $0x1f, %eax, %r15d
movq %r12, %rdi
movq %r12, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq 0x48(%rsp), %r9
callq 0x3d261
cmpl %r15d, 0x20(%rsp)
je 0x183e62
movl %r15d, %ecx
xorl $0x1, %ecx
andq $0x0, (%rsp)
leaq 0xc8(%rsp), %rdx
movq %rdx, 0x8(%rsp)
movq 0x58(%rsp), %rbp
movq %rbp, %rdi
movq 0xc0(%rsp), %rsi
movl 0x7c(%rsp), %edx
movq 0x98(%rsp), %r8
movsd 0xa0(%rsp), %xmm0
movq %rax, %r9
callq 0x1631f5
testq %rax, %rax
je 0x183e62
addq %rbx, %r14
movq 0x148(%rsp), %rax
movq (%rax,%r13,8), %rax
movq %rax, %r12
shrq $0x20, %r12
leaq -0x80000000(%r12), %rcx
negq %r12
testq %rax, %rax
cmovsq %rcx, %r12
addq 0x90(%rsp), %r12
movq (%rbp), %rax
movq 0x18(%rax), %rdx
movl %r15d, %edi
movq %r14, %rsi
callq 0x183a49
cmpl $0x0, 0x78(%rsp)
je 0x183e31
testl %eax, %eax
je 0x183e31
movq 0xc8(%rsp), %rax
movq 0x90(%rsp), %rcx
cmpq %rcx, %rax
cmovgeq %rcx, %rax
subq %rax, %r12
movq 0x148(%rsp), %rdx
movq 0x30(%rsp), %rax
cmpq %rax, %r12
cmovgq %r12, %rax
movq %rax, 0x30(%rsp)
movq 0x10(%rsp), %rax
cmovgq %r13, %rax
movq %rax, 0x10(%rsp)
movq 0x88(%rsp), %r12
jmp 0x183e6a
movq 0x148(%rsp), %rdx
decq %r13
addq $-0x1c, %rbx
jmp 0x183d19
movq 0x30(%rsp), %rsi
shlq $0x20, %rsi
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmovsq %rax, %rcx
orq %rsi, %rcx
btsq $0x3f, %rcx
movq 0xb8(%rsp), %r13
movq %rcx, (%rdx,%r13,8)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
andq %rax, %rcx
orq %r13, %rcx
movq 0x138(%rsp), %rbp
movq %rcx, (%rbp,%r13,8)
incq %r13
jmp 0x183bbf
movq %rbp, %rdi
movq 0xa8(%rsp), %rsi
callq 0x156b0d
xorl %edx, %edx
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
leaq 0x68(%rsp), %rbp
movq $0x0, 0x20(%rsp)
movq 0x148(%rsp), %rdi
movq %rdx, 0x30(%rsp)
imulq $0x1c, %rdx, %rax
addq 0x18(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x80(%rsp), %r14
testq %r14, %r14
jle 0x1840aa
leaq -0x1(%r14), %rax
movq %rax, 0x80(%rsp)
movq 0x138(%rsp), %rax
movl -0x8(%rax,%r14,8), %r13d
movq 0x10(%rsp), %r15
xorl %ebx, %ebx
testq %r13, %r13
js 0x183ffe
movq (%rdi,%r13,8), %rcx
testl %ecx, %ecx
js 0x184009
movq 0x28(%rsp), %rax
movq (%rax), %rax
imulq $0x1c, %r13, %rcx
movupd (%rax,%rcx), %xmm0
movups 0xc(%rax,%rcx), %xmm1
movups %xmm1, 0xc(%r15)
movupd %xmm0, (%r15)
movq 0x48(%rsp), %rax
movq 0x10(%rax), %rax
movl 0xc(%r15), %ecx
imulq $0x28, %rcx, %rcx
movl (%rax,%rcx), %edx
andq %r12, %rdx
leaq 0x60(%rsp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
movq 0x40(%rsp), %rsi
leaq 0x70(%rsp), %rcx
leaq 0x38(%rsp), %r8
movq %rbp, %r9
callq 0x18399e
movq 0x148(%rsp), %rdi
movl 0x70(%rsp), %eax
movl %eax, 0x4(%r15)
movl 0x38(%rsp), %eax
movl %eax, 0x8(%r15)
movl 0x68(%rsp), %eax
movl %eax, 0x10(%r15)
movl 0x60(%rsp), %eax
movl %eax, 0x14(%r15)
movq (%rdi,%r13,8), %rax
movq %rax, %rcx
btsq $0x1f, %rcx
andq %r12, %rax
cmpq %r12, %rax
pushq $-0x1
popq %rdx
cmoveq %rdx, %rax
movq %rcx, (%rdi,%r13,8)
decq %rbx
addq $0x1c, %r15
movq %rax, %r13
jmp 0x183f3b
testq %rbx, %rbx
je 0x183f0a
jmp 0x184055
testq %rbx, %rbx
je 0x183f0a
movq 0x138(%rsp), %rax
movq -0x8(%rax,%r14,8), %rdx
movq %rdx, %rax
shrq $0x20, %rax
movq $-0x80000000, %r8       # imm = 0x80000000
leaq (%rax,%r8), %rsi
negq %rax
testq %rdx, %rdx
cmovsq %rsi, %rax
movq %rcx, %rdx
shrq $0x20, %rdx
leaq (%rdx,%r8), %rsi
negq %rdx
testq %rcx, %rcx
cmovsq %rsi, %rdx
subq %rdx, %rax
jmp 0x18407a
movq 0x138(%rsp), %rax
movq -0x8(%rax,%r14,8), %rcx
movq %rcx, %rax
shrq $0x20, %rax
leaq -0x80000000(%rax), %rdx
negq %rax
testq %rcx, %rcx
cmovsq %rdx, %rax
shlq $0x20, %rax
movq 0x30(%rsp), %rdx
subq %rbx, %rdx
negq %rbx
orq %rax, %rbx
movq 0x140(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rbx, (%rax,%rcx,8)
incq %rcx
movq %rcx, 0x20(%rsp)
jmp 0x183ef7
movq 0x18(%rsp), %rax
addq $-0x1c, %rax
xorl %ecx, %ecx
pushq $-0x1
popq %rdx
xorl %r14d, %r14d
movq 0x28(%rsp), %r12
cmpq 0x20(%rsp), %rcx
je 0x1841e0
movq 0x140(%rsp), %rsi
movq (%rsi,%rcx,8), %r10
movl %r10d, %esi
addq %r14, %rsi
movq %r10, %r9
shrq $0x20, %r9
movq (%r12), %r8
imulq $0x1c, %rcx, %rdi
movl %r9d, (%r8,%rdi)
movl %r14d, 0x10(%r8,%rdi)
movl %esi, 0x14(%r8,%rdi)
shrl %r10d
orl $-0x1, 0x4(%r8,%rdi)
imulq $0x1c, %r14, %r11
movq 0x18(%rsp), %rbx
leaq (%rbx,%r11), %r9
movl 0x8(%rbx,%r11), %r11d
movl %r11d, 0x8(%r8,%rdi)
imulq $0x1c, %r10, %r10
imulq $0x1c, %rsi, %r11
addq %rax, %r11
xorl %ebx, %ebx
movl %edx, %ebp
cmpq %rbx, %r10
je 0x1841b7
movups (%r9,%rbx), %xmm0
movups 0xc(%r9,%rbx), %xmm1
movups %xmm1, 0xdc(%rsp)
movaps %xmm0, 0xd0(%rsp)
movups (%r11), %xmm0
movups 0xc(%r11), %xmm1
movups %xmm1, 0xc(%r9,%rbx)
movups %xmm0, (%r9,%rbx)
movups 0xdc(%rsp), %xmm0
movups %xmm0, 0xc(%r11)
movapd 0xd0(%rsp), %xmm0
movupd %xmm0, (%r11)
movq (%r12), %r8
leaq (%r8,%rdi), %r14
addq $0x4, %r14
movl (%r14), %ebp
movl 0x4(%r9,%rbx), %r15d
cmpl %r15d, %ebp
jbe 0x18419b
movl %r15d, (%r14)
movl %r15d, %ebp
movl 0x4(%r11), %r15d
cmpl %r15d, %ebp
jbe 0x1841aa
movl %r15d, (%r14)
movl %r15d, %ebp
addq $0x1c, %rbx
addq $-0x1c, %r11
jmp 0x184129
movq 0x140(%rsp), %r11
testb $0x1, (%r11,%rcx,8)
je 0x1841d5
movl 0x4(%r9,%r10), %r9d
cmpl %r9d, %ebp
jbe 0x1841d5
movl %r9d, 0x4(%r8,%rdi)
incq %rcx
movq %rsi, %r14
jmp 0x1840c0
movq 0x20(%rsp), %rax
movq %rax, 0x8(%r12)
movq (%r12), %rdi
imulq $0x1c, %rax, %rsi
addq %rdi, %rsi
callq 0x15775d
jmp 0x1841ff
xorl %r14d, %r14d
movq %r14, %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq