// File: /workspace/github_c_makefile_star2_O2/lh3[P]minimap2/pe.c
// Function: mm_pair
void mm_pair(void *km, int max_gap_ref, int pe_bonus, int sub_diff, int match_sc, const int *qlens, int *n_regs, mm_reg1_t **regs)
{
	int i, j, s, n, last[2], dp_thres, segs = 0, max_idx[2];
	int64_t max;
	pair_arr_t *a;
	kvec_t(uint64_t) sc = {0,0,0};

	a = (pair_arr_t*)kmalloc(km, (n_regs[0] + n_regs[1]) * sizeof(pair_arr_t));
	for (s = n = 0, dp_thres = 0; s < 2; ++s) {
		int max = 0;
		for (i = 0; i < n_regs[s]; ++i) {
			a[n].s = s;
			a[n].r = &regs[s][i];
			a[n].rev = a[n].r->rev;
			a[n].key = (uint64_t)a[n].r->rid << 32 | a[n].r->rs<<1 | (s^a[n].rev);
			max = max > a[n].r->p->dp_max? max : a[n].r->p->dp_max;
			++n;
			segs |= 1<<s;
		}
		dp_thres += max;
	}
	if (segs != 3) {
		kfree(km, a); // only one end is mapped
		return;
	}
	dp_thres -= pe_bonus;
	if (dp_thres < 0) dp_thres = 0;
	radix_sort_pair(a, a + n);

	max = -1;
	max_idx[0] = max_idx[1] = -1;
	last[0] = last[1] = -1;
	kv_resize(uint64_t, km, sc, (size_t)n);
	for (i = 0; i < n; ++i) {
		if (a[i].key & 1) { // reverse first read or forward second read
			mm_reg1_t *q, *r;
			if (last[a[i].rev] < 0) continue;
			r = a[i].r;
			q = a[last[a[i].rev]].r;
			if (r->rid != q->rid || r->rs - q->re > max_gap_ref) continue;
			for (j = last[a[i].rev]; j >= 0; --j) {
				int64_t score;
				if (a[j].rev != a[i].rev || a[j].s == a[i].s) continue;
				q = a[j].r;
				if (r->rid != q->rid || r->rs - q->re > max_gap_ref) break;
				if (r->p->dp_max + q->p->dp_max < dp_thres) continue;
				score = (int64_t)(r->p->dp_max + q->p->dp_max) << 32 | (r->hash + q->hash);
				if (score > max)
					max = score, max_idx[a[j].s] = j, max_idx[a[i].s] = i;
				kv_push(uint64_t, km, sc, score);
			}
		} else { // forward first read or reverse second read
			last[a[i].rev] = i;
		}
	}
	if (sc.n > 1)
		radix_sort_64(sc.a, sc.a + sc.n);

	if (sc.n > 0 && max > 0) { // found at least one pair
		int n_sub = 0, mapq_pe;
		mm_reg1_t *r[2];
		r[0] = a[max_idx[0]].r, r[1] = a[max_idx[1]].r;
		r[0]->proper_frag = r[1]->proper_frag = 1;
		for (s = 0; s < 2; ++s) {
			if (r[s]->id != r[s]->parent) { // then lift to primary and update parent
				mm_reg1_t *p = &regs[s][r[s]->parent];
				for (i = 0; i < n_regs[s]; ++i)
					if (regs[s][i].parent == p->id)
						regs[s][i].parent = r[s]->id;
				p->mapq = 0;
			}
			if (!r[s]->sam_pri) { // then sync sam_pri
				for (i = 0; i < n_regs[s]; ++i)
					regs[s][i].sam_pri = 0;
				r[s]->sam_pri = 1;
			}
		}
		mapq_pe = r[0]->mapq > r[1]->mapq? r[0]->mapq : r[1]->mapq;
		for (i = 0; i < (int)sc.n; ++i)
			if ((sc.a[i]>>32) + sub_diff >= (uint64_t)max>>32)
				++n_sub;
		if (sc.n > 1) {
			int mapq_pe_alt;
			mapq_pe_alt = (int)(6.02f * ((max>>32) - (sc.a[sc.n - 2]>>32)) / match_sc - 4.343f * logf(n_sub)); // n_sub > 0 because it counts the optimal, too
			mapq_pe = mapq_pe < mapq_pe_alt? mapq_pe : mapq_pe_alt;
		}
		if (r[0]->mapq < mapq_pe) r[0]->mapq = (int)(.2f * r[0]->mapq + .8f * mapq_pe + .499f);
		if (r[1]->mapq < mapq_pe) r[1]->mapq = (int)(.2f * r[1]->mapq + .8f * mapq_pe + .499f);
		if (sc.n == 1) {
			if (r[0]->mapq < 2) r[0]->mapq = 2;
			if (r[1]->mapq < 2) r[1]->mapq = 2;
		} else if ((uint64_t)max>>32 > sc.a[sc.n - 2]>>32) {
			if (r[0]->mapq < 1) r[0]->mapq = 1;
			if (r[1]->mapq < 1) r[1]->mapq = 1;
		}
	}

	kfree(km, a);
	kfree(km, sc.a);

	mm_set_pe_thru(qlens, n_regs, regs);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, 0x60(%rsp)
movl %r8d, 0x24(%rsp)
movl %ecx, 0x48(%rsp)
movl %edx, (%rsp)
movl %esi, 0xc(%rsp)
movq 0xc8(%rsp), %r14
movq 0xc0(%rsp), %r15
movslq (%r15), %rax
movslq 0x4(%r15), %rcx
addq %rax, %rcx
imulq $0x18, %rcx, %rsi
movq %rdi, 0x28(%rsp)
callq 0x10afc
movq %rax, 0x10(%rsp)
addq $0x10, %rax
xorl %ecx, %ecx
xorl %r10d, %r10d
xorl %ebx, %ebx
xorl %r12d, %r12d
movslq %r12d, %r13
cmpq $0x2, %rcx
je 0xf91f
movl %r10d, %edx
btsl %ecx, %edx
imulq $0x18, %r13, %rsi
addq %rax, %rsi
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
movslq (%r15,%rcx,4), %r11
cmpq %r11, %r8
jge 0xf911
movl %ecx, -0x10(%rsi)
movq (%r14,%rcx,8), %r10
leaq (%r10,%rdi), %r11
movq %r11, (%rsi)
movl 0x3c(%r10,%rdi), %r11d
shrl $0xa, %r11d
andl $0x1, %r11d
movl %r11d, -0xc(%rsi)
movl 0x8(%r10,%rdi), %r13d
movl 0x18(%r10,%rdi), %ebp
shlq $0x20, %r13
addl %ebp, %ebp
movslq %ebp, %rbp
xorl %ecx, %r11d
orq %r13, %r11
orq %rbp, %r11
movq %r11, -0x8(%rsi)
movq 0x48(%r10,%rdi), %r10
movl 0x8(%r10), %r10d
cmpl %r10d, %r9d
cmovlel %r10d, %r9d
incq %r8
addq $0x50, %rdi
addq $0x18, %rsi
movl %edx, %r10d
jmp 0xf8a9
addl %r8d, %r12d
addl %r9d, %ebx
incq %rcx
jmp 0xf887
cmpl $0x3, %r10d
jne 0xf9b2
subl (%rsp), %ebx
xorl %eax, %eax
testl %ebx, %ebx
cmovlel %eax, %ebx
movl %ebx, 0x4c(%rsp)
imulq $0x18, %r13, %rsi
movq 0x10(%rsp), %rbx
addq %rbx, %rsi
movq %rbx, %rdi
callq 0xf808
orq $-0x1, 0x30(%rsp)
orq $-0x1, 0x58(%rsp)
testl %r13d, %r13d
je 0xf9d2
decq %r13
movq %r13, %rax
shrq %rax
orq %r13, %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 0x8(,%rax,8), %rdx
incq %rax
movq %rax, 0x40(%rsp)
movq 0x28(%rsp), %rdi
xorl %esi, %esi
callq 0x10c04
movq %rax, (%rsp)
jmp 0xf9e3
movq 0x28(%rsp), %rdi
movq 0x10(%rsp), %rsi
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10a07
movq $0x0, 0x40(%rsp)
movq $0x0, (%rsp)
movl 0xc(%rsp), %r8d
testl %r12d, %r12d
movl $0x0, %edi
cmovlel %edi, %r12d
pushq $-0x1
popq %r9
xorl %r10d, %r10d
cmpq %r12, %rdi
je 0xfb50
imulq $0x18, %rdi, %r13
testb $0x1, 0x8(%rbx,%r13)
movslq 0x4(%rbx,%r13), %rax
jne 0xfa1b
movl %edi, 0x58(%rsp,%rax,4)
jmp 0xfa4a
movslq 0x58(%rsp,%rax,4), %rbp
testq %rbp, %rbp
js 0xfa4a
addq %rbx, %r13
movq 0x10(%r13), %r11
imulq $0x18, %rbp, %rax
movq 0x10(%rbx,%rax), %rax
movl 0x8(%r11), %ecx
cmpl 0x8(%rax), %ecx
jne 0xfa4a
movl 0x18(%r11), %ecx
subl 0x1c(%rax), %ecx
cmpl %r8d, %ecx
jle 0xfa4f
incq %rdi
jmp 0xf9fb
movq %rdi, 0x18(%rsp)
movq %r11, 0x50(%rsp)
testl %ebp, %ebp
js 0xfa4a
movl %ebp, %eax
imulq $0x18, %rax, %rdx
movl 0x4(%rbx,%rdx), %eax
cmpl 0x4(%r13), %eax
jne 0xfb49
addq %rbx, %rdx
movslq (%rdx), %rax
movslq (%r13), %rcx
cmpl %ecx, %eax
je 0xfb49
movq 0x10(%rdx), %rdx
movl 0x8(%r11), %esi
cmpl 0x8(%rdx), %esi
jne 0xfa4a
movl 0x18(%r11), %esi
subl 0x1c(%rdx), %esi
cmpl %r8d, %esi
jg 0xfa4a
movq 0x48(%r11), %rsi
movslq 0x8(%rsi), %rdi
movq 0x48(%rdx), %rsi
movslq 0x8(%rsi), %rsi
addq %rdi, %rsi
cmpl 0x4c(%rsp), %esi
jge 0xfabf
movq 0x18(%rsp), %rdi
jmp 0xfb49
shlq $0x20, %rsi
movl 0x40(%rdx), %ebx
addl 0x40(%r11), %ebx
orq %rsi, %rbx
cmpq %r9, %rbx
movq 0x18(%rsp), %rdi
jle 0xfae2
movl %ebp, 0x30(%rsp,%rax,4)
movl %edi, 0x30(%rsp,%rcx,4)
movq %rbx, %r9
cmpq 0x40(%rsp), %r10
jne 0xfb34
leaq (%r10,%r10), %rax
testq %r10, %r10
pushq $0x2
popq %rcx
cmoveq %rcx, %rax
movq %rax, 0x40(%rsp)
leaq (,%rax,8), %rdx
movq 0x28(%rsp), %rdi
movq (%rsp), %rsi
movq %r9, 0x38(%rsp)
movq %r10, 0x68(%rsp)
callq 0x10c04
movq 0x18(%rsp), %rdi
movl 0xc(%rsp), %r8d
movq 0x68(%rsp), %r10
movq 0x38(%rsp), %r9
movq %rax, (%rsp)
movq 0x50(%rsp), %r11
movq (%rsp), %rax
movq %rbx, (%rax,%r10,8)
incq %r10
movq 0x10(%rsp), %rbx
decl %ebp
jmp 0xfa59
cmpq $0x2, %r10
movq %r10, %rbp
jb 0xfb6f
movq (%rsp), %rdi
leaq (%rdi,%r10,8), %rsi
movq %r9, %r12
callq 0x49de
movq %rbp, %r10
movq %r12, %r9
testq %r10, %r10
je 0xfde7
testq %r9, %r9
jle 0xfde7
movslq 0x30(%rsp), %rax
imulq $0x18, %rax, %rax
movq 0x10(%rsp), %rcx
movq 0x10(%rcx,%rax), %rbx
movq %rbx, 0x70(%rsp)
movslq 0x34(%rsp), %rax
imulq $0x18, %rax, %rax
movq 0x10(%rcx,%rax), %r12
movq %r12, 0x78(%rsp)
orb $0x20, 0x3d(%r12)
orb $0x20, 0x3d(%rbx)
xorl %eax, %eax
pushq $0x3c
popq %rcx
cmpq $0x2, %rax
je 0xfc45
movq 0x70(%rsp,%rax,8), %rdx
movl (%rdx), %esi
movslq 0x20(%rdx), %rdi
cmpl %edi, %esi
je 0xfc10
movq %r9, %r13
movq (%r14,%rax,8), %r8
imulq $0x50, %rdi, %rdi
addq %r8, %rdi
addq $0x20, %r8
xorl %r9d, %r9d
movslq (%r15,%rax,4), %r10
cmpq %r10, %r9
jge 0xfc06
movl (%r8), %r10d
cmpl (%rdi), %r10d
jne 0xfbfd
movl %esi, (%r8)
incq %r9
addq $0x50, %r8
jmp 0xfbe9
movb $0x0, 0x3c(%rdi)
movq %r13, %r9
movq %rbp, %r10
testb $0x10, 0x3d(%rdx)
jne 0xfc3d
movq %rcx, %rsi
xorl %edi, %edi
movslq (%r15,%rax,4), %r8
cmpq %r8, %rdi
jge 0xfc39
movq (%r14,%rax,8), %r8
andl $0xffffefff, (%r8,%rsi) # imm = 0xFFFFEFFF
incq %rdi
addq $0x50, %rsi
jmp 0xfc1b
orb $0x10, 0x3d(%rdx)
incq %rax
jmp 0xfbbb
movl 0x3c(%rbx), %eax
movzbl 0x3c(%r12), %r13d
movslq 0x48(%rsp), %rdx
shrq $0x20, %r9
xorl %esi, %esi
testl %r10d, %r10d
movl $0x0, %edi
cmovgl %r10d, %edi
xorl %ecx, %ecx
cmpq %rsi, %rdi
je 0xfc83
movq (%rsp), %r8
movl 0x4(%r8,%rsi,8), %r8d
addq %rdx, %r8
cmpq %r9, %r8
sbbl $-0x1, %ecx
incq %rsi
jmp 0xfc67
movzbl %al, %edx
cmpl %r13d, %edx
cmoval %edx, %r13d
cmpq $0x2, %r10
jb 0xfd12
movq (%rsp), %rax
movl -0xc(%rax,%r10,8), %edx
movq %r9, %rax
subq %rdx, %rax
movq %r9, 0x38(%rsp)
js 0xfcb0
cvtsi2ss %rax, %xmm0
jmp 0xfcc5
movq %rax, %rdx
shrq %rdx
andl $0x1, %eax
orq %rdx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movl 0x24(%rsp), %eax
mulss 0x1928f(%rip), %xmm0    # 0x28f60
movaps %xmm0, %xmm1
cvtsi2ss %eax, %xmm0
divss %xmm0, %xmm1
movss %xmm1, 0x18(%rsp)
cvtsi2ss %ecx, %xmm0
callq 0x2200
mulss 0x19271(%rip), %xmm0    # 0x28f64
addss 0x18(%rsp), %xmm0
cvttss2si %xmm0, %eax
cmpl %eax, %r13d
cmovgel %eax, %r13d
movl 0x3c(%rbx), %eax
movzbl %al, %edx
movq 0x38(%rsp), %r9
movq %rbp, %r10
cvtsi2ss %r13d, %xmm0
cmpl %r13d, %edx
jge 0xfd51
cvtsi2ss %edx, %xmm1
movss 0x19240(%rip), %xmm2    # 0x28f68
mulss %xmm0, %xmm2
mulss 0x18a54(%rip), %xmm1    # 0x28788
addss %xmm2, %xmm1
addss 0x1922c(%rip), %xmm1    # 0x28f6c
cvttss2si %xmm1, %ecx
movzbl %cl, %ecx
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
orl %ecx, %eax
movl %eax, 0x3c(%rbx)
movl 0x3c(%r12), %eax
movzbl %al, %ecx
cmpl %r13d, %ecx
jge 0xfd91
cvtsi2ss %ecx, %xmm1
mulss 0x191fe(%rip), %xmm0    # 0x28f68
mulss 0x18a16(%rip), %xmm1    # 0x28788
addss %xmm0, %xmm1
addss 0x191ee(%rip), %xmm1    # 0x28f6c
cvttss2si %xmm1, %ecx
movzbl %cl, %ecx
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
orl %ecx, %eax
movl %eax, 0x3c(%r12)
cmpq $0x1, %r10
jne 0xfdbe
movl 0x3c(%rbx), %ecx
testb $-0x2, %cl
jne 0xfdb0
andl $0xffffff00, %ecx       # imm = 0xFFFFFF00
orl $0x2, %ecx
movl %ecx, 0x3c(%rbx)
movl 0x3c(%r12), %eax
testb $-0x2, %al
jne 0xfde7
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
orl $0x2, %eax
jmp 0xfde2
movq (%rsp), %rcx
cmpl -0xc(%rcx,%r10,8), %r9d
jbe 0xfde7
movl 0x3c(%rbx), %ecx
testb %cl, %cl
jne 0xfddb
orl $0x1, %ecx
movl %ecx, 0x3c(%rbx)
movl 0x3c(%r12), %eax
testb %al, %al
jne 0xfde7
orl $0x1, %eax
movl %eax, 0x3c(%r12)
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x10a07
movq %rbx, %rdi
movq (%rsp), %rsi
callq 0x10a07
movq 0x60(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf4a5
nop