// File: attractivechaos[P]udb2/tommyds/./tommyhashdyn.c
// Function: tommy_hashdyn_resize
static void tommy_hashdyn_resize(tommy_hashdyn* hashdyn, tommy_count_t new_bucket_bit)
{
	tommy_count_t bucket_bit;
	tommy_count_t bucket_max;
	tommy_count_t new_bucket_max;
	tommy_count_t new_bucket_mask;
	tommy_hashdyn_node** new_bucket;

	bucket_bit = hashdyn->bucket_bit;
	bucket_max = hashdyn->bucket_max;

	new_bucket_max = 1 << new_bucket_bit;
	new_bucket_mask = new_bucket_max - 1;

	/* allocate the new vector using malloc() and not calloc() */
	/* because data is fully initialized in the update process */
	new_bucket = tommy_cast(tommy_hashdyn_node**, tommy_malloc(new_bucket_max * sizeof(tommy_hashdyn_node*)));

	/* reinsert all the elements */
	if (new_bucket_bit > bucket_bit) {
		tommy_count_t i;

		/* grow */
		for (i = 0; i < bucket_max; ++i) {
			tommy_hashdyn_node* j;

			/* setup the new two buckets */
			new_bucket[i] = 0;
			new_bucket[i + bucket_max] = 0;

			/* reinsert the bucket */
			j = hashdyn->bucket[i];
			while (j) {
				tommy_hashdyn_node* j_next = j->next;
				tommy_count_t pos = j->key & new_bucket_mask;
				if (new_bucket[pos])
					tommy_list_insert_tail_not_empty(new_bucket[pos], j);
				else
					tommy_list_insert_first(&new_bucket[pos], j);
				j = j_next;
			}
		}
	} else {
		tommy_count_t i;

		/* shrink */
		for (i = 0; i < new_bucket_max; ++i) {
			/* setup the new bucket with the lower bucket*/
			new_bucket[i] = hashdyn->bucket[i];

			/* concat the upper bucket */
			tommy_list_concat(&new_bucket[i], &hashdyn->bucket[i + new_bucket_max]);
		}
	}

	tommy_free(hashdyn->bucket);

	/* setup */
	hashdyn->bucket_bit = new_bucket_bit;
	hashdyn->bucket_max = new_bucket_max;
	hashdyn->bucket_mask = new_bucket_mask;
	hashdyn->bucket = new_bucket;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movl %esi, 0x3c(%rsp)
movq 0x40(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x38(%rsp)
movq 0x40(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0x34(%rsp)
movl 0x3c(%rsp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, 0x30(%rsp)
movl 0x30(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x2c(%rsp)
movl 0x30(%rsp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x1090
movq %rax, 0x20(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x38(%rsp), %eax
jbe 0x635c
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x34(%rsp), %eax
jae 0x635a
movq 0x20(%rsp), %rax
movl 0x1c(%rsp), %ecx
movq $0x0, (%rax,%rcx,8)
movq 0x20(%rsp), %rax
movl 0x1c(%rsp), %ecx
addl 0x34(%rsp), %ecx
movl %ecx, %ecx
movq $0x0, (%rax,%rcx,8)
movq 0x40(%rsp), %rax
movq (%rax), %rax
movl 0x1c(%rsp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x6348
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rax
movl 0x18(%rax), %eax
andl 0x2c(%rsp), %eax
movl %eax, 0x4(%rsp)
movq 0x20(%rsp), %rax
movl 0x4(%rsp), %ecx
cmpq $0x0, (%rax,%rcx,8)
je 0x6322
movq 0x20(%rsp), %rax
movl 0x4(%rsp), %ecx
movq (%rax,%rcx,8), %rdi
movq 0x10(%rsp), %rsi
callq 0x60d0
jmp 0x633c
movq 0x20(%rsp), %rdi
movl 0x4(%rsp), %eax
shlq $0x3, %rax
addq %rax, %rdi
movq 0x10(%rsp), %rsi
callq 0x6090
movq 0x8(%rsp), %rax
movq %rax, 0x10(%rsp)
jmp 0x62d4
jmp 0x634a
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x6289
jmp 0x63c0
movl $0x0, (%rsp)
movl (%rsp), %eax
cmpl 0x30(%rsp), %eax
jae 0x63be
movq 0x40(%rsp), %rax
movq (%rax), %rax
movl (%rsp), %ecx
movq (%rax,%rcx,8), %rdx
movq 0x20(%rsp), %rax
movl (%rsp), %ecx
movq %rdx, (%rax,%rcx,8)
movq 0x20(%rsp), %rdi
movl (%rsp), %eax
shlq $0x3, %rax
addq %rax, %rdi
movq 0x40(%rsp), %rax
movq (%rax), %rsi
movl (%rsp), %eax
addl 0x30(%rsp), %eax
movl %eax, %eax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x6410
movl (%rsp), %eax
addl $0x1, %eax
movl %eax, (%rsp)
jmp 0x6363
jmp 0x63c0
movq 0x40(%rsp), %rax
movq (%rax), %rdi
callq 0x1030
movl 0x3c(%rsp), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x8(%rax)
movl 0x30(%rsp), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0xc(%rax)
movl 0x2c(%rsp), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x10(%rax)
movq 0x20(%rsp), %rcx
movq 0x40(%rsp), %rax
movq %rcx, (%rax)
addq $0x48, %rsp
retq
nopw %cs:(%rax,%rax)