// File: Alcaro[P]bridges/arlib/array.cpp
// Function: bitarray::resize(unsigned long)
void bitarray::resize(size_t len)
{
	size_t prevlen = this->nbits;
	this->nbits = len;
	
	switch ((prevlen > n_inline)<<1 | (len > n_inline))
	{
	case 0: // small->small
		if (len < prevlen)
			clear_unused(this->nbits, sizeof(this->bits_inline));
		break;
	case 1: // small->big
		{
			size_t newbytes = alloc_size(len);
			uint8_t* newbits = xmalloc(newbytes);
			memcpy(newbits, this->bits_inline, sizeof(this->bits_inline));
			memset(newbits+sizeof(this->bits_inline), 0, newbytes-sizeof(this->bits_inline));
			bits_outline = newbits;
		}
		break;
	case 2: // big->small
		{
			uint8_t* freethis = this->bits_outline;
			memcpy(this->bits_inline, this->bits_outline, sizeof(this->bits_inline));
			free(freethis);
			clear_unused(this->nbits, sizeof(this->bits_inline));
		}
		break;
	case 3: // big->big
		{
			size_t prevbytes = alloc_size(prevlen);
			size_t newbytes = alloc_size(len);
			if (newbytes > prevbytes)
			{
				bits_outline = xrealloc(this->bits_outline, newbytes);
				clear_unused(prevlen, newbytes);
			}
			else if (len < prevlen)
			{
				clear_unused(this->nbits, newbytes);
			}
		}
		break;
	}
	
//printf("%lu->%lu t=%d", prevlen, len, ((prevlen > n_inline)<<1 | (len > n_inline)));
//size_t bytes;
//if (len > n_inline) bytes = alloc_size(len);
//else bytes = sizeof(bits_inline);
//for (size_t i=0;i<bitround(bytes);i++)
//{
//printf(" %.2X", bits()[i]);
//}
//puts("");
//bool fail=false;
//if (len>prevlen)
//{
//for (size_t n=prevlen;n<len;n++)
//{
//	if (get(n))
//	{
//		printf("%lu->%lu: unexpected at %lu\n", prevlen, len, n);
//		fail=true;
//	}
//}
//}
//if(fail)abort();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x18(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
subq $0x41, %rax
setae %al
movzbl %al, %eax
movq -0x10(%rbp), %rcx
subq $0x41, %rcx
setae %cl
movzbl %cl, %edx
movl %eax, %ecx
movl %edx, %eax
leal (%rax,%rcx,2), %eax
movl %eax, %ecx
movq %rcx, -0x60(%rbp)
subl $0x3, %eax
ja 0x689e
movq -0x60(%rbp), %rax
leaq 0x5ca03(%rip), %rcx     # 0x63158
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x677a
movq -0x68(%rbp), %rdi
movq 0x8(%rdi), %rsi
movl $0x8, %edx
callq 0x6630
jmp 0x689e
movq -0x68(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x6b90
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x2d5e0
movq %rax, -0x30(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x6bc0
movq -0x68(%rbp), %rcx
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq (%rcx), %rcx
movq %rcx, (%rax)
movq -0x28(%rbp), %rdi
addq $0x8, %rdi
movq -0x20(%rbp), %rdx
subq $0x8, %rdx
xorl %esi, %esi
callq 0x4170
movq -0x68(%rbp), %rax
movq -0x28(%rbp), %rcx
movq %rcx, (%rax)
jmp 0x689e
movq -0x68(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x38(%rbp)
movq (%rax), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax)
movq -0x38(%rbp), %rdi
callq 0x4420
movq -0x68(%rbp), %rdi
movq 0x8(%rdi), %rsi
movl $0x8, %edx
callq 0x6630
jmp 0x689e
movq -0x68(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x6b90
movq -0x68(%rbp), %rdi
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rsi
callq 0x6b90
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x40(%rbp), %rax
jbe 0x687f
movq -0x68(%rbp), %rax
movq (%rax), %rsi
leaq -0x58(%rbp), %rdi
callq 0x6be0
movq -0x48(%rbp), %rsi
movq -0x58(%rbp), %rdi
callq 0x2d690
movq %rax, -0x50(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x6bc0
movq -0x68(%rbp), %rdi
movq %rax, (%rdi)
movq -0x18(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x6630
jmp 0x689c
movq -0x10(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x689a
movq -0x68(%rbp), %rdi
movq 0x8(%rdi), %rsi
movq -0x48(%rbp), %rdx
callq 0x6630
jmp 0x689c
jmp 0x689e
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)