// File: /workspace/github_c_makefile_star2_O0/lh3[P]miniprot/chain.c
// Function: compact_a
static uint64_t *compact_a(void *km, int32_t n_u, uint64_t *u, int32_t n_v, int32_t *v, uint64_t *a)
{
	mp128_t *w;
	uint64_t *b, *u2;
	int64_t i, j, k;

	// write the result to b[]
	KMALLOC(km, b, n_v);
	for (i = 0, k = 0; i < n_u; ++i) {
		int32_t k0 = k, ni = (int32_t)u[i];
		for (j = 0; j < ni; ++j)
			b[k++] = a[v[k0 + (ni - j - 1)]];
	}
	kfree(km, v);

	// sort u[] and a[] by the target position, such that adjacent chains may be joined
	KMALLOC(km, w, n_u);
	for (i = k = 0; i < n_u; ++i) {
		w[i].x = b[k]>>32, w[i].y = (uint64_t)k<<32|i;
		k += (int32_t)u[i];
	}
	radix_sort_mp128x(w, w + n_u);
	KMALLOC(km, u2, n_u);
	for (i = k = 0; i < n_u; ++i) {
		int32_t j = (int32_t)w[i].y, n = (int32_t)u[j];
		u2[i] = u[j];
		memcpy(&a[k], &b[w[i].y>>32], n * sizeof(*a));
		k += n;
	}
	memcpy(u, u2, n_u * 8);
	memcpy(b, a, k * sizeof(*a)); // write _a_ to _b_ and deallocate _a_ because _a_ is oversized, sometimes a lot
	kfree(km, a); kfree(km, w); kfree(km, u2);
	return b;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movslq -0x1c(%rbp), %rsi
shlq $0x3, %rsi
callq 0xaeb0
movq %rax, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x50(%rbp), %rax
movslq -0xc(%rbp), %rcx
cmpq %rcx, %rax
jge 0x19d8a
movq -0x60(%rbp), %rax
movl %eax, -0x64(%rbp)
movq -0x18(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, -0x68(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movslq -0x68(%rbp), %rcx
cmpq %rcx, %rax
jge 0x19d77
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
movslq -0x64(%rbp), %rdx
movslq -0x68(%rbp), %rsi
subq -0x58(%rbp), %rsi
subq $0x1, %rsi
addq %rsi, %rdx
movslq (%rcx,%rdx,4), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x40(%rbp), %rax
movq -0x60(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x60(%rbp)
movq %rdx, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x19d22
jmp 0x19d79
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x19cf3
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0xac90
movq -0x8(%rbp), %rdi
movslq -0xc(%rbp), %rsi
shlq $0x4, %rsi
callq 0xaeb0
movq %rax, -0x38(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movslq -0xc(%rbp), %rcx
cmpq %rcx, %rax
jge 0x19e2e
movq -0x40(%rbp), %rax
movq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
shrq $0x20, %rcx
movq -0x38(%rbp), %rax
movq -0x50(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq -0x60(%rbp), %rcx
shlq $0x20, %rcx
orq -0x50(%rbp), %rcx
movq -0x38(%rbp), %rax
movq -0x50(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cltq
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x19dbc
movq -0x38(%rbp), %rdi
movq -0x38(%rbp), %rsi
movslq -0xc(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0x4bd0
movq -0x8(%rbp), %rdi
movslq -0xc(%rbp), %rsi
shlq $0x3, %rsi
callq 0xaeb0
movq %rax, -0x48(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movslq -0xc(%rbp), %rcx
cmpq %rcx, %rax
jge 0x19f14
movq -0x38(%rbp), %rax
movq -0x50(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movl %eax, -0x6c(%rbp)
movq -0x18(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl %eax, -0x70(%rbp)
movq -0x18(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x48(%rbp), %rax
movq -0x50(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x30(%rbp), %rdi
movq -0x60(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rax
movq -0x50(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shrq $0x20, %rax
shlq $0x3, %rax
addq %rax, %rsi
movslq -0x70(%rbp), %rdx
shlq $0x3, %rdx
callq 0x2200
movslq -0x70(%rbp), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x19e6b
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl -0xc(%rbp), %eax
shll $0x3, %eax
movslq %eax, %rdx
callq 0x2200
movq -0x40(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x60(%rbp), %rdx
shlq $0x3, %rdx
callq 0x2200
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0xac90
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0xac90
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0xac90
movq -0x40(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq