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

	// write the result to b[]
	b = Kmalloc(km, mm128_t, 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
	w = Kmalloc(km, mm128_t, n_u);
	for (i = k = 0; i < n_u; ++i) {
		w[i].x = b[k].x, w[i].y = (uint64_t)k<<32|i;
		k += (int32_t)u[i];
	}
	radix_sort_128x(w, w + n_u);
	u2 = Kmalloc(km, uint64_t, 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(mm128_t));
		k += n;
	}
	memcpy(u, u2, n_u * 8);
	memcpy(b, a, k * sizeof(mm128_t)); // 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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %r14
movq %r8, %rbp
movq %rdx, (%rsp)
movl %esi, %ebx
movq %rdi, %r15
movslq %ecx, %rsi
shlq $0x4, %rsi
callq 0x10afc
movq %rax, %r12
movl %ebx, 0x14(%rsp)
movslq %ebx, %rbx
xorl %eax, %eax
testq %rbx, %rbx
movl $0x0, %r13d
cmovgq %rbx, %r13
leaq -0x4(%rbp), %rcx
xorl %edx, %edx
cmpq %r13, %rdx
je 0x1372c
movq (%rsp), %rsi
movslq (%rsi,%rdx,8), %r8
movslq %eax, %r9
shlq $0x2, %r9
testq %r8, %r8
movl $0x0, %esi
cmovgq %r8, %rsi
movq %rax, %rdi
shlq $0x4, %rdi
addq %r12, %rdi
negq %rsi
leaq (%r9,%r8,4), %r9
addq %rcx, %r9
xorl %r8d, %r8d
cmpq %r8, %rsi
je 0x13724
movslq (%r9,%r8,4), %r10
shlq $0x4, %r10
movups (%r14,%r10), %xmm0
movups %xmm0, (%rdi)
decq %r8
addq $0x10, %rdi
jmp 0x13706
incq %rdx
subq %r8, %rax
jmp 0x136cf
movq %r15, %rdi
movq %rbp, %rsi
callq 0x10a07
movq %rbx, %rbp
shlq $0x4, %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x10afc
movq %rax, 0x8(%rsp)
addq $0x8, %rax
xorl %ecx, %ecx
xorl %edx, %edx
cmpq %rdx, %r13
je 0x1378b
movq %rcx, %rsi
shlq $0x4, %rsi
movq (%r12,%rsi), %rsi
movq %rsi, -0x8(%rax)
movq %rcx, %rsi
shlq $0x20, %rsi
orq %rdx, %rsi
movq %rsi, (%rax)
movq (%rsp), %rsi
movslq (%rsi,%rdx,8), %rsi
addq %rsi, %rcx
incq %rdx
addq $0x10, %rax
jmp 0x13756
movq 0x8(%rsp), %rdi
movq %rdi, %rsi
addq %rbx, %rsi
callq 0x47dc
movq %rbp, %rsi
shlq $0x3, %rsi
movq %r15, 0x20(%rsp)
movq %r15, %rdi
callq 0x10afc
movq %rax, 0x18(%rsp)
shlq $0x3, %r13
xorl %r15d, %r15d
xorl %ebx, %ebx
cmpq %r15, %r13
je 0x13811
movq 0x8(%rsp), %rdx
movslq 0x8(%rdx,%r15,2), %rax
movq (%rsp), %rcx
movq (%rcx,%rax,8), %rax
movq 0x18(%rsp), %rcx
movq %rax, (%rcx,%r15)
movq %rbx, %rdi
shlq $0x4, %rdi
addq %r14, %rdi
movl 0xc(%rdx,%r15,2), %esi
shlq $0x4, %rsi
addq %r12, %rsi
movq %r12, %rbp
movslq %eax, %r12
movq %r12, %rdx
shlq $0x4, %rdx
callq 0x22a0
addq %r12, %rbx
movq %rbp, %r12
addq $0x8, %r15
jmp 0x137bd
movl 0x14(%rsp), %eax
shll $0x3, %eax
movslq %eax, %rdx
movq (%rsp), %rdi
movq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0x22a0
shlq $0x4, %rbx
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x22a0
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x10a07
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0x10a07
movq %rbx, %rdi
movq %r15, %rsi
callq 0x10a07
movq %r12, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq