// File: /workspace/github_c_makefile_star2_O3/rikuu[P]kermit/index.c
// Function: km_build_idx
km_idx_t *km_build_idx(const char *fn, sdict_t *d, const uint32_t length, const uint32_t max_overhang) {
	km_idx_t *idx = (km_idx_t *) calloc(1, sizeof(km_idx_t));
	idx->length = length;
	idx->d = sd_init();

	paf_file_t *fp = paf_open(fn);
	if (fp == 0) {
		fprintf(stderr, "ERROR: Failed to open \"%s\"\n", fn);
		exit(1);
	}

	// Find longest matches for reads
	kvec_t(km_hit2_t) v = {0,0,0};
	uint32_t tot = 0;
	paf_rec_t r;
	while (paf_read(fp, &r) >= 0) {
		tot++;
		uint32_t tl = r.te - r.ts;
		int32_t id = sd_get(d, r.qn);
		if (id >= 0 && d->seq[id].len > tl) continue;
		km_hit2_t *h;
		kv_pushp(km_hit2_t, v, &h);
		h->qn = sd_put(d, r.qn, tl);
		h->tn = sd_put(idx->d, r.tn, r.tl);
		h->tl = d->seq[h->qn].len = tl;
		// extend mapping
		h->s = r.ts > min(r.qs, max_overhang) ? (r.ts - min(r.qs, max_overhang)) : 0;
		h->e = min(r.tl, r.te + min(r.ql - r.qe, max_overhang));
	}
	paf_close(fp);

	// Allocate bins
	idx->targets = (km_target_t*) calloc(idx->d->n_seq, sizeof(km_target_t));
	for (uint32_t i = 0; i < idx->d->n_seq; i++) {
		if (idx->d->seq[i].len == 0) continue;
		uint32_t n_bins = (idx->d->seq[i].len / length) + 2;
		idx->targets[i].n_bins = n_bins;
		idx->targets[i].bins = (km_hit_v*) calloc(n_bins, sizeof(km_hit_v));
	}

	// Add hits to bins
	uint32_t stored = 0;
	for (size_t i = 0; i < v.n; i++) {
		km_hit2_t h2 = v.a[i];
		if (d->seq[h2.qn].len != h2.tl) {
			d->seq[h2.qn].del = 1;
			continue;
		}

		stored++;
		km_hit_v *bins = idx->targets[h2.tn].bins;
		size_t sid = (size_t) floorf(h2.s / (float) length), eid = (size_t) ceilf(h2.e / (float) length);

		km_hit_t *h;
		kv_pushp(km_hit_t, bins[sid], &h);
		h->qn = h2.qn;
		h->s = h2.s;
		h->e = h2.e;

		for (size_t i = sid+1; i < eid+1; i++) {
			kv_push(km_hit_t, bins[i], *h);
		}
	}
	// TODO: Squeeze dict; affects qns?
	// sd_squeeze(d);
	free(v.a);
	fprintf(stderr, "[M::%s] read %u hits; stored %u hits\n", __func__, tot, stored);
	return idx;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %ecx, %ebx
movl %edx, %r12d
movq %rsi, %rbp
movq %rdi, %r15
movl $0x1, %edi
movl $0x18, %esi
callq 0x10f0
movq %rax, %r14
movl %r12d, (%rax)
callq 0x2ff0
movq %rax, %r13
movq %r14, 0x30(%rsp)
movq %rax, 0x8(%r14)
movq %r15, %rdi
callq 0x3850
testq %rax, %rax
je 0x8c6a
movq %rax, %r14
movq %r13, 0x8(%rsp)
movl %r12d, 0x4(%rsp)
leaq 0x48(%rsp), %rsi
movq %rax, %rdi
callq 0x3a85
testl %eax, %eax
js 0x897a
xorl %r15d, %r15d
movq $0x0, 0x10(%rsp)
xorl %r12d, %r12d
movq $0x0, 0x20(%rsp)
movq %r14, 0x18(%rsp)
movl 0x6c(%rsp), %r13d
subl 0x68(%rsp), %r13d
movq 0x48(%rsp), %rsi
movq %rbp, %rdi
callq 0x336b
testl %eax, %eax
js 0x888c
movq 0x8(%rbp), %rcx
movl %eax, %eax
shlq $0x4, %rax
cmpl %r13d, 0x8(%rcx,%rax)
ja 0x8957
cmpq 0x10(%rsp), %r15
jne 0x88bf
leaq (%r15,%r15), %rcx
testq %r15, %r15
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%rsp)
leaq (,%rcx,4), %rax
leaq (%rax,%rax,4), %rsi
movq %r12, %rdi
callq 0x1180
movq %rax, %r12
movq %r15, %r14
leaq (%r15,%r15,4), %r15
incq %r14
movq 0x48(%rsp), %rsi
movq %rbp, %rdi
movl %r13d, %edx
callq 0x3091
movl %eax, (%r12,%r15,4)
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0x50(%rsp), %rsi
movl 0x64(%rsp), %edx
movq %rdi, 0x8(%rsp)
callq 0x3091
movl %eax, 0x4(%r12,%r15,4)
movq 0x8(%rbp), %rax
movslq (%r12,%r15,4), %rcx
shlq $0x4, %rcx
movl %r13d, 0x8(%rax,%rcx)
movl %r13d, 0x10(%r12,%r15,4)
movl 0x5c(%rsp), %eax
movl 0x68(%rsp), %ecx
cmpl %ebx, %eax
cmovael %ebx, %eax
subl %eax, %ecx
movl $0x0, %eax
cmovbl %eax, %ecx
movl %ecx, 0x8(%r12,%r15,4)
movl 0x58(%rsp), %eax
movl 0x64(%rsp), %ecx
subl 0x60(%rsp), %eax
cmpl %ebx, %eax
cmovael %ebx, %eax
addl 0x6c(%rsp), %eax
cmpl %eax, %ecx
cmovbl %ecx, %eax
movl %eax, 0xc(%r12,%r15,4)
movq %r14, %r15
movq 0x18(%rsp), %r14
movq %r14, %rdi
leaq 0x48(%rsp), %rsi
callq 0x3a85
movq 0x20(%rsp), %rcx
incl %ecx
movq %rcx, 0x20(%rsp)
testl %eax, %eax
jns 0x885c
jmp 0x8989
movq $0x0, 0x20(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %r15, 0x28(%rsp)
movq %r14, %rdi
callq 0x38e0
movq 0x8(%rsp), %r14
movl (%r14), %ebx
movl $0x10, %esi
movq %rbx, %rdi
callq 0x10f0
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x10(%rsp)
movq %rcx, 0x10(%rax)
testq %rbx, %rbx
je 0x8a00
movq 0x8(%r14), %r14
shlq $0x4, %rbx
xorl %r15d, %r15d
movq 0x10(%rsp), %r13
movl 0x8(%r14,%r15), %eax
testl %eax, %eax
je 0x89f7
xorl %edx, %edx
divl 0x4(%rsp)
leal 0x2(%rax), %edi
movl %edi, (%r13,%r15)
movl $0x18, %esi
callq 0x10f0
movq %rax, 0x8(%r13,%r15)
addq $0x10, %r15
cmpq %r15, %rbx
jne 0x89d1
movq $0x0, 0x8(%rsp)
movq 0x28(%rsp), %r15
testq %r15, %r15
je 0x8c22
movl 0x4(%rsp), %eax
cvtsi2ss %rax, %xmm0
movss %xmm0, 0x18(%rsp)
movq $0x0, 0x8(%rsp)
xorl %r13d, %r13d
movq %rbp, 0x40(%rsp)
leaq (,%r13,4), %rcx
addq %r13, %rcx
movslq (%r12,%rcx,4), %r14
movq 0x8(%rbp), %rax
movq %r14, %rdx
shlq $0x4, %rdx
movl 0x8(%rax,%rdx), %esi
cmpl 0x10(%r12,%rcx,4), %esi
jne 0x8b27
leaq (%r12,%rcx,4), %rax
movl 0x8(%rax), %edx
movl 0xc(%rax), %ebp
movslq 0x4(%rax), %rax
shlq $0x4, %rax
movq 0x10(%rsp), %rcx
movq 0x8(%rcx,%rax), %r15
movl %edx, 0x4(%rsp)
movl %edx, %eax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x18(%rsp), %xmm0
callq 0x11d0
cvttss2si %xmm0, %rax
movq %rax, %rcx
subss 0x1e17(%rip), %xmm0     # 0xa8bc
cvttss2si %xmm0, %rbx
sarq $0x3f, %rcx
andq %rcx, %rbx
orq %rax, %rbx
movl %ebp, 0x3c(%rsp)
movl %ebp, %eax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x18(%rsp), %xmm0
callq 0x10e0
leaq (%rbx,%rbx,2), %rdi
movq %r15, 0x78(%rsp)
leaq (%r15,%rdi,8), %r15
movq (%r15), %rcx
cmpq 0x8(%r15), %rcx
jne 0x8b33
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rax
movq %rax, 0x8(%r15)
movq %rdi, %rbp
movq 0x10(%r15), %rdi
shlq $0x2, %rax
leaq (%rax,%rax,2), %rsi
movaps %xmm0, 0x80(%rsp)
callq 0x1180
movaps 0x80(%rsp), %xmm0
movq %rbp, %rdi
movq %rax, 0x10(%r15)
movq (%r15), %rcx
jmp 0x8b37
addq %rdx, %rax
orb $-0x80, 0xf(%rax)
jmp 0x8c16
movq 0x10(%r15), %rax
movq 0x8(%rsp), %rdx
incl %edx
movq %rdx, 0x8(%rsp)
cvttss2si %xmm0, %rdx
movq %rdx, %rsi
subss 0x1d69(%rip), %xmm0     # 0xa8bc
cvttss2si %xmm0, %rbp
sarq $0x3f, %rsi
andq %rsi, %rbp
orq %rdx, %rbp
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
leaq (%rcx,%rcx,2), %rcx
movl %r14d, (%rax,%rcx,4)
movl 0x4(%rsp), %edx
movl %edx, 0x4(%rax,%rcx,4)
movl 0x3c(%rsp), %edx
movl %edx, 0x8(%rax,%rcx,4)
leaq 0x1(%rbp), %rdx
leaq 0x1(%rbx), %rsi
cmpq %rdx, %rsi
jae 0x8c0c
leaq (%rax,%rcx,4), %r14
subq %rbx, %rbp
movq 0x78(%rsp), %rax
leaq (%rax,%rdi,8), %rbx
addq $0x28, %rbx
movq 0x28(%rsp), %r15
movq -0x10(%rbx), %rcx
cmpq -0x8(%rbx), %rcx
jne 0x8bde
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rax
movq %rax, -0x8(%rbx)
movq (%rbx), %rdi
shlq $0x2, %rax
leaq (%rax,%rax,2), %rsi
callq 0x1180
movq %rax, (%rbx)
movq -0x10(%rbx), %rcx
jmp 0x8be1
movq (%rbx), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, -0x10(%rbx)
leaq (%rcx,%rcx,2), %rcx
movl 0x8(%r14), %edx
movl %edx, 0x8(%rax,%rcx,4)
movq (%r14), %rdx
movq %rdx, (%rax,%rcx,4)
addq $0x18, %rbx
decq %rbp
jne 0x8ba7
movq 0x40(%rsp), %rbp
jmp 0x8c16
movq 0x40(%rsp), %rbp
movq 0x28(%rsp), %r15
incq %r13
cmpq %r15, %r13
jne 0x8a37
movq %r12, %rdi
callq 0x1040
movq 0x53c7(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x1ca1(%rip), %rsi      # 0xa8dc
leaq 0x1cc0(%rip), %rdx      # 0xa902
movq 0x20(%rsp), %rcx
movq 0x8(%rsp), %r8
xorl %eax, %eax
callq 0x1120
movq 0x30(%rsp), %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x2784
nop