// File: splatlab[P]debgr/threadsafe-gqf/gqf.c
// Function: decode_counter(quotient_filter const*, unsigned long, unsigned long*, unsigned long*)
static inline uint64_t decode_counter(const QF *qf, uint64_t index, uint64_t
																			*remainder, uint64_t *count)
{
	uint64_t base;
	uint64_t rem;
	uint64_t cnt;
	uint64_t digit;
	uint64_t end;

	*remainder = rem = get_slot(qf, index);

	if (is_runend(qf, index)) { /* Entire run is "0" */
		*count = 1; 
		return index;
	}

	digit = get_slot(qf, index + 1);

	if (is_runend(qf, index + 1)) {
		*count = digit == rem ? 2 : 1;
		return index + (digit == rem ? 1 : 0);
	}

	if (rem > 0 && digit >= rem) {
		*count = digit == rem ? 2 : 1;
		return index + (digit == rem ? 1 : 0);
	}

	if (rem > 0 && digit == 0 && get_slot(qf, index + 2) == rem) {
		*count = 3;
		return index + 2;
	}

	if (rem == 0 && digit == 0) {
		if (get_slot(qf, index + 2) == 0) {
			*count = 3;
			return index + 2;
		} else {
			*count = 2;
			return index + 1;
		}
	}

	cnt = 0;
	base = (1ULL << qf->metadata->bits_per_slot) - (rem ? 2 : 1);

	end = index + 1;
	while (digit != rem && !is_runend(qf, end)) {
		if (digit > rem)
			digit--;
		if (digit && rem)
			digit--;
		cnt = cnt * base + digit;

		end++;
		digit = get_slot(qf, end);
	}

	if (rem) {
		*count = cnt + 3;
		return end;
	}

	if (is_runend(qf, end) || get_slot(qf, end + 1) != 0) {
		*count = 1;
		return index;
	}

	*count = cnt + 4;
	return end + 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x281b0
movq %rax, %rcx
movq %rcx, -0x38(%rbp)
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x27380
cmpl $0x0, %eax
je 0x270ad
movq -0x28(%rbp), %rax
movq $0x1, (%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2736e
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x1, %rsi
callq 0x281b0
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x1, %rsi
callq 0x27380
cmpl $0x0, %eax
je 0x27122
movq -0x48(%rbp), %rdx
movq -0x38(%rbp), %rsi
movl $0x1, %eax
movl $0x2, %ecx
cmpq %rsi, %rdx
cmovel %ecx, %eax
movslq %eax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq -0x48(%rbp), %rsi
movq -0x38(%rbp), %rdi
xorl %ecx, %ecx
movl $0x1, %edx
cmpq %rdi, %rsi
cmovel %edx, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x8(%rbp)
jmp 0x2736e
cmpq $0x0, -0x38(%rbp)
jbe 0x2717d
movq -0x48(%rbp), %rax
cmpq -0x38(%rbp), %rax
jb 0x2717d
movq -0x48(%rbp), %rdx
movq -0x38(%rbp), %rsi
movl $0x1, %eax
movl $0x2, %ecx
cmpq %rsi, %rdx
cmovel %ecx, %eax
movslq %eax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq -0x48(%rbp), %rsi
movq -0x38(%rbp), %rdi
xorl %ecx, %ecx
movl $0x1, %edx
cmpq %rdi, %rsi
cmovel %edx, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x8(%rbp)
jmp 0x2736e
cmpq $0x0, -0x38(%rbp)
jbe 0x271be
cmpq $0x0, -0x48(%rbp)
jne 0x271be
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x2, %rsi
callq 0x281b0
cmpq -0x38(%rbp), %rax
jne 0x271be
movq -0x28(%rbp), %rax
movq $0x3, (%rax)
movq -0x18(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x8(%rbp)
jmp 0x2736e
cmpq $0x0, -0x38(%rbp)
jne 0x2721b
cmpq $0x0, -0x48(%rbp)
jne 0x2721b
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x2, %rsi
callq 0x281b0
cmpq $0x0, %rax
jne 0x271ff
movq -0x28(%rbp), %rax
movq $0x3, (%rax)
movq -0x18(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x8(%rbp)
jmp 0x2736e
movq -0x28(%rbp), %rax
movq $0x2, (%rax)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8(%rbp)
jmp 0x2736e
movq $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x38(%rax), %rcx
movl $0x1, %eax
shlq %cl, %rax
movq -0x38(%rbp), %rsi
movl $0x1, %ecx
movl $0x2, %edx
cmpq $0x0, %rsi
cmovnel %edx, %ecx
movslq %ecx, %rcx
subq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rcx
xorl %eax, %eax
cmpq -0x38(%rbp), %rcx
movb %al, -0x51(%rbp)
je 0x27289
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x27380
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, -0x51(%rbp)
movb -0x51(%rbp), %al
testb $0x1, %al
jne 0x27292
jmp 0x272f5
movq -0x48(%rbp), %rax
cmpq -0x38(%rbp), %rax
jbe 0x272a8
movq -0x48(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x272c2
cmpq $0x0, -0x38(%rbp)
je 0x272c2
movq -0x48(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
imulq -0x30(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x281b0
movq %rax, -0x48(%rbp)
jmp 0x27262
cmpq $0x0, -0x38(%rbp)
je 0x27315
movq -0x40(%rbp), %rcx
addq $0x3, %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x50(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2736e
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x27380
cmpl $0x0, %eax
jne 0x2733e
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rsi
addq $0x1, %rsi
callq 0x281b0
cmpq $0x0, %rax
je 0x27353
movq -0x28(%rbp), %rax
movq $0x1, (%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2736e
movq -0x40(%rbp), %rcx
addq $0x4, %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)