// File: /workspace/github_c_makefile_star2_O2/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, %r14
movq %rdi, %r15
movq 0x18(%rdi), %r13
testq %r13, %r13
je 0x7daca
movq %r13, 0x20(%rsp)
cmpq 0x10(%rbx), %r13
je 0x7daf3
leaq 0x49436(%rip), %rdi     # 0xc6ed6
leaq 0x48fc9(%rip), %rsi     # 0xc6a70
leaq 0x49444(%rip), %rcx     # 0xc6ef2
movl $0x851, %edx            # imm = 0x851
xorl %eax, %eax
callq 0x60031
movq 0x18(%r15), %rax
movq %r13, 0x20(%rsp)
testq %rax, %rax
jne 0x7dae1
jmp 0x7dad8
movq 0x20(%r15), %rax
movq 0x8(%r14,%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x20(%r15), %rax
movq 0x10(%r14,%rax), %rax
cmpq %rax, 0x20(%rsp)
jbe 0x7daf3
movq %r15, %rdi
movq %r14, %rsi
callq 0x7b108
cmpb $0x1, 0x42(%r15)
movq %r15, 0x38(%rsp)
movq %r14, 0x30(%rsp)
jne 0x7dbe8
movq %r15, %rdi
movq %r14, %rsi
callq 0x7bf18
movq %rax, %r13
leaq 0x40(%rsp), %r8
andq $0x0, (%r8)
movq 0x10(%rbx), %rcx
pushq $0x8
popq %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x60263
movq %rax, %r12
leaq 0x4939d(%rip), %r14     # 0xc6ed6
leaq 0x48f30(%rip), %rbp     # 0xc6a70
xorl %r15d, %r15d
cmpq 0x10(%rbx), %r15
jae 0x7db94
movq 0x8(%rbx), %rax
movq (%rax,%r15,8), %rax
cmpq 0x20(%rsp), %rax
jb 0x7db81
leaq 0x2cae8(%rip), %rdi     # 0xaa647
leaq 0x493cd(%rip), %rsi     # 0xc6f33
movq %r14, %rdx
movq %rbp, %rcx
movl $0x85e, %r8d            # imm = 0x85E
xorl %eax, %eax
callq 0x5ffac
movq 0x8(%rbx), %rax
movq (%rax,%r15,8), %rax
movq (%r13,%rax,8), %rdi
callq 0x6026d
movq %rax, (%r12,%r15,8)
incq %r15
jmp 0x7db43
movq 0x38(%rsp), %rdi
cmpb $0x0, 0x43(%rdi)
je 0x7ddfb
movq 0x30(%rsp), %rsi
callq 0x7ccc9
movq %rax, %rdi
callq 0x7b39a
movq %rax, %r14
xorl %r15d, %r15d
leaq 0x3cc(%rip), %rbp       # 0x7df8e
cmpq %r15, 0x20(%rsp)
je 0x7dee6
movq (%r13,%r15,8), %rdx
movq %rbp, %rdi
movq %r14, %rsi
callq 0x6027c
andq $0x0, (%r13,%r15,8)
incq %r15
jmp 0x7dbc2
movq %r15, %rdi
movq %r14, %rsi
callq 0x7ccc9
movq %rax, %rdi
callq 0x7b39a
movq %rax, %r12
cmpb $0x1, 0x29(%rax)
jne 0x7dcc0
cmpb $0x1, 0x2b(%r12)
jne 0x7dcc0
cmpb $0x1, 0x2a(%r12)
jne 0x7dcc0
leaq 0x40(%rsp), %rcx
andq $0x0, (%rcx)
movq 0x18(%r12), %rdi
movq 0x10(%rbx), %rdx
xorl %esi, %esi
callq 0x6025e
movq %rax, %rbp
movq %r15, %rdi
movq %r14, %rsi
callq 0x7bf18
movq %rax, %r14
leaq 0x48e1f(%rip), %r13     # 0xc6a70
xorl %r15d, %r15d
movq %rbp, 0x28(%rsp)
movq 0x10(%rbx), %rdx
cmpq %rdx, %r15
jae 0x7df43
movq 0x8(%rbx), %rax
movq (%rax,%r15,8), %rsi
cmpq 0x20(%rsp), %rsi
jb 0x7dca2
leaq 0x2c9cb(%rip), %rdi     # 0xaa647
leaq 0x492b0(%rip), %rsi     # 0xc6f33
leaq 0x4924c(%rip), %rdx     # 0xc6ed6
movq %r13, %rcx
movl $0x88d, %r8d            # imm = 0x88D
xorl %eax, %eax
callq 0x5ffac
movq 0x8(%rbx), %rax
movq (%rax,%r15,8), %rsi
movq 0x18(%r12), %rdx
imulq %rdx, %rsi
addq %r14, %rsi
movq %rbp, %rdi
callq 0x60300
addq 0x18(%r12), %rbp
incq %r15
jmp 0x7dc59
movq %r13, 0x58(%rsp)
leaq 0x60(%rsp), %rcx
andq $0x0, (%rcx)
movq 0x18(%r12), %rdi
movq 0x10(%rbx), %rdx
xorl %esi, %esi
callq 0x6025e
movq %rax, 0x28(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x7bf18
movq %rax, %rbp
xorl %r13d, %r13d
movq 0x10(%rbx), %rax
cmpq %rax, %r13
jae 0x7dd82
movq 0x8(%rbx), %rax
movq (%rax,%r13,8), %r14
cmpq 0x20(%rsp), %r14
jb 0x7dd41
leaq 0x2c930(%rip), %rdi     # 0xaa647
leaq 0x49215(%rip), %rsi     # 0xc6f33
leaq 0x491b1(%rip), %rdx     # 0xc6ed6
leaq 0x48d44(%rip), %rcx     # 0xc6a70
movl $0x89c, %r8d            # imm = 0x89C
xorl %eax, %eax
callq 0x5ffac
movq 0x8(%rbx), %rax
movq (%rax,%r13,8), %r14
movq 0x18(%r12), %r15
imulq %r15, %r14
imulq %r13, %r15
addq 0x28(%rsp), %r15
addq %rbp, %r14
movq %r12, %rdi
movq %r15, %rsi
callq *0x80(%r12)
cmpq %r14, %r15
je 0x7dd7a
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq *0x90(%r12)
incq %r13
jmp 0x7dcf4
cmpq $0x0, 0x58(%rsp)
je 0x7de32
xorl %r14d, %r14d
movq 0x30(%rsp), %r13
movq 0x38(%rsp), %rbp
cmpq %rax, %r14
jae 0x7deb4
movq 0x18(%r12), %r15
imulq %r14, %r15
addq 0x28(%rsp), %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)
incq %r14
movq 0x10(%rbx), %rax
jmp 0x7dd9b
cmpb $0x1, 0x41(%rdi)
jne 0x7dec5
xorl %r15d, %r15d
leaq 0x188(%rip), %r14       # 0x7df97
cmpq %r15, 0x20(%rsp)
je 0x7dee6
movq (%r13,%r15,8), %rsi
movq %r14, %rdi
callq 0x60277
andq $0x0, (%r13,%r15,8)
incq %r15
jmp 0x7de0f
xorl %r15d, %r15d
movq 0x38(%rsp), %rbp
movq %rbp, %rdi
movq 0x30(%rsp), %r13
movq %r13, %rsi
xorl %edx, %edx
callq 0x7b8b6
cmpq 0x10(%rbx), %r15
jae 0x7deb4
movq 0x18(%r12), %r14
imulq %r15, %r14
addq 0x28(%rsp), %r14
movq %r14, 0x40(%rsp)
movq %r12, 0x48(%rsp)
movq $0x1, 0x50(%rsp)
movq 0x18(%rbp), %rdx
testq %rdx, %rdx
jne 0x7de85
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
jmp 0x7de4c
movq 0x18(%r12), %rdi
movq 0x60(%rsp), %rdx
movq 0x28(%rsp), %rsi
jmp 0x7df2a
leaq 0x4900a(%rip), %rdi     # 0xc6ed6
leaq 0x48b9d(%rip), %rsi     # 0xc6a70
leaq 0x49079(%rip), %rcx     # 0xc6f53
movl $0x877, %edx            # imm = 0x877
xorl %eax, %eax
callq 0x60031
movq 0x10(%rbx), %rdx
movq 0x38(%rsp), %r15
movq %r15, %rdi
movq 0x30(%rsp), %r14
movq %r14, %rsi
callq 0x7b8b6
movq %r15, %rdi
movq %r14, %rsi
callq 0x7bf18
xorl %ecx, %ecx
cmpq 0x10(%rbx), %rcx
jae 0x7df1f
movq (%r12,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
incq %rcx
jmp 0x7df0c
movq 0x40(%rsp), %rdx
pushq $0x8
popq %rdi
movq %r12, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x60263
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38(%rsp), %r13
movq %r13, %rdi
movq 0x30(%rsp), %r15
movq %r15, %rsi
callq 0x7b8b6
movq 0x18(%r12), %r14
movq %r13, %rdi
movq %r15, %rsi
callq 0x7bf18
movq 0x10(%rbx), %rcx
movq %r14, %rdi
movq %rax, %rsi
movq 0x28(%rsp), %rbx
movq %rbx, %rdx
callq 0x6033c
movq 0x18(%r12), %rdi
movq 0x40(%rsp), %rdx
movq %rbx, %rsi
jmp 0x7df2a