// File: /workspace/github_c_makefile_star2_O3/johsteffens[P]beth/lib/bcore/bcore_spect_array.c
// Function: bcore_array_default_set_size
void bcore_array_default_set_size( const bcore_array_s* p, bcore_array* o, uz_t size )
{
    if( p->size_fix > 0 )
    {
        if( size == p->size_fix ) return;
        ERR( "Cannot change size for fixed-size-array" );
    }

    uz_t space = bcore_array_p_get_space( p, o );

    if( size > space ) bcore_array_p_set_space( p, o, ( size <= space * 2 ) ? space * 2 : size );

    // If array was referencing external data and size > 0, it has now copied over external data (taken ownership)
    // If size == 0, array will release external references below

    vd_t obj = ( u0_t* )o + p->caps_offset;
    switch( p->type_caps )
    {
        case BCORE_CAPS_ARRAY_DYN_SOLID_STATIC:
        {
            bcore_array_dyn_solid_static_s* arr = obj;
            if( arr->size > arr->space )
            {
                arr->size = 0;
                arr->data = NULL;
            }
            const bcore_inst_s* instance_p = p->item_p;
            uz_t unit_size = instance_p->size;
            if( size < arr->size )
            {
                if( instance_p->down_flat )
                {
                    arr->size = size;
                }
                else
                {
                    while( size < arr->size )
                    {
                        arr->size--;
                        instance_p->down( instance_p, ( u0_t* )arr->data + unit_size * arr->size );
                    }
                }
            }
            else if( size > arr->size )
            {
                if( instance_p->init_flat )
                {
                    bcore_memzero( ( u0_t* )arr->data + unit_size * arr->size, unit_size * ( size - arr->size ) );
                    arr->size = size;
                }
                else
                {
                    while( size > arr->size )
                    {
                        instance_p->init( instance_p, ( u0_t* )arr->data + unit_size * arr->size );
                        arr->size++;
                    }
                }
            }
        }
        break;

        case BCORE_CAPS_ARRAY_DYN_SOLID_TYPED:
        {
            bcore_array_dyn_solid_typed_s* arr = obj;
            if( arr->size > arr->space )
            {
                arr->size = 0;
                arr->data = NULL;
            }
            if( size == arr->size ) break;
            if( !arr->type ) ERR( "attempt to change size on type-zero array" );
            const bcore_inst_s* instance_p = bcore_inst_s_get_typed( arr->type );
            uz_t unit_size = instance_p->size;
            if( size < arr->size )
            {
                if( instance_p->down_flat )
                {
                    arr->size = size;
                }
                else
                {
                    while( size < arr->size )
                    {
                        arr->size--;
                        instance_p->down( instance_p, ( u0_t* )arr->data + unit_size * arr->size );
                    }
                }
            }
            else if( size > arr->size )
            {
                if( instance_p->init_flat )
                {
                    bcore_memzero( ( u0_t* )arr->data + unit_size * arr->size, unit_size * ( size - arr->size ) );
                    arr->size = size;
                }
                else
                {
                    while( size > arr->size )
                    {
                        instance_p->init( instance_p, ( u0_t* )arr->data + unit_size * arr->size );
                        arr->size++;
                    }
                }
            }
        }
        break;

        case BCORE_CAPS_ARRAY_DYN_LINK_STATIC:
        {
            bcore_array_dyn_link_static_s* arr = obj;
            if( arr->size > arr->space )
            {
                arr->size = 0;
                arr->data = NULL;
            }
            const bcore_inst_s* instance_p = p->item_p;
            if( size < arr->size )
            {
                while( size < arr->size )
                {
                    arr->size--;
                    instance_p->discard( instance_p, arr->data[ arr->size ] );
                }
            }
            else if( size > arr->size )
            {
                bcore_memzero( arr->data + arr->size, sizeof( vd_t ) * ( size - arr->size ) );
                arr->size = size;
            }
        }
        break;

        case BCORE_CAPS_ARRAY_DYN_LINK_TYPED:
        {
            bcore_array_dyn_link_typed_s* arr = obj;
            if( arr->size > arr->space )
            {
                arr->size = 0;
                arr->data = NULL;
            }
            if( size < arr->size )
            {
                const bcore_inst_s* instance_p = ( arr->type ) ? bcore_inst_s_get_typed( arr->type ) : NULL;
                while( size < arr->size )
                {
                    arr->size--;
                    if( arr->data[ arr->size ] )
                    {
                        if( !instance_p ) ERR( "cannot discard element on type-zero array" );
                        instance_p->discard( instance_p, arr->data[ arr->size ] );
                    }
                }
            }
            else if( size > arr->size )
            {
                bcore_memzero( arr->data + arr->size, sizeof( vd_t ) * ( size - arr->size ) );
                arr->size = size;
            }
        }
        break;

        case BCORE_CAPS_ARRAY_DYN_LINK_AWARE:
        {
            bcore_array_dyn_link_aware_s* arr = obj;
            if( arr->size > arr->space )
            {
                arr->size = 0;
                arr->data = NULL;
            }
            if( size < arr->size )
            {
                while( size < arr->size )
                {
                    arr->size--;
                    bcore_inst_a_discard( arr->data[ arr->size ] );
                }
            }
            else if( size > arr->size )
            {
                bcore_memzero( arr->data + arr->size, sizeof( vd_t ) * ( size - arr->size ) );
                arr->size = size;
            }
        }
        break;

        default: ERR( "invalid type_caps (%"PRIu32")", ( u2_t )p->type_caps );
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq 0x18(%rdi), %rax
testq %rax, %rax
je 0xec598
cmpq %rbx, %rax
je 0xeca81
leaq 0x5c729(%rip), %rdi     # 0x148c9e
leaq 0x5c62c(%rip), %rsi     # 0x148ba8
leaq 0x5c738(%rip), %rcx     # 0x148cbb
movl $0x16a, %edx            # imm = 0x16A
xorl %eax, %eax
callq 0xbe592
movq 0x18(%r15), %rdx
testq %rdx, %rdx
jne 0xec5a1
movq 0x20(%r15), %rax
movq 0x10(%r14,%rax), %rdx
cmpq %rbx, %rdx
jae 0xec5bb
addq %rdx, %rdx
cmpq %rbx, %rdx
cmovbeq %rbx, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0xebeb6
movq 0x10(%r15), %r8
leaq -0x6(%r8), %rax
cmpq $0x4, %rax
ja 0xec878
addq 0x20(%r15), %r14
leaq 0x5c420(%rip), %rcx     # 0x1489f8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
jbe 0xec953
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movq 0x28(%r15), %r15
movq 0x18(%r15), %r12
xorl %esi, %esi
jmp 0xec991
movq 0x8(%r14), %rdi
cmpq 0x10(%r14), %rdi
ja 0xec72e
cmpq %rbx, %rdi
jbe 0xec737
leaq 0xafe45(%rip), %r12     # 0x19c468
leaq -0x1(%rdi), %rax
movq %rax, 0x8(%r14)
movq (%r14), %rcx
movq -0x8(%rcx,%rdi,8), %r13
testq %r13, %r13
je 0xec6d4
movq (%r13), %rbp
movl (%r12), %eax
cltq
leaq (%rax,%rax,2), %rax
movq 0x20(%r12,%rax,8), %rcx
movq 0x18(%r12,%rax,8), %rdx
andq %rbp, %rdx
shlq $0x4, %rdx
leaq (%rcx,%rdx), %rax
movq (%rcx,%rdx), %rdx
movl $0x8, %ecx
cmpq %rbp, %rdx
je 0xec697
movq 0x10(%rax), %rdx
movl $0x18, %ecx
cmpq %rbp, %rdx
je 0xec697
movq 0x20(%rax), %rdx
movl $0x28, %ecx
cmpq %rbp, %rdx
je 0xec697
movq 0x30(%rax), %rdx
movl $0x38, %ecx
cmpq %rbp, %rdx
jne 0xec6a0
movq (%rax,%rcx), %r15
testq %r15, %r15
jne 0xec6c3
movabsq $0x782b96602f424981, %rdi # imm = 0x782B96602F424981
movq %rbp, %rsi
callq 0xe9bc5
movq %rax, %r15
movq %r12, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x115fac
movq %r15, %rdi
movq %r13, %rsi
callq *0xb8(%r15)
movq 0x8(%r14), %rax
movq %rax, %rdi
cmpq %rbx, %rax
ja 0xec623
jmp 0xeca81
movq 0x8(%r14), %rdi
cmpq 0x10(%r14), %rdi
ja 0xec72e
cmpq %rbx, %rdi
jbe 0xec737
movq 0x28(%r15), %r15
leaq -0x1(%rdi), %rax
movq %rax, 0x8(%r14)
movq (%r14), %rax
movq -0x8(%rax,%rdi,8), %rsi
movq %r15, %rdi
callq *0xb8(%r15)
movq 0x8(%r14), %rdi
cmpq %rbx, %rdi
ja 0xec6f8
jmp 0xeca81
movq 0x8(%r14), %rdi
cmpq 0x10(%r14), %rdi
jbe 0xec8a7
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
xorl %edi, %edi
movq %rbx, %rsi
subq %rdi, %rsi
jbe 0xeca81
shlq $0x3, %rdi
addq (%r14), %rdi
shlq $0x3, %rsi
callq 0xbe802
movq %rbx, 0x8(%r14)
jmp 0xeca81
movq 0x8(%r14), %rax
cmpq 0x10(%r14), %rax
jbe 0xec76f
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
xorl %eax, %eax
cmpq %rbx, %rax
je 0xeca81
movq 0x18(%r14), %r12
testq %r12, %r12
jne 0xec7a6
leaq 0x5c516(%rip), %rdi     # 0x148c9e
leaq 0x5c419(%rip), %rsi     # 0x148ba8
leaq 0x5c54d(%rip), %rcx     # 0x148ce3
movl $0x1ac, %edx            # imm = 0x1AC
xorl %eax, %eax
callq 0xbe592
movq 0x18(%r14), %r12
leaq 0xafcbb(%rip), %rax     # 0x19c468
movl (%rax), %ecx
movslq %ecx, %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x20(%rax,%rcx,8), %rdx
movq 0x18(%rax,%rcx,8), %rcx
andq %r12, %rcx
shlq $0x4, %rcx
leaq (%rdx,%rcx), %rax
movq (%rdx,%rcx), %rdx
movl $0x8, %ecx
cmpq %r12, %rdx
je 0xec803
movq 0x10(%rax), %rdx
movl $0x18, %ecx
cmpq %r12, %rdx
je 0xec803
movq 0x20(%rax), %rdx
movl $0x28, %ecx
cmpq %r12, %rdx
je 0xec803
movq 0x30(%rax), %rdx
movl $0x38, %ecx
cmpq %r12, %rdx
jne 0xec80c
movq (%rax,%rcx), %r15
testq %r15, %r15
jne 0xec833
movabsq $0x782b96602f424981, %rdi # imm = 0x782B96602F424981
movq %r12, %rsi
callq 0xe9bc5
movq %rax, %r15
leaq 0xafc40(%rip), %rdi     # 0x19c468
movq %r12, %rsi
movq %rax, %rdx
callq 0x115fac
movq 0x18(%r15), %r12
movq 0x8(%r14), %rsi
movq %rbx, %rax
subq %rsi, %rax
jae 0xec9ca
cmpb $0x0, 0x2a(%r15)
jne 0xec753
decq %rsi
movq %rsi, 0x8(%r14)
imulq %r12, %rsi
addq (%r14), %rsi
movq %r15, %rdi
callq *0x88(%r15)
movq 0x8(%r14), %rsi
cmpq %rbx, %rsi
ja 0xec852
jmp 0xeca81
leaq 0x5c41f(%rip), %rdi     # 0x148c9e
leaq 0x5c322(%rip), %rsi     # 0x148ba8
leaq 0x5c35c(%rip), %rcx     # 0x148be9
movl $0x21f, %edx            # imm = 0x21F
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xbe592
cmpq %rbx, %rdi
jbe 0xec737
movq 0x18(%r14), %r12
testq %r12, %r12
je 0xeca10
leaq 0xafba4(%rip), %rax     # 0x19c468
movl (%rax), %ecx
movslq %ecx, %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x20(%rax,%rcx,8), %rdx
movq 0x18(%rax,%rcx,8), %rcx
andq %r12, %rcx
shlq $0x4, %rcx
leaq (%rdx,%rcx), %rax
movq (%rdx,%rcx), %rdx
movl $0x8, %ecx
cmpq %r12, %rdx
je 0xec91a
movq 0x10(%rax), %rdx
movl $0x18, %ecx
cmpq %r12, %rdx
je 0xec91a
movq 0x20(%rax), %rdx
movl $0x28, %ecx
cmpq %r12, %rdx
je 0xec91a
movq 0x30(%rax), %rdx
movl $0x38, %ecx
cmpq %r12, %rdx
jne 0xec927
movq (%rax,%rcx), %r15
testq %r15, %r15
jne 0xeca13
movabsq $0x782b96602f424981, %rdi # imm = 0x782B96602F424981
movq %r12, %rsi
callq 0xe9bc5
movq %rax, %r15
leaq 0xafb25(%rip), %rdi     # 0x19c468
movq %r12, %rsi
movq %rax, %rdx
callq 0x115fac
jmp 0xeca13
movq 0x28(%r15), %r15
movq 0x18(%r15), %r12
cmpq %rbx, %rsi
jbe 0xec991
cmpb $0x0, 0x2a(%r15)
jne 0xec753
decq %rsi
movq %rsi, 0x8(%r14)
imulq %r12, %rsi
addq (%r14), %rsi
movq %r15, %rdi
callq *0x88(%r15)
movq 0x8(%r14), %rsi
cmpq %rbx, %rsi
ja 0xec96b
jmp 0xeca81
movq %rbx, %rax
subq %rsi, %rax
jbe 0xeca81
cmpb $0x0, 0x29(%r15)
jne 0xec9d7
imulq %r12, %rsi
addq (%r14), %rsi
movq %r15, %rdi
callq *0x80(%r15)
movq 0x8(%r14), %rsi
incq %rsi
movq %rsi, 0x8(%r14)
cmpq %rbx, %rsi
jb 0xec9a4
jmp 0xeca81
jbe 0xeca81
cmpb $0x0, 0x29(%r15)
je 0xec9ed
imulq %r12, %rsi
addq (%r14), %rsi
imulq %r12, %rax
movq %rsi, %rdi
movq %rax, %rsi
jmp 0xec74e
imulq %r12, %rsi
addq (%r14), %rsi
movq %r15, %rdi
callq *0x80(%r15)
movq 0x8(%r14), %rsi
incq %rsi
movq %rsi, 0x8(%r14)
cmpq %rbx, %rsi
jb 0xec9ed
jmp 0xeca81
xorl %r15d, %r15d
movq 0x8(%r14), %rax
cmpq %rbx, %rax
jbe 0xeca81
leaq 0x5c27b(%rip), %r12     # 0x148c9e
leaq 0x5c17e(%rip), %r13     # 0x148ba8
leaq 0x5c2dc(%rip), %rbp     # 0x148d0d
leaq -0x1(%rax), %rcx
movq %rcx, 0x8(%r14)
movq (%r14), %rdx
movq -0x8(%rdx,%rax,8), %rsi
testq %rsi, %rsi
je 0xeca79
testq %r15, %r15
jne 0xeca6b
movq %r12, %rdi
movq %r13, %rsi
movl $0x1fa, %edx            # imm = 0x1FA
movq %rbp, %rcx
xorl %eax, %eax
callq 0xbe592
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq (%rax,%rcx,8), %rsi
movq %r15, %rdi
callq *0xb8(%r15)
movq 0x8(%r14), %rcx
movq %rcx, %rax
cmpq %rbx, %rcx
ja 0xeca31
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq