// File: /workspace/github_c_makefile_star2_O2/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 0x178d0
movq %r9, %r15
movq %r8, %r14
movslq 0x18(%r8), %rax
cmpl 0x18(%r9), %eax
jne 0x178e1
movq (%r14), %rcx
movq (%r15), %r10
cmpq %r10, %rcx
jg 0x178e1
movq %rdx, %r13
movq %rsi, %r12
movq (%rsi), %rdx
cmpq %rdx, %rcx
setl %sil
cmpq %rdx, %r10
setge %dl
testb %dl, %sil
jne 0x178d0
movl 0x10(%r14), %esi
movl 0x10(%r15), %r9d
cmpl %r9d, %esi
jge 0x178d0
movl 0x14(%r14), %r8d
movl 0x14(%r15), %edx
cmpl %edx, %r8d
jge 0x178d0
movl %r8d, 0x8(%rsp)
movq 0x8(%r14), %r8
movq %r8, 0x10(%rsp)
movq 0x8(%r15), %r11
movq %r11, 0x20(%rsp)
cmpq %r11, %r8
jge 0x178d0
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 0xc16c(%rip), %xmm1     # 0x237f0
xorpd %xmm0, %xmm1
maxsd %xmm1, %xmm0
leaq 0x157ad(%rip), %rdx     # 0x2ce40
cmpl $0x4, (%rdx)
jl 0x17701
movq 0x10(%r12), %rdx
movq %rdx, 0x30(%rsp)
imulq $0x28, %rax, %rax
movq %rax, 0x38(%rsp)
subq $0x8, %rsp
leaq 0xc3fb(%rip), %rdi      # 0x23ab1
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)
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 0x17717
movl 0x14(%r14), %eax
cmpl 0x10(%r15), %eax
jge 0x1773c
movl 0x24(%rsi), %eax
leal (%rax,%rax), %ecx
movq (%rsp), %rdx
cmpl %ecx, %edx
jg 0x178d0
ucomisd 0xc0d7(%rip), %xmm0     # 0x23808
jae 0x178d0
shll $0x2, %eax
jmp 0x1775c
movl 0x24(%rsi), %eax
shll $0x2, %eax
movq (%rsp), %rdx
cmpl %eax, %edx
jg 0x178d0
ucomisd 0xc0aa(%rip), %xmm0     # 0x23800
jae 0x178d0
addl 0x34(%r14), %edx
addl 0x34(%r15), %edx
cmpl %eax, %edx
cmovgel %eax, %edx
movq %rdx, (%rsp)
leaq 0x156cc(%rip), %rax     # 0x2ce40
cmpl $0x4, (%rax)
jl 0x17790
leaq 0xc390(%rip), %rdi      # 0x23b10
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 0x10572
addq $0x50, %rsp
movl 0x14(%r15), %eax
movl 0x10(%r14), %ecx
movl %eax, %edx
subl %ecx, %edx
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 0x8f60(%rip), %xmm1     # 0x207a0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ebp
movq 0x8(%r15), %rax
movq (%r14), %rcx
movq %rax, %rdx
subq %rcx, %rdx
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 0x155bf(%rip), %rax     # 0x2ce40
cmpl $0x4, (%rax)
jl 0x1789d
movl 0xc(%rsp), %esi
leaq 0xc2b7(%rip), %rdi      # 0x23b48
movl %ebp, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x3060
movl 0xc(%rsp), %eax
cvtsi2sd %eax, %xmm0
cmpl %r14d, %ebp
cmovgl %ebp, %r14d
cvtsi2sd %r14d, %xmm1
divsd %xmm1, %xmm0
movsd 0xbf53(%rip), %xmm1     # 0x23810
ucomisd %xmm0, %xmm1
ja 0x178d0
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 0xc120(%rip), %rdi      # 0x23a08
leaq 0xbf61(%rip), %rsi      # 0x23850
leaq 0xc135(%rip), %rcx      # 0x23a2b
movl $0x1e9, %edx            # imm = 0x1E9
callq 0x31a0