// File: /workspace/github_c_makefile_star2_O2/ToniWestbrook[P]paladin/bwamem.c
// Function: mem_sort_dedup_patch
int mem_sort_dedup_patch(const mem_opt_t *opt, const bntseq_t *bns, const uint8_t *pac, uint8_t *query, int n, mem_alnreg_t *a)
{
	int m, i, j;
	if (n <= 1) return n;
	ks_introsort(mem_ars2, n, a); // sort by the END position, not START!
	for (i = 0; i < n; ++i) a[i].n_comp = 1;
	for (i = 1; i < n; ++i) {
		mem_alnreg_t *p = &a[i];
		if (p->rid != a[i-1].rid || p->rb >= a[i-1].re + opt->max_chain_gap) continue; // then no need to go into the loop below
		for (j = i - 1; j >= 0 && p->rid == a[j].rid && p->rb < a[j].re + opt->max_chain_gap; --j) {
			mem_alnreg_t *q = &a[j];
			int64_t or, oq, mr, mq;
			int score, w;
			if (q->qe == q->qb) continue; // a[j] has been excluded
			or = q->re - p->rb; // overlap length on the reference
			oq = q->qb < p->qb? q->qe - p->qb : p->qe - q->qb; // overlap length on the query
			mr = q->re - q->rb < p->re - p->rb? q->re - q->rb : p->re - p->rb; // min ref len in alignment
			mq = q->qe - q->qb < p->qe - p->qb? q->qe - q->qb : p->qe - p->qb; // min qry len in alignment
			if (or > opt->mask_level_redun * mr && oq > opt->mask_level_redun * mq) { // one of the hits is redundant
				if (p->score < q->score) {
					p->qe = p->qb;
					break;
				} else q->qe = q->qb;
			} else if (q->rb < p->rb && (score = mem_patch_reg(opt, bns, pac, query, q, p, &w)) > 0) { // then merge q into p
				p->n_comp += q->n_comp + 1;
				p->seedcov = p->seedcov > q->seedcov? p->seedcov : q->seedcov;
				p->sub = p->sub > q->sub? p->sub : q->sub;
				p->csub = p->csub > q->csub? p->csub : q->csub;
				p->qb = q->qb, p->rb = q->rb;
				p->truesc = p->score = score;
				p->w = w;
				q->qb = q->qe;
			}
		}
	}
	for (i = 0, m = 0; i < n; ++i) // exclude identical hits
		if (a[i].qe > a[i].qb) {
			if (m != i) a[m++] = a[i];
			else ++m;
		}
	n = m;
	ks_introsort(mem_ars, n, a);
	for (i = 1; i < n; ++i) { // mark identical hits
		if (a[i].score == a[i-1].score && a[i].rb == a[i-1].rb && a[i].qb == a[i-1].qb)
			a[i].qe = a[i].qb;
	}
	for (i = 1, m = 1; i < n; ++i) // exclude identical hits
		if (a[i].qe > a[i].qb) {
			if (m != i) a[m++] = a[i];
			else ++m;
		}
	return m;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
cmpl $0x2, %r8d
jge 0x17935
movl %r8d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r9, %rbx
movq %rdi, %rbp
movl %r8d, %r14d
movq %r14, %rdi
movq %r9, %rsi
callq 0x14998
imulq $0x60, %r14, %rax
xorl %ecx, %ecx
movl $0xc0000000, %edx       # imm = 0xC0000000
cmpq %rcx, %rax
je 0x1796b
movl 0x48(%rbx,%rcx), %esi
andl %edx, %esi
incl %esi
movl %esi, 0x48(%rbx,%rcx)
addq $0x60, %rcx
jmp 0x17954
pushq $0x1
popq %r13
movq %r14, 0x30(%rsp)
cmpq %r14, %r13
je 0x17b4f
imulq $0x60, %r13, %rax
movl 0x18(%rbx,%rax), %ecx
cmpl -0x48(%rbx,%rax), %ecx
jne 0x17b42
leaq (%rbx,%rax), %r15
addq %rbx, %rax
addq $-0x60, %rax
movslq 0x90(%rbp), %rcx
addq 0x8(%rax), %rcx
cmpq %rcx, (%r15)
jge 0x17b42
leaq -0x1(%r13), %r14
testl %r14d, %r14d
js 0x17b42
movl 0x18(%r15), %eax
movl %r14d, %ecx
imulq $0x60, %rcx, %r12
cmpl 0x18(%rbx,%r12), %eax
jne 0x17b42
addq %rbx, %r12
movq (%r15), %rax
movq 0x8(%r12), %r8
movslq 0x90(%rbp), %rcx
addq %r8, %rcx
cmpq %rcx, %rax
jge 0x17b42
movl 0x10(%r12), %ecx
movl 0x14(%r12), %esi
movl %esi, %edi
subl %ecx, %edi
je 0x17b36
movl 0x10(%r15), %edx
cmpl %edx, %ecx
jge 0x17a0f
subl %edx, %esi
jmp 0x17a15
movl 0x14(%r15), %esi
subl %ecx, %esi
movq %r8, %r10
subq %rax, %r10
movq (%r12), %r9
subq %r9, %r8
movq 0x8(%r15), %r11
subq %rax, %r11
cmpq %r11, %r8
cmovlq %r8, %r11
cvtsi2ss %r10, %xmm1
cvtsi2ss %r11, %xmm2
movss 0xa8(%rbp), %xmm0
mulss %xmm0, %xmm2
ucomiss %xmm2, %xmm1
jbe 0x17a84
movl 0x14(%r15), %r8d
subl %edx, %r8d
cmpl %r8d, %edi
cmovll %edi, %r8d
cvtsi2ss %esi, %xmm1
cvtsi2ss %r8d, %xmm2
mulss %xmm2, %xmm0
ucomiss %xmm0, %xmm1
jbe 0x17a84
movl 0x1c(%r15), %eax
cmpl 0x1c(%r12), %eax
jl 0x17b3e
movl %ecx, 0x14(%r12)
jmp 0x17b36
cmpq %rax, %r9
jge 0x17b36
leaq 0x14(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq %r12, %r8
movq %r15, %r9
callq 0x17566
testl %eax, %eax
jle 0x17b36
movl 0x48(%r12), %ecx
movl 0x48(%r15), %edx
addl %edx, %ecx
incl %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl $0xc0000000, %edx       # imm = 0xC0000000
orl %ecx, %edx
movl %edx, 0x48(%r15)
movl 0x38(%r15), %ecx
movl 0x38(%r12), %edx
cmpl %edx, %ecx
cmovgl %ecx, %edx
movl %edx, 0x38(%r15)
movl 0x24(%r12), %ecx
movl 0x24(%r15), %edx
movl 0x2c(%r15), %esi
cmpl %ecx, %edx
cmovgl %edx, %ecx
movl %ecx, 0x24(%r15)
movl 0x2c(%r12), %ecx
cmpl %ecx, %esi
cmovgl %esi, %ecx
movl %ecx, 0x2c(%r15)
movl 0x10(%r12), %ecx
movl %ecx, 0x10(%r15)
movq (%r12), %rcx
movq %rcx, (%r15)
movl %eax, 0x1c(%r15)
movl %eax, 0x20(%r15)
movl 0x14(%rsp), %eax
movl %eax, 0x34(%r15)
movl 0x14(%r12), %eax
movl %eax, 0x10(%r12)
decl %r14d
jmp 0x179b2
movl %edx, 0x14(%r15)
incq %r13
movq 0x30(%rsp), %r14
jmp 0x17974
leaq 0x14(%rbx), %rdx
xorl %r8d, %r8d
pushq $0xc
popq %rax
xorl %r9d, %r9d
movslq %r9d, %r15
cmpq %r8, %r14
je 0x17b90
movl (%rdx), %ecx
cmpl -0x4(%rdx), %ecx
jle 0x17b87
movl %r9d, %ecx
incl %r9d
cmpq %rcx, %r8
je 0x17b87
leaq -0x14(%rdx), %rsi
imulq $0x60, %r15, %rdi
addq %rbx, %rdi
movq %rax, %rcx
rep  movsq (%rsi), %es:(%rdi)
incq %r8
addq $0x60, %rdx
jmp 0x17b5c
movq %r15, %rdi
movq %rbx, %rsi
callq 0x154c5
leaq 0x7c(%rbx), %rax
pushq $0x1
popq %rcx
cmpq %r15, %rcx
jge 0x17bcc
movl (%rax), %edx
cmpl -0x60(%rax), %edx
jne 0x17bc3
movq -0x1c(%rax), %rdx
cmpq -0x7c(%rax), %rdx
jne 0x17bc3
movl -0xc(%rax), %edx
cmpl -0x6c(%rax), %edx
jne 0x17bc3
movl %edx, -0x8(%rax)
incq %rcx
addq $0x60, %rax
jmp 0x17ba2
leaq 0x74(%rbx), %rdx
pushq $0x1
popq %r8
pushq $0x1
popq %r9
pushq $0xc
popq %rax
cmpq %r15, %r9
jge 0x17923
movl (%rdx), %ecx
cmpl -0x4(%rdx), %ecx
jle 0x17c0d
movl %r8d, %ecx
leal 0x1(%r8), %r10d
cmpq %rcx, %r9
je 0x17c10
leaq -0x14(%rdx), %rsi
movslq %r8d, %rcx
imulq $0x60, %rcx, %rdi
addq %rbx, %rdi
movq %rax, %rcx
rep  movsq (%rsi), %es:(%rdi)
jmp 0x17c10
movl %r8d, %r10d
incq %r9
addq $0x60, %rdx
movl %r10d, %r8d
jmp 0x17bdb