// File: /workspace/github_cpp_makefile_star2_O2/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 %rbp
pushq %rbx
pushq %rax
movq %rdi, %rbx
movl 0x118(%rdi), %ebp
movl 0x11c(%rdi), %ecx
cmpl $0x2, %ecx
jne 0x2de4
shll $0x3, %ebp
movl 0x114(%rbx), %eax
movl 0x10c(%rbx), %ecx
leal (%rcx,%rax), %edx
cmpl %ebp, %edx
jbe 0x2e2b
leal -0x1(%rax), %edi
movl %edi, 0x114(%rbx)
movl %edi, %edx
orl $0x1, %edx
bsrl %edx, %edx
shlq $0x3, %rdi
addq (%rbx,%rdx,8), %rdi
leal (%rax,%rcx), %esi
decl %esi
movl %esi, %eax
orl $0x1, %eax
bsrl %eax, %eax
shlq $0x3, %rsi
addq (%rbx,%rax,8), %rsi
callq 0x32a8
movl 0x114(%rbx), %eax
testl %eax, %eax
jne 0x2d44
movl 0x100(%rbx), %ecx
decl %ecx
pushq $0x1
popq %rax
shll %cl, %eax
movl %ecx, 0x100(%rbx)
movl %eax, 0x104(%rbx)
decl %eax
pushq $0x1
popq %rdi
shlq %cl, %rdi
movl %eax, 0x108(%rbx)
shlq $0x3, %rdi
addq (%rbx,%rcx,8), %rdi
callq 0x1030
andl $0x0, 0x11c(%rbx)
movq 0x104(%rbx), %rax
movq %rax, 0x10c(%rbx)
andl $0x0, 0x114(%rbx)
jmp 0x2e2b
movl 0x104(%rbx), %eax
movl %eax, %edx
shrl $0x3, %edx
cmpl %edx, %ebp
jae 0x2e2b
cmpl $0x7, 0x100(%rbx)
jb 0x2e2b
testl %ecx, %ecx
jne 0x2e1c
shrl %eax
movl %eax, 0x10c(%rbx)
movl 0x108(%rbx), %ecx
shrl %ecx
movl %ecx, 0x110(%rbx)
movl %eax, 0x114(%rbx)
movl $0x2, 0x11c(%rbx)
jmp 0x2d3b
addq $0x8, %rsp
popq %rbx
popq %rbp
retq