// File: /workspace/github_c_makefile_star2_O1/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, %r13
testq %rsi, %rsi
sete %al
testq %rdx, %rdx
sete %cl
orb %al, %cl
testq %r13, %r13
sete %al
xorl %ebx, %ebx
orb %cl, %al
jne 0x19c7a
movq %r9, %r15
movq %r8, %r14
movslq 0x18(%r8), %rax
cmpl 0x18(%r9), %eax
jne 0x19c8b
movq (%r14), %rcx
movq (%r15), %r10
cmpq %r10, %rcx
jg 0x19c8b
movq %rdx, %r12
movq %rsi, %rbp
movq (%rsi), %rdx
cmpq %rdx, %rcx
setl %sil
cmpq %rdx, %r10
setge %dl
testb %dl, %sil
jne 0x19c7a
movl 0x10(%r14), %esi
movl 0x10(%r15), %r9d
cmpl %r9d, %esi
jge 0x19c7a
movl 0x14(%r14), %edx
movl 0x14(%r15), %r8d
cmpl %r8d, %edx
jge 0x19c7a
movl %edx, (%rsp)
movq 0x8(%r14), %r11
movq 0x8(%r15), %rdx
movq %r11, 0x10(%rsp)
cmpq %rdx, %r11
jge 0x19c7a
movq %rdi, 0x20(%rsp)
movq 0x10(%rsp), %rdi
subq %r10, %rdi
movq %r8, 0x30(%rsp)
movl (%rsp), %r8d
subl %r9d, %r8d
cvtsi2sd %rdi, %xmm0
subl %r8d, %edi
movq %rdx, 0x28(%rsp)
movl %edi, %r11d
negl %r11d
cmovsl %edi, %r11d
movq %r11, 0x8(%rsp)
movq 0x28(%rsp), %rdi
subq %rcx, %rdi
cvtsi2sd %rdi, %xmm1
divsd %xmm1, %xmm0
cvtsi2sd %r8d, %xmm2
movq 0x30(%rsp), %r8
movl %r8d, %edi
subl %esi, %edi
xorps %xmm1, %xmm1
cvtsi2sd %edi, %xmm1
divsd %xmm1, %xmm2
subsd %xmm2, %xmm0
movapd 0xed4d(%rip), %xmm1     # 0x28770
xorpd %xmm0, %xmm1
maxsd %xmm1, %xmm0
leaq 0x1740e(%rip), %rdi     # 0x30e40
cmpl $0x4, (%rdi)
jl 0x19a93
movq 0x10(%rbp), %rdx
movq %rdx, 0x40(%rsp)
leaq (%rax,%rax,4), %rax
movq %rax, 0x38(%rsp)
subq $0x8, %rsp
leaq 0xefdd(%rip), %rdi      # 0x28a31
movl 0x8(%rsp), %edx
movq 0x18(%rsp), %r8
movb $0x1, %al
pushq 0x10(%rsp)
movq 0x50(%rsp), %r11
movq 0x48(%rsp), %rbx
pushq 0x18(%r11,%rbx,8)
xorl %ebx, %ebx
pushq 0x40(%rsp)
pushq %r10
pushq 0x58(%rsp)
movapd %xmm0, 0x40(%rsp)
callq 0x3060
movapd 0x40(%rsp), %xmm0
addq $0x30, %rsp
movq 0x8(%r14), %rax
cmpq (%r15), %rax
jl 0x19aa6
movl 0x14(%r14), %eax
cmpl 0x10(%r15), %eax
jge 0x19aca
movq 0x20(%rsp), %r8
movl 0x24(%r8), %eax
addl %eax, %eax
cmpl %eax, 0x8(%rsp)
jg 0x19c7a
ucomisd 0xecc5(%rip), %xmm0     # 0x28788
jb 0x19aee
jmp 0x19c7a
movq 0x20(%rsp), %r8
movl 0x24(%r8), %eax
shll $0x2, %eax
cmpl %eax, 0x8(%rsp)
jg 0x19c7a
ucomisd 0xec98(%rip), %xmm0     # 0x28780
jae 0x19c7a
movq 0x8(%rsp), %rcx
addl 0x34(%r14), %ecx
addl 0x34(%r15), %ecx
movl 0x24(%r8), %eax
shll $0x2, %eax
cmpl %eax, %ecx
cmovll %ecx, %eax
movl %eax, 0x10(%rsp)
leaq 0x1732e(%rip), %rax     # 0x30e40
cmpl $0x4, (%rax)
jl 0x19b2e
leaq 0xef72(%rip), %rdi      # 0x28a90
movl 0x10(%rsp), %esi
xorl %eax, %eax
callq 0x3060
movq 0x20(%rsp), %r8
leaq 0xc4(%r8), %rdi
movl 0x8(%r8), %esi
movl 0xc(%r8), %edx
movl 0x10(%r8), %ecx
movl 0x14(%r8), %r8d
movq (%rbp), %rax
movl 0x14(%r15), %r9d
movslq 0x10(%r14), %r10
subl %r10d, %r9d
addq %r10, %r13
movq (%r14), %r10
movq 0x8(%r15), %r11
subq $0x50, %rsp
xorpd %xmm0, %xmm0
movupd %xmm0, 0x38(%rsp)
leaq 0x54(%rsp), %rbp
movq %rbp, 0x30(%rsp)
movq %r11, 0x28(%rsp)
movq %r10, 0x20(%rsp)
movq %r13, 0x18(%rsp)
movl %r9d, 0x10(%rsp)
movq %r12, 0x8(%rsp)
movq %rax, (%rsp)
movl 0x60(%rsp), %r12d
movl %r12d, %r9d
callq 0x11f18
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 0xbbbf(%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 0x1721b(%rip), %rax     # 0x30e40
cmpl $0x4, (%rax)
jl 0x19c41
movl 0x4(%rsp), %esi
leaq 0xee93(%rip), %rdi      # 0x28ac8
movl %ebp, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x3060
movl 0x4(%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
cmpl %r14d, %ebp
cmovgl %ebp, %r14d
xorps %xmm1, %xmm1
cvtsi2sd %r14d, %xmm1
divsd %xmm1, %xmm0
movsd 0xeb29(%rip), %xmm1     # 0x28790
ucomisd %xmm0, %xmm1
ja 0x19c7a
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 0xecf6(%rip), %rdi      # 0x28988
leaq 0xeb37(%rip), %rsi      # 0x287d0
leaq 0xed0b(%rip), %rcx      # 0x289ab
movl $0x1e9, %edx            # imm = 0x1E9
callq 0x31a0