// File: chhylp123[P]hifiasm/inter.cpp
// Function: compact_a(void*, int, unsigned long*, int, int*, mg128_t*)
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)]];///write all elements of a chain together
	}
	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) {///n_u: how many chains
		///x: ref_id(31)rev(1)r_pos(32)
		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);///sort by ref_id(31)rev(1)r_pos(32); r_pos is the start pos of chain
	KMALLOC(km, u2, n_u);
	for (i = k = 0; i < n_u; ++i) {///note merge chain; just place close chains together
		///j is chain id; n is how many elements in j-th chain
		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
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 $0x4, %rsi
callq 0x30e4f0
movq %rax, -0x38(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x50(%rbp), %rax
movslq -0xc(%rbp), %rcx
cmpq %rcx, %rax
jge 0x28473e
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 0x28472b
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rax
movslq -0x64(%rbp), %rdx
movslq -0x68(%rbp), %rsi
subq -0x58(%rbp), %rsi
subq $0x1, %rsi
addq %rsi, %rdx
movslq (%rax,%rdx,4), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq -0x38(%rbp), %rax
movq -0x60(%rbp), %rdx
movq %rdx, %rsi
addq $0x1, %rsi
movq %rsi, -0x60(%rbp)
shlq $0x4, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x2846c2
jmp 0x28472d
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x284693
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x30e2d0
movq -0x8(%rbp), %rdi
movslq -0xc(%rbp), %rsi
shlq $0x4, %rsi
callq 0x30e4f0
movq %rax, -0x40(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movslq -0xc(%rbp), %rcx
cmpq %rcx, %rax
jge 0x2847e4
movq -0x38(%rbp), %rax
movq -0x60(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq -0x40(%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 -0x40(%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 0x284770
movq -0x40(%rbp), %rdi
movq -0x40(%rbp), %rsi
movslq -0xc(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0x27ea40
movq -0x8(%rbp), %rdi
movslq -0xc(%rbp), %rsi
shlq $0x3, %rsi
callq 0x30e4f0
movq %rax, -0x48(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movslq -0xc(%rbp), %rcx
cmpq %rcx, %rax
jge 0x2848ca
movq -0x40(%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 $0x4, %rax
addq %rax, %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rax
movq -0x50(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shrq $0x20, %rax
shlq $0x4, %rax
addq %rax, %rsi
movslq -0x70(%rbp), %rdx
shlq $0x4, %rdx
callq 0x2180
movslq -0x70(%rbp), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x284821
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl -0xc(%rbp), %eax
shll $0x3, %eax
movslq %eax, %rdx
callq 0x2180
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x60(%rbp), %rdx
shlq $0x4, %rdx
callq 0x2180
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x30e2d0
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x30e2d0
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x30e2d0
movq -0x38(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)