// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/tovlp.cpp
// Function: pt_cal_sim(pt128_v*, mz_ds_t*, ma_ug_t*, unsigned int, double)
kv_u_trans_t *pt_cal_sim(pt128_v *pa, mz_ds_t *mz_idx, ma_ug_t *ug, uint32_t min_cnt, double min_sim)
{
	int64_t st, i, j;
    kvec_t(uint64_t) a; kv_init(a);
    kvec_t(int32_t) b; kv_init(b);
    kvec_t(int32_t) M; kv_init(M);
	kv_u_trans_t *ma = NULL; CALLOC(ma, 1);
    u_trans_t m;
	///x = (uint64_t)mz[l].rid << 33 | mz[k].rid << 1 | rev;
	for (st = 0, i = 1; i <= pa->n; ++i) {
		if (i == pa->n || pa->a[i].x != pa->a[st].x) {///minimizers between a pair of unitigs
            if((pa->a[st].x>>33) == (((uint32_t)pa->a[st].x)>>1)) goto end_chain;
			uint32_t nn[2], nn_min;
            a.n = 0; memset(&m, 0, sizeof(m));
			if (i - st < min_cnt) goto end_chain;
            //(uint64_t)mz[l].pos << 32 | (rev? lk - (mz[k].pos + 1 - span) - 1 : mz[k].pos);
			for (j = st; j < i; ++j) kv_push(uint64_t, a, pa->a[j].y);
			radix_sort_tb64(a.a, a.a + a.n);///sort by query pos + target pos
			// for (l = 0; l < a.n; ++l) a.a[l] = (uint32_t)a.a[l];///only need target pos to do LIS
            kv_resize(int32_t, b, a.n); kv_resize(int32_t, M, a.n+1);
			m.occ = pt_lis_64(a.n, a.a, b.a, M.a);
            /*******************************for debug************************************/
            // debug_lis_64(a.a, b.a, m.occ);
            /*******************************for debug************************************/
			if (m.occ == 0 || m.occ < min_cnt) goto end_chain;//chain occ
            m.qn = pa->a[st].x >> 33;///query id
            m.tn = ((uint32_t)pa->a[st].x) >> 1;///target id
			if (m.qn == m.tn) goto end_chain;
            m.qs = a.a[b.a[0]]>>32; m.qe = a.a[b.a[m.occ-1]]>>32;
            m.ts = (uint32_t)(a.a[b.a[0]]); m.te = (uint32_t)(a.a[b.a[m.occ-1]]);
            m.rev = (pa->a[st].x>>32&1) ^ (pa->a[st].x&1);
            update_mz_ovlp(&(m.qs), &(m.qe), ug->u.a[m.qn].len, &(m.ts), &(m.te), ug->u.a[m.tn].len, m.rev);

            nn[0] = get_mz_occ(mz_idx, m.qn, m.qs, m.qe-1);
            nn[1] = get_mz_occ(mz_idx, m.tn, m.ts, m.te-1);
			nn_min = MIN(nn[0], nn[1]);
            nn_min = MAX(nn_min, m.occ);
            // m.sim = pow(2.0 * m.m / (nn[0] + nn[1]), 1.0 / k);
            if(m.occ >= nn_min*min_sim){
                m.nw = (double)(m.occ) - (double)(nn_min-m.occ)*0.2;
                if(m.nw > 0) kv_push(u_trans_t, *ma, m);
			}
            end_chain:	st = i;
		}
	}
	kv_destroy(b); kv_destroy(a); kv_destroy(M);
	return ma;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movsd %xmm0, 0xb0(%rsp)
movl %ecx, %ebx
movq %rdx, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq %rdi, %r15
pushq $0x1
popq %r14
pushq $0x30
popq %rsi
movq %r14, %rdi
callq 0x2120
movq %r14, %rsi
movq %rax, 0x78(%rsp)
movl %ebx, 0x74(%rsp)
movl %ebx, %r12d
xorl %r8d, %r8d
movq $0x0, 0x28(%rsp)
movq $0x0, 0x90(%rsp)
xorl %edi, %edi
xorl %r13d, %r13d
movq $0x0, 0x88(%rsp)
xorl %r14d, %r14d
movq %r15, 0x68(%rsp)
movq %r12, 0x80(%rsp)
movl (%r15), %eax
cmpq %rax, %rsi
ja 0x155939
movq 0x8(%r15), %rax
jne 0x155554
movq %r14, %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %rcx
jmp 0x155570
movq %rsi, %rdx
shlq $0x4, %rdx
movq %r14, %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %rcx
cmpq %rcx, (%rax,%rdx)
je 0x155883
movq %rcx, %rdx
shrq $0x21, %rdx
shrl %ecx
cmpl %ecx, %edx
je 0x15559e
xorpd %xmm0, %xmm0
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0x30(%rsp)
movq %rsi, %rcx
subq %r14, %rcx
cmpq %r12, %rcx
jge 0x1555a6
movq %rsi, %r14
jmp 0x155883
movq %r13, 0x98(%rsp)
movq %r14, %r13
shlq $0x4, %r13
xorl %ebx, %ebx
pushq $-0x1
popq %r12
movq %rsi, 0x18(%rsp)
leaq 0x1(%r12), %rbp
leaq (%r12,%r14), %rcx
incq %rcx
cmpq %rsi, %rcx
jge 0x15561c
cmpq %r8, %rbp
jne 0x155605
leaq (%r8,%r8), %r15
testq %r8, %r8
pushq $0x2
popq %rax
cmoveq %rax, %r15
leaq (,%r15,8), %rsi
callq 0x2370
movq 0x18(%rsp), %rsi
movq %rax, %rdi
movq 0x68(%rsp), %rax
movq 0x8(%rax), %rax
movq %r15, %r8
movq 0x8(%rax,%r13), %rcx
movq %rcx, 0x8(%rdi,%r12,8)
addq $-0x8, %rbx
addq $0x10, %r13
movq %rbp, %r12
jmp 0x1555c0
movq %r8, 0x20(%rsp)
movq %rdi, %rsi
subq %rbx, %rsi
movq %rdi, 0x10(%rsp)
callq 0x15432b
cmpq %rbp, 0x90(%rsp)
jae 0x15568e
movq %r12, %rax
shrq %rax
orq %r12, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x4(,%rax,4), %rsi
incq %rax
movq %rax, 0x90(%rsp)
movq 0x28(%rsp), %rdi
callq 0x2370
movq %rax, 0x28(%rsp)
movq 0x98(%rsp), %r13
leaq 0x2(%r12), %rax
cmpq %rax, 0x88(%rsp)
jae 0x1556f4
movq %rbp, %rax
shrq %rax
orq %rbp, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x4(,%rax,4), %rsi
incq %rax
movq %rax, 0x88(%rsp)
movq %r13, %rdi
callq 0x2370
movq %rax, %r13
incl %r12d
movl %r12d, %edi
movq 0x10(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %r13, %rcx
callq 0x15523d
movl %eax, 0x48(%rsp)
testl %eax, %eax
sete %cl
cmpl 0x74(%rsp), %eax
setb %dl
orb %cl, %dl
je 0x15573a
movq 0x18(%rsp), %rsi
movq %rsi, %r14
movq 0x68(%rsp), %r15
movq 0x80(%rsp), %r12
jmp 0x155879
movq 0x68(%rsp), %r15
movq 0x8(%r15), %rcx
shlq $0x4, %r14
movq (%rcx,%r14), %rdx
movq %rdx, %rcx
shrq $0x21, %rcx
movl %ecx, 0x38(%rsp)
movl %edx, %esi
shrl %esi
movl %esi, 0x44(%rsp)
cmpl %ecx, %esi
movq 0x80(%rsp), %r12
je 0x155871
movq 0x28(%rsp), %r9
movslq (%r9), %rdi
movq 0x10(%rsp), %r8
movl 0x4(%r8,%rdi,8), %edi
movl %edi, 0x30(%rsp)
decl %eax
movslq (%r9,%rax,4), %rdi
movl 0x4(%r8,%rdi,8), %edi
movl %edi, 0x34(%rsp)
movslq (%r9), %rdi
movl (%r8,%rdi,8), %edi
movl %edi, 0x3c(%rsp)
movslq (%r9,%rax,4), %rax
movl (%r8,%rax,8), %eax
movl %eax, 0x40(%rsp)
movq %rdx, %rax
shrq $0x20, %rax
xorq %rdx, %rax
movb 0x58(%rsp), %dl
movl %eax, %edi
shlb $0x6, %dil
andb $0x40, %dil
andb $-0x41, %dl
orb %dil, %dl
movb %dl, 0x58(%rsp)
movq 0xa8(%rsp), %rdx
movq 0x10(%rdx), %rdi
imulq $0x28, %rcx, %rcx
movl (%rdi,%rcx), %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andq %r8, %rdx
movl %esi, %ecx
imulq $0x28, %rcx, %rcx
movl (%rdi,%rcx), %r9d
andq %r8, %r9
andl $0x1, %eax
movl %eax, (%rsp)
leaq 0x30(%rsp), %rdi
leaq 0x34(%rsp), %rsi
leaq 0x3c(%rsp), %rcx
leaq 0x40(%rsp), %r8
callq 0x1553a8
movl 0x38(%rsp), %esi
movl 0x30(%rsp), %edx
movl 0x34(%rsp), %ecx
decl %ecx
movq 0xa0(%rsp), %rbx
movq %rbx, %rdi
callq 0x155455
movl %eax, %ebp
movl 0x44(%rsp), %esi
movl 0x3c(%rsp), %edx
movl 0x40(%rsp), %ecx
decl %ecx
movq %rbx, %rdi
callq 0x155455
cmpl %eax, %ebp
cmovbl %ebp, %eax
movl 0x48(%rsp), %ecx
cmpl %ecx, %eax
cmovbel %ecx, %eax
cvtsi2sd %rcx, %xmm0
cvtsi2sd %rax, %xmm1
mulsd 0xb0(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jae 0x15588b
movq 0x18(%rsp), %rsi
movq %rsi, %r14
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %r8
incq %rsi
jmp 0x155535
subl %ecx, %eax
cvtsi2sd %rax, %xmm1
mulsd 0x9bf7e(%rip), %xmm1    # 0x1f1818
addsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
ucomisd 0x8dcbc(%rip), %xmm0    # 0x1e3568
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %r8
jbe 0x15592c
movq 0x78(%rsp), %r14
movq (%r14), %rbx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rbx
jne 0x1558f6
leaq (%rbx,%rbx), %rcx
testq %rbx, %rbx
pushq $0x2
popq %rdx
cmoveq %rdx, %rcx
movq %rcx, 0x8(%r14)
imulq $0x30, %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x20(%rsp), %r8
movq 0x18(%rsp), %rsi
movq %rax, 0x10(%r14)
movq 0x10(%rsp), %rdi
leaq 0x1(%rbx), %rcx
movq %rcx, (%r14)
imulq $0x30, %rbx, %rcx
movapd 0x30(%rsp), %xmm0
movapd 0x40(%rsp), %xmm1
movaps 0x50(%rsp), %xmm2
movups %xmm2, 0x20(%rax,%rcx)
movupd %xmm1, 0x10(%rax,%rcx)
movupd %xmm0, (%rax,%rcx)
jmp 0x15559e
movq %rsi, %r14
movq 0x10(%rsp), %rdi
jmp 0x155883
movq %rdi, %rbx
movq 0x28(%rsp), %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x2220
movq %r13, %rdi
callq 0x2220
movq 0x78(%rsp), %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq