// File: /workspace/github_cpp_makefile_star2_O1/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, 0xa8(%rsp)
movl %ecx, %ebp
movq %rdx, 0xa0(%rsp)
movq %rsi, 0x98(%rsp)
movq %rdi, %r15
movl $0x1, %ebx
movl $0x1, %edi
movl $0x30, %esi
callq 0x2120
movq %rax, 0x48(%rsp)
movl (%r15), %ecx
xorl %r12d, %r12d
testq %rcx, %rcx
je 0x193e11
movl %ebp, %eax
movq %rax, 0xb0(%rsp)
xorl %eax, %eax
movq $0x0, 0x68(%rsp)
xorl %r13d, %r13d
movq $0x0, 0x8(%rsp)
movq $0x0, 0x60(%rsp)
movq $0x0, 0x18(%rsp)
xorl %r8d, %r8d
movl %ebp, 0x34(%rsp)
cmpq %rcx, %rbx
je 0x1939ac
movq 0x8(%r15), %rcx
movq %rbx, %rdx
shlq $0x4, %rdx
movq (%rcx,%rdx), %rdx
movq %rax, %rsi
shlq $0x4, %rsi
cmpq (%rcx,%rsi), %rdx
je 0x193b6d
movq 0x8(%r15), %rcx
movq %rax, %r14
shlq $0x4, %r14
movq (%rcx,%r14), %rcx
movq %rcx, %rdx
shrq $0x21, %rdx
shrl %ecx
cmpl %ecx, %edx
je 0x1939e2
movl %r12d, 0x3b(%rsp)
movl %r12d, 0x38(%rsp)
movq %rbx, %rcx
subq %rax, %rcx
cmpq 0xb0(%rsp), %rcx
jge 0x1939ea
movq %rbx, %rax
jmp 0x193b6d
movq %r13, 0x10(%rsp)
movq %rbx, 0x20(%rsp)
movq %rbx, %rbp
subq %rax, %rbp
movq 0x8(%rsp), %rdi
jle 0x193a4f
movq %r14, %r13
xorl %ebx, %ebx
cmpq %r8, %rbx
jne 0x193a34
movq %r15, %r12
leaq (%r8,%r8), %r15
testq %r8, %r8
movl $0x2, %eax
cmoveq %rax, %r15
leaq (,%r15,8), %rsi
callq 0x2370
movq %rax, %rdi
movq %r15, %r8
movq %r12, %r15
movq 0x8(%r15), %rax
movq 0x8(%rax,%r13), %rax
movq %rax, (%rdi,%rbx,8)
incq %rbx
addq $0x10, %r13
cmpq %rbx, %rbp
jne 0x193a06
jmp 0x193a51
xorl %ebx, %ebx
movq %r8, 0x28(%rsp)
leaq (%rdi,%rbx,8), %rsi
movq %rdi, 0x8(%rsp)
callq 0x1926ca
cmpq %rbx, 0x60(%rsp)
jae 0x193abf
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, 0x60(%rsp)
movq 0x18(%rsp), %rdi
callq 0x2370
movq %rax, 0x18(%rsp)
movl 0x34(%rsp), %ebp
xorl %r12d, %r12d
movq 0x10(%rsp), %r13
leaq 0x1(%rbx), %rax
cmpq %rax, 0x68(%rsp)
jae 0x193b22
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, 0x68(%rsp)
movq %r13, %rdi
callq 0x2370
movq %rax, %r13
movq 0x8(%rsp), %rsi
movl %ebx, %edi
movq 0x18(%rsp), %rdx
movq %r13, %rcx
callq 0x193680
movl %eax, %r9d
testl %eax, %eax
sete %al
cmpl %ebp, %r9d
setb %cl
orb %al, %cl
jne 0x193b60
movq 0x8(%r15), %rax
movq (%rax,%r14), %rcx
movq %rcx, %r8
shrq $0x21, %r8
movl %ecx, %ebx
shrl %ebx
cmpl %r8d, %ebx
jne 0x193b85
movq 0x20(%rsp), %rbx
movq %rbx, %rax
movq 0x28(%rsp), %r8
leaq 0x1(%rbx), %rdx
movl (%r15), %ecx
cmpq %rcx, %rbx
movq %rdx, %rbx
jb 0x193987
jmp 0x193e26
movq %r13, 0x10(%rsp)
movq 0x18(%rsp), %rdi
movslq (%rdi), %rax
movq 0x8(%rsp), %rsi
movq (%rsi,%rax,8), %rax
leal -0x1(%r9), %edx
movslq (%rdi,%rdx,4), %rdx
movq (%rsi,%rdx,8), %rsi
movq %rsi, %rdx
shrq $0x20, %rdx
movq 0xa0(%rsp), %rdi
movq 0x10(%rdi), %rdi
movq %r8, %r13
leaq (%r8,%r8,4), %r8
movl (%rdi,%r8,8), %r14d
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
andq %r10, %r14
movq %rdx, %r8
notq %r8
addq %r14, %r8
movq %r9, 0x58(%rsp)
leal (%r8,%rsi), %r9d
notl %esi
movabsq $-0x100000000, %r11     # imm = 0xFFFFFFFF00000000
orq %r11, %rsi
movq %rcx, %r11
shrq $0x20, %r11
xorl %ecx, %r11d
movl %ebx, 0x54(%rsp)
movl %ebx, %ecx
movq %rcx, 0x40(%rsp)
leaq (%rcx,%rcx,4), %rcx
movl (%rdi,%rcx,8), %ebx
andq %r10, %rbx
movq %rbx, %rdi
addq %rsi, %rdi
movq %rax, %rsi
shrq $0x20, %rsi
cmpq %rdi, %r8
leal -0x1(%rbx), %ecx
cmovlel %r9d, %ecx
leal 0x1(%rdx,%rdi), %edx
cmovgl %edx, %r14d
movl %esi, %edi
subl %eax, %edi
movl %edi, %edx
cmovbl %r12d, %edx
negl %edi
cmpl %esi, %eax
movl %ecx, %ebp
notl %ebp
cmovbl %r12d, %edi
incl %ecx
addl %ebx, %ebp
subl %edi, %ebx
movq %r11, 0x80(%rsp)
testb $0x1, %r11b
cmovel %edi, %ebp
cmovel %ecx, %ebx
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq %r14, 0x88(%rsp)
leal (%r14,%r12), %ecx
movq 0x98(%rsp), %r14
movq %r14, %rdi
movq %r13, 0x90(%rsp)
movq %r13, %rsi
movq %rdx, 0x78(%rsp)
callq 0x193865
movl %eax, %r13d
leal (%rbx,%r12), %ecx
movq %r14, %rdi
movq 0x40(%rsp), %rsi
movq %rbp, 0x40(%rsp)
movq %rbp, %rdx
callq 0x193865
movq 0x58(%rsp), %rsi
cmpl %eax, %r13d
cmovbl %r13d, %eax
cmpl %esi, %eax
cmovbel %esi, %eax
movl %esi, %ecx
cvtsi2sd %rcx, %xmm1
cvtsi2sd %rax, %xmm0
mulsd 0xa8(%rsp), %xmm0
ucomisd %xmm0, %xmm1
jae 0x193cea
movq 0x20(%rsp), %rbx
movq %rbx, %rax
xorl %r12d, %r12d
movq 0x10(%rsp), %r13
jmp 0x193b68
subl %esi, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x9fa0c(%rip), %xmm0    # 0x233708
addsd %xmm0, %xmm1
ucomisd 0x91810(%rip), %xmm1    # 0x225518
movq 0x48(%rsp), %rdx
movq 0x28(%rsp), %r8
jbe 0x193dfc
movq (%rdx), %r14
movq 0x8(%rdx), %rax
cmpq %rax, %r14
movl $0x0, %r12d
movq 0x10(%rsp), %r13
jne 0x193d73
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%rdx)
movq 0x10(%rdx), %rdi
shlq $0x4, %rcx
leaq (%rcx,%rcx,2), %rsi
movsd %xmm1, 0x70(%rsp)
callq 0x2370
movsd 0x70(%rsp), %xmm1
movq 0x58(%rsp), %rsi
movq 0x28(%rsp), %r8
movq 0x48(%rsp), %rdx
movq %rax, 0x10(%rdx)
movq 0x80(%rsp), %rdi
shlb $0x6, %dil
andb $0x40, %dil
movq 0x10(%rdx), %rax
leaq 0x1(%r14), %rcx
movq %rcx, (%rdx)
leaq (%r14,%r14,2), %rcx
shlq $0x4, %rcx
movq 0x78(%rsp), %rdx
movl %edx, (%rax,%rcx)
movq 0x88(%rsp), %rdx
movl %edx, 0x4(%rax,%rcx)
movq 0x90(%rsp), %rdx
movl %edx, 0x8(%rax,%rcx)
movq 0x40(%rsp), %rdx
movl %edx, 0xc(%rax,%rcx)
movl %ebx, 0x10(%rax,%rcx)
movl 0x54(%rsp), %edx
movl %edx, 0x14(%rax,%rcx)
movl %esi, 0x18(%rax,%rcx)
movl $0x0, 0x1c(%rax,%rcx)
movsd %xmm1, 0x20(%rax,%rcx)
movb %dil, 0x28(%rax,%rcx)
movl 0x38(%rsp), %edx
movl 0x3b(%rsp), %esi
movl %edx, 0x29(%rax,%rcx)
movl %esi, 0x2c(%rax,%rcx)
movq 0x20(%rsp), %rbx
jmp 0x1939e2
movq 0x20(%rsp), %rbx
movq %rbx, %rax
xorl %r12d, %r12d
movq 0x10(%rsp), %r13
jmp 0x193b6d
movq $0x0, 0x18(%rsp)
movq $0x0, 0x8(%rsp)
xorl %r13d, %r13d
movq 0x18(%rsp), %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
callq 0x2220
movq %r13, %rdi
callq 0x2220
movq 0x48(%rsp), %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq