// File: /workspace/github_c_makefile_star2_O3/johsteffens[P]beth/lib/bcore/bcore_spect_array.c
// Function: bcore_array_default_reorder
void bcore_array_default_reorder( const bcore_array_s* p, bcore_array* o, const bcore_arr_uz_s* index_arr )
{
    uz_t arr_size = bcore_array_p_get_size( p, o );
    bl_t is_fixed = bcore_array_p_is_fixed( p );

    if( is_fixed )
    {
        if( arr_size != index_arr->size ) ERR( "Cannot reorder a fixed-size-array to a different index_arr size." );
    }

    if( arr_size > get_space( p, o ) ) bcore_array_p_make_strong( p, o );

    if( bcore_array_p_is_of_links( p ) )
    {
        vd_t* data = bcore_array_p_get_d_data( p, o );
        uz_t buf_space = 0;
        vd_t* buf = bcore_un_alloc( sizeof( vd_t ), NULL, 0, index_arr->size, &buf_space );

        for( uz_t i = 0; i < index_arr->size; i++ )
        {
            ASSERT( index_arr->data[ i ] < arr_size );
            buf[ i ] = bcore_fork( data[ index_arr->data[ i ] ] );
        }

        if( bcore_array_p_is_mono_typed( p ) )
        {
            tp_t mono_type = bcore_array_p_get_mono_type( p, o );
            const bcore_inst_s* inst = bcore_inst_s_get_typed( mono_type );
            for( uz_t i = 0; i < arr_size; i++ )
            {
                bcore_release_arg( ( fp_t )bcore_inst_p_down, inst, data[ i ] );
                data[ i ] = NULL;
            }
        }
        else if( bcore_array_p_is_of_aware( p ) )
        {
            for( uz_t i = 0; i < arr_size; i++ )
            {
                bcore_release_obj( ( fp_t )bcore_inst_a_down, data[ i ] );
                data[ i ] = NULL;
            }
        }
        else
        {
            // not reachable as of 2017-10-12
            ERR( "Unhandled array architecture" );
        }

        bcore_array_p_set_size( p, o, index_arr->size );
        data = bcore_array_p_get_d_data( p, o );
        for( uz_t i = 0; i < index_arr->size; i++ ) data[ i ] = buf[ i ];

        bcore_un_alloc( sizeof( vd_t ), buf, buf_space, 0, NULL );
    }
    else
    {
        // nested elements are always mono-typed
        tp_t mono_type = bcore_array_p_get_mono_type( p, o );
        const bcore_inst_s* inst = bcore_inst_s_get_typed( mono_type );
        if( inst->init_flat && inst->copy_flat && inst->down_flat ) // all flat
        {
            uz_t buf_space = 0;
            vd_t buf = bcore_u_alloc( inst->size, NULL, index_arr->size, &buf_space );
            vd_t data = bcore_array_p_get_d_data( p, o );
            vd_t dst = buf;
            for( uz_t i = 0; i < index_arr->size; i++ )
            {
                ASSERT( index_arr->data[ i ] < arr_size );
                bcore_memcpy( dst, ( u0_t* )data + index_arr->data[ i ] * inst->size, inst->size );
                dst = ( u0_t* )dst + inst->size;
            }
            bcore_array_p_set_size( p, o, index_arr->size );
            bcore_u_memcpy( inst->size, bcore_array_p_get_d_data( p, o ), buf, index_arr->size );
            bcore_un_alloc( inst->size, buf, buf_space, 0, NULL );
        }
        else
        {
            uz_t buf_space = 0;
            vd_t buf = bcore_u_alloc( inst->size, NULL, index_arr->size, &buf_space );
            vd_t data = bcore_array_p_get_d_data( p, o );
            for( uz_t i = 0; i < index_arr->size; i++ )
            {
                ASSERT( index_arr->data[ i ] < arr_size );
                vd_t dst = ( u0_t* )buf  + i * inst->size;
                vc_t src = ( u0_t* )data + index_arr->data[ i ] * inst->size;
                bcore_inst_p_init( inst, dst );
                bcore_inst_p_copy( inst, dst, src );
            }
            if( is_fixed )
            {
                for( uz_t i = 0; i < index_arr->size; i++ )
                {
                    vd_t src = ( u0_t* )buf + i * inst->size;
                    bcore_array_p_set( p, o, i, sr_pwc( inst, src ) );
                    bcore_inst_p_down( inst, src );
                }
            }
            else
            {
                bcore_array_p_set_size( p, o, 0 );
                for( uz_t i = 0; i < index_arr->size; i++ )
                {
                    vd_t src = ( u0_t* )buf + i * inst->size;
                    bcore_array_p_push( p, o, sr_pwc( inst, src ) );
                    bcore_inst_p_down( inst, src );
                }
            }
            bcore_un_alloc( inst->size, buf, buf_space, 0, NULL );
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %rbp
movq 0x18(%rdi), %r14
testq %r14, %r14
je 0xef334
movq %r14, %r15
cmpq 0x10(%rbx), %r14
je 0xef356
leaq 0x59d02(%rip), %rdi     # 0x14900e
leaq 0x59895(%rip), %rsi     # 0x148ba8
leaq 0x59d10(%rip), %rcx     # 0x14902a
movl $0x851, %edx            # imm = 0x851
xorl %eax, %eax
callq 0xbe592
movq 0x18(%rbp), %rax
movq %r14, %r15
testq %rax, %rax
jne 0xef346
jmp 0xef33d
movq 0x20(%rbp), %rax
movq 0x8(%r13,%rax), %r15
movq 0x20(%rbp), %rax
movq 0x10(%r13,%rax), %rax
cmpq %rax, %r15
jbe 0xef356
movq %rbp, %rdi
movq %r13, %rsi
callq 0xeba34
cmpb $0x1, 0x42(%rbp)
movq %rbp, 0x30(%rsp)
jne 0xef4db
movq %r15, %r14
movq %rbp, %rdi
movq %r13, 0x28(%rsp)
movq %r13, %rsi
callq 0xece27
movq %rax, %r13
leaq 0x40(%rsp), %r8
movq $0x0, (%r8)
movq 0x10(%rbx), %rcx
movl $0x8, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0xbe7da
movq %rax, %r12
cmpq $0x0, 0x10(%rbx)
je 0xef3fd
leaq 0x597fe(%rip), %r15     # 0x148ba8
xorl %ebp, %ebp
movq 0x8(%rbx), %rax
movq (%rax,%rbp,8), %rax
cmpq %r14, %rax
jb 0xef3e6
leaq 0x3e287(%rip), %rdi     # 0x12d647
leaq 0x59ca4(%rip), %rsi     # 0x14906b
leaq 0x59c40(%rip), %rdx     # 0x14900e
movq %r15, %rcx
movl $0x85e, %r8d            # imm = 0x85E
xorl %eax, %eax
callq 0xbe4ef
movq 0x8(%rbx), %rax
movq (%rax,%rbp,8), %rax
movq (%r13,%rax,8), %rdi
callq 0xbe7e4
movq %rax, (%r12,%rbp,8)
incq %rbp
cmpq 0x10(%rbx), %rbp
jb 0xef3ac
movq 0x30(%rsp), %rdi
cmpb $0x0, 0x43(%rdi)
je 0xef7fe
movq 0x28(%rsp), %rsi
callq 0xee1d0
movq %rax, %rbp
leaq 0xad048(%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 %rbp, %rcx
shlq $0x4, %rcx
leaq (%rdx,%rcx), %rax
movq (%rdx,%rcx), %rdx
movl $0x8, %ecx
cmpq %rbp, %rdx
je 0xef476
movq 0x10(%rax), %rdx
movl $0x18, %ecx
cmpq %rbp, %rdx
je 0xef476
movq 0x20(%rax), %rdx
movl $0x28, %ecx
cmpq %rbp, %rdx
je 0xef476
movq 0x30(%rax), %rdx
movl $0x38, %ecx
cmpq %rbp, %rdx
jne 0xef47f
movq (%rax,%rcx), %r15
testq %r15, %r15
jne 0xef4a6
movabsq $0x782b96602f424981, %rdi # imm = 0x782B96602F424981
movq %rbp, %rsi
callq 0xe9bc5
movq %rax, %r15
leaq 0xacfcd(%rip), %rdi     # 0x19c468
movq %rbp, %rsi
movq %rax, %rdx
callq 0x115fac
testq %r14, %r14
je 0xef909
xorl %ebp, %ebp
movq (%r13,%rbp,8), %rdx
leaq 0x4b0(%rip), %rdi       # 0xef96d
movq %r15, %rsi
callq 0xbe7f3
movq $0x0, (%r13,%rbp,8)
incq %rbp
cmpq %rbp, %r14
jne 0xef4b1
jmp 0xef909
movq %r14, 0x58(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
callq 0xee1d0
movq %rax, %r14
leaq 0xacf73(%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 %r14, %rcx
shlq $0x4, %rcx
leaq (%rdx,%rcx), %rax
movq (%rdx,%rcx), %rdx
movl $0x8, %ecx
cmpq %r14, %rdx
je 0xef54b
movq 0x10(%rax), %rdx
movl $0x18, %ecx
cmpq %r14, %rdx
je 0xef54b
movq 0x20(%rax), %rdx
movl $0x28, %ecx
cmpq %r14, %rdx
je 0xef54b
movq 0x30(%rax), %rdx
movl $0x38, %ecx
cmpq %r14, %rdx
jne 0xef554
movq (%rax,%rcx), %r12
testq %r12, %r12
jne 0xef57b
movabsq $0x782b96602f424981, %rdi # imm = 0x782B96602F424981
movq %r14, %rsi
callq 0xe9bc5
movq %rax, %r12
leaq 0xacef8(%rip), %rdi     # 0x19c468
movq %r14, %rsi
movq %rax, %rdx
callq 0x115fac
cmpb $0x1, 0x29(%r12)
jne 0xef6a0
cmpb $0x1, 0x2b(%r12)
jne 0xef6a0
cmpb $0x1, 0x2a(%r12)
jne 0xef6a0
movq %r15, 0x20(%rsp)
leaq 0x40(%rsp), %rcx
movq $0x0, (%rcx)
movq 0x18(%r12), %rdi
movq 0x10(%rbx), %rdx
xorl %r15d, %r15d
xorl %esi, %esi
callq 0xbe7d5
movq %rax, 0x38(%rsp)
movq %rbp, %rdi
movq %r13, 0x28(%rsp)
movq %r13, %rsi
callq 0xece27
cmpq $0x0, 0x10(%rbx)
je 0xef64f
movq %rax, %r14
movq 0x38(%rsp), %rbp
xorl %r13d, %r13d
movq 0x8(%rbx), %rax
movq (%rax,%r13,8), %rsi
cmpq 0x20(%rsp), %rsi
jb 0xef62a
leaq 0x3e047(%rip), %rdi     # 0x12d647
leaq 0x59a64(%rip), %rsi     # 0x14906b
leaq 0x59a00(%rip), %rdx     # 0x14900e
leaq 0x59593(%rip), %rcx     # 0x148ba8
movl $0x88d, %r8d            # imm = 0x88D
xorl %eax, %eax
callq 0xbe4ef
movq 0x8(%rbx), %rax
movq (%rax,%r13,8), %rsi
movq 0x18(%r12), %rdx
imulq %rdx, %rsi
addq %r14, %rsi
movq %rbp, %rdi
callq 0xbe87d
addq 0x18(%r12), %rbp
incq %r13
movq 0x10(%rbx), %r15
cmpq %r15, %r13
jb 0xef5ea
movq 0x30(%rsp), %rbp
movq %rbp, %rdi
movq 0x28(%rsp), %r13
movq %r13, %rsi
movq %r15, %rdx
callq 0xec548
movq 0x18(%r12), %r14
movq %rbp, %rdi
movq %r13, %rsi
callq 0xece27
movq 0x10(%rbx), %rcx
movq %r14, %rdi
movq %rax, %rsi
movq 0x38(%rsp), %rbx
movq %rbx, %rdx
callq 0xbe8bb
movq 0x18(%r12), %rdi
movq 0x40(%rsp), %rdx
movq %rbx, %rsi
jmp 0xef954
leaq 0x60(%rsp), %rcx
movq $0x0, (%rcx)
movq 0x18(%r12), %rdi
movq 0x10(%rbx), %rdx
xorl %esi, %esi
callq 0xbe7d5
movq %rax, 0x20(%rsp)
movq %rbp, %rdi
movq %r13, %rsi
callq 0xece27
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x10(%rbx)
je 0xef841
movq %r15, %rbp
movq %r13, 0x28(%rsp)
xorl %r13d, %r13d
movq 0x20(%rsp), %rsi
movq 0x8(%rbx), %rax
movq (%rax,%r13,8), %r15
cmpq %rbp, %r15
jb 0xef72f
leaq 0x3df47(%rip), %rdi     # 0x12d647
leaq 0x59964(%rip), %rsi     # 0x14906b
leaq 0x59900(%rip), %rdx     # 0x14900e
leaq 0x59493(%rip), %rcx     # 0x148ba8
movl $0x89c, %r8d            # imm = 0x89C
xorl %eax, %eax
callq 0xbe4ef
movq 0x20(%rsp), %rsi
movq 0x8(%rbx), %rax
movq (%rax,%r13,8), %r15
movq 0x18(%r12), %r14
imulq %r14, %r15
imulq %r13, %r14
addq %rsi, %r14
addq 0x38(%rsp), %r15
movq %r12, %rdi
movq %r14, %rsi
callq *0x80(%r12)
cmpq %r15, %r14
je 0xef768
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq *0x90(%r12)
incq %r13
movq 0x10(%rbx), %rax
cmpq %rax, %r13
movq 0x20(%rsp), %rsi
jb 0xef6ec
cmpq $0x0, 0x58(%rsp)
movq 0x28(%rsp), %r13
movq 0x30(%rsp), %rbp
je 0xef857
testq %rax, %rax
je 0xef8dc
xorl %r14d, %r14d
movq 0x18(%r12), %r15
imulq %r14, %r15
addq %rsi, %r15
movq %r15, 0x40(%rsp)
movq %r12, 0x48(%rsp)
movq $0x1, 0x50(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x40(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %rbp, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq *0x38(%rbp)
movq %r12, %rdi
movq %r15, %rsi
callq *0x88(%r12)
movq 0x20(%rsp), %rsi
incq %r14
cmpq 0x10(%rbx), %r14
jb 0xef79f
jmp 0xef8dc
cmpb $0x1, 0x41(%rdi)
jne 0xef8e8
testq %r14, %r14
je 0xef909
movq %r14, %rbp
xorl %r15d, %r15d
leaq 0x158(%rip), %r14       # 0xef976
movq (%r13,%r15,8), %rsi
movq %r14, %rdi
callq 0xbe7ee
movq $0x0, (%r13,%r15,8)
incq %r15
cmpq %r15, %rbp
jne 0xef81e
jmp 0xef909
cmpq $0x0, 0x58(%rsp)
movq 0x30(%rsp), %rbp
movq 0x20(%rsp), %rsi
jne 0xef8dc
movq %rbp, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0xec548
movq 0x20(%rsp), %rsi
cmpq $0x0, 0x10(%rbx)
je 0xef8dc
xorl %r15d, %r15d
movq 0x18(%r12), %r14
imulq %r15, %r14
addq %rsi, %r14
movq %r14, 0x40(%rsp)
movq %r12, 0x48(%rsp)
movq $0x1, 0x50(%rsp)
movq 0x18(%rbp), %rdx
testq %rdx, %rdx
jne 0xef8a4
movq 0x20(%rbp), %rax
movq 0x8(%r13,%rax), %rdx
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x40(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %rbp, %rdi
movq %r13, %rsi
callq *0x38(%rbp)
movq %r12, %rdi
movq %r14, %rsi
callq *0x88(%r12)
incq %r15
cmpq 0x10(%rbx), %r15
movq 0x20(%rsp), %rsi
jb 0xef873
movq 0x18(%r12), %rdi
movq 0x60(%rsp), %rdx
jmp 0xef954
leaq 0x5971f(%rip), %rdi     # 0x14900e
leaq 0x592b2(%rip), %rsi     # 0x148ba8
leaq 0x5978e(%rip), %rcx     # 0x14908b
movl $0x877, %edx            # imm = 0x877
xorl %eax, %eax
callq 0xbe592
movq 0x10(%rbx), %rdx
movq 0x30(%rsp), %r15
movq %r15, %rdi
movq 0x28(%rsp), %r14
movq %r14, %rsi
callq 0xec548
movq %r15, %rdi
movq %r14, %rsi
callq 0xece27
cmpq $0x0, 0x10(%rbx)
je 0xef947
xorl %ecx, %ecx
movq (%r12,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
incq %rcx
cmpq 0x10(%rbx), %rcx
jb 0xef936
movq 0x40(%rsp), %rdx
movl $0x8, %edi
movq %r12, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0xbe7da
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq