// File: /workspace/github_c_makefile_star2_O2/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 0x7b906
cmpq %rbx, %rax
je 0x7bc2e
leaq 0x4b283(%rip), %rdi     # 0xc6b66
leaq 0x4b186(%rip), %rsi     # 0xc6a70
leaq 0x4b292(%rip), %rcx     # 0xc6b83
movl $0x16a, %edx            # imm = 0x16A
xorl %eax, %eax
callq 0x60031
movq 0x18(%r15), %rdx
testq %rdx, %rdx
jne 0x7b90f
movq 0x20(%r15), %rax
movq 0x10(%r14,%rax), %rdx
cmpq %rbx, %rdx
jae 0x7b929
addq %rdx, %rdx
cmpq %rbx, %rdx
cmovbeq %rbx, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x7b3ea
movq 0x10(%r15), %r8
leaq -0x6(%r8), %rax
cmpq $0x4, %rax
ja 0x7bab2
addq 0x20(%r15), %r14
leaq 0x4af7a(%rip), %rcx     # 0xc68c0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
jbe 0x7bb04
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movq 0x28(%r15), %r15
movq 0x18(%r15), %r12
xorl %esi, %esi
jmp 0x7bb43
movq 0x8(%r14), %rdi
cmpq 0x10(%r14), %rdi
ja 0x7b9f0
cmpq %rbx, %rdi
jbe 0x7b9f9
cmpq %rbx, %rdi
jbe 0x7bc2e
leaq -0x1(%rdi), %rax
movq %rax, 0x8(%r14)
movq (%r14), %rax
movq -0x8(%rax,%rdi,8), %rdi
callq 0x7b894
movq 0x8(%r14), %rdi
jmp 0x7b982
movq 0x8(%r14), %rdi
cmpq 0x10(%r14), %rdi
ja 0x7b9f0
cmpq %rbx, %rdi
jbe 0x7b9f9
movq 0x28(%r15), %r15
cmpq %rbx, %rdi
jbe 0x7bc2e
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
jmp 0x7b9b9
movq 0x8(%r14), %rdi
cmpq 0x10(%r14), %rdi
jbe 0x7bae1
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
xorl %edi, %edi
movq %rbx, %rsi
subq %rdi, %rsi
jbe 0x7bc2e
shlq $0x3, %rdi
addq (%r14), %rdi
shlq $0x3, %rsi
callq 0x6028b
movq %rbx, 0x8(%r14)
jmp 0x7bc2e
movq 0x8(%r14), %rax
cmpq 0x10(%r14), %rax
jbe 0x7ba31
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
xorl %eax, %eax
cmpq %rbx, %rax
je 0x7bc2e
movq 0x18(%r14), %rdi
testq %rdi, %rdi
jne 0x7ba68
leaq 0x4b11c(%rip), %rdi     # 0xc6b66
leaq 0x4b01f(%rip), %rsi     # 0xc6a70
leaq 0x4b153(%rip), %rcx     # 0xc6bab
movl $0x1ac, %edx            # imm = 0x1AC
xorl %eax, %eax
callq 0x60031
movq 0x18(%r14), %rdi
callq 0x7b39a
movq %rax, %r15
movq 0x18(%rax), %r12
movq 0x8(%r14), %rsi
movq %rbx, %rax
subq %rsi, %rax
jae 0x7bb7d
cmpb $0x0, 0x2a(%r15)
jne 0x7ba15
cmpq %rbx, %rsi
jbe 0x7bc2e
decq %rsi
movq %rsi, 0x8(%r14)
imulq %r12, %rsi
addq (%r14), %rsi
movq %r15, %rdi
callq *0x88(%r15)
movq 0x8(%r14), %rsi
jmp 0x7ba8b
leaq 0x4b0ad(%rip), %rdi     # 0xc6b66
leaq 0x4afb0(%rip), %rsi     # 0xc6a70
leaq 0x4afea(%rip), %rcx     # 0xc6ab1
movl $0x21f, %edx            # imm = 0x21F
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x60031
cmpq %rbx, %rdi
jbe 0x7b9f9
movq 0x18(%r14), %rdi
testq %rdi, %rdi
je 0x7bbc7
callq 0x7b39a
movq %rax, %r15
jmp 0x7bbca
movq 0x28(%r15), %r15
movq 0x18(%r15), %r12
cmpq %rbx, %rsi
jbe 0x7bb43
cmpb $0x0, 0x2a(%r15)
jne 0x7ba15
cmpq %rbx, %rsi
jbe 0x7bc2e
decq %rsi
movq %rsi, 0x8(%r14)
imulq %r12, %rsi
addq (%r14), %rsi
movq %r15, %rdi
callq *0x88(%r15)
movq 0x8(%r14), %rsi
jmp 0x7bb1c
movq %rbx, %rax
subq %rsi, %rax
jbe 0x7bc2e
cmpb $0x0, 0x29(%r15)
jne 0x7bb8a
cmpq %rbx, %rsi
jae 0x7bc2e
imulq %r12, %rsi
addq (%r14), %rsi
movq %r15, %rdi
callq *0x80(%r15)
movq 0x8(%r14), %rsi
incq %rsi
movq %rsi, 0x8(%r14)
jmp 0x7bb56
jbe 0x7bc2e
cmpb $0x0, 0x29(%r15)
je 0x7bba0
imulq %r12, %rsi
addq (%r14), %rsi
imulq %r12, %rax
movq %rsi, %rdi
movq %rax, %rsi
jmp 0x7ba10
cmpq %rbx, %rsi
jae 0x7bc2e
imulq %r12, %rsi
addq (%r14), %rsi
movq %r15, %rdi
callq *0x80(%r15)
movq 0x8(%r14), %rsi
incq %rsi
movq %rsi, 0x8(%r14)
jmp 0x7bba0
xorl %r15d, %r15d
leaq 0x4af95(%rip), %r12     # 0xc6b66
leaq 0x4ae98(%rip), %r13     # 0xc6a70
leaq 0x4aff6(%rip), %rbp     # 0xc6bd5
movq 0x8(%r14), %rax
cmpq %rbx, %rax
jbe 0x7bc2e
leaq -0x1(%rax), %rcx
movq %rcx, 0x8(%r14)
movq (%r14), %rcx
movq -0x8(%rcx,%rax,8), %rsi
testq %rsi, %rsi
je 0x7bbdf
testq %r15, %r15
jne 0x7bc22
movq %r12, %rdi
movq %r13, %rsi
movl $0x1fa, %edx            # imm = 0x1FA
movq %rbp, %rcx
xorl %eax, %eax
callq 0x60031
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq (%rax,%rcx,8), %rsi
movq %r15, %rdi
callq *0xb8(%r15)
jmp 0x7bbdf
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq