// File: /workspace/github_c_makefile_star2_O3/rikuu[P]kermit/miniasm/hit.c
// Function: ma_hit_sub
ma_sub_t *ma_hit_sub(int min_dp, float min_iden, int end_clip, size_t n, const ma_hit_t *a, size_t n_sub)
{
	size_t i, j, last, n_remained = 0;
	kvec_t(uint32_t) b = {0,0,0};
	ma_sub_t *sub = 0;

	sub = (ma_sub_t*)calloc(n_sub, sizeof(ma_sub_t));
	for (i = 1, last = 0; i <= n; ++i) {
		if (i == n || a[i].qns>>32 != a[i-1].qns>>32) { // we come to a new query sequence
			size_t start = 0;
			int dp, qid = a[i-1].qns>>32;
			ma_sub_t max, max2;
			kv_resize(uint32_t, b, i - last);
			b.n = 0;
			for (j = last; j < i; ++j) { // collect all starts and ends
				uint32_t qs, qe;
				if (a[j].tn == qid || a[j].ml < a[j].bl * min_iden) continue; // skip self match
				qs = (uint32_t)a[j].qns + end_clip, qe = a[j].qe - end_clip;
				if (qe > qs) {
					kv_push(uint32_t, b, qs<<1);
					kv_push(uint32_t, b, qe<<1|1);
				}
			}
			ks_introsort_uint32_t(b.n, b.a);
			max.s = max.e = max.del = max2.s = max2.e = max2.del = 0;
			for (j = 0, dp = 0; j < b.n; ++j) {
				int old_dp = dp;
				if (b.a[j]&1) --dp;
				else ++dp;
				if (old_dp < min_dp && dp >= min_dp) {
					start = b.a[j]>>1;
				} else if (old_dp >= min_dp && dp < min_dp) {
					int len = (b.a[j]>>1) - start;
					if (len > max.e - max.s) max2 = max, max.s = start, max.e = b.a[j]>>1;
					else if (len > max2.e - max2.s) max2.s = start, max2.e = b.a[j]>>1;
				}
			}
			if (max.e - max.s > 0) {
				assert(qid < n_sub);
				sub[qid].s = max.s - end_clip;
				sub[qid].e = max.e + end_clip;
				sub[qid].del = 0;
				++n_remained;
			} else sub[qid].del = 1;
			last = i;
		}
	}
	free(b.a);
	if (ma_verbose >= 3)
		fprintf(stderr, "[M::%s::%s] %ld query sequences remain after sub\n", __func__, sys_timestamp(), n_remained);
	return sub;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, %r14
movq %rdx, %r12
movl %esi, 0x4(%rsp)
movss %xmm0, (%rsp)
movl %edi, %ebp
movl $0x8, %esi
movq %r8, 0x40(%rsp)
movq %r8, %rdi
callq 0x10f0
movq %rax, %rbx
testq %r12, %r12
je 0x5d11
movl %ebp, 0x24(%rsp)
movq %r12, %rax
shlq $0x5, %rax
addq %r14, %rax
addq $-0x20, %rax
movq %rax, 0x48(%rsp)
movl $0x1, %edx
xorl %ebp, %ebp
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq $0x0, 0x10(%rsp)
xorl %r8d, %r8d
xorl %r9d, %r9d
movq %rbx, 0x30(%rsp)
movq %r14, 0x28(%rsp)
movq %r12, 0x38(%rsp)
cmpq %r12, %rdx
jne 0x5a41
movq 0x48(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x18(%rsp)
jmp 0x5a63
movq %rdx, %rax
shlq $0x5, %rax
movq -0x20(%r14,%rax), %rcx
movq (%r14,%rax), %rax
movq %rcx, 0x18(%rsp)
xorq %rcx, %rax
shrq $0x20, %rax
je 0x5d03
movq %rdx, %rax
subq %rbp, %rax
cmpq %rax, %r9
movq %rdx, 0x8(%rsp)
jae 0x5aca
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %r15
shrq $0x10, %r15
orq %rax, %r15
leaq 0x4(,%r15,4), %rsi
incq %r15
movq %r8, %rdi
callq 0x1180
movq %r15, %r9
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq 0x8(%rsp), %rdx
movq %rax, %r8
movss (%rsp), %xmm2
cmpq %rbp, %rdx
jbe 0x5c88
movq 0x18(%rsp), %r12
shrq $0x20, %r12
movq %rbp, %r15
shlq $0x5, %r15
addq %r14, %r15
xorl %ebx, %ebx
movq %r12, 0x50(%rsp)
cmpl %r12d, 0xc(%r15)
je 0x5bce
movq 0x18(%r15), %rax
movl %eax, %ecx
andl %esi, %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
shrq $0x20, %rax
andl %esi, %eax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
mulss %xmm2, %xmm1
ucomiss %xmm0, %xmm1
ja 0x5bce
movl (%r15), %r14d
movl 0x4(%rsp), %eax
addl %eax, %r14d
movl 0x8(%r15), %r13d
subl %eax, %r13d
cmpl %r14d, %r13d
jbe 0x5bce
cmpq %r9, %rbx
jne 0x5b76
movq %rbx, %r12
addq %rbx, %rbx
testq %r12, %r12
movl $0x2, %eax
cmoveq %rax, %rbx
leaq (,%rbx,4), %rsi
movq %r8, %rdi
callq 0x1180
movq %rbx, %r9
movq %r12, %rbx
movq 0x50(%rsp), %r12
movq %rax, %rdi
jmp 0x5b79
movq %r8, %rdi
addl %r14d, %r14d
leaq 0x1(%rbx), %rax
movl %r14d, (%rdi,%rbx,4)
cmpq %r9, %rax
jne 0x5bac
leaq (%r9,%r9), %r14
testq %r9, %r9
movl $0x2, %eax
cmoveq %rax, %r14
leaq (,%r14,4), %rsi
callq 0x1180
movq %rax, %rdi
movq %r14, %r9
movss (%rsp), %xmm2
movq 0x8(%rsp), %rdx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
leal 0x1(,%r13,2), %eax
movq %rdi, %r8
movl %eax, 0x4(%rdi,%rbx,4)
addq $0x2, %rbx
incq %rbp
addq $0x20, %r15
cmpq %rbp, %rdx
jne 0x5af2
movq %r9, %rbp
movq %rbx, %rdi
movq %r8, %r13
movq %r8, %rsi
callq 0x5162
testq %rbx, %rbx
movq 0x38(%rsp), %r12
je 0x5c9f
xorl %ecx, %ecx
xorl %eax, %eax
xorl %r15d, %r15d
xorl %esi, %esi
xorl %edx, %edx
movq 0x28(%rsp), %r14
movl 0x24(%rsp), %r11d
movl %esi, %r8d
movl (%r13,%rcx,4), %edi
movl %edi, %esi
andl $0x1, %esi
negl %esi
orl $0x1, %esi
addl %r8d, %esi
cmpl %r11d, %r8d
setge %r9b
cmpl %r11d, %esi
setl %r10b
orb %r9b, %r10b
je 0x5c75
cmpl %r11d, %esi
setl %r9b
cmpl %r11d, %r8d
setge %r8b
andb %r9b, %r8b
cmpb $0x1, %r8b
jne 0x5c79
shrl %edi
movl %edi, %r8d
subl %edx, %r8d
movl %eax, %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movl %r15d, %r10d
subl %r9d, %r10d
cmpl %r10d, %r8d
cmoval %edi, %r15d
cmoval %edx, %eax
jmp 0x5c79
shrl %edi
movl %edi, %edx
incq %rcx
cmpq %rcx, %rbx
jne 0x5c12
movq 0x30(%rsp), %rbx
jmp 0x5cae
movq %r9, %rbp
xorl %r15d, %r15d
xorl %edi, %edi
movq %r8, %r13
movq %r8, %rsi
callq 0x5162
xorl %eax, %eax
jmp 0x5cae
xorl %r15d, %r15d
xorl %eax, %eax
movq 0x30(%rsp), %rbx
movq 0x28(%rsp), %r14
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movq 0x18(%rsp), %rdi
sarq $0x20, %rdi
cmpl %ecx, %r15d
movq 0x8(%rsp), %rdx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq %rbp, %r9
jne 0x5cdb
movq %r13, %r8
orb $-0x80, 0x3(%rbx,%rdi,8)
jmp 0x5d00
cmpq 0x40(%rsp), %rdi
jae 0x5d4f
movq %r13, %r8
movl 0x4(%rsp), %ecx
subl %ecx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addl %ecx, %r15d
movl %r15d, 0x4(%rbx,%rdi,8)
movl %eax, (%rbx,%rdi,8)
incq 0x10(%rsp)
movq %rdx, %rbp
incq %rdx
cmpq %r12, %rdx
jbe 0x5a2d
jmp 0x5d1d
xorl %r8d, %r8d
movq $0x0, 0x10(%rsp)
movq %r8, %rdi
callq 0x1040
leaq 0x840c(%rip), %rax      # 0xe138
cmpl $0x3, (%rax)
jge 0x5d43
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rdi
callq 0x1c75
jmp 0x5d31
leaq 0x464b(%rip), %rdi      # 0xa3a1
leaq 0x4650(%rip), %rsi      # 0xa3ad
leaq 0x4657(%rip), %rcx      # 0xa3bb
movl $0x93, %edx
callq 0x10c0