// File: /workspace/github_c_makefile_star2_O1/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
movl %r8d, %eax
movq %rcx, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
cmpl $0x2, %r8d
jl 0x19ffa
movq %r9, %rbx
movq %rdi, %r13
movl %eax, 0x10(%rsp)
movl %eax, %r14d
movq %r14, %rdi
movq %r9, %rsi
callq 0x168c9
movq %r14, 0x30(%rsp)
shlq $0x5, %r14
leaq (%r14,%r14,2), %rax
xorl %ecx, %ecx
movl $0xc0000000, %edx       # imm = 0xC0000000
movl 0x48(%rbx,%rcx), %esi
andl %edx, %esi
incl %esi
movl %esi, 0x48(%rbx,%rcx)
addq $0x60, %rcx
cmpq %rcx, %rax
jne 0x19d00
movl $0x1, %r14d
leaq (%r14,%r14,2), %rax
shlq $0x5, %rax
movl 0x18(%rbx,%rax), %ecx
cmpl -0x48(%rbx,%rax), %ecx
jne 0x19ee7
leaq (%rbx,%rax), %r15
addq %rbx, %rax
addq $-0x60, %rax
movslq 0x90(%r13), %rcx
addq 0x8(%rax), %rcx
cmpq %rcx, (%r15)
jge 0x19ee7
movl %r14d, %ebp
leal -0x1(%rbp), %eax
movl 0x18(%r15), %ecx
leaq (%rax,%rax,2), %r12
shlq $0x5, %r12
cmpl 0x18(%rbx,%r12), %ecx
jne 0x19ee7
addq %rbx, %r12
movq (%r15), %rax
movq 0x8(%r12), %r8
movslq 0x90(%r13), %rcx
addq %r8, %rcx
cmpq %rcx, %rax
jge 0x19ee7
movl 0x10(%r12), %ecx
movl 0x14(%r12), %esi
movl %esi, %edi
subl %ecx, %edi
je 0x19edf
movl 0x10(%r15), %edx
cmpl %edx, %ecx
jge 0x19dab
subl %edx, %esi
jmp 0x19db1
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
xorps %xmm1, %xmm1
cvtsi2ss %r10, %xmm1
xorps %xmm2, %xmm2
cvtsi2ss %r11, %xmm2
movss 0xa8(%r13), %xmm0
mulss %xmm0, %xmm2
ucomiss %xmm2, %xmm1
jbe 0x19e2d
movl 0x14(%r15), %r8d
subl %edx, %r8d
cmpl %r8d, %edi
cmovll %edi, %r8d
xorps %xmm1, %xmm1
cvtsi2ss %esi, %xmm1
xorps %xmm2, %xmm2
cvtsi2ss %r8d, %xmm2
mulss %xmm2, %xmm0
ucomiss %xmm0, %xmm1
jbe 0x19e2d
movl 0x1c(%r15), %eax
cmpl 0x1c(%r12), %eax
jl 0x19ef7
movl %ecx, 0x14(%r12)
jmp 0x19edf
cmpq %rax, %r9
jge 0x19edf
leaq 0x14(%rsp), %rax
movq %rax, (%rsp)
movq %r13, %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq %r12, %r8
movq %r15, %r9
callq 0x1990b
testl %eax, %eax
jle 0x19edf
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 %ebp
jg 0x19d53
incq %r14
cmpq 0x30(%rsp), %r14
jne 0x19d1b
jmp 0x19efd
movl %edx, 0x14(%r15)
jmp 0x19ee7
movl 0x10(%rsp), %ecx
cmpl $0x2, %ecx
movl $0x1, %eax
cmovgel %ecx, %eax
leaq 0x14(%rbx), %rdx
xorl %r8d, %r8d
xorl %r15d, %r15d
movl (%rdx), %ecx
cmpl -0x4(%rdx), %ecx
jle 0x19f46
movl %r15d, %ecx
leal 0x1(%r15), %r9d
cmpq %rcx, %r8
je 0x19f43
leaq -0x14(%rdx), %rsi
movslq %r15d, %rcx
leaq (%rcx,%rcx,2), %rdi
shlq $0x5, %rdi
addq %rbx, %rdi
movl $0xc, %ecx
rep  movsq (%rsi), %es:(%rdi)
movl %r9d, %r15d
incq %r8
addq $0x60, %rdx
cmpq %r8, %rax
jne 0x19f16
movslq %r15d, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x174fb
movl %r15d, %r9d
cmpl $0x2, %r14d
jl 0x19fa3
leaq (%r9,%r9,2), %rcx
shlq $0x5, %rcx
addq $-0x60, %rcx
xorl %edx, %edx
movl 0x7c(%rbx,%rdx), %esi
cmpl 0x1c(%rbx,%rdx), %esi
jne 0x19f9a
movq 0x60(%rbx,%rdx), %rsi
cmpq (%rbx,%rdx), %rsi
jne 0x19f9a
movl 0x70(%rbx,%rdx), %esi
cmpl 0x10(%rbx,%rdx), %esi
jne 0x19f9a
movl %esi, 0x74(%rbx,%rdx)
addq $0x60, %rdx
cmpq %rdx, %rcx
jne 0x19f77
movl $0x1, %eax
cmpl $0x2, %r15d
jl 0x19ffa
leaq 0x74(%rbx), %rdx
movl $0x1, %ecx
movl $0x1, %r8d
movl (%rdx), %esi
cmpl -0x4(%rdx), %esi
jle 0x19fea
movl %ecx, %esi
leal 0x1(%rcx), %eax
cmpq %rsi, %r8
je 0x19fec
leaq -0x14(%rdx), %rsi
movslq %ecx, %rcx
leaq (%rcx,%rcx,2), %rdi
shlq $0x5, %rdi
addq %rbx, %rdi
movl $0xc, %ecx
rep  movsq (%rsi), %es:(%rdi)
jmp 0x19fec
movl %ecx, %eax
incq %r8
addq $0x60, %rdx
movl %eax, %ecx
cmpq %r8, %r9
jne 0x19fbd
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq