// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl $0x1, %edi
movl $0x18, %esi
callq 0x10e0
movq %rax, -0x20(%rbp)
movl -0x14(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
callq 0x1490
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
callq 0x22e0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xc9df
movq 0x6638(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
movq -0x8(%rbp), %rdx
leaq 0x2e5e(%rip), %rsi      # 0xf82c
movb $0x0, %al
callq 0x1110
movl $0x1, %edi
callq 0x11f0
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x10c0
movl $0x0, -0x44(%rbp)
movq -0x28(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x2720
cmpl $0x0, %eax
jl 0xcbc8
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
movl -0x54(%rbp), %eax
subl -0x58(%rbp), %eax
movl %eax, -0x7c(%rbp)
movq -0x10(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x1a60
movl %eax, -0x80(%rbp)
cmpl $0x0, -0x80(%rbp)
jl 0xca51
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x80(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0x7c(%rbp), %eax
jbe 0xca51
jmp 0xc9f6
jmp 0xca53
movq -0x40(%rbp), %rax
cmpq -0x38(%rbp), %rax
jne 0xca9f
cmpq $0x0, -0x38(%rbp)
je 0xca74
movq -0x38(%rbp), %rax
shlq %rax
movq %rax, -0xe8(%rbp)
jmp 0xca82
movl $0x2, %eax
movq %rax, -0xe8(%rbp)
jmp 0xca82
movq -0xe8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
imulq $0x14, -0x38(%rbp), %rsi
callq 0x1170
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x40(%rbp)
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rdi
movq -0x78(%rbp), %rsi
movl -0x7c(%rbp), %edx
callq 0x15c0
movl %eax, %ecx
movq -0x88(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x70(%rbp), %rsi
movl -0x5c(%rbp), %edx
callq 0x15c0
movl %eax, %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x7c(%rbp), %ecx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x88(%rbp), %rdx
movslq (%rdx), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movq -0x88(%rbp), %rax
movl %ecx, 0x10(%rax)
movl -0x58(%rbp), %eax
movl %eax, -0xec(%rbp)
movl -0x64(%rbp), %edi
movl -0x18(%rbp), %esi
callq 0xd150
movl %eax, %ecx
movl -0xec(%rbp), %eax
cmpl %ecx, %eax
jbe 0xcb6a
movl -0x58(%rbp), %eax
movl %eax, -0xf4(%rbp)
movl -0x64(%rbp), %edi
movl -0x18(%rbp), %esi
callq 0xd150
movl %eax, %ecx
movl -0xf4(%rbp), %eax
subl %ecx, %eax
movl %eax, -0xf0(%rbp)
jmp 0xcb74
xorl %eax, %eax
movl %eax, -0xf0(%rbp)
jmp 0xcb74
movl -0xf0(%rbp), %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x5c(%rbp), %eax
movl %eax, -0xf8(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0xfc(%rbp)
movl -0x68(%rbp), %edi
subl -0x60(%rbp), %edi
movl -0x18(%rbp), %esi
callq 0xd150
movl -0xfc(%rbp), %esi
movl -0xf8(%rbp), %edi
addl %eax, %esi
callq 0xd150
movl %eax, %ecx
movq -0x88(%rbp), %rax
movl %ecx, 0xc(%rax)
jmp 0xc9f6
movq -0x28(%rbp), %rdi
callq 0x2400
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movl (%rax), %eax
movl %eax, %edi
movl $0x10, %esi
callq 0x10e0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movq -0x20(%rbp), %rcx
movq 0x8(%rcx), %rcx
cmpl (%rcx), %eax
jae 0xccbc
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movl -0x8c(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
cmpl $0x0, 0x8(%rax)
jne 0xcc33
jmp 0xcca8
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
movl -0x8c(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
xorl %edx, %edx
divl -0x14(%rbp)
addl $0x2, %eax
movl %eax, -0x90(%rbp)
movl -0x90(%rbp), %ecx
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x8c(%rbp), %edx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movl -0x90(%rbp), %eax
movl %eax, %edi
movl $0x18, %esi
callq 0x10e0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x8c(%rbp), %edx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0xcbfc
movl $0x0, -0x94(%rbp)
movq $0x0, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
cmpq -0x40(%rbp), %rax
jae 0xd10a
movq -0x30(%rbp), %rax
imulq $0x14, -0xa0(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0xb4(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0xac(%rbp)
movl 0x10(%rax), %eax
movl %eax, -0xa4(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0xb4(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpl -0xa4(%rbp), %eax
je 0xcd5d
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0xb4(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
jmp 0xd0f3
movl -0x94(%rbp), %eax
incl %eax
movl %eax, -0x94(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0xb0(%rbp), %rcx
shlq $0x4, %rcx
movq 0x8(%rax,%rcx), %rax
movq %rax, -0xc0(%rbp)
movl -0xac(%rbp), %eax
cvtsi2ss %rax, %xmm0
movl -0x14(%rbp), %eax
cvtsi2ss %rax, %xmm1
divss %xmm1, %xmm0
callq 0x11c0
movaps %xmm0, %xmm1
cvttss2si %xmm1, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
movss 0x2a6b(%rip), %xmm1     # 0xf828
movss %xmm1, -0x100(%rbp)
subss %xmm1, %xmm0
cvttss2si %xmm0, %rcx
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0xc8(%rbp)
movl -0xa8(%rbp), %eax
cvtsi2ss %rax, %xmm0
movl -0x14(%rbp), %eax
cvtsi2ss %rax, %xmm1
divss %xmm1, %xmm0
callq 0x10d0
movss -0x100(%rbp), %xmm1
movaps %xmm0, %xmm2
cvttss2si %xmm2, %rax
movq %rax, %rdx
sarq $0x3f, %rdx
subss %xmm1, %xmm0
cvttss2si %xmm0, %rcx
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, -0xd0(%rbp)
movq -0xc0(%rbp), %rax
imulq $0x18, -0xc8(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0xc0(%rbp), %rcx
imulq $0x18, -0xc8(%rbp), %rdx
addq %rdx, %rcx
cmpq 0x8(%rcx), %rax
jne 0xcf06
movq -0xc0(%rbp), %rax
imulq $0x18, -0xc8(%rbp), %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0xce90
movq -0xc0(%rbp), %rax
imulq $0x18, -0xc8(%rbp), %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x108(%rbp)
jmp 0xce9e
movl $0x2, %eax
movq %rax, -0x108(%rbp)
jmp 0xce9e
movq -0x108(%rbp), %rcx
movq -0xc0(%rbp), %rax
imulq $0x18, -0xc8(%rbp), %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0xc0(%rbp), %rax
imulq $0x18, -0xc8(%rbp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
movq -0xc0(%rbp), %rax
imulq $0x18, -0xc8(%rbp), %rcx
addq %rcx, %rax
imulq $0xc, 0x8(%rax), %rsi
callq 0x1170
movq %rax, %rcx
movq -0xc0(%rbp), %rax
imulq $0x18, -0xc8(%rbp), %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movq -0xc0(%rbp), %rax
imulq $0x18, -0xc8(%rbp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq -0xc0(%rbp), %rdx
imulq $0x18, -0xc8(%rbp), %rcx
addq %rcx, %rdx
movq (%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, (%rdx)
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xd8(%rbp)
movl -0xb4(%rbp), %ecx
movq -0xd8(%rbp), %rax
movl %ecx, (%rax)
movl -0xac(%rbp), %ecx
movq -0xd8(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0xa8(%rbp), %ecx
movq -0xd8(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0xc8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movq -0xd0(%rbp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jae 0xd0f1
jmp 0xcfa7
movq -0xc0(%rbp), %rax
imulq $0x18, -0xe0(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0xc0(%rbp), %rcx
imulq $0x18, -0xe0(%rbp), %rdx
addq %rdx, %rcx
cmpq 0x8(%rcx), %rax
jne 0xd089
movq -0xc0(%rbp), %rax
imulq $0x18, -0xe0(%rbp), %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0xd013
movq -0xc0(%rbp), %rax
imulq $0x18, -0xe0(%rbp), %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x110(%rbp)
jmp 0xd021
movl $0x2, %eax
movq %rax, -0x110(%rbp)
jmp 0xd021
movq -0x110(%rbp), %rcx
movq -0xc0(%rbp), %rax
imulq $0x18, -0xe0(%rbp), %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0xc0(%rbp), %rax
imulq $0x18, -0xe0(%rbp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
movq -0xc0(%rbp), %rax
imulq $0x18, -0xe0(%rbp), %rcx
addq %rcx, %rax
imulq $0xc, 0x8(%rax), %rsi
callq 0x1170
movq %rax, %rcx
movq -0xc0(%rbp), %rax
imulq $0x18, -0xe0(%rbp), %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movq -0xc0(%rbp), %rax
imulq $0x18, -0xe0(%rbp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq -0xc0(%rbp), %rdx
imulq $0x18, -0xe0(%rbp), %rcx
addq %rcx, %rdx
movq (%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, (%rdx)
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq -0xd8(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movl 0x8(%rcx), %ecx
movl %ecx, 0x8(%rax)
jmp 0xd0da
movq -0xe0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe0(%rbp)
jmp 0xcf8a
jmp 0xd0f3
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0xccd1
movq -0x30(%rbp), %rdi
callq 0x1040
movq 0x5ede(%rip), %rax      # 0x12ff8
movq (%rax), %rdi
movl -0x44(%rbp), %ecx
movl -0x94(%rbp), %r8d
leaq 0x271a(%rip), %rsi      # 0xf848
leaq 0x2739(%rip), %rdx      # 0xf86e
movb $0x0, %al
callq 0x1110
movq -0x20(%rbp), %rax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopl (%rax)