// File: /workspace/github_c_makefile_star2_O3/lh3[P]minigraph/lchain.c
// Function: compact_a
static mg128_t *compact_a(void *km, int32_t n_u, uint64_t *u, int32_t n_v, int32_t *v, mg128_t *a)
{
	mg128_t *b, *w;
	uint64_t *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].x, w[i].y = (uint64_t)k<<32|i;
		k += (int32_t)u[i];
	}
	radix_sort_128x(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(mg128_t));
		k += n;
	}
	memcpy(u, u2, n_u * 8);
	memcpy(b, a, k * sizeof(mg128_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 0x30e5
movq %rax, %r12
movl %ebp, 0x14(%rsp)
movslq %ebp, %rbp
testl %ebp, %ebp
jle 0x16cb0
leaq -0x4(%r13), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movslq (%rbx,%rcx,8), %rsi
testq %rsi, %rsi
jle 0x16ca8
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 0x16c88
subq %r9, %rdx
incq %rcx
cmpq %rbp, %rcx
jne 0x16c61
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, 0x8(%rsp)
callq 0x301f
movq %rbp, %rbx
shlq $0x4, %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x30e5
movq 0x8(%rsp), %rdi
movq %rax, %r13
testl %ebp, %ebp
jle 0x16d15
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 0x16ce6
addq %r13, %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0xf27f
leaq (,%rbp,8), %rsi
movq %r15, %rdi
callq 0x30e5
testl %ebp, %ebp
jle 0x16dc6
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 0x2230
movq %rbp, %rax
movq 0x30(%rsp), %rbp
addq %r12, %rbx
incq %r15
addq $0x10, %r13
cmpq %r15, %rbp
jne 0x16d58
shlq $0x4, %rbx
movq 0x28(%rsp), %r15
movq 0x18(%rsp), %r12
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %r13
jmp 0x16dcd
xorl %ebx, %ebx
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %ecx
shll $0x3, %ecx
movslq %ecx, %rdx
movq %rax, %rbp
movq %rax, %rsi
callq 0x2230
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x2230
movq %r15, %rdi
movq %r14, %rsi
callq 0x301f
movq %r15, %rdi
movq %r13, %rsi
callq 0x301f
movq %r15, %rdi
movq %rbp, %rsi
callq 0x301f
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq