// File: /workspace/github_c_makefile_star2_O2/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 $0x68, %rsp
movq %rcx, %rbx
movq %rdx, %r14
movl %esi, %r15d
movss %xmm0, 0xc(%rsp)
movl %edi, 0x2c(%rsp)
pushq $0x8
popq %rsi
movq %r8, 0x48(%rsp)
movq %r8, %rdi
callq 0x10f0
movq %rax, 0x20(%rsp)
movq %r14, 0x58(%rsp)
shlq $0x5, %r14
movq %rbx, 0x38(%rsp)
leaq (%rbx,%r14), %rax
addq $-0x20, %rax
movq %rax, 0x50(%rsp)
pushq $0x1
popq %rdx
xorl %r8d, %r8d
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
xorl %r12d, %r12d
movq $0x0, 0x30(%rsp)
xorl %ebp, %ebp
movl %r15d, 0x1c(%rsp)
cmpq 0x58(%rsp), %rdx
ja 0x4561
jne 0x42ca
movq 0x50(%rsp), %rax
movq (%rax), %r14
jmp 0x42ec
movq %rdx, %rax
shlq $0x5, %rax
movq 0x38(%rsp), %rcx
movq -0x20(%rcx,%rax), %r14
movq (%rcx,%rax), %rax
xorq %r14, %rax
shrq $0x20, %rax
je 0x4559
movq %rdx, %rax
subq %rbp, %rax
cmpq %rax, %r8
movq %rdx, 0x10(%rsp)
jae 0x4353
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, %rbx
shrq $0x10, %rbx
orq %rax, %rbx
leaq 0x4(,%rbx,4), %rsi
incq %rbx
movq %r12, %rdi
callq 0x1180
movq %rbx, %r8
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq 0x10(%rsp), %rdx
movq %rax, %r12
movss 0xc(%rsp), %xmm2
movq %r14, 0x60(%rsp)
movq %r14, %r9
shrq $0x20, %r9
movq %rbp, %rbx
shlq $0x5, %rbx
addq 0x38(%rsp), %rbx
xorl %r13d, %r13d
movq %r9, 0x40(%rsp)
cmpq %rdx, %rbp
jae 0x4461
cmpl %r9d, 0xc(%rbx)
je 0x4455
movq 0x18(%rbx), %rax
movl %eax, %ecx
andl %esi, %ecx
cvtsi2ss %ecx, %xmm0
shrq $0x20, %rax
andl %esi, %eax
cvtsi2ss %eax, %xmm1
mulss %xmm2, %xmm1
ucomiss %xmm0, %xmm1
ja 0x4455
movq %r12, %rdi
movl (%rbx), %r12d
addl %r15d, %r12d
movl 0x8(%rbx), %r14d
subl %r15d, %r14d
cmpl %r12d, %r14d
jbe 0x4452
cmpq %r8, %r13
jne 0x43ee
leaq (%r8,%r8), %r15
testq %r8, %r8
pushq $0x2
popq %rax
cmoveq %rax, %r15
leaq (,%r15,4), %rsi
callq 0x1180
movq %rax, %rdi
movq %r15, %r8
addl %r12d, %r12d
leaq 0x1(%r13), %rax
movl %r12d, (%rdi,%r13,4)
cmpq %r8, %rax
movq %rdi, %r12
jne 0x4425
leaq (%r8,%r8), %r15
testq %r8, %r8
pushq $0x2
popq %rax
cmoveq %rax, %r15
leaq (,%r15,4), %rsi
movq %r12, %rdi
callq 0x1180
movq %rax, %r12
movq %r15, %r8
movl 0x1c(%rsp), %r15d
movss 0xc(%rsp), %xmm2
movq 0x10(%rsp), %rdx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq 0x40(%rsp), %r9
leal 0x1(,%r14,2), %eax
movl %eax, 0x4(%r12,%r13,4)
addq $0x2, %r13
jmp 0x4455
movq %rdi, %r12
incq %rbp
addq $0x20, %rbx
jmp 0x4379
movq %r8, %r14
movq %r13, %rdi
movq %r12, %rsi
callq 0x3a0f
xorl %edx, %edx
xorl %r9d, %r9d
xorl %eax, %eax
xorl %ecx, %ecx
xorl %esi, %esi
movl 0x2c(%rsp), %ebx
cmpq %rsi, %r13
je 0x44f0
movl (%r12,%rsi,4), %r8d
movl %r8d, %edi
andl $0x1, %edi
negl %edi
orl $0x1, %edi
addl %r9d, %edi
cmpl %ebx, %r9d
setge %r10b
cmpl %ebx, %edi
setl %r11b
orb %r10b, %r11b
je 0x44e2
cmpl %ebx, %edi
setl %r10b
cmpl %ebx, %r9d
setge %r9b
andb %r10b, %r9b
cmpb $0x1, %r9b
jne 0x44e8
shrl %r8d
movl %r8d, %r9d
subl %edx, %r9d
movl %ecx, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movl %eax, %r11d
subl %r10d, %r11d
cmpl %r11d, %r9d
cmoval %r8d, %eax
cmoval %edx, %ecx
jmp 0x44e8
shrl %r8d
movl %r8d, %edx
incq %rsi
movl %edi, %r9d
jmp 0x447e
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq 0x60(%rsp), %rdi
sarq $0x20, %rdi
cmpl %edx, %eax
jne 0x4521
movq 0x20(%rsp), %rax
orb $-0x80, 0x3(%rax,%rdi,8)
movq 0x10(%rsp), %rdx
movq %rdx, %rbp
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq %r14, %r8
jmp 0x4559
cmpq 0x48(%rsp), %rdi
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
jae 0x45bc
movq %r14, %r8
subl %r15d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addl %r15d, %eax
movq 0x20(%rsp), %rdx
movl %eax, 0x4(%rdx,%rdi,8)
movl %ecx, (%rdx,%rdi,8)
incq 0x30(%rsp)
movq 0x10(%rsp), %rdx
movq %rdx, %rbp
incq %rdx
jmp 0x42b3
movq %r12, %rdi
callq 0x1040
leaq 0x9bc8(%rip), %rax      # 0xe138
cmpl $0x3, (%rax)
jge 0x4589
movq 0x20(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x9a68(%rip), %rax      # 0xdff8
movq (%rax), %r14
xorl %eax, %eax
callq 0x1412
leaq 0x4e62(%rip), %rsi      # 0x9403
leaq 0x4e8d(%rip), %rdx      # 0x9435
movq %r14, %rdi
movq %rax, %rcx
movq 0x30(%rsp), %r8
xorl %eax, %eax
callq 0x1120
jmp 0x4575
leaq 0x4dde(%rip), %rdi      # 0x93a1
leaq 0x4de3(%rip), %rsi      # 0x93ad
leaq 0x4dea(%rip), %rcx      # 0x93bb
movl $0x93, %edx
callq 0x10c0