// File: /workspace/github_c_makefile_star2_O3/ToniWestbrook[P]paladin/bwamem.c
// Function: mem_patch_reg
int mem_patch_reg(const mem_opt_t *opt, const bntseq_t *bns, const uint8_t *pac, uint8_t *query, const mem_alnreg_t *a, const mem_alnreg_t *b, int *_w)
{
	int w, score, q_s, r_s;
	double r;
	if (bns == 0 || pac == 0 || query == 0) return 0;
	assert(a->rid == b->rid && a->rb <= b->rb);
	if (a->rb < bns->l_pac && b->rb >= bns->l_pac) return 0; // on different strands
	if (a->qb >= b->qb || a->qe >= b->qe || a->re >= b->re) return 0; // not colinear
	w = (a->re - b->rb) - (a->qe - b->qb); // required bandwidth
	w = w > 0? w : -w; // l = abs(l)
	r = (double)(a->re - b->rb) / (b->re - a->rb) - (double)(a->qe - b->qb) / (b->qe - a->qb); // relative bandwidth
	r = r > 0.? r : -r; // r = fabs(r)
	if (bwa_verbose >= 4)
		printf("* potential hit merge between [%d,%d)<=>[%ld,%ld) and [%d,%d)<=>[%ld,%ld), @ %s; w=%d, r=%.4g\n",
			   a->qb, a->qe, (long)a->rb, (long)a->re, b->qb, b->qe, (long)b->rb, (long)b->re, bns->anns[a->rid].name, w, r);
	if (a->re < b->rb || a->qe < b->qb) { // no overlap on query or on ref
		if (w > opt->w<<1 || r >= PATCH_MAX_R_BW) return 0; // the bandwidth or the relative bandwidth is too large
	} else if (w > opt->w<<2 || r >= PATCH_MAX_R_BW*2) return 0; // more permissive if overlapping on both ref and query
	// global alignment
	w += a->w + b->w;
	w = w < opt->w<<2? w : opt->w<<2;
	if (bwa_verbose >= 4) printf("* test potential hit merge with global alignment; w=%d\n", w);
	bwa_gen_cigar2(opt->mat, opt->o_del, opt->e_del, opt->o_ins, opt->e_ins, w, bns->l_pac, pac, b->qe - a->qb, query + a->qb, a->rb, b->re, &score, 0, 0);
	q_s = (int)((double)(b->qe - a->qb) / ((b->qe - b->qb) + (a->qe - a->qb)) * (b->score + a->score) + .499); // predicted score from query
	r_s = (int)((double)(b->re - a->rb) / ((b->re - b->rb) + (a->re - a->rb)) * (b->score + a->score) + .499); // predicted score from ref
	if (bwa_verbose >= 4) printf("* score=%d;(%d,%d)\n", score, q_s, r_s);
	if ((double)score / (q_s > r_s? q_s : r_s) < PATCH_MIN_SC_RATIO) return 0;
	*_w = w;
	return score;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %rbp
testq %rsi, %rsi
sete %al
testq %rdx, %rdx
sete %cl
orb %al, %cl
testq %rbp, %rbp
sete %al
xorl %ebx, %ebx
orb %cl, %al
jne 0x1a236
movq %r9, %r15
movq %r8, %r14
movslq 0x18(%r8), %rax
cmpl 0x18(%r9), %eax
jne 0x1a247
movq (%r14), %rcx
movq (%r15), %r10
cmpq %r10, %rcx
jg 0x1a247
movq %rdx, %r13
movq %rsi, %r12
movq (%rsi), %rdx
cmpq %rdx, %rcx
setl %sil
cmpq %rdx, %r10
setge %dl
testb %dl, %sil
jne 0x1a236
movl 0x10(%r14), %esi
movl 0x10(%r15), %r9d
cmpl %r9d, %esi
jge 0x1a236
movl 0x14(%r14), %r8d
movl 0x14(%r15), %edx
cmpl %edx, %r8d
jge 0x1a236
movl %r8d, 0x8(%rsp)
movq 0x8(%r14), %r8
movq %r8, 0x10(%rsp)
movq 0x8(%r15), %r11
movq %r11, 0x20(%rsp)
cmpq %r11, %r8
jge 0x1a236
movq %rdi, 0x18(%rsp)
movq 0x10(%rsp), %rdi
subq %r10, %rdi
movq %rdx, %r8
movl 0x8(%rsp), %edx
subl %r9d, %edx
cvtsi2sd %rdi, %xmm0
subl %edx, %edi
movl %edi, %r11d
negl %r11d
cmovsl %edi, %r11d
movq %r11, (%rsp)
movq 0x20(%rsp), %rdi
subq %rcx, %rdi
cvtsi2sd %rdi, %xmm3
cvtsi2sd %edx, %xmm1
movq %r8, 0x40(%rsp)
movl %r8d, %edx
subl %esi, %edx
cvtsi2sd %edx, %xmm2
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
unpcklpd %xmm2, %xmm3            # xmm3 = xmm3[0],xmm2[0]
divpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
subsd %xmm1, %xmm0
movapd 0xe882(%rip), %xmm1     # 0x28860
xorpd %xmm0, %xmm1
maxsd %xmm1, %xmm0
leaq 0x16e53(%rip), %rdx     # 0x30e40
cmpl $0x4, (%rdx)
jl 0x1a05b
movq 0x10(%r12), %rdx
movq %rdx, 0x30(%rsp)
leaq (%rax,%rax,4), %rax
movq %rax, 0x38(%rsp)
subq $0x8, %rsp
leaq 0xeb11(%rip), %rdi      # 0x28b21
movl 0x10(%rsp), %edx
movq 0x18(%rsp), %r8
movb $0x1, %al
pushq 0x8(%rsp)
movq 0x48(%rsp), %r11
movq 0x40(%rsp), %rbx
pushq 0x18(%rbx,%r11,8)
xorl %ebx, %ebx
pushq 0x38(%rsp)
pushq %r10
pushq 0x68(%rsp)
movapd %xmm0, 0x50(%rsp)
callq 0x3060
movapd 0x50(%rsp), %xmm0
addq $0x30, %rsp
movq 0x8(%r14), %rax
movq %rax, 0x10(%rsp)
movq (%r15), %r10
movq 0x18(%rsp), %rsi
cmpq %r10, 0x10(%rsp)
jl 0x1a071
movl 0x14(%r14), %eax
cmpl 0x10(%r15), %eax
jge 0x1a096
movl 0x24(%rsi), %eax
leal (%rax,%rax), %ecx
movq (%rsp), %rdx
cmpl %ecx, %edx
jg 0x1a236
ucomisd 0xe7ed(%rip), %xmm0     # 0x28878
jae 0x1a236
shll $0x2, %eax
jmp 0x1a0b6
movl 0x24(%rsi), %eax
shll $0x2, %eax
movq (%rsp), %rdx
cmpl %eax, %edx
jg 0x1a236
ucomisd 0xe7c0(%rip), %xmm0     # 0x28870
jae 0x1a236
addl 0x34(%r14), %edx
addl 0x34(%r15), %edx
cmpl %eax, %edx
cmovgel %eax, %edx
movq %rdx, (%rsp)
leaq 0x16d72(%rip), %rax     # 0x30e40
cmpl $0x4, (%rax)
jl 0x1a0ea
leaq 0xeaa6(%rip), %rdi      # 0x28b80
movq (%rsp), %rsi
xorl %eax, %eax
callq 0x3060
movq 0x18(%rsp), %rsi
movq %rsi, %rax
leaq 0xc4(%rsi), %rdi
movl 0x8(%rsi), %esi
movl 0xc(%rax), %edx
movl 0x10(%rax), %ecx
movl 0x14(%rax), %r8d
movq (%r12), %rax
movl 0x14(%r15), %r9d
movslq 0x10(%r14), %r10
subl %r10d, %r9d
addq %r10, %rbp
movq (%r14), %r10
movq 0x8(%r15), %r11
subq $0x50, %rsp
xorpd %xmm0, %xmm0
movupd %xmm0, 0x38(%rsp)
leaq 0x5c(%rsp), %r12
movq %r12, 0x30(%rsp)
movq %r11, 0x28(%rsp)
movq %r10, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
movl %r9d, 0x10(%rsp)
movq %r13, 0x8(%rsp)
movq %rax, (%rsp)
movq 0x50(%rsp), %r9
movq %r9, %r12
callq 0x122ac
addq $0x50, %rsp
movl 0x14(%r15), %eax
movl 0x10(%r14), %ecx
movl %eax, %edx
subl %ecx, %edx
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
addl 0x10(%r15), %ecx
subl %ecx, %eax
addl 0x14(%r14), %eax
cvtsi2sd %eax, %xmm1
movl 0x1c(%r14), %eax
addl 0x1c(%r15), %eax
cvtsi2sd %eax, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
movsd 0xb603(%rip), %xmm1     # 0x257a0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ebp
movq 0x8(%r15), %rax
movq (%r14), %rcx
movq %rax, %rdx
subq %rcx, %rdx
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
addq (%r15), %rcx
subq %rcx, %rax
addq 0x8(%r14), %rax
cvtsi2sd %rax, %xmm3
divsd %xmm3, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %r14d
leaq 0x16c5f(%rip), %rax     # 0x30e40
cmpl $0x4, (%rax)
jl 0x1a1fd
movl 0xc(%rsp), %esi
leaq 0xe9c7(%rip), %rdi      # 0x28bb8
movl %ebp, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x3060
movl 0xc(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
cmpl %r14d, %ebp
cmovgl %ebp, %r14d
xorps %xmm1, %xmm1
cvtsi2sd %r14d, %xmm1
divsd %xmm1, %xmm0
movsd 0xe65d(%rip), %xmm1     # 0x28880
ucomisd %xmm0, %xmm1
ja 0x1a236
movq 0x80(%rsp), %rcx
movl %r12d, (%rcx)
movl %eax, %ebx
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe82a(%rip), %rdi      # 0x28a78
leaq 0xe66b(%rip), %rsi      # 0x288c0
leaq 0xe83f(%rip), %rcx      # 0x28a9b
movl $0x1e9, %edx            # imm = 0x1E9
callq 0x31a0