// File: /workspace/github_c_makefile_star2_O1/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, 0x20(%rsp)
movl %edx, %ebp
movq %rsi, %r13
movq %rdi, %r15
movl $0x1, %edi
movl $0x18, %esi
callq 0x10f0
movq %rax, %r14
movl %ebp, 0xc(%rsp)
movl %ebp, (%rax)
callq 0x30d0
movq %r14, 0x28(%rsp)
movq %rax, 0x8(%r14)
movq %r15, %rdi
callq 0x394c
testq %rax, %rax
je 0x8c63
movq %rax, %rbx
leaq 0x40(%rsp), %rsi
movq %rax, %rdi
callq 0x3b99
xorl %r14d, %r14d
movl $0x0, %r12d
movl $0x0, %ecx
movq %rcx, 0x18(%rsp)
testl %eax, %eax
js 0x8996
movq $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
xorl %r12d, %r12d
xorl %r14d, %r14d
movl 0x64(%rsp), %ebp
subl 0x60(%rsp), %ebp
movq 0x40(%rsp), %rsi
movq %r13, %rdi
callq 0x3428
testl %eax, %eax
js 0x88b2
movq 0x8(%r13), %rcx
movl %eax, %eax
shlq $0x4, %rax
cmpl %ebp, 0x8(%rcx,%rax)
ja 0x897e
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
jne 0x88ea
leaq (%rax,%rax), %r15
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r15
leaq (,%r15,4), %rax
leaq (%rax,%rax,4), %rsi
movq %r12, %rdi
callq 0x1180
movq %rax, %r12
movq %r15, 0x10(%rsp)
movq 0x18(%rsp), %rax
leaq (%rax,%rax,4), %r15
incq %rax
movq %rax, 0x18(%rsp)
movq 0x40(%rsp), %rsi
movq %r13, %rdi
movl %ebp, %edx
callq 0x3171
movl %eax, (%r12,%r15,4)
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0x48(%rsp), %rsi
movl 0x5c(%rsp), %edx
callq 0x3171
movl %eax, 0x4(%r12,%r15,4)
movq 0x8(%r13), %rax
movslq (%r12,%r15,4), %rcx
shlq $0x4, %rcx
movl %ebp, 0x8(%rax,%rcx)
movl %ebp, 0x10(%r12,%r15,4)
movl 0x54(%rsp), %eax
movl 0x60(%rsp), %ecx
movl 0x20(%rsp), %edx
cmpl %edx, %eax
cmovael %edx, %eax
subl %eax, %ecx
movl $0x0, %eax
cmovbl %eax, %ecx
movl %ecx, 0x8(%r12,%r15,4)
movl 0x50(%rsp), %eax
movl 0x5c(%rsp), %ecx
subl 0x58(%rsp), %eax
cmpl %edx, %eax
cmovael %edx, %eax
addl 0x64(%rsp), %eax
cmpl %eax, %ecx
cmovbl %ecx, %eax
movl %eax, 0xc(%r12,%r15,4)
movq %rbx, %rdi
leaq 0x40(%rsp), %rsi
callq 0x3b99
incl %r14d
testl %eax, %eax
jns 0x8885
movq %r14, 0x70(%rsp)
movq %rbx, %rdi
callq 0x39ef
movq 0x28(%rsp), %r15
movq 0x8(%r15), %r14
movl (%r14), %ebx
movl $0x10, %esi
movq %rbx, %rdi
callq 0x10f0
movq %rax, 0x20(%rsp)
movq %rax, 0x10(%r15)
testq %rbx, %rbx
je 0x8a09
movq 0x8(%r14), %r14
shlq $0x4, %rbx
xorl %r15d, %r15d
movq 0x20(%rsp), %rbp
movl 0x8(%r14,%r15), %eax
testl %eax, %eax
je 0x8a00
xorl %edx, %edx
divl 0xc(%rsp)
leal 0x2(%rax), %edi
movl %edi, (%rbp,%r15)
movl $0x18, %esi
callq 0x10f0
movq %rax, 0x8(%rbp,%r15)
addq $0x10, %r15
cmpq %r15, %rbx
jne 0x89da
xorl %ebx, %ebx
cmpq $0x0, 0x18(%rsp)
je 0x8c1d
movl 0xc(%rsp), %eax
cvtsi2ss %rax, %xmm0
movss %xmm0, 0xc(%rsp)
xorl %ebx, %ebx
xorl %ebp, %ebp
movq %r13, 0x38(%rsp)
leaq (,%rbp,4), %rcx
addq %rbp, %rcx
movslq (%r12,%rcx,4), %r14
movq 0x8(%r13), %rax
movq %r14, %rdx
shlq $0x4, %rdx
movl 0x8(%rax,%rdx), %esi
cmpl 0x10(%r12,%rcx,4), %esi
jne 0x8bfc
movq %rbx, 0x10(%rsp)
leaq (%r12,%rcx,4), %rax
movl 0x8(%rax), %edx
movl 0xc(%rax), %ebx
movslq 0x4(%rax), %rax
shlq $0x4, %rax
movq 0x20(%rsp), %rcx
movq 0x8(%rcx,%rax), %r13
movl %edx, 0x34(%rsp)
movl %edx, %eax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0xc(%rsp), %xmm0
callq 0x11d0
cvttss2si %xmm0, %rax
movq %rax, %rcx
subss 0x1e4a(%rip), %xmm0     # 0xa8ec
cvttss2si %xmm0, %r15
sarq $0x3f, %rcx
andq %rcx, %r15
orq %rax, %r15
movl %ebx, 0x30(%rsp)
movl %ebx, %eax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0xc(%rsp), %xmm0
callq 0x10e0
leaq (%r15,%r15,2), %rdi
movq %r13, 0x78(%rsp)
leaq (%r13,%rdi,8), %r13
movq 0x8(%r13), %rax
cmpq %rax, (%r13)
jne 0x8b21
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%r13)
movq %rdi, %rbx
movq 0x10(%r13), %rdi
shlq $0x2, %rcx
leaq (%rcx,%rcx,2), %rsi
movaps %xmm0, 0x80(%rsp)
callq 0x1180
movaps 0x80(%rsp), %xmm0
movq %rbx, %rdi
movq %rax, 0x10(%r13)
movq 0x10(%rsp), %rax
incl %eax
movq %rax, 0x10(%rsp)
cvttss2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subss 0x1dab(%rip), %xmm0     # 0xa8ec
cvttss2si %xmm0, %rbx
andq %rcx, %rbx
orq %rax, %rbx
movq (%r13), %rcx
movq 0x10(%r13), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%r13)
leaq (%rcx,%rcx,2), %rcx
movl %r14d, (%rax,%rcx,4)
movl 0x34(%rsp), %edx
movl %edx, 0x4(%rax,%rcx,4)
movl 0x30(%rsp), %edx
movl %edx, 0x8(%rax,%rcx,4)
leaq 0x1(%rbx), %rdx
leaq 0x1(%r15), %rsi
cmpq %rdx, %rsi
jae 0x8c05
leaq (%rax,%rcx,4), %r14
subq %r15, %rbx
movq 0x78(%rsp), %rax
leaq (%rax,%rdi,8), %r15
addq $0x28, %r15
movq 0x38(%rsp), %r13
movq -0x8(%r15), %rax
cmpq %rax, -0x10(%r15)
jne 0x8bcf
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, -0x8(%r15)
movq (%r15), %rdi
shlq $0x2, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x1180
movq %rax, (%r15)
movq -0x10(%r15), %rax
movq (%r15), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, -0x10(%r15)
leaq (%rax,%rax,2), %rax
movl 0x8(%r14), %edx
movl %edx, 0x8(%rcx,%rax,4)
movq (%r14), %rdx
movq %rdx, (%rcx,%rax,4)
addq $0x18, %r15
decq %rbx
jne 0x8b9e
jmp 0x8c0a
addq %rdx, %rax
orb $-0x80, 0xf(%rax)
jmp 0x8c0f
movq 0x38(%rsp), %r13
movq 0x10(%rsp), %rbx
incq %rbp
cmpq 0x18(%rsp), %rbp
jne 0x8a2f
movq %r12, %rdi
callq 0x1040
movq 0x63cc(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x1cd6(%rip), %rsi      # 0xa90c
leaq 0x1cf5(%rip), %rdx      # 0xa932
movq 0x70(%rsp), %rcx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x1120
movq 0x28(%rsp), %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x2822
nop