// File: /workspace/github_c_makefile_star2_O2/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 $0xa8, %rsp
movl %ecx, 0xc(%rsp)
movl %edx, %ebp
movq %rsi, %r12
movq %rdi, %r15
pushq $0x1
popq %rdi
pushq $0x18
popq %rsi
callq 0x10f0
movq %rax, %rbx
movl %ebp, 0x30(%rsp)
movl %ebp, (%rax)
callq 0x145c
movq %rbx, 0x40(%rsp)
movq %rax, 0x10(%rsp)
movq %rax, 0x8(%rbx)
movq %r15, %rdi
callq 0x1c10
testq %rax, %rax
je 0x7845
movq %rax, %r13
leaq 0x50(%rsp), %rbx
xorl %eax, %eax
movq $0x0, 0x18(%rsp)
xorl %r14d, %r14d
movq $0x0, 0x38(%rsp)
movl %eax, %r15d
movq %r13, %rdi
movq %rbx, %rsi
callq 0x1e42
testl %eax, %eax
js 0x7590
movl 0x74(%rsp), %ebp
subl 0x70(%rsp), %ebp
movq 0x50(%rsp), %rsi
movq %r12, %rdi
callq 0x17a3
testl %eax, %eax
js 0x74a5
movq 0x8(%r12), %rcx
movl %eax, %edx
shlq $0x4, %rdx
leal 0x1(%r15), %eax
cmpl %ebp, 0x8(%rcx,%rdx)
ja 0x7461
cmpq %r14, 0x38(%rsp)
jne 0x74d2
leaq (%r14,%r14), %rbx
testq %r14, %r14
pushq $0x2
popq %rax
cmoveq %rax, %rbx
imulq $0x14, %rbx, %rsi
movq 0x18(%rsp), %rdi
callq 0x1180
movq %rax, %r14
movq %rbx, 0x20(%rsp)
jmp 0x74dc
movq %r14, 0x20(%rsp)
movq 0x18(%rsp), %r14
incl %r15d
movq 0x38(%rsp), %rax
imulq $0x14, %rax, %rbx
incq %rax
movq %rax, 0x38(%rsp)
movq 0x50(%rsp), %rsi
movq %r12, %rdi
movl %ebp, %edx
callq 0x14fd
movl %eax, (%r14,%rbx)
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0x58(%rsp), %rsi
movl 0x6c(%rsp), %edx
movq %rdi, 0x10(%rsp)
callq 0x14fd
movl %eax, 0x4(%r14,%rbx)
movq 0x8(%r12), %rax
movslq (%r14,%rbx), %rcx
shlq $0x4, %rcx
movl %ebp, 0x8(%rax,%rcx)
movl %ebp, 0x10(%r14,%rbx)
movl 0x64(%rsp), %eax
movl 0xc(%rsp), %edx
cmpl %edx, %eax
cmovael %edx, %eax
movl 0x70(%rsp), %ecx
subl %eax, %ecx
movl $0x0, %eax
cmovbl %eax, %ecx
movl %ecx, 0x8(%r14,%rbx)
movl 0x60(%rsp), %eax
subl 0x68(%rsp), %eax
cmpl %edx, %eax
cmovael %edx, %eax
movl 0x6c(%rsp), %ecx
addl 0x74(%rsp), %eax
cmpl %eax, %ecx
cmovbl %ecx, %eax
movq %r14, 0x18(%rsp)
movl %eax, 0xc(%r14,%rbx)
movl %r15d, %eax
leaq 0x50(%rsp), %rbx
movq 0x20(%rsp), %r14
jmp 0x7461
movq %r13, %rdi
callq 0x1ca9
movq 0x10(%rsp), %rax
movl (%rax), %ebp
pushq $0x10
popq %rsi
movq %rbp, %rdi
callq 0x10f0
movq %rax, %rcx
movq 0x40(%rsp), %rax
movq %rcx, 0x20(%rsp)
movq %rcx, 0x10(%rax)
shlq $0x4, %rbp
xorl %ebx, %ebx
movq 0x18(%rsp), %r13
movq 0x20(%rsp), %r14
cmpq %rbx, %rbp
je 0x7601
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movl 0x8(%rax,%rbx), %eax
testl %eax, %eax
je 0x75fb
xorl %edx, %edx
divl 0x30(%rsp)
leal 0x2(%rax), %edi
movl %edi, (%r14,%rbx)
pushq $0x18
popq %rsi
callq 0x10f0
movq %rax, 0x8(%r14,%rbx)
addq $0x10, %rbx
jmp 0x75cb
movl 0x30(%rsp), %eax
cvtsi2ss %rax, %xmm0
movss %xmm0, 0x10(%rsp)
xorl %edi, %edi
movl $0x0, 0x2c(%rsp)
movq %r15, 0x80(%rsp)
movq %r12, 0x88(%rsp)
cmpq 0x38(%rsp), %rdi
je 0x77ff
imulq $0x14, %rdi, %rax
movslq (%r13,%rax), %r14
movq 0x8(%r12), %rcx
movq %r14, %rdx
shlq $0x4, %rdx
movl 0x8(%rcx,%rdx), %esi
cmpl 0x10(%r13,%rax), %esi
jne 0x7718
movq %rdi, 0x30(%rsp)
leaq (%rax,%r13), %rcx
movl 0x8(%r13,%rax), %edx
movl 0xc(%r13,%rax), %ebp
movslq 0x4(%rcx), %rax
shlq $0x4, %rax
movq 0x20(%rsp), %rcx
movq 0x8(%rcx,%rax), %rbx
movl %edx, 0xc(%rsp)
movl %edx, %eax
cvtsi2ss %rax, %xmm0
divss 0x10(%rsp), %xmm0
callq 0x11d0
cvttss2si %xmm0, %rax
movq %rax, %rcx
subss 0x2218(%rip), %xmm0     # 0x98bc
cvttss2si %xmm0, %r13
sarq $0x3f, %rcx
andq %rcx, %r13
orq %rax, %r13
movl %ebp, 0x4c(%rsp)
movl %ebp, %eax
cvtsi2ss %rax, %xmm0
divss 0x10(%rsp), %xmm0
callq 0x10e0
imulq $0x18, %r13, %rbp
leaq (%rbx,%rbp), %r12
movq (%rbx,%rbp), %rcx
cmpq 0x8(%rbx,%rbp), %rcx
jne 0x7724
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rax
movq %rax, 0x8(%r12)
movq 0x10(%r12), %rdi
imulq $0xc, %rax, %rsi
movaps %xmm0, 0x90(%rsp)
callq 0x1180
movaps 0x90(%rsp), %xmm0
movq %rax, 0x10(%r12)
movq (%r12), %rcx
jmp 0x7729
addq %rdx, %rcx
orb $-0x80, 0xf(%rcx)
jmp 0x77f7
movq 0x10(%r12), %rax
incl 0x2c(%rsp)
cvttss2si %xmm0, %rdx
movq %rdx, %rsi
sarq $0x3f, %rsi
subss 0x217b(%rip), %xmm0     # 0x98bc
cvttss2si %xmm0, %r15
andq %rsi, %r15
orq %rdx, %r15
leaq 0x1(%rcx), %rdx
movq %rdx, (%r12)
imulq $0xc, %rcx, %rcx
leaq (%rax,%rcx), %r12
movl %r14d, (%rax,%rcx)
movl 0xc(%rsp), %edx
movl %edx, 0x4(%rax,%rcx)
movl 0x4c(%rsp), %edx
movl %edx, 0x8(%rax,%rcx)
incq %r15
incq %r13
addq %rbp, %rbx
addq $0x28, %rbx
pushq $0x2
popq %r14
cmpq %r15, %r13
jae 0x77dd
movq -0x10(%rbx), %rcx
cmpq -0x8(%rbx), %rcx
jne 0x77b4
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
cmoveq %r14, %rax
movq %rax, -0x8(%rbx)
movq (%rbx), %rdi
imulq $0xc, %rax, %rsi
callq 0x1180
movq %rax, (%rbx)
movq -0x10(%rbx), %rcx
jmp 0x77b7
movq (%rbx), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, -0x10(%rbx)
imulq $0xc, %rcx, %rcx
movl 0x8(%r12), %edx
movl %edx, 0x8(%rax,%rcx)
movq (%r12), %rdx
movq %rdx, (%rax,%rcx)
incq %r13
addq $0x18, %rbx
jmp 0x7781
movq 0x88(%rsp), %r12
movq 0x80(%rsp), %r15
movq 0x18(%rsp), %r13
movq 0x30(%rsp), %rdi
incq %rdi
jmp 0x762a
movq %r13, %rdi
callq 0x1040
movq 0x67ea(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x20c4(%rip), %rsi      # 0x98dc
leaq 0x20e3(%rip), %rdx      # 0x9902
movl %r15d, %ecx
movl 0x2c(%rsp), %r8d
xorl %eax, %eax
callq 0x1120
movq 0x40(%rsp), %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x67ac(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x206a(%rip), %rsi      # 0x98c0
movq %r15, %rdx
xorl %eax, %eax
callq 0x1120
pushq $0x1
popq %rdi
callq 0x1200