// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movq %r9, %r14
movq %r8, %r13
movq %rdx, %rbx
movl %esi, %ebp
movq %rdi, %r15
movslq %ecx, %rsi
shlq $0x4, %rsi
callq 0x1264d
movq %rax, %r12
movl %ebp, 0x14(%rsp)
movslq %ebp, %rbp
testl %ebp, %ebp
jle 0x1577d
leaq -0x4(%r13), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movslq (%rbx,%rcx,8), %rsi
testq %rsi, %rsi
jle 0x15775
movslq %edx, %rdi
shlq $0x2, %rdi
leaq (%rdi,%rsi,4), %rdi
negq %rsi
addq %rax, %rdi
movq %rdx, %r8
shlq $0x4, %r8
addq %r12, %r8
xorl %r9d, %r9d
movslq (%rdi,%r9,4), %r10
shlq $0x4, %r10
movups (%r14,%r10), %xmm0
movups %xmm0, (%r8)
decq %r9
addq $0x10, %r8
cmpq %r9, %rsi
jne 0x15755
subq %r9, %rdx
incq %rcx
cmpq %rbp, %rcx
jne 0x1572e
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, 0x8(%rsp)
callq 0x12587
movq %rbp, %rbx
shlq $0x4, %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1264d
movq 0x8(%rsp), %rdi
movq %rax, %r13
testl %ebp, %ebp
jle 0x157e2
leaq 0x8(%r13), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movq %rdx, %rsi
shlq $0x4, %rsi
movq (%r12,%rsi), %rsi
movq %rsi, -0x8(%rax)
movq %rdx, %rsi
shlq $0x20, %rsi
orq %rcx, %rsi
movq %rsi, (%rax)
movslq (%rdi,%rcx,8), %rsi
addq %rsi, %rdx
incq %rcx
addq $0x10, %rax
cmpq %rcx, %rbp
jne 0x157b3
addq %r13, %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x5a49
leaq (,%rbp,8), %rsi
movq %r15, %rdi
callq 0x1264d
testl %ebp, %ebp
jle 0x15893
movq %r15, 0x28(%rsp)
movq %r13, 0x20(%rsp)
addq $0x8, %r13
xorl %r15d, %r15d
xorl %ebx, %ebx
movq %r12, 0x18(%rsp)
movq %rbp, 0x30(%rsp)
movq %rbx, %r12
movslq (%r13), %rcx
movq 0x8(%rsp), %rdx
movq (%rdx,%rcx,8), %rcx
movq %rcx, (%rax,%r15,8)
movq %rbx, %rdi
shlq $0x4, %rdi
addq %r14, %rdi
movq %rax, %rbp
movl 0x4(%r13), %esi
shlq $0x4, %rsi
addq 0x18(%rsp), %rsi
movslq %ecx, %rbx
movq %rbx, %rdx
shlq $0x4, %rdx
callq 0x22a0
movq %rbp, %rax
movq 0x30(%rsp), %rbp
addq %r12, %rbx
incq %r15
addq $0x10, %r13
cmpq %r15, %rbp
jne 0x15825
shlq $0x4, %rbx
movq 0x28(%rsp), %r15
movq 0x18(%rsp), %r12
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %r13
jmp 0x1589a
xorl %ebx, %ebx
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %ecx
shll $0x3, %ecx
movslq %ecx, %rdx
movq %rax, %rbp
movq %rax, %rsi
callq 0x22a0
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x22a0
movq %r15, %rdi
movq %r14, %rsi
callq 0x12587
movq %r15, %rdi
movq %r13, %rsi
callq 0x12587
movq %r15, %rdi
movq %rbp, %rsi
callq 0x12587
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq