// File: 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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movsd %xmm0, -0x28(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x90(%rbp)
movl $0x1, %edi
movl $0x30, %esi
callq 0x2140
movq %rax, -0x90(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x1, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x8(%rbp), %rcx
movl (%rcx), %ecx
cmpq %rcx, %rax
jg 0x278a23
movq -0x38(%rbp), %rax
movq -0x8(%rbp), %rcx
movl (%rcx), %ecx
cmpq %rcx, %rax
je 0x2783cf
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq -0x30(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
cmpq (%rcx), %rax
je 0x278a10
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x30(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x21, %rax
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq -0x30(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
shrl %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jne 0x27840d
jmp 0x278a08
movq $0x0, -0x58(%rbp)
leaq -0xc0(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x2100
movq -0x38(%rbp), %rax
subq -0x30(%rbp), %rax
movl -0x1c(%rbp), %ecx
cmpq %rcx, %rax
jge 0x27843d
jmp 0x278a08
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0x2784e5
jmp 0x278455
movq -0x58(%rbp), %rax
cmpq -0x50(%rbp), %rax
jne 0x2784a4
cmpq $0x0, -0x50(%rbp)
je 0x278476
movq -0x50(%rbp), %rax
shlq %rax
movq %rax, -0xd8(%rbp)
jmp 0x278484
movl $0x2, %eax
movq %rax, -0xd8(%rbp)
jmp 0x278484
movq -0xd8(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x40(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdx
movq -0x48(%rbp), %rax
movq -0x58(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x58(%rbp)
movq %rdx, (%rax,%rcx,8)
jmp 0x2784d4
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x278445
movq -0x48(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x58(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x2764b0
movq -0x68(%rbp), %rax
cmpq -0x58(%rbp), %rax
jae 0x27858f
movq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
shrq %rax
orq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
shrq $0x2, %rax
orq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
shrq $0x4, %rax
orq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
shrq $0x8, %rax
orq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
shrq $0x10, %rax
orq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, -0x60(%rbp)
jmp 0x278591
jmp 0x278593
movq -0x80(%rbp), %rax
movq -0x58(%rbp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jae 0x278630
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
shrq %rax
orq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
shrq $0x2, %rax
orq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
shrq $0x4, %rax
orq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
shrq $0x8, %rax
orq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
shrq $0x10, %rax
orq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
movq -0x80(%rbp), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, -0x78(%rbp)
jmp 0x278632
movq -0x58(%rbp), %rax
movl %eax, %edi
movq -0x48(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x78(%rbp), %rcx
callq 0x277da0
movl %eax, -0xa8(%rbp)
cmpl $0x0, -0xa8(%rbp)
je 0x278663
movl -0xa8(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x278668
jmp 0x278a08
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x30(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x21, %rax
movl %eax, -0xb8(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x30(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrl %eax
movl %eax, -0xac(%rbp)
movl -0xb8(%rbp), %eax
cmpl -0xac(%rbp), %eax
jne 0x2786b9
jmp 0x278a08
movq -0x48(%rbp), %rax
movq -0x60(%rbp), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0xc0(%rbp)
movq -0x48(%rbp), %rax
movq -0x60(%rbp), %rcx
movl -0xa8(%rbp), %edx
subl $0x1, %edx
movl %edx, %edx
movslq (%rcx,%rdx,4), %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0xbc(%rbp)
movq -0x48(%rbp), %rax
movq -0x60(%rbp), %rcx
movslq (%rcx), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, -0xb4(%rbp)
movq -0x48(%rbp), %rax
movq -0x60(%rbp), %rcx
movl -0xa8(%rbp), %edx
subl $0x1, %edx
movl %edx, %edx
movslq (%rcx,%rdx,4), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, -0xb0(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x30(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
andq $0x1, %rax
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq -0x30(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
andq $0x1, %rcx
xorq %rcx, %rax
movb %al, %cl
movb -0x98(%rbp), %al
andb $0x1, %cl
shlb $0x6, %cl
andb $-0x41, %al
orb %cl, %al
movb %al, -0x98(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
addq $0x4, %rsi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0xb8(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
leaq -0xc0(%rbp), %rcx
addq $0xc, %rcx
leaq -0xc0(%rbp), %r8
addq $0x10, %r8
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0xac(%rbp), %r9d
imulq $0x28, %r9, %r9
addq %r9, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %r9d
movb -0x98(%rbp), %al
shrb $0x6, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, (%rsp)
callq 0x278050
movq -0x10(%rbp), %rdi
movl -0xb8(%rbp), %eax
movl %eax, %esi
movl -0xc0(%rbp), %eax
movl %eax, %edx
movl -0xbc(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %ecx
callq 0x278230
movl %eax, -0xc8(%rbp)
movq -0x10(%rbp), %rdi
movl -0xac(%rbp), %eax
movl %eax, %esi
movl -0xb4(%rbp), %eax
movl %eax, %edx
movl -0xb0(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %ecx
callq 0x278230
movl %eax, -0xc4(%rbp)
movl -0xc8(%rbp), %eax
cmpl -0xc4(%rbp), %eax
ja 0x278874
movl -0xc8(%rbp), %eax
movl %eax, -0xdc(%rbp)
jmp 0x278880
movl -0xc4(%rbp), %eax
movl %eax, -0xdc(%rbp)
movl -0xdc(%rbp), %eax
movl %eax, -0xcc(%rbp)
movl -0xcc(%rbp), %eax
cmpl -0xa8(%rbp), %eax
jb 0x2788a8
movl -0xcc(%rbp), %eax
movl %eax, -0xe0(%rbp)
jmp 0x2788b4
movl -0xa8(%rbp), %eax
movl %eax, -0xe0(%rbp)
movl -0xe0(%rbp), %eax
movl %eax, -0xcc(%rbp)
movl -0xa8(%rbp), %eax
cvtsi2sd %rax, %xmm0
movl -0xcc(%rbp), %eax
cvtsi2sd %rax, %xmm1
mulsd -0x28(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x278a06
movl -0xa8(%rbp), %eax
movl %eax, %ecx
cvtsi2sd %rax, %xmm1
movl -0xcc(%rbp), %eax
subl %ecx, %eax
cvtsi2sd %rax, %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0x1066f2(%rip), %xmm2   # 0x37f010
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0xa0(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x278a04
jmp 0x278945
movq -0x90(%rbp), %rax
movq (%rax), %rax
movq -0x90(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x2789cb
movq -0x90(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x278981
movq -0x90(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xe8(%rbp)
jmp 0x27898f
movl $0x2, %eax
movq %rax, -0xe8(%rbp)
jmp 0x27898f
movq -0xe8(%rbp), %rcx
movq -0x90(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x90(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x90(%rbp), %rax
imulq $0x30, 0x8(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq -0x90(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x90(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x90(%rbp), %rcx
movq (%rcx), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, (%rcx)
imulq $0x30, %rax, %rax
addq %rax, %rdi
leaq -0xc0(%rbp), %rsi
movl $0x30, %edx
callq 0x2180
jmp 0x278a04
jmp 0x278a06
jmp 0x278a08
movq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x278a12
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x27837b
movq -0x60(%rbp), %rdi
callq 0x2210
movq -0x48(%rbp), %rdi
callq 0x2210
movq -0x78(%rbp), %rdi
callq 0x2210
movq -0x90(%rbp), %rax
addq $0xf0, %rsp
popq %rbp
retq
nop