// File: /workspace/github_c_makefile_star2_O1/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, 0x1c(%rsp)
movss %xmm0, 0xc(%rsp)
movl %edi, %r13d
movl $0x8, %esi
movq %r8, 0x50(%rsp)
movq %r8, %rdi
callq 0x10f0
movq %rax, 0x30(%rsp)
testq %r14, %r14
je 0x5fb4
movq %rbx, 0x40(%rsp)
leaq -0x20(%rbx), %rax
movq %rax, 0x58(%rsp)
movl $0x1, %esi
xorl %ebp, %ebp
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq $0x0, 0x28(%rsp)
xorl %r12d, %r12d
xorl %r9d, %r9d
movl 0x1c(%rsp), %ebx
movq %r14, 0x60(%rsp)
cmpq %r14, %rsi
je 0x5cd4
movq %rsi, %rax
shlq $0x5, %rax
movq 0x40(%rsp), %rdx
movq -0x20(%rdx,%rax), %rcx
xorq (%rdx,%rax), %rcx
shrq $0x20, %rcx
je 0x5fa6
movq %rsi, %rax
shlq $0x5, %rax
movq 0x58(%rsp), %rcx
movq (%rcx,%rax), %rax
movq %rax, 0x38(%rsp)
movq %rsi, %rax
subq %rbp, %rax
cmpq %rax, %r9
movq %rsi, 0x20(%rsp)
jae 0x5d51
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 %r12, %rdi
callq 0x1180
movq %r15, %r9
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq 0x20(%rsp), %rsi
movq %rax, %r12
movss 0xc(%rsp), %xmm2
cmpq %rbp, %rsi
jbe 0x5e85
movq 0x38(%rsp), %rdx
shrq $0x20, %rdx
movq %rbp, %r15
shlq $0x5, %r15
addq 0x40(%rsp), %r15
movq $0x0, 0x10(%rsp)
movq %rdx, 0x48(%rsp)
cmpl %edx, 0xc(%r15)
je 0x5e73
movq 0x18(%r15), %rax
movl %eax, %ecx
andl %r8d, %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
shrq $0x20, %rax
andl %r8d, %eax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
mulss %xmm2, %xmm1
ucomiss %xmm0, %xmm1
ja 0x5e73
movq %r12, %rdi
movl (%r15), %r12d
addl %ebx, %r12d
movl 0x8(%r15), %r14d
subl %ebx, %r14d
cmpl %r12d, %r14d
jbe 0x5e70
cmpq %r9, 0x10(%rsp)
jne 0x5dfb
leaq (%r9,%r9), %rbx
testq %r9, %r9
movl $0x2, %eax
cmoveq %rax, %rbx
leaq (,%rbx,4), %rsi
callq 0x1180
movq %rax, %rdi
movq %rbx, %r9
addl %r12d, %r12d
movq 0x10(%rsp), %rcx
leaq 0x1(%rcx), %rax
movl %r12d, (%rdi,%rcx,4)
cmpq %r9, %rax
movq %rdi, %r12
jne 0x5e39
leaq (%r9,%r9), %rbx
testq %r9, %r9
movl $0x2, %eax
cmoveq %rax, %rbx
leaq (,%rbx,4), %rsi
movq %r12, %rdi
callq 0x1180
movq %rax, %r12
movq %rbx, %r9
movl 0x1c(%rsp), %ebx
movss 0xc(%rsp), %xmm2
movq 0x20(%rsp), %rsi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq 0x48(%rsp), %rdx
leal 0x1(,%r14,2), %eax
movq 0x10(%rsp), %rcx
movl %eax, 0x4(%r12,%rcx,4)
addq $0x2, %rcx
movq %rcx, 0x10(%rsp)
jmp 0x5e73
movq %rdi, %r12
incq %rbp
addq $0x20, %r15
cmpq %rbp, %rsi
jne 0x5d83
jmp 0x5e8e
movq $0x0, 0x10(%rsp)
movq %r9, %r15
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x53de
testq %r14, %r14
movq 0x60(%rsp), %r14
je 0x5f3e
xorl %edx, %edx
xorl %eax, %eax
xorl %ecx, %ecx
xorl %esi, %esi
xorl %edi, %edi
movq 0x38(%rsp), %rbp
movl %esi, %r9d
movl (%r12,%rdx,4), %esi
movl %esi, %r8d
andl $0x1, %esi
negl %esi
orl $0x1, %esi
addl %r9d, %esi
cmpl %r13d, %r9d
setge %r10b
cmpl %r13d, %esi
setl %r11b
orb %r10b, %r11b
je 0x5f2c
cmpl %r13d, %esi
setl %r10b
cmpl %r13d, %r9d
setge %r9b
andb %r10b, %r9b
cmpb $0x1, %r9b
jne 0x5f32
shrl %r8d
movl %r8d, %r10d
subl %edi, %r10d
movl %eax, %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movl %ecx, %r11d
subl %r9d, %r11d
movl %edi, %r9d
cmpl %r11d, %r10d
ja 0x5f24
movl %ecx, %r8d
movl %eax, %r9d
movl %r8d, %ecx
movl %r9d, %eax
jmp 0x5f32
shrl %r8d
movq %r8, %rdi
incq %rdx
cmpq %rdx, 0x10(%rsp)
jne 0x5ebe
jmp 0x5f47
xorl %ecx, %ecx
xorl %eax, %eax
movq 0x38(%rsp), %rbp
movl %eax, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
sarq $0x20, %rbp
cmpl %edx, %ecx
jne 0x5f74
movq 0x30(%rsp), %rax
orb $-0x80, 0x3(%rax,%rbp,8)
movq 0x20(%rsp), %rsi
movq %rsi, %rbp
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq %r15, %r9
jmp 0x5fa6
cmpq 0x50(%rsp), %rbp
movq 0x20(%rsp), %rsi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
jae 0x5ff4
movq %r15, %r9
subl %ebx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addl %ebx, %ecx
movq 0x30(%rsp), %rdx
movl %ecx, 0x4(%rdx,%rbp,8)
movl %eax, (%rdx,%rbp,8)
incq 0x28(%rsp)
movq %rsi, %rbp
incq %rsi
cmpq %r14, %rsi
jbe 0x5cb0
jmp 0x5fc0
xorl %r12d, %r12d
movq $0x0, 0x28(%rsp)
movq %r12, %rdi
callq 0x1040
leaq 0x9169(%rip), %rax      # 0xf138
cmpl $0x3, (%rax)
jge 0x5fe8
movq 0x30(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28(%rsp), %rdi
callq 0x1c5a
jmp 0x5fd4
leaq 0x43d6(%rip), %rdi      # 0xa3d1
leaq 0x43db(%rip), %rsi      # 0xa3dd
leaq 0x43e2(%rip), %rcx      # 0xa3eb
movl $0x93, %edx
callq 0x10c0