// File: /workspace/github_cpp_makefile_star2_O3/attractivechaos[P]udb2/tommyds/./tommyhashlin.c
// Function: hashlin_shrink_step
tommy_inline void hashlin_shrink_step(tommy_hashlin* hashlin)
{
	/* shrink if less than 12.5% full */
	if (hashlin->state != TOMMY_HASHLIN_STATE_SHRINK
		&& hashlin->count < hashlin->bucket_max / 8
	) {
		/* avoid to shrink the first bucket */
		if (hashlin->bucket_bit > TOMMY_HASHLIN_BIT) {
			/* if we are stable, setup a new shrink state */
			/* otherwise continue with the already setup grow one */
			/* but in backward direction */
			if (hashlin->state == TOMMY_HASHLIN_STATE_STABLE) {
				/* set the lower size */
				hashlin->low_max = hashlin->bucket_max / 2;
				hashlin->low_mask = hashlin->bucket_mask / 2;

				/* start from the half going backward */
				hashlin->split = hashlin->low_max;
			}

			/* start reallocation */
			hashlin->state = TOMMY_HASHLIN_STATE_SHRINK;
		}
	}

	/* if we are shrinking */
	if (hashlin->state == TOMMY_HASHLIN_STATE_SHRINK) {
		/* compute the split target required to finish the reallocation before the next resize */
		tommy_count_t split_target = 8 * hashlin->count;

		/* reallocate buckets until the split target */
		while (hashlin->split + hashlin->low_max > split_target) {
			tommy_hashlin_node** split[2];

			/* go backward position */
			--hashlin->split;

			/* get the low bucket */
			split[0] = tommy_hashlin_pos(hashlin, hashlin->split);

			/* get the high bucket */
			split[1] = tommy_hashlin_pos(hashlin, hashlin->split + hashlin->low_max);

			/* concat the high bucket into the low one */
			tommy_list_concat(split[0], split[1]);

			/* if we have finished, clean up and change the state */
			if (hashlin->split == 0) {
				tommy_hashlin_node** segment;

				/* shrink the hash size */
				--hashlin->bucket_bit;
				hashlin->bucket_max = 1 << hashlin->bucket_bit;
				hashlin->bucket_mask = hashlin->bucket_max - 1;

				/* free the last segment */
				segment = hashlin->bucket[hashlin->bucket_bit];
				tommy_free(&segment[((tommy_ptrdiff_t)1) << hashlin->bucket_bit]);

				/* go in stable mode */
				tommy_hashlin_stable(hashlin);
				break;
			}
		}
	}
}

// Assembly:
pushq %rbx
movq %rdi, %rbx
movl 0x118(%rdi), %eax
movl 0x11c(%rdi), %edx
cmpl $0x2, %edx
jne 0x363c
shll $0x3, %eax
movl 0x114(%rbx), %ecx
movl 0x10c(%rbx), %edx
addl %ecx, %edx
decl %ecx
leaq (,%rcx,8), %rsi
cmpl %eax, %edx
jbe 0x3683
movl %ecx, 0x114(%rbx)
decl %edx
movl %edx, %edi
orl $0x1, %edi
bsrl %edi, %edi
movq (%rbx,%rdi,8), %rdi
movq (%rdi,%rdx,8), %rdi
testq %rdi, %rdi
je 0x35dd
movl %ecx, %r8d
orl $0x1, %r8d
bsrl %r8d, %r8d
movq (%rbx,%r8,8), %r8
movq (%r8,%rsi), %r9
testq %r9, %r9
je 0x35d7
movq 0x8(%r9), %r8
movq 0x8(%rdi), %r10
movq %r10, 0x8(%r9)
movq %r8, 0x8(%rdi)
jmp 0x35da
addq %rsi, %r8
movq %rdi, (%r8)
addq $-0x8, %rsi
addl $-0x1, %ecx
jb 0x3588
movl 0x100(%rbx), %ecx
decl %ecx
movl $0x1, %eax
shll %cl, %eax
movl %ecx, 0x100(%rbx)
movl %eax, 0x104(%rbx)
decl %eax
movl $0x1, %edi
shlq %cl, %rdi
movl %eax, 0x108(%rbx)
shlq $0x3, %rdi
addq (%rbx,%rcx,8), %rdi
callq 0x1030
xorl %eax, %eax
movl %eax, 0x11c(%rbx)
movq 0x104(%rbx), %rcx
movq %rcx, 0x10c(%rbx)
movl %eax, 0x114(%rbx)
jmp 0x3683
movl 0x104(%rbx), %ecx
movl %ecx, %esi
shrl $0x3, %esi
cmpl %esi, %eax
jae 0x3683
cmpl $0x7, 0x100(%rbx)
jb 0x3683
testl %edx, %edx
jne 0x3674
shrl %ecx
movl %ecx, 0x10c(%rbx)
movl 0x108(%rbx), %edx
shrl %edx
movl %edx, 0x110(%rbx)
movl %ecx, 0x114(%rbx)
movl $0x2, 0x11c(%rbx)
jmp 0x356d
popq %rbx
retq