// File: splatlab[P]debgr/threadsafe-gqf/gqf.c
// Function: encode_counter(quotient_filter*, unsigned long, unsigned long, unsigned long*)
static inline uint64_t *encode_counter(QF *qf, uint64_t remainder, uint64_t
																			 counter, uint64_t *slots)
{
	uint64_t digit = remainder;
	uint64_t base = (1ULL << qf->metadata->bits_per_slot) - 1;
	uint64_t *p = slots;

	if (counter == 0)
		return p;

	*--p = remainder;

	if (counter == 1)
		return p;

	if (counter == 2) {
		*--p = remainder;
		return p;
	}

	if (counter == 3 && remainder == 0) {
		*--p = remainder;
		*--p = remainder;
		return p;    
	}

	if (counter == 3 && remainder > 0) {
		*--p = 0;
		*--p = remainder;
		return p;
	}

	if (remainder == 0)
		*--p = remainder;
	else 
		base--;

	if (remainder)
		counter -= 3;
	else
		counter -= 4;
	do {
		digit = counter % base;
		digit++; /* Zero not allowed */
		if (remainder && digit >= remainder)
			digit++; /* Cannot overflow since digit is mod 2^r-2 */
		*--p = digit;
		counter /= base;
	} while (counter);

	if (remainder && digit >= remainder)
		*--p = 0;

	*--p = remainder;

	return p;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x38(%rax), %rcx
movl $0x1, %eax
shlq %cl, %rax
subq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x28a44
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x28c2a
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rax, %rdx
addq $-0x8, %rdx
movq %rdx, -0x40(%rbp)
movq %rcx, -0x8(%rax)
cmpq $0x1, -0x20(%rbp)
jne 0x28a6f
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x28c2a
cmpq $0x2, -0x20(%rbp)
jne 0x28a9a
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rax, %rdx
addq $-0x8, %rdx
movq %rdx, -0x40(%rbp)
movq %rcx, -0x8(%rax)
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x28c2a
cmpq $0x3, -0x20(%rbp)
jne 0x28ae3
cmpq $0x0, -0x18(%rbp)
jne 0x28ae3
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rax, %rdx
addq $-0x8, %rdx
movq %rdx, -0x40(%rbp)
movq %rcx, -0x8(%rax)
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rax, %rdx
addq $-0x8, %rdx
movq %rdx, -0x40(%rbp)
movq %rcx, -0x8(%rax)
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x28c2a
cmpq $0x3, -0x20(%rbp)
jne 0x28b2c
cmpq $0x0, -0x18(%rbp)
jbe 0x28b2c
movq -0x40(%rbp), %rax
movq %rax, %rcx
addq $-0x8, %rcx
movq %rcx, -0x40(%rbp)
movq $0x0, -0x8(%rax)
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rax, %rdx
addq $-0x8, %rdx
movq %rdx, -0x40(%rbp)
movq %rcx, -0x8(%rax)
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x28c2a
cmpq $0x0, -0x18(%rbp)
jne 0x28b4c
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rax, %rdx
addq $-0x8, %rdx
movq %rdx, -0x40(%rbp)
movq %rcx, -0x8(%rax)
jmp 0x28b58
movq -0x38(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x28b6d
movq -0x20(%rbp), %rax
subq $0x3, %rax
movq %rax, -0x20(%rbp)
jmp 0x28b79
movq -0x20(%rbp), %rax
subq $0x4, %rax
movq %rax, -0x20(%rbp)
jmp 0x28b7b
movq -0x20(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x38(%rbp)
movq %rdx, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x28bb4
movq -0x30(%rbp), %rax
cmpq -0x18(%rbp), %rax
jb 0x28bb4
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rax, %rdx
addq $-0x8, %rdx
movq %rdx, -0x40(%rbp)
movq %rcx, -0x8(%rax)
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %rax
xorl %edx, %edx
divq %rcx
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x28b7b
cmpq $0x0, -0x18(%rbp)
je 0x28c0b
movq -0x30(%rbp), %rax
cmpq -0x18(%rbp), %rax
jb 0x28c0b
movq -0x40(%rbp), %rax
movq %rax, %rcx
addq $-0x8, %rcx
movq %rcx, -0x40(%rbp)
movq $0x0, -0x8(%rax)
movq -0x18(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rax, %rdx
addq $-0x8, %rdx
movq %rdx, -0x40(%rbp)
movq %rcx, -0x8(%rax)
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
popq %rbp
retq