// File: /workspace/github_c_makefile_star2_O1/lh3[P]minigraph/map-algo.c
// Function: collect_matches
static mg_match_t *collect_matches(void *km, int *_n_m, int max_occ, const mg_idx_t *gi, const mg128_v *mv, int64_t *n_a, int *rep_len, int *n_mini_pos, int32_t **mini_pos)
{
	int rep_st = 0, rep_en = 0, n_m;
	size_t i;
	mg_match_t *m;
	*n_mini_pos = 0;
	KMALLOC(km, *mini_pos, mv->n);
	m = (mg_match_t*)kmalloc(km, mv->n * sizeof(mg_match_t));
	for (i = 0, n_m = 0, *rep_len = 0, *n_a = 0; i < mv->n; ++i) {
		const uint64_t *cr;
		mg128_t *p = &mv->a[i];
		uint32_t q_pos = (uint32_t)p->y, q_span = p->x & 0xff;
		int t;
		cr = mg_idx_get(gi, p->x>>8, &t);
		if (t >= max_occ) {
			int en = (q_pos >> 1) + 1, st = en - q_span;
			if (st > rep_en) {
				*rep_len += rep_en - rep_st;
				rep_st = st, rep_en = en;
			} else rep_en = en;
		} else {
			mg_match_t *q = &m[n_m++];
			q->q_pos = q_pos, q->q_span = q_span, q->cr = cr, q->n = t, q->seg_id = p->y >> 32;
			q->is_tandem = 0;
			if (i > 0 && p->x>>8 == mv->a[i - 1].x>>8) q->is_tandem = 1;
			if (i < mv->n - 1 && p->x>>8 == mv->a[i + 1].x>>8) q->is_tandem = 1;
			*n_a += q->n;
			(*mini_pos)[(*n_mini_pos)++] = q_pos>>1;
		}
	}
	*rep_len += rep_en - rep_st;
	*_n_m = n_m;
	return m;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %r12
movq %r8, %r13
movq %rcx, 0x40(%rsp)
movl %edx, 0x24(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
movq 0x90(%rsp), %rbp
movq 0x88(%rsp), %rax
xorl %r15d, %r15d
movl %r15d, (%rax)
movq (%r8), %rsi
shlq $0x2, %rsi
callq 0x31c1
movq %rax, (%rbp)
movq (%r13), %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
movq %rbx, %rdi
callq 0x31c1
movq %rax, 0x28(%rsp)
movq 0x80(%rsp), %rax
movl %r15d, (%rax)
xorl %ecx, %ecx
movq %r12, 0x38(%rsp)
movq $0x0, (%r12)
cmpq $0x0, (%r13)
movl $0x0, %edx
je 0x1ebc5
movq %r14, 0x30(%rsp)
xorl %ebx, %ebx
xorl %r15d, %r15d
movl $0x0, 0x10(%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
xorl %ebp, %ebp
movq %r13, 0x18(%rsp)
movq 0x10(%r13), %r14
movl 0x8(%r14,%rbx), %r12d
movq (%r14,%rbx), %rsi
movzbl %sil, %r13d
shrq $0x8, %rsi
movq 0x40(%rsp), %rdi
leaq 0x14(%rsp), %rdx
callq 0x151cd
movl 0x24(%rsp), %ecx
cmpl %ecx, 0x14(%rsp)
jge 0x1eb64
movl 0xc(%rsp), %edi
movslq %edi, %rcx
leaq (%rcx,%rcx,2), %rdx
movq 0x28(%rsp), %r8
leaq (%r8,%rdx,8), %rcx
movl %r12d, 0x4(%r8,%rdx,8)
movl %r13d, 0x8(%r8,%rdx,8)
movq %rax, 0x10(%r8,%rdx,8)
movl 0x14(%rsp), %eax
movl %eax, (%r8,%rdx,8)
movl 0xc(%r14,%rbx), %eax
movl %eax, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl %esi, 0xc(%r8,%rdx,8)
testq %rbp, %rbp
movq 0x18(%rsp), %r13
je 0x1eb0b
movq 0x10(%r13), %rdx
movq -0x10(%rdx,%rbx), %rdx
xorq (%r14,%rbx), %rdx
cmpq $0xff, %rdx
ja 0x1eb0b
orl $0x80000000, %eax       # imm = 0x80000000
movl %eax, 0xc(%rcx)
movq (%r13), %rax
decq %rax
cmpq %rax, %rbp
jae 0x1eb30
movq 0x10(%r13), %rax
movq 0x10(%rax,%rbx), %rax
xorq (%r14,%rbx), %rax
cmpq $0xff, %rax
ja 0x1eb30
orb $-0x80, 0xf(%rcx)
movl (%rcx), %eax
movq 0x38(%rsp), %rcx
addq %rax, (%rcx)
incl %edi
movl %edi, 0xc(%rsp)
shrl %r12d
movq 0x90(%rsp), %rax
movq (%rax), %rax
movq 0x88(%rsp), %rsi
movslq (%rsi), %rcx
leal 0x1(%rcx), %edx
movl %edx, (%rsi)
movl %r12d, (%rax,%rcx,4)
jmp 0x1eb9b
shrl %r12d
incl %r12d
movl %r12d, %eax
subl %r13d, %eax
movl 0x8(%rsp), %ecx
cmpl %ecx, %eax
jle 0x1eb91
addl %ecx, %r15d
movq 0x80(%rsp), %rcx
addl %r15d, (%rcx)
movl %r12d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
jmp 0x1eb96
movl %r12d, 0x8(%rsp)
movq 0x18(%rsp), %r13
incq %rbp
movl 0x10(%rsp), %r15d
negl %r15d
addq $0x10, %rbx
cmpq (%r13), %rbp
jb 0x1ea74
movl 0x8(%rsp), %edx
subl 0x10(%rsp), %edx
movq 0x30(%rsp), %r14
movl 0xc(%rsp), %ecx
movq 0x80(%rsp), %rax
addl %edx, (%rax)
movl %ecx, (%r14)
movq 0x28(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq