// File: /workspace/github_c_makefile_star2_O3/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 0x1a5af
movq %r9, %rbx
movq %rdi, %rbp
movl %eax, %r12d
movq %r12, %rdi
movq %r9, %rsi
callq 0x16e8e
movq %r12, %rax
shlq $0x5, %rax
leaq (%rax,%rax,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 0x1a2b6
movl $0x1, %r14d
movq %r12, 0x30(%rsp)
leaq (%r14,%r14,2), %rax
shlq $0x5, %rax
movl 0x18(%rbx,%rax), %ecx
cmpl -0x48(%rbx,%rax), %ecx
jne 0x1a4a3
leaq (%rbx,%rax), %r15
addq %rbx, %rax
addq $-0x60, %rax
movslq 0x90(%rbp), %rcx
addq 0x8(%rax), %rcx
cmpq %rcx, (%r15)
jge 0x1a4a3
movl %r14d, %r13d
leal -0x1(%r13), %eax
movl 0x18(%r15), %ecx
leaq (%rax,%rax,2), %r12
shlq $0x5, %r12
cmpl 0x18(%rbx,%r12), %ecx
jne 0x1a4a3
addq %rbx, %r12
movq (%r15), %rax
movq 0x8(%r12), %r8
movslq 0x90(%rbp), %rcx
addq %r8, %rcx
cmpq %rcx, %rax
jge 0x1a4a3
movl 0x10(%r12), %ecx
movl 0x14(%r12), %esi
movl %esi, %edi
subl %ecx, %edi
je 0x1a49a
movl 0x10(%r15), %edx
cmpl %edx, %ecx
jge 0x1a367
subl %edx, %esi
jmp 0x1a36d
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(%rbp), %xmm0
mulss %xmm0, %xmm2
ucomiss %xmm2, %xmm1
jbe 0x1a3e8
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 0x1a3e8
movl 0x1c(%r15), %eax
cmpl 0x1c(%r12), %eax
jl 0x1a4b6
movl %ecx, 0x14(%r12)
jmp 0x1a49a
cmpq %rax, %r9
jge 0x1a49a
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 0x19ec0
testl %eax, %eax
jle 0x1a49a
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 %r13d
jg 0x1a30e
incq %r14
movq 0x30(%rsp), %r12
cmpq %r12, %r14
jne 0x1a2d6
jmp 0x1a4bc
movl %edx, 0x14(%r15)
jmp 0x1a4a3
leaq 0x14(%rbx), %rax
xorl %edx, %edx
xorl %r15d, %r15d
movl (%rax), %ecx
cmpl -0x4(%rax), %ecx
jle 0x1a4f5
movl %r15d, %ecx
leal 0x1(%r15), %r8d
cmpq %rcx, %rdx
je 0x1a4f2
leaq -0x14(%rax), %rsi
movslq %r15d, %rcx
leaq (%rcx,%rcx,2), %rdi
shlq $0x5, %rdi
addq %rbx, %rdi
movl $0xc, %ecx
rep  movsq (%rsi), %es:(%rdi)
movl %r8d, %r15d
incq %rdx
addq $0x60, %rax
cmpq %rdx, %r12
jne 0x1a4c5
movslq %r15d, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x17abd
movl $0x1, %eax
cmpl $0x2, %r14d
jl 0x1a5af
movl %r15d, %edx
leaq (%rdx,%rdx,2), %rcx
shlq $0x5, %rcx
addq $-0x60, %rcx
xorl %esi, %esi
movl 0x7c(%rbx,%rsi), %edi
cmpl 0x1c(%rbx,%rsi), %edi
jne 0x1a552
movq 0x60(%rbx,%rsi), %rdi
cmpq (%rbx,%rsi), %rdi
jne 0x1a552
movl 0x70(%rbx,%rsi), %edi
cmpl 0x10(%rbx,%rsi), %edi
jne 0x1a552
movl %edi, 0x74(%rbx,%rsi)
addq $0x60, %rsi
cmpq %rsi, %rcx
jne 0x1a52f
cmpl $0x2, %r15d
jl 0x1a5af
leaq 0x74(%rbx), %r8
movl $0x1, %ecx
movl $0x1, %r9d
movl (%r8), %eax
cmpl -0x4(%r8), %eax
jle 0x1a59f
movl %ecx, %esi
leal 0x1(%rcx), %eax
cmpq %rsi, %r9
je 0x1a5a1
leaq -0x14(%r8), %rsi
movslq %ecx, %rcx
leaq (%rcx,%rcx,2), %rdi
shlq $0x5, %rdi
addq %rbx, %rdi
movl $0xc, %ecx
rep  movsq (%rsi), %es:(%rdi)
jmp 0x1a5a1
movl %ecx, %eax
incq %r9
addq $0x60, %r8
movl %eax, %ecx
cmpq %r9, %rdx
jne 0x1a570
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq