// File: /workspace/github_c_makefile_star2_O1/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 $0x98, %rsp
movq %r9, 0x68(%rsp)
movl %r8d, 0x50(%rsp)
movl %ecx, 0x54(%rsp)
movl %edx, 0x8(%rsp)
movl %esi, 0x24(%rsp)
movq 0xd8(%rsp), %r14
movq 0xd0(%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 0x126e9
movq %rax, 0x18(%rsp)
addq $0x10, %rax
movq %rax, 0x30(%rsp)
xorl %ecx, %ecx
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %edx, %edx
cmpl $0x0, (%r15,%rcx,4)
jle 0x114db
movl $0x1, %esi
shll %cl, %esi
movslq %ebx, %rdi
leaq (%rdi,%rdi,2), %rdi
movq 0x30(%rsp), %rax
leaq (%rax,%rdi,8), %r8
xorl %r9d, %r9d
xorl %edi, %edi
xorl %r10d, %r10d
movl %r10d, %r11d
movl %ecx, -0x10(%r8)
movq (%r14,%rcx,8), %r10
leaq (%r10,%r9), %r13
movq %r13, (%r8)
movl 0x3c(%r10,%r9), %r13d
shrl $0xa, %r13d
andl $0x1, %r13d
movl %r13d, -0xc(%r8)
movl 0x8(%r10,%r9), %ebp
movl 0x18(%r10,%r9), %eax
shlq $0x20, %rbp
addl %eax, %eax
cltq
xorl %ecx, %r13d
orq %rbp, %r13
orq %rax, %r13
movq %r13, -0x8(%r8)
movq 0x48(%r10,%r9), %rax
movl 0x8(%rax), %r10d
cmpl %r10d, %r11d
cmovgl %r11d, %r10d
incq %rdi
movslq (%r15,%rcx,4), %rax
addq $0x18, %r8
addq $0x50, %r9
cmpq %rax, %rdi
jl 0x1146f
orl %esi, %edx
addl %edi, %ebx
jmp 0x114de
xorl %r10d, %r10d
addl %r10d, %r12d
leaq 0x1(%rcx), %rax
testq %rcx, %rcx
movq %rax, %rcx
je 0x11445
cmpl $0x3, %edx
jne 0x11596
subl 0x8(%rsp), %r12d
xorl %eax, %eax
testl %r12d, %r12d
cmovlel %eax, %r12d
movslq %ebx, %r13
leaq (,%r13,2), %rax
addq %r13, %rax
movq 0x18(%rsp), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x113b9
movq $-0x1, %rax
movq %rax, 0x40(%rsp)
movq $-0x1, 0x10(%rsp)
movq %rax, 0x60(%rsp)
testl %r13d, %r13d
je 0x115b6
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, %r13
movq 0x38(%rsp), %rdi
xorl %esi, %esi
callq 0x127ec
movq %rax, %r11
jmp 0x115bc
movq 0x38(%rsp), %rdi
movq 0x18(%rsp), %rsi
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1261e
xorl %r13d, %r13d
xorl %r11d, %r11d
movl 0x24(%rsp), %r9d
testl %ebx, %ebx
movq 0x18(%rsp), %rsi
jle 0x11772
movl %ebx, %eax
movq %rax, 0x30(%rsp)
movq $-0x1, 0x10(%rsp)
xorl %edi, %edi
xorl %r8d, %r8d
movq %r13, 0x58(%rsp)
leaq (%rdi,%rdi,2), %rcx
testb $0x1, 0x8(%rsi,%rcx,8)
movslq 0x4(%rsi,%rcx,8), %rax
jne 0x115fe
movl %edi, 0x60(%rsp,%rax,4)
jmp 0x11633
movslq 0x60(%rsp,%rax,4), %rax
testq %rax, %rax
js 0x11633
leaq (%rsi,%rcx,8), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rcx), %r10
leaq (%rax,%rax,2), %rcx
movq 0x10(%rsi,%rcx,8), %rcx
movl 0x8(%r10), %edx
cmpl 0x8(%rcx), %edx
jne 0x11633
movl 0x18(%r10), %edx
subl 0x1c(%rcx), %edx
cmpl %r9d, %edx
jle 0x11642
incq %rdi
cmpq 0x30(%rsp), %rdi
jne 0x115e8
jmp 0x11775
movq %r10, 0x78(%rsp)
movl %eax, %r13d
leaq (,%r13,2), %rax
addq %r13, %rax
leaq (%rsi,%rax,8), %rbp
movq 0x8(%rsp), %rdx
movq %rdi, 0x28(%rsp)
movl 0x4(%rbp), %eax
movb $0x1, %bl
cmpl 0x4(%rdx), %eax
jne 0x116a2
movslq (%rbp), %rax
movslq (%rdx), %rcx
cmpl %ecx, %eax
je 0x116a2
movq 0x10(%rbp), %rdx
movq 0x78(%rsp), %r10
movl 0x8(%r10), %edi
cmpl 0x8(%rdx), %edi
jne 0x11696
movl 0x18(%r10), %edi
subl 0x1c(%rdx), %edi
cmpl %r9d, %edi
jle 0x116bb
xorl %ebx, %ebx
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %rdx
testb %bl, %bl
je 0x11633
leaq -0x1(%r13), %rax
addq $-0x18, %rbp
testq %r13, %r13
movq %rax, %r13
jg 0x11663
jmp 0x11633
movq %r8, 0x48(%rsp)
movq 0x48(%r10), %rdi
movslq 0x8(%rdi), %rdi
movq 0x48(%rdx), %r8
movslq 0x8(%r8), %r8
addq %rdi, %r8
cmpl %r12d, %r8d
jge 0x116df
movq 0x48(%rsp), %r8
jmp 0x11698
shlq $0x20, %r8
movl 0x40(%rdx), %edx
addl 0x40(%r10), %edx
orq %r8, %rdx
cmpq 0x10(%rsp), %rdx
movq 0x28(%rsp), %rdi
jle 0x11707
movl %r13d, 0x40(%rsp,%rax,4)
movl %edi, 0x40(%rsp,%rcx,4)
movq %rdx, 0x10(%rsp)
movq %rdx, 0x70(%rsp)
movq 0x48(%rsp), %r8
movq 0x58(%rsp), %rax
cmpq %rax, %r8
jne 0x1175c
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x58(%rsp)
leaq (,%rcx,8), %rdx
movq 0x38(%rsp), %rdi
movq %r11, %rsi
callq 0x127ec
movq 0x28(%rsp), %rdi
movl 0x24(%rsp), %r9d
movq 0x48(%rsp), %r8
movq 0x18(%rsp), %rsi
movq %rax, %r11
movq 0x8(%rsp), %rdx
movq 0x70(%rsp), %rax
movq %rax, (%r11,%r8,8)
incq %r8
jmp 0x116a2
xorl %r8d, %r8d
cmpq $0x2, %r8
movq %r8, %r13
jb 0x11798
leaq (%r11,%r8,8), %rsi
movq %r11, %rdi
movq %r11, %rbx
callq 0x5d2f
movq %r13, %r8
movq %rbx, %r11
movq 0x18(%rsp), %rsi
testq %r8, %r8
je 0x11a34
cmpq $0x0, 0x10(%rsp)
jle 0x11a34
movslq 0x40(%rsp), %rax
leaq (%rax,%rax,2), %rax
movq 0x10(%rsi,%rax,8), %r12
movq %r12, 0x80(%rsp)
movslq 0x44(%rsp), %rax
leaq (%rax,%rax,2), %rax
movq 0x10(%rsi,%rax,8), %rbx
movq %rbx, 0x88(%rsp)
orb $0x20, 0x3d(%rbx)
orb $0x20, 0x3d(%r12)
xorl %eax, %eax
movq 0x80(%rsp,%rax,8), %rcx
movl (%rcx), %edx
movslq 0x20(%rcx), %r8
cmpl %r8d, %edx
je 0x11838
movq (%r14,%rax,8), %rdi
leaq (%r8,%r8,4), %rsi
shlq $0x4, %rsi
addq %rdi, %rsi
cmpl $0x0, (%r15,%rax,4)
jle 0x1182f
movl (%rsi), %r8d
addq $0x20, %rdi
xorl %r9d, %r9d
cmpl %r8d, (%rdi)
jne 0x1181f
movl %edx, (%rdi)
incq %r9
movslq (%r15,%rax,4), %r10
addq $0x50, %rdi
cmpq %r10, %r9
jl 0x11818
movb $0x0, 0x3c(%rsi)
movq 0x18(%rsp), %rsi
testb $0x10, 0x3d(%rcx)
jne 0x1186c
cmpl $0x0, (%r15,%rax,4)
jle 0x11868
movl $0x3c, %edx
xorl %r8d, %r8d
movq (%r14,%rax,8), %rdi
andl $0xffffefff, (%rdi,%rdx) # imm = 0xFFFFEFFF
incq %r8
movslq (%r15,%rax,4), %rdi
addq $0x50, %rdx
cmpq %rdi, %r8
jl 0x1184d
orb $0x10, 0x3d(%rcx)
leaq 0x1(%rax), %rcx
testq %rax, %rax
movq %rcx, %rax
je 0x117e5
movzbl 0x3c(%r12), %eax
movzbl 0x3c(%rbx), %ebp
cmpl %ebp, %eax
cmoval %eax, %ebp
movq %r13, %rdx
testl %edx, %edx
jle 0x118ca
movslq 0x54(%rsp), %rax
movq 0x10(%rsp), %rcx
shrq $0x20, %rcx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %r9d, %r9d
xorl %edi, %edi
movl 0x4(%r11,%r9,8), %r8d
addq %rax, %r8
cmpq %rcx, %r8
sbbl $-0x1, %edi
incq %r9
cmpq %r9, %rdx
jne 0x118ab
cvtsi2ss %edi, %xmm0
movq %r13, %rdx
jmp 0x118cd
xorps %xmm0, %xmm0
cmpq $0x2, %rdx
jb 0x1194e
movq 0x10(%rsp), %rax
shrq $0x20, %rax
movl -0xc(%r11,%rdx,8), %ecx
subq %rcx, %rax
movq %r11, 0x30(%rsp)
js 0x118f2
cvtsi2ss %rax, %xmm1
jmp 0x11907
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm1
addss %xmm1, %xmm1
mulss 0x1b635(%rip), %xmm1    # 0x2cf44
movaps %xmm1, %xmm2
xorps %xmm1, %xmm1
cvtsi2ssl 0x50(%rsp), %xmm1
divss %xmm1, %xmm2
movss %xmm2, 0x8(%rsp)
callq 0x2200
mulss 0x1b616(%rip), %xmm0    # 0x2cf48
addss 0x8(%rsp), %xmm0
cvttss2si %xmm0, %eax
cmpl %eax, %ebp
cmovgel %eax, %ebp
movq 0x18(%rsp), %rsi
movq 0x30(%rsp), %r11
movq %r13, %rdx
movl 0x3c(%r12), %eax
movzbl %al, %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ebp, %xmm0
cmpl %ebp, %ecx
jge 0x1199b
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
movss 0x1b5dc(%rip), %xmm2    # 0x2cf4c
mulss %xmm0, %xmm2
mulss 0x1adec(%rip), %xmm1    # 0x2c768
addss %xmm2, %xmm1
addss 0x1b5c8(%rip), %xmm1    # 0x2cf50
cvttss2si %xmm1, %ecx
movzbl %cl, %ecx
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
orl %ecx, %eax
movl %eax, 0x3c(%r12)
movl 0x3c(%rbx), %eax
movzbl %al, %ecx
cmpl %ebp, %ecx
jge 0x119d9
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
mulss 0x1b598(%rip), %xmm0    # 0x2cf4c
mulss 0x1adac(%rip), %xmm1    # 0x2c768
addss %xmm0, %xmm1
addss 0x1b588(%rip), %xmm1    # 0x2cf50
cvttss2si %xmm1, %ecx
movzbl %cl, %ecx
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
orl %ecx, %eax
movl %eax, 0x3c(%rbx)
cmpq $0x1, %rdx
jne 0x11a06
movl 0x3c(%r12), %eax
testb $-0x2, %al
jne 0x119f5
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
orl $0x2, %eax
movl %eax, 0x3c(%r12)
movl 0x3c(%rbx), %eax
testb $-0x2, %al
jne 0x11a34
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
orl $0x2, %eax
jmp 0x11a31
movq 0x10(%rsp), %rax
shrq $0x20, %rax
cmpl -0xc(%r11,%rdx,8), %eax
jbe 0x11a34
movl 0x3c(%r12), %eax
testb %al, %al
jne 0x11a27
orl $0x1, %eax
movl %eax, 0x3c(%r12)
movl 0x3c(%rbx), %eax
testb %al, %al
jne 0x11a34
orl $0x1, %eax
movl %eax, 0x3c(%rbx)
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
movq %r11, %r12
callq 0x1261e
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1261e
movq 0x68(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x11062