// File: /workspace/github_cpp_makefile_star2_O3/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 $0xa8, %rsp
movsd %xmm0, 0xa0(%rsp)
movl %ecx, %ebx
movq %rdx, 0x98(%rsp)
movq %rsi, 0x90(%rsp)
movq %rdi, %r15
movl $0x1, %r13d
movl $0x1, %edi
movl $0x30, %esi
callq 0x2120
movl (%r15), %ecx
testq %rcx, %rcx
movq %rax, 0x50(%rsp)
je 0x19cb9a
movl %ebx, %r12d
xorl %esi, %esi
movq $0x0, 0x60(%rsp)
movq $0x0, 0x8(%rsp)
xorl %r14d, %r14d
movq $0x0, 0x58(%rsp)
movq $0x0, (%rsp)
movl %ebx, 0x44(%rsp)
xorl %ebp, %ebp
movq %r15, 0x10(%rsp)
movq %r12, 0x20(%rsp)
movq 0x8(%r15), %rax
cmpq %rcx, %r13
jne 0x19c77f
movq %rsi, %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %rcx
jmp 0x19c79b
movq %r13, %rdx
shlq $0x4, %rdx
movq %rsi, %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %rcx
cmpq %rcx, (%rax,%rdx)
je 0x19c935
movq %rcx, %rdx
shrq $0x21, %rdx
shrl %ecx
cmpl %ecx, %edx
je 0x19c932
movq %r13, %rbx
subq %rsi, %rbx
cmpq %r12, %rbx
jl 0x19c932
cmpq %rsi, %r13
movq %rsi, 0x38(%rsp)
jle 0x19c823
movq %rsi, %r15
xorl %r12d, %r12d
cmpq %rbp, %r12
jne 0x19c7fc
leaq (%r12,%r12), %rbp
testq %r12, %r12
movl $0x2, %eax
cmoveq %rax, %rbp
leaq (,%rbp,8), %rsi
movq %r14, %rdi
callq 0x2370
movq %rax, %r14
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movq %r15, %rcx
shlq $0x4, %rcx
movq 0x8(%rax,%rcx), %rcx
movq %rcx, (%r14,%r12,8)
incq %r12
incq %r15
cmpq %rbx, %r12
jne 0x19c7cb
movq 0x10(%rsp), %r15
movq 0x20(%rsp), %r12
jmp 0x19c825
xorl %ebx, %ebx
leaq (%r14,%rbx,8), %rsi
movq %r14, %rdi
callq 0x19b437
cmpq %rbx, 0x58(%rsp)
jae 0x19c88a
leaq -0x1(%rbx), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x4(,%rcx,4), %rsi
incq %rcx
movq %rcx, 0x58(%rsp)
movq (%rsp), %rdi
callq 0x2370
movq %rax, (%rsp)
leaq 0x1(%rbx), %rax
cmpq %rax, 0x60(%rsp)
jae 0x19c8e2
movq %rbx, %rax
shrq %rax
orq %rbx, %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, 0x60(%rsp)
movq 0x8(%rsp), %rdi
callq 0x2370
jmp 0x19c8e7
movq 0x8(%rsp), %rax
movl %ebx, %edi
movq %r14, %rsi
movq (%rsp), %rdx
movq %rax, %rcx
movq %rax, 0x8(%rsp)
callq 0x19c467
movl %eax, %ebx
testl %eax, %eax
sete %al
cmpl 0x44(%rsp), %ebx
setb %cl
orb %al, %cl
jne 0x19c932
movq 0x8(%r15), %rax
movq 0x38(%rsp), %rcx
shlq $0x4, %rcx
movq (%rax,%rcx), %rcx
movq %rcx, %r8
shrq $0x21, %r8
movl %ecx, %r10d
shrl %r10d
cmpl %r8d, %r10d
jne 0x19c94d
movq %r13, %rsi
leaq 0x1(%r13), %rax
movl (%r15), %ecx
cmpq %rcx, %r13
movq %rax, %r13
jb 0x19c769
jmp 0x19cbae
movq (%rsp), %rsi
movslq (%rsi), %rax
movq (%r14,%rax,8), %rdx
movq %rdx, 0x28(%rsp)
shrq $0x20, %rdx
leal -0x1(%rbx), %edi
movslq (%rsi,%rdi,4), %rsi
movq (%r14,%rsi,8), %rsi
movl %esi, %r11d
shrq $0x20, %rsi
movq %rcx, %r9
shrq $0x20, %r9
xorl %ecx, %r9d
movq 0x98(%rsp), %rcx
movq 0x10(%rcx), %rcx
leaq (%r8,%r8,4), %rdi
movl (%rcx,%rdi,8), %r12d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %r12
movl %r10d, 0x1c(%rsp)
movl %r10d, %edi
movq %rdi, 0x30(%rsp)
leaq (%rdi,%rdi,4), %rdi
movq %rbx, 0x38(%rsp)
movl (%rcx,%rdi,8), %r15d
andq %rax, %r15
movq 0x28(%rsp), %r10
movq %r10, %rcx
subq %rdx, %rcx
movq %rsi, %rdi
notq %rdi
addq %r12, %rdi
leaq (%rdi,%r11), %rax
notq %r11
addq %r15, %r11
cmpq %r11, %rdi
leaq -0x1(%r15), %rdi
cmovleq %rax, %rdi
leal 0x1(%rsi,%r11), %esi
cmovgl %esi, %r12d
movq %rdi, %rsi
notq %rsi
addq %r15, %rsi
subl %r10d, %edx
movl $0x0, %ebx
cmovbeq %rcx, %rbx
movl $0x0, %eax
cmovbl %eax, %edx
subl %ebx, %r15d
movq %r9, 0x80(%rsp)
testb $0x1, %r9b
cmovneq %rsi, %rbx
leal 0x1(%rdi), %eax
cmovel %eax, %r15d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %r12, 0x78(%rsp)
leal (%r12,%rax), %ecx
movq 0x90(%rsp), %r12
movq %r12, %rdi
movq %r8, 0x48(%rsp)
movq %r8, %rsi
movq %rdx, 0x88(%rsp)
callq 0x19c640
movl %eax, 0x28(%rsp)
movq %rbx, 0x68(%rsp)
movl %ebx, %edx
movq %r15, 0x70(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
leal (%r15,%rax), %ecx
movq 0x38(%rsp), %rbx
movq %r12, %rdi
movq 0x30(%rsp), %rsi
callq 0x19c640
movl 0x28(%rsp), %ecx
cmpl %eax, %ecx
cmovbl %ecx, %eax
cmpl %ebx, %eax
cmovbel %ebx, %eax
movl %ebx, %ecx
cvtsi2sd %rcx, %xmm1
cvtsi2sd %rax, %xmm0
mulsd 0xa0(%rsp), %xmm0
ucomisd %xmm0, %xmm1
jb 0x19cb88
subl %ebx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0xa2ea1(%rip), %xmm0    # 0x23f958
addsd %xmm0, %xmm1
ucomisd 0x94b55(%rip), %xmm1    # 0x231618
jbe 0x19cb88
movq 0x50(%rsp), %r12
movq (%r12), %r15
movq 0x10(%r12), %rax
cmpq 0x8(%r12), %r15
jne 0x19cb14
leaq (%r15,%r15), %rcx
testq %r15, %r15
movl $0x2, %edx
cmoveq %rdx, %rcx
movq %rcx, 0x8(%r12)
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
movq %rax, %rdi
movsd %xmm1, 0x30(%rsp)
callq 0x2370
movsd 0x30(%rsp), %xmm1
movq %rax, 0x10(%r12)
movq 0x48(%rsp), %rdx
movl 0x1c(%rsp), %esi
movq 0x80(%rsp), %r8
shlb $0x6, %r8b
andb $0x40, %r8b
leaq 0x1(%r15), %rcx
movq %rcx, (%r12)
leaq (%r15,%r15,2), %rcx
shlq $0x4, %rcx
movq 0x88(%rsp), %rdi
movl %edi, (%rax,%rcx)
movq 0x78(%rsp), %rdi
movl %edi, 0x4(%rax,%rcx)
movl %edx, 0x8(%rax,%rcx)
movq 0x68(%rsp), %rdx
movl %edx, 0xc(%rax,%rcx)
movq 0x70(%rsp), %rdx
movl %edx, 0x10(%rax,%rcx)
movl %esi, 0x14(%rax,%rcx)
movl %ebx, 0x18(%rax,%rcx)
xorl %edx, %edx
movl %edx, 0x1c(%rax,%rcx)
movsd %xmm1, 0x20(%rax,%rcx)
movb %r8b, 0x28(%rax,%rcx)
movl %edx, 0x29(%rax,%rcx)
movl %edx, 0x2c(%rax,%rcx)
movq %r13, %rsi
movq 0x10(%rsp), %r15
movq 0x20(%rsp), %r12
jmp 0x19c935
movq $0x0, (%rsp)
xorl %r14d, %r14d
movq $0x0, 0x8(%rsp)
movq (%rsp), %rdi
callq 0x2220
movq %r14, %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
callq 0x2220
movq 0x50(%rsp), %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq