// File: /workspace/github_c_makefile_star2_O3/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 $0x78, %rsp
movq %r9, 0x70(%rsp)
movl %r8d, 0x48(%rsp)
movl %ecx, 0x4c(%rsp)
movl %edx, 0x30(%rsp)
movl %esi, 0x1c(%rsp)
movq 0xb8(%rsp), %r14
movq 0xb0(%rsp), %r15
movslq (%r15), %rax
movslq 0x4(%r15), %rcx
addq %rax, %rcx
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
movq %rdi, 0x38(%rsp)
callq 0x1264d
movq %rax, 0x10(%rsp)
addq $0x10, %rax
movq %rax, 0x20(%rsp)
xorl %ecx, %ecx
movb $0x1, %dl
xorl %r13d, %r13d
xorl %ebp, %ebp
xorl %eax, %eax
cmpl $0x0, (%r15,%rcx,4)
jle 0x1141e
movl %eax, 0x8(%rsp)
movl $0x1, %edi
shll %cl, %edi
movl %r13d, %esi
movslq %r13d, %r8
leaq (%r8,%r8,2), %r8
movq 0x20(%rsp), %rax
leaq (%rax,%r8,8), %r9
xorl %r10d, %r10d
xorl %r8d, %r8d
xorl %r11d, %r11d
movl %r11d, %r13d
movl %ecx, -0x10(%r9)
movq (%r14,%rcx,8), %r11
leaq (%r11,%r10), %r12
movq %r12, (%r9)
movl 0x3c(%r11,%r10), %r12d
shrl $0xa, %r12d
andl $0x1, %r12d
movl %r12d, -0xc(%r9)
movl 0x8(%r11,%r10), %eax
movl 0x18(%r11,%r10), %ebx
shlq $0x20, %rax
addl %ebx, %ebx
movslq %ebx, %rbx
xorl %ecx, %r12d
orq %rax, %r12
orq %rbx, %r12
movq %r12, -0x8(%r9)
movq 0x48(%r11,%r10), %rax
movl 0x8(%rax), %r11d
cmpl %r11d, %r13d
cmovgl %r13d, %r11d
incq %r8
movslq (%r15,%rcx,4), %rax
addq $0x18, %r9
addq $0x50, %r10
cmpq %rax, %r8
jl 0x113a9
movl 0x8(%rsp), %eax
orl %edi, %eax
movl %esi, %r13d
addl %r8d, %r13d
jmp 0x11421
xorl %r11d, %r11d
addl %r11d, %ebp
movl $0x1, %ecx
testb $0x1, %dl
movl $0x0, %edx
jne 0x11377
cmpl $0x3, %eax
jne 0x117df
subl 0x30(%rsp), %ebp
xorl %eax, %eax
testl %ebp, %ebp
cmovlel %eax, %ebp
movslq %r13d, %r12
leaq (%r12,%r12,2), %rax
movq 0x10(%rsp), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x112ec
movq $-0x1, %rax
movq %rax, 0x40(%rsp)
movq %rax, 0x58(%rsp)
testl %r12d, %r12d
je 0x117fc
leaq -0x1(%r12), %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, %rbx
shrq $0x10, %rbx
orq %rax, %rbx
leaq 0x8(,%rbx,8), %rdx
movq 0x38(%rsp), %rdi
xorl %esi, %esi
callq 0x12755
testl %r12d, %r12d
jle 0x11807
incq %rbx
movq %rbx, 0x50(%rsp)
movl %r13d, %r8d
movq $-0x1, 0x28(%rsp)
xorl %ebx, %ebx
xorl %r10d, %r10d
movq 0x10(%rsp), %rsi
movq %r8, 0x30(%rsp)
leaq (%rbx,%rbx,2), %rdx
testb $0x1, 0x8(%rsi,%rdx,8)
movslq 0x4(%rsi,%rdx,8), %rcx
jne 0x11509
movl %ebx, 0x58(%rsp,%rcx,4)
jmp 0x1165a
movslq 0x58(%rsp,%rcx,4), %r12
testq %r12, %r12
js 0x1165a
leaq (%rsi,%rdx,8), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rcx), %r9
leaq (%r12,%r12,2), %rdi
movq 0x10(%rsi,%rdi,8), %rcx
movl 0x8(%r9), %edx
cmpl 0x8(%rcx), %edx
jne 0x1165a
movl 0x18(%r9), %edx
subl 0x1c(%rcx), %edx
cmpl 0x1c(%rsp), %edx
jg 0x1165a
movq %r9, 0x20(%rsp)
leaq (%rsi,%rdi,8), %r13
movq 0x8(%rsp), %rdx
movl 0x4(%r13), %ecx
cmpl 0x4(%rdx), %ecx
jne 0x11645
movslq (%r13), %r9
movslq (%rdx), %rcx
cmpl %ecx, %r9d
je 0x11645
movq 0x10(%r13), %rdx
movq 0x20(%rsp), %r11
movl 0x8(%r11), %edi
cmpl 0x8(%rdx), %edi
jne 0x1165a
movl 0x18(%r11), %edi
subl 0x1c(%rdx), %edi
cmpl 0x1c(%rsp), %edi
jg 0x1165a
movq 0x48(%r11), %rdi
movslq 0x8(%rdi), %rdi
movq 0x48(%rdx), %r8
movslq 0x8(%r8), %r8
addq %rdi, %r8
cmpl %ebp, %r8d
jge 0x115c4
movq 0x30(%rsp), %r8
movq 0x8(%rsp), %rdx
jmp 0x11645
shlq $0x20, %r8
movl 0x40(%rdx), %edi
addl 0x40(%r11), %edi
orq %r8, %rdi
cmpq 0x28(%rsp), %rdi
jle 0x115e7
movl %r12d, 0x40(%rsp,%r9,4)
movl %ebx, 0x40(%rsp,%rcx,4)
movq %rdi, 0x28(%rsp)
movq 0x50(%rsp), %rcx
cmpq %rcx, %r10
jne 0x11634
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x50(%rsp)
leaq (,%rdx,8), %rdx
movq %rdi, 0x60(%rsp)
movq 0x38(%rsp), %rdi
movq %rax, %rsi
movq %r10, 0x68(%rsp)
callq 0x12755
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %r10
movq 0x10(%rsp), %rsi
movq 0x30(%rsp), %r8
movq 0x8(%rsp), %rdx
movq %rdi, (%rax,%r10,8)
incq %r10
leaq -0x1(%r12), %rcx
addq $-0x18, %r13
testq %r12, %r12
movq %rcx, %r12
jg 0x11559
incq %rbx
cmpq %r8, %rbx
jne 0x114f0
cmpq $0x2, %r10
movq %rax, 0x8(%rsp)
movq %r10, %rbp
jb 0x11688
leaq (%rax,%r10,8), %rsi
movq %rax, %rdi
callq 0x5c97
movq %rbp, %r10
movq 0x10(%rsp), %rsi
testq %r10, %r10
je 0x11811
cmpq $0x0, 0x28(%rsp)
jle 0x11811
movslq 0x40(%rsp), %rax
leaq (%rax,%rax,2), %rax
movq 0x10(%rsi,%rax,8), %rdx
movslq 0x44(%rsp), %rax
leaq (%rax,%rax,2), %rax
movq 0x10(%rsi,%rax,8), %rbx
orl $0x2000, 0x3c(%rbx)     # imm = 0x2000
orb $0x20, 0x3d(%rdx)
movb $0x1, %al
xorl %ecx, %ecx
movq %rdx, 0x20(%rsp)
movl (%rdx), %r13d
movslq 0x20(%rdx), %rdi
cmpl %edi, %r13d
je 0x11722
movq (%r14,%rcx,8), %r8
leaq (%rdi,%rdi,4), %rdi
shlq $0x4, %rdi
addq %r8, %rdi
movl (%r15,%rcx,4), %r9d
testl %r9d, %r9d
jle 0x11716
movl (%rdi), %r10d
addq $0x20, %r8
xorl %r11d, %r11d
cmpl %r10d, (%r8)
jne 0x11707
movl %r13d, (%r8)
movl (%r15,%rcx,4), %r9d
incq %r11
movslq %r9d, %r12
addq $0x50, %r8
cmpq %r12, %r11
jl 0x116fb
movb $0x0, 0x3c(%rdi)
movq 0x10(%rsp), %rsi
movq %rbp, %r10
movl 0x3c(%rdx), %edi
btl $0xc, %edi
jb 0x11762
cmpl $0x0, (%r15,%rcx,4)
jle 0x11759
movl $0x3c, %r9d
xorl %edi, %edi
movq (%r14,%rcx,8), %r8
andl $0xffffefff, (%r8,%r9)  # imm = 0xFFFFEFFF
incq %rdi
movslq (%r15,%rcx,4), %r8
addq $0x50, %r9
cmpq %r8, %rdi
jl 0x1173a
movl 0x3c(%rdx), %edi
orl $0x1000, %edi           # imm = 0x1000
movl %edi, 0x3c(%rdx)
movl $0x1, %ecx
movq %rbx, %rdx
testb $0x1, %al
movl $0x0, %eax
jne 0x116cd
movq 0x20(%rsp), %rdi
movl 0x3c(%rdi), %eax
movzbl %al, %ecx
movzbl 0x3c(%rbx), %r12d
cmpl %r12d, %ecx
cmoval %ecx, %r12d
testl %r10d, %r10d
jle 0x11849
movslq 0x4c(%rsp), %rdx
movq 0x28(%rsp), %r11
shrq $0x20, %r11
movl %r10d, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
xorl %r8d, %r8d
xorl %r9d, %r9d
movq 0x8(%rsp), %r13
movl 0x4(%r13,%r8,8), %r10d
addq %rdx, %r10
cmpq %r11, %r10
sbbl $-0x1, %r9d
incq %r8
cmpq %r8, %rdi
jne 0x117b9
cvtsi2ss %r9d, %xmm0
movq %rbp, %r10
movq 0x20(%rsp), %rdi
jmp 0x1184c
movq 0x38(%rsp), %rdi
movq 0x10(%rsp), %rsi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x12587
movq $0x0, 0x8(%rsp)
jmp 0x1180c
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rsi
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
callq 0x12587
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0x12587
movq 0x70(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10f6c
xorps %xmm0, %xmm0
cmpq $0x2, %r10
movq 0x8(%rsp), %rdx
jb 0x118de
movq 0x28(%rsp), %rax
shrq $0x20, %rax
movl -0xc(%rdx,%r10,8), %ecx
subq %rcx, %rax
js 0x11875
cvtsi2ss %rax, %xmm1
jmp 0x1188a
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm1
addss %xmm1, %xmm1
mulss 0x1c6ce(%rip), %xmm1    # 0x2df60
movaps %xmm1, %xmm2
xorps %xmm1, %xmm1
cvtsi2ssl 0x48(%rsp), %xmm1
divss %xmm1, %xmm2
movss %xmm2, 0x1c(%rsp)
callq 0x2200
mulss 0x1c6af(%rip), %xmm0    # 0x2df64
addss 0x1c(%rsp), %xmm0
cvttss2si %xmm0, %eax
cmpl %eax, %r12d
cmovgel %eax, %r12d
movq 0x20(%rsp), %rdi
movl 0x3c(%rdi), %eax
movzbl %al, %ecx
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %rbp, %r10
xorps %xmm0, %xmm0
cvtsi2ss %r12d, %xmm0
cmpl %r12d, %ecx
jge 0x11923
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
movss 0x1c66e(%rip), %xmm2    # 0x2df68
mulss %xmm0, %xmm2
mulss 0x1be82(%rip), %xmm1    # 0x2d788
addss %xmm2, %xmm1
addss 0x1c65a(%rip), %xmm1    # 0x2df6c
cvttss2si %xmm1, %ecx
movzbl %cl, %ecx
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
orl %ecx, %eax
movl %eax, 0x3c(%rdi)
movl 0x3c(%rbx), %eax
movzbl %al, %ecx
cmpl %r12d, %ecx
jge 0x11962
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
mulss 0x1c62b(%rip), %xmm0    # 0x2df68
mulss 0x1be43(%rip), %xmm1    # 0x2d788
addss %xmm0, %xmm1
addss 0x1c61b(%rip), %xmm1    # 0x2df6c
cvttss2si %xmm1, %ecx
movzbl %cl, %ecx
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
orl %ecx, %eax
movl %eax, 0x3c(%rbx)
cmpq $0x1, %r10
jne 0x11991
movl 0x3c(%rdi), %ecx
testb $-0x2, %cl
jne 0x1197f
andl $0xffffff00, %ecx       # imm = 0xFFFFFF00
orl $0x2, %ecx
movl %ecx, 0x3c(%rdi)
movl 0x3c(%rbx), %eax
testb $-0x2, %al
jne 0x11811
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
orl $0x2, %eax
jmp 0x119c0
movq 0x28(%rsp), %rcx
shrq $0x20, %rcx
cmpl -0xc(%rdx,%r10,8), %ecx
jbe 0x11811
movl 0x3c(%rdi), %ecx
testb %cl, %cl
jne 0x119b5
orl $0x1, %ecx
movl %ecx, 0x3c(%rdi)
movl 0x3c(%rbx), %eax
testb %al, %al
jne 0x11811
orl $0x1, %eax
movl %eax, 0x3c(%rbx)
jmp 0x11811