// File: /workspace/github_c_makefile_star2_O1/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 0xf6a1f
cmpq %rbx, %rax
je 0xf6f18
leaq 0x5d271(%rip), %rdi     # 0x153c76
leaq 0x5d174(%rip), %rsi     # 0x153b80
leaq 0x5d280(%rip), %rcx     # 0x153c93
movl $0x16a, %edx            # imm = 0x16A
xorl %eax, %eax
callq 0xc80f6
movq 0x18(%r15), %rdx
testq %rdx, %rdx
jne 0xf6a31
movq 0x20(%r15), %rax
movq 0x10(%r14,%rax), %rdx
cmpq %rbx, %rdx
jae 0xf6a4b
addq %rdx, %rdx
cmpq %rbx, %rdx
cmovbeq %rbx, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0xf6359
movq 0x10(%r15), %r8
leaq -0x6(%r8), %rax
cmpq $0x4, %rax
ja 0xf6e24
addq 0x20(%r15), %r14
leaq 0x5cf68(%rip), %rcx     # 0x1539d0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%r14), %rax
cmpq 0x10(%r14), %rax
jbe 0xf6a82
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movq 0x28(%r15), %r15
movq 0x18(%r15), %r12
movq 0x8(%r14), %rdi
movq %rbx, %rsi
subq %rdi, %rsi
jae 0xf6e53
cmpb $0x0, 0x2a(%r15)
jne 0xf6cfd
movq 0x8(%r14), %rsi
cmpq %rbx, %rsi
jbe 0xf6f18
decq %rsi
movq %rsi, 0x8(%r14)
imulq %r12, %rsi
addq (%r14), %rsi
movq %r15, %rdi
callq *0x88(%r15)
jmp 0xf6aa5
movq 0x8(%r14), %rax
cmpq 0x10(%r14), %rax
jbe 0xf6add
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movq 0x8(%r14), %rdi
movq %rbx, %rsi
subq %rdi, %rsi
jae 0xf6ce7
movq 0x8(%r14), %rax
cmpq %rbx, %rax
jbe 0xf6f18
leaq 0xb2967(%rip), %r12     # 0x1a9468
leaq -0x1(%rax), %rcx
movq %rcx, 0x8(%r14)
movq (%r14), %rcx
movq -0x8(%rcx,%rax,8), %r13
testq %r13, %r13
je 0xf6bae
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 0xf6b75
movq 0x10(%rax), %rdx
movl $0x18, %ecx
cmpq %rbp, %rdx
je 0xf6b75
movq 0x20(%rax), %rdx
movl $0x28, %ecx
cmpq %rbp, %rdx
je 0xf6b75
movq 0x30(%rax), %rdx
movl $0x38, %ecx
cmpq %rbp, %rdx
jne 0xf6bc0
movq (%rax,%rcx), %r15
testq %r15, %r15
jne 0xf6ba1
movabsq $0x782b96602f424981, %rdi # imm = 0x782B96602F424981
movq %rbp, %rsi
callq 0xf3f85
movq %rax, %r15
movq %r12, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x12128c
movq %r15, %rdi
movq %r13, %rsi
callq *0xb8(%r15)
movq 0x8(%r14), %rax
cmpq %rbx, %rax
ja 0xf6b01
jmp 0xf6f18
xorl %r15d, %r15d
jmp 0xf6b79
movq 0x8(%r14), %rax
cmpq 0x10(%r14), %rax
jbe 0xf6bd6
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movq 0x8(%r14), %rdi
movq %rbx, %rsi
subq %rdi, %rsi
jae 0xf6ce7
movq 0x8(%r14), %rax
cmpq %rbx, %rax
jbe 0xf6f18
movq 0x28(%r15), %r15
leaq -0x1(%rax), %rcx
movq %rcx, 0x8(%r14)
movq (%r14), %rcx
movq -0x8(%rcx,%rax,8), %rsi
movq %r15, %rdi
callq *0xb8(%r15)
movq 0x8(%r14), %rax
cmpq %rbx, %rax
ja 0xf6bf7
jmp 0xf6f18
movq 0x8(%r14), %rax
cmpq 0x10(%r14), %rax
jbe 0xf6c30
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movq 0x8(%r14), %rdi
movq %rbx, %rsi
subq %rdi, %rsi
jae 0xf6ce7
movq 0x18(%r14), %r12
testq %r12, %r12
je 0xf6eac
leaq 0xb2814(%rip), %rax     # 0x1a9468
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 0xf6cae
movq 0x10(%rax), %rdx
movl $0x18, %ecx
cmpq %r12, %rdx
je 0xf6cae
movq 0x20(%rax), %rdx
movl $0x28, %ecx
cmpq %r12, %rdx
je 0xf6cae
movq 0x30(%rax), %rdx
movl $0x38, %ecx
cmpq %r12, %rdx
jne 0xf6f56
movq (%rax,%rcx), %r15
testq %r15, %r15
jne 0xf6eaf
movabsq $0x782b96602f424981, %rdi # imm = 0x782B96602F424981
movq %r12, %rsi
callq 0xf3f85
movq %rax, %r15
leaq 0xb2791(%rip), %rdi     # 0x1a9468
movq %r12, %rsi
movq %rax, %rdx
callq 0x12128c
jmp 0xf6eaf
jbe 0xf6f18
shlq $0x3, %rdi
addq (%r14), %rdi
shlq $0x3, %rsi
callq 0xc8366
movq %rbx, 0x8(%r14)
jmp 0xf6f18
movq 0x8(%r14), %rax
cmpq 0x10(%r14), %rax
jbe 0xf6d17
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
cmpq %rbx, 0x8(%r14)
je 0xf6f18
cmpq $0x0, 0x18(%r14)
jne 0xf6d49
leaq 0x5cf47(%rip), %rdi     # 0x153c76
leaq 0x5ce4a(%rip), %rsi     # 0x153b80
leaq 0x5cf7e(%rip), %rcx     # 0x153cbb
movl $0x1ac, %edx            # imm = 0x1AC
xorl %eax, %eax
callq 0xc80f6
movq 0x18(%r14), %r12
leaq 0xb2714(%rip), %rax     # 0x1a9468
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 0xf6dae
movq 0x10(%rax), %rdx
movl $0x18, %ecx
cmpq %r12, %rdx
je 0xf6dae
movq 0x20(%rax), %rdx
movl $0x28, %ecx
cmpq %r12, %rdx
je 0xf6dae
movq 0x30(%rax), %rdx
movl $0x38, %ecx
cmpq %r12, %rdx
jne 0xf6f4e
movq (%rax,%rcx), %r15
testq %r15, %r15
jne 0xf6dde
movabsq $0x782b96602f424981, %rdi # imm = 0x782B96602F424981
movq %r12, %rsi
callq 0xf3f85
movq %rax, %r15
leaq 0xb2695(%rip), %rdi     # 0x1a9468
movq %r12, %rsi
movq %rax, %rdx
callq 0x12128c
movq 0x18(%r15), %r12
movq 0x8(%r14), %rdi
movq %rbx, %rsi
subq %rdi, %rsi
jae 0xf6e8b
cmpb $0x0, 0x2a(%r15)
jne 0xf6cfd
movq 0x8(%r14), %rsi
cmpq %rbx, %rsi
jbe 0xf6f18
decq %rsi
movq %rsi, 0x8(%r14)
imulq %r12, %rsi
addq (%r14), %rsi
movq %r15, %rdi
callq *0x88(%r15)
jmp 0xf6dfd
leaq 0x5ce4b(%rip), %rdi     # 0x153c76
leaq 0x5cd4e(%rip), %rsi     # 0x153b80
leaq 0x5cd88(%rip), %rcx     # 0x153bc1
movl $0x21f, %edx            # imm = 0x21F
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xc80f6
jbe 0xf6f18
cmpb $0x0, 0x29(%r15)
jne 0xf6e9c
movq 0x8(%r14), %rsi
cmpq %rbx, %rsi
jae 0xf6f18
imulq %r12, %rsi
addq (%r14), %rsi
movq %r15, %rdi
callq *0x80(%r15)
movq 0x8(%r14), %rsi
incq %rsi
movq %rsi, 0x8(%r14)
jmp 0xf6e64
jbe 0xf6f18
cmpb $0x0, 0x29(%r15)
je 0xf6f27
imulq %r12, %rdi
addq (%r14), %rdi
imulq %r12, %rsi
jmp 0xf6cf8
xorl %r15d, %r15d
movq 0x8(%r14), %rax
cmpq %rbx, %rax
jbe 0xf6f18
leaq 0x5cdb7(%rip), %r12     # 0x153c76
leaq 0x5ccba(%rip), %r13     # 0x153b80
leaq 0x5ce18(%rip), %rbp     # 0x153ce5
leaq -0x1(%rax), %rcx
movq %rcx, 0x8(%r14)
movq (%r14), %rcx
cmpq $0x0, -0x8(%rcx,%rax,8)
je 0xf6f0f
testq %r15, %r15
jne 0xf6efa
movq %r12, %rdi
movq %r13, %rsi
movl $0x1fa, %edx            # imm = 0x1FA
movq %rbp, %rcx
xorl %eax, %eax
callq 0xc80f6
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq (%rax,%rcx,8), %rsi
movq %r15, %rdi
callq *0xb8(%r15)
movq 0x8(%r14), %rax
cmpq %rbx, %rax
ja 0xf6ecd
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r14), %rsi
cmpq %rbx, %rsi
jae 0xf6f18
imulq %r12, %rsi
addq (%r14), %rsi
movq %r15, %rdi
callq *0x80(%r15)
movq 0x8(%r14), %rsi
incq %rsi
movq %rsi, 0x8(%r14)
jmp 0xf6f2b
xorl %r15d, %r15d
jmp 0xf6db2
xorl %r15d, %r15d
jmp 0xf6cb2