// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: collect_seed_hits(void*, mg_idxopt_t const*, int, void const*, ha_pt_s const*, ma_ug_t const*, ha_mzl_v const*, long*, int*, int*, int**)
mg128_t *collect_seed_hits(void *km, const mg_idxopt_t *opt, int max_occ, const void *ha_flt_tab, const ha_pt_t *ha_idx, 
const ma_ug_t *ug, const ha_mzl_v *mv, int64_t *n_a, int *rep_len, int *n_mini_pos, int32_t **mini_pos)
{
    int i, n_m;
    mg128_t *a = NULL;
    mg_match_t *m = collect_matches(km, &n_m, max_occ, ha_flt_tab, ha_idx, 1, mv, n_a, rep_len, n_mini_pos, mini_pos);
    a = (mg128_t*)kmalloc(km, *n_a * sizeof(mg128_t));///n_a: how many available candidates in total 
    for (i = 0, *n_a = 0; i < n_m; ++i) {///n_m: how many available seeds, instead of candidates
		mg_match_t *q = &m[i];
		const ha_idxposl_t *r = q->cr;
		uint32_t k;
		for (k = 0; k < q->n; ++k) {///q->n: number of candidates belonging to seed m[i]
			mg128_t *p;
			p = &a[(*n_a)++];///pick up a slot for one candidate
			if (r[k].rev == q->rev) // forward strand
				p->x = (uint64_t)(r[k].rid)<<33|r[k].pos; ///reference: rid(31)|rev(1)|pos(32)                
			else // reverse strand
				p->x = (uint64_t)(r[k].rid)<<33 | 1ULL<<32 | (ug->g->seq[r[k].rid].len - (r[k].pos + 1 - r[k].span) - 1);
			p->y = (uint64_t)q->q_span << 32 | q->q_pos;
			p->y |= (uint64_t)q->qid << MG_SEED_SEG_SHIFT;
			if (q->is_tandem) p->y |= MG_SEED_TANDEM;
			p->y |= (uint64_t)q->weight << MG_SEED_WT_SHIFT; 
			///p->y: weight(8)seg_id(8)flag(8)span(8)pos(32)
			///p->x: rid(31)rev(1)pos(33); keep reference
		}
	}
    kfree(km, m);
	radix_sort_128x(a, a + (*n_a));
	return a;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %rbx
movq %r8, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movl %edx, 0x20(%rsp)
movq %rdi, %r14
movq 0xa0(%rsp), %r12
movq 0x98(%rsp), %rax
movq 0x90(%rsp), %r15
movq 0x88(%rsp), %r13
andl $0x0, (%rax)
movq 0x80(%rsp), %rbp
movl (%rbp), %esi
shlq $0x2, %rsi
callq 0x1a39c4
movq %rax, (%r12)
movl (%rbp), %eax
imulq $0x18, %rax, %rsi
movq %r14, 0x28(%rsp)
movq %r14, %rdi
callq 0x1a39c4
andl $0x0, (%r15)
andq $0x0, (%r13)
movq %rax, 0x10(%rsp)
xorl %edx, %edx
pushq $0x10
popq %rbp
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
addq $0xff, %rax
movq %rax, 0x30(%rsp)
movq $0x0, 0x8(%rsp)
xorl %r12d, %r12d
xorl %esi, %esi
movq 0x80(%rsp), %rcx
movl (%rcx), %eax
cmpq %rax, %rdx
jae 0x15ad56
movl %esi, 0x4(%rsp)
movq %rdx, 0x18(%rsp)
movq 0x8(%rcx), %r15
movq -0x10(%r15,%rbp), %rsi
movq 0x40(%rsp), %rdi
leaq 0x24(%rsp), %rdx
callq 0xd4e67
movq %rax, %r14
movq -0x10(%r15,%rbp), %rsi
movq 0x38(%rsp), %rdi
callq 0xd5bc2
cmpl $0x0, 0x20(%rsp)
jle 0x15ad15
cmpl $0x1, %eax
jne 0x15ad15
movq 0x8(%rsp), %rdi
movslq %edi, %rax
imulq $0x18, %rax, %rax
movq -0x8(%r15,%rbp), %rcx
movabsq $-0x100000000, %rdx     # imm = 0xFFFFFFFF00000000
andq %rdx, %rcx
movq 0x10(%rsp), %rsi
movl 0x4(%rsi,%rax), %edx
orq %rcx, %rdx
movq %rdx, 0x4(%rsi,%rax)
movzbl (%r15,%rbp), %ecx
andq $-0x80000000, %rdx      # imm = 0x80000000
orq %rcx, %rdx
movq %rdx, 0x4(%rsi,%rax)
movq -0x8(%r15,%rbp), %rcx
andl $0x80000000, %ecx       # imm = 0x80000000
andq 0x30(%rsp), %rdx
orq %rcx, %rdx
movq %rdx, 0x4(%rsi,%rax)
movq %r14, 0x10(%rsi,%rax)
movl 0x24(%rsp), %ecx
movl %ecx, (%rsi,%rax)
movl $0xff0000, 0xc(%rsi,%rax) # imm = 0xFF0000
incl %edi
movq %rdi, 0x8(%rsp)
addq %rcx, (%r13)
movl -0x4(%r15,%rbp), %eax
movq 0xa0(%rsp), %rcx
movq (%rcx), %rcx
movq 0x98(%rsp), %rdi
movslq (%rdi), %rdx
leal 0x1(%rdx), %esi
movl %esi, (%rdi)
movl %eax, (%rcx,%rdx,4)
movl %r12d, %eax
jmp 0x15ad3c
movl -0x4(%r15,%rbp), %eax
incl %eax
movzbl (%r15,%rbp), %edx
movl %eax, %ecx
subl %edx, %ecx
cmpl %r12d, %ecx
jle 0x15ad3c
subl 0x4(%rsp), %r12d
movq 0x90(%rsp), %rdx
addl %r12d, (%rdx)
jmp 0x15ad40
movl 0x4(%rsp), %ecx
movq 0x18(%rsp), %rdx
incq %rdx
addq $0x18, %rbp
movl %eax, %r12d
movl %ecx, %esi
jmp 0x15ac19
subl %esi, %r12d
movq 0x90(%rsp), %rax
addl %r12d, (%rax)
movq (%r13), %rsi
shlq $0x4, %rsi
movq 0x28(%rsp), %rdi
callq 0x1a39c4
movq %rax, %r12
andq $0x0, (%r13)
xorl %eax, %eax
movq 0x8(%rsp), %rbp
testl %ebp, %ebp
cmovlel %eax, %ebp
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rbp, %rax
je 0x15ae84
imulq $0x18, %rax, %rdi
movq 0x10(%rsp), %r8
leaq (%r8,%rdi), %rdx
movq 0x10(%r8,%rdi), %rsi
movl (%r8,%rdi), %edi
shlq $0x4, %rdi
xorl %r8d, %r8d
cmpq %r8, %rdi
je 0x15ae7c
movq (%r13), %r9
leaq 0x1(%r9), %r10
movq %r10, (%r13)
movq (%rsi,%r8), %r10
movl 0x4(%rdx), %r11d
xorl %r10d, %r11d
js 0x15ade7
movq %r10, %r11
shlq $0x21, %r11
shrq $0x20, %r10
orq %r11, %r10
jmp 0x15ae24
movl %r10d, %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movq %r10, %r14
shlq $0x21, %r14
movq 0x18(%rbx), %r15
movq 0x20(%r15), %r15
movl (%r15,%r11,8), %r11d
andl %ecx, %r11d
shrq $0x20, %r10
movzbl 0x8(%rsi,%r8), %r15d
subl %r10d, %r11d
leal (%r11,%r15), %r10d
addl $-0x2, %r10d
orq %r14, %r10
btsq $0x20, %r10
shlq $0x4, %r9
addq %r12, %r9
movq %r10, (%r9)
movq 0x4(%rdx), %r11
rolq $0x20, %r11
btrq $0x3f, %r11
movq %r11, 0x8(%r9)
movl 0xc(%rdx), %r10d
shlq $0x30, %r10
orq %r11, %r10
movq %r10, 0x8(%r9)
movl 0xc(%rdx), %r11d
testl %r11d, %r11d
jns 0x15ae64
btsq $0x2a, %r10
movq %r10, 0x8(%r9)
movl 0xc(%rdx), %r11d
shrl $0x10, %r11d
shlq $0x38, %r11
orq %r10, %r11
movq %r11, 0x8(%r9)
addq $0x10, %r8
jmp 0x15adb5
incq %rax
jmp 0x15ad8f
movq 0x28(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x1a38cf
movq (%r13), %rax
shlq $0x4, %rax
movq %r12, %rsi
addq %rax, %rsi
movq %r12, %rdi
callq 0x158af1
movq %r12, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq