// File: /workspace/github_c_makefile_star2_O1/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), %r12
movq %r12, %rbp
testq %r12, %r12
jne 0xf9874
movq 0x20(%r15), %rax
movq 0x8(%r14,%rax), %rbp
testq %r12, %r12
je 0xf98a0
cmpq 0x10(%rbx), %rbp
je 0xf98a0
leaq 0x5a760(%rip), %rdi     # 0x153fe6
leaq 0x5a2f3(%rip), %rsi     # 0x153b80
leaq 0x5a76e(%rip), %rcx     # 0x154002
movl $0x851, %edx            # imm = 0x851
xorl %eax, %eax
callq 0xc80f6
movq 0x18(%r15), %rax
testq %rax, %rax
jne 0xf98b2
movq 0x20(%r15), %rax
movq 0x10(%r14,%rax), %rax
cmpq %rax, %rbp
jbe 0xf98c2
movq %r15, %rdi
movq %r14, %rsi
callq 0xf5ec4
cmpb $0x1, 0x42(%r15)
movq %r14, 0x18(%rsp)
movq %r15, 0x28(%rsp)
jne 0xf9a4a
movq %r15, %rdi
movq %r14, %rsi
callq 0xf730c
movq %rax, %r13
leaq 0x30(%rsp), %r8
movq $0x0, (%r8)
movq 0x10(%rbx), %rcx
movl $0x8, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0xc833e
movq %rax, %r12
cmpq $0x0, 0x10(%rbx)
je 0xf9965
leaq 0x5a26c(%rip), %r14     # 0x153b80
xorl %r15d, %r15d
movq 0x8(%rbx), %rax
cmpq %rbp, (%rax,%r15,8)
jb 0xf9946
leaq 0x3ed1f(%rip), %rdi     # 0x138647
leaq 0x5a714(%rip), %rsi     # 0x154043
leaq 0x5a6b0(%rip), %rdx     # 0x153fe6
movq %r14, %rcx
movl $0x85e, %r8d            # imm = 0x85E
xorl %eax, %eax
callq 0xc8053
movq 0x8(%rbx), %rax
movq (%rax,%r15,8), %rax
movq (%r13,%rax,8), %rdi
callq 0xc8348
movq %rax, (%r12,%r15,8)
incq %r15
cmpq 0x10(%rbx), %r15
jb 0xf9917
movq 0x28(%rsp), %rdi
cmpb $0x0, 0x43(%rdi)
je 0xf9d67
movq 0x18(%rsp), %rsi
callq 0xf86fe
movq %rax, %r15
leaq 0xafae0(%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 %r15, %rcx
shlq $0x4, %rcx
leaq (%rdx,%rcx), %rax
movq (%rdx,%rcx), %rdx
movl $0x8, %ecx
cmpq %r15, %rdx
je 0xf99e2
movq 0x10(%rax), %rdx
movl $0x18, %ecx
cmpq %r15, %rdx
je 0xf99e2
movq 0x20(%rax), %rdx
movl $0x28, %ecx
cmpq %r15, %rdx
je 0xf99e2
movq 0x30(%rax), %rdx
movl $0x38, %ecx
cmpq %r15, %rdx
jne 0xf9ed3
movq (%rax,%rcx), %r14
testq %r14, %r14
jne 0xf9a12
movabsq $0x782b96602f424981, %rdi # imm = 0x782B96602F424981
movq %r15, %rsi
callq 0xf3f85
movq %rax, %r14
leaq 0xafa61(%rip), %rdi     # 0x1a9468
movq %r15, %rsi
movq %rax, %rdx
callq 0x12128c
testq %rbp, %rbp
je 0xf9e67
movq %rbp, %r15
xorl %ebp, %ebp
movq (%r13,%rbp,8), %rdx
leaq 0x4af(%rip), %rdi       # 0xf9edb
movq %r14, %rsi
callq 0xc8357
movq $0x0, (%r13,%rbp,8)
incq %rbp
cmpq %rbp, %r15
jne 0xf9a20
jmp 0xf9e67
movq %r12, 0x50(%rsp)
movq %rbp, 0x48(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0xf86fe
movq %rax, %r13
leaq 0xaf9ff(%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 %r13, %rcx
shlq $0x4, %rcx
leaq (%rdx,%rcx), %rax
movq (%rdx,%rcx), %rdx
movl $0x8, %ecx
cmpq %r13, %rdx
je 0xf9ac3
movq 0x10(%rax), %rdx
movl $0x18, %ecx
cmpq %r13, %rdx
je 0xf9ac3
movq 0x20(%rax), %rdx
movl $0x28, %ecx
cmpq %r13, %rdx
je 0xf9ac3
movq 0x30(%rax), %rdx
movl $0x38, %ecx
cmpq %r13, %rdx
jne 0xf9ecb
movq (%rax,%rcx), %r12
testq %r12, %r12
jne 0xf9af3
movabsq $0x782b96602f424981, %rdi # imm = 0x782B96602F424981
movq %r13, %rsi
callq 0xf3f85
movq %rax, %r12
leaq 0xaf980(%rip), %rdi     # 0x1a9468
movq %r13, %rsi
movq %rax, %rdx
callq 0x12128c
cmpb $0x1, 0x29(%r12)
jne 0xf9c0d
cmpb $0x1, 0x2b(%r12)
jne 0xf9c0d
cmpb $0x1, 0x2a(%r12)
jne 0xf9c0d
leaq 0x30(%rsp), %rcx
movq $0x0, (%rcx)
movq 0x18(%r12), %rdi
movq 0x10(%rbx), %rdx
xorl %esi, %esi
callq 0xc8339
movq %rax, 0x20(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0xf730c
movq 0x10(%rbx), %rdx
testq %rdx, %rdx
je 0xf9bbf
movq %rax, %rbp
leaq 0x5a02a(%rip), %r13     # 0x153b80
movq 0x20(%rsp), %r14
xorl %r15d, %r15d
movq 0x8(%rbx), %rax
movq 0x48(%rsp), %rcx
cmpq %rcx, (%rax,%r15,8)
jb 0xf9b92
leaq 0x3ead3(%rip), %rdi     # 0x138647
leaq 0x5a4c8(%rip), %rsi     # 0x154043
leaq 0x5a464(%rip), %rdx     # 0x153fe6
movq %r13, %rcx
movl $0x88d, %r8d            # imm = 0x88D
xorl %eax, %eax
callq 0xc8053
movq 0x8(%rbx), %rax
movq 0x18(%r12), %rdx
movq (%rax,%r15,8), %rsi
imulq %rdx, %rsi
addq %rbp, %rsi
movq %r14, %rdi
callq 0xc83f1
addq 0x18(%r12), %r14
incq %r15
movq 0x10(%rbx), %rdx
cmpq %rdx, %r15
jb 0xf9b5e
movq 0x28(%rsp), %r13
movq %r13, %rdi
movq 0x18(%rsp), %r15
movq %r15, %rsi
callq 0xf69d8
movq 0x18(%r12), %r14
movq %r13, %rdi
movq %r15, %rsi
callq 0xf730c
movq 0x10(%rbx), %rcx
movq %r14, %rdi
movq %rax, %rsi
movq 0x20(%rsp), %rbx
movq %rbx, %rdx
callq 0xc842f
movq 0x18(%r12), %rdi
movq 0x30(%rsp), %rdx
movq %rbx, %rsi
jmp 0xf9eb2
leaq 0x60(%rsp), %rcx
movq $0x0, (%rcx)
movq 0x18(%r12), %rdi
movq 0x10(%rbx), %rdx
xorl %esi, %esi
callq 0xc8339
movq %rax, 0x20(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0xf730c
cmpq $0x0, 0x10(%rbx)
je 0xf9cd0
movq %rax, %rbp
xorl %r13d, %r13d
movq 0x8(%rbx), %rax
movq 0x48(%rsp), %rcx
cmpq %rcx, (%rax,%r13,8)
jb 0xf9c82
leaq 0x3e9e7(%rip), %rdi     # 0x138647
leaq 0x5a3dc(%rip), %rsi     # 0x154043
leaq 0x5a378(%rip), %rdx     # 0x153fe6
leaq 0x59f0b(%rip), %rcx     # 0x153b80
movl $0x89c, %r8d            # imm = 0x89C
xorl %eax, %eax
callq 0xc8053
movq 0x18(%r12), %r15
movq %r15, %r14
imulq %r13, %r14
addq 0x20(%rsp), %r14
movq 0x8(%rbx), %rax
imulq (%rax,%r13,8), %r15
addq %rbp, %r15
movq %r12, %rdi
movq %r14, %rsi
callq *0x80(%r12)
cmpq %r15, %r14
je 0xf9cc3
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq *0x90(%r12)
incq %r13
cmpq 0x10(%rbx), %r13
jb 0xf9c4a
cmpq $0x0, 0x50(%rsp)
je 0xf9da7
cmpq $0x0, 0x10(%rbx)
movq %rbx, %rax
movq 0x18(%rsp), %rbx
movq 0x28(%rsp), %r13
movq 0x20(%rsp), %rsi
je 0xf9e3a
xorl %r14d, %r14d
movq %rax, 0x58(%rsp)
movq 0x18(%r12), %r15
imulq %r14, %r15
addq %rsi, %r15
movq %r15, 0x30(%rsp)
movq %r12, 0x38(%rsp)
movq $0x1, 0x40(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r13, %rdi
movq %rsi, %rbp
movq %rbx, %rsi
movq %r14, %rdx
callq *0x38(%r13)
movq %r12, %rdi
movq %r15, %rsi
callq *0x88(%r12)
movq %rbp, %rsi
movq 0x58(%rsp), %rax
incq %r14
cmpq 0x10(%rax), %r14
jb 0xf9d01
jmp 0xf9e3a
cmpb $0x1, 0x41(%rdi)
jne 0xf9e46
testq %rbp, %rbp
je 0xf9e67
xorl %r15d, %r15d
leaq 0x160(%rip), %r14       # 0xf9ee4
movq (%r13,%r15,8), %rsi
movq %r14, %rdi
callq 0xc8352
movq $0x0, (%r13,%r15,8)
incq %r15
cmpq %r15, %rbp
jne 0xf9d84
jmp 0xf9e67
movq 0x28(%rsp), %rbp
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
xorl %edx, %edx
callq 0xf69d8
cmpq $0x0, 0x10(%rbx)
movq 0x20(%rsp), %rsi
je 0xf9e3a
xorl %r15d, %r15d
movq 0x18(%r12), %r14
imulq %r15, %r14
addq %rsi, %r14
movq %r14, 0x30(%rsp)
movq %r12, 0x38(%rsp)
movq $0x1, 0x40(%rsp)
movq 0x18(%rbp), %rdx
testq %rdx, %rdx
jne 0xf9e00
movq 0x20(%rbp), %rax
movq 0x18(%rsp), %rcx
movq 0x8(%rcx,%rax), %rdx
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
movups 0x30(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
callq *0x38(%rbp)
movq %r12, %rdi
movq %r14, %rsi
callq *0x88(%r12)
incq %r15
cmpq 0x10(%rbx), %r15
movq 0x20(%rsp), %rsi
jb 0xf9dca
movq 0x18(%r12), %rdi
movq 0x60(%rsp), %rdx
jmp 0xf9eb2
leaq 0x5a199(%rip), %rdi     # 0x153fe6
leaq 0x59d2c(%rip), %rsi     # 0x153b80
leaq 0x5a208(%rip), %rcx     # 0x154063
movl $0x877, %edx            # imm = 0x877
xorl %eax, %eax
callq 0xc80f6
movq 0x10(%rbx), %rdx
movq 0x28(%rsp), %r15
movq %r15, %rdi
movq 0x18(%rsp), %r14
movq %r14, %rsi
callq 0xf69d8
movq %r15, %rdi
movq %r14, %rsi
callq 0xf730c
cmpq $0x0, 0x10(%rbx)
je 0xf9ea5
xorl %ecx, %ecx
movq (%r12,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
incq %rcx
cmpq 0x10(%rbx), %rcx
jb 0xf9e94
movq 0x30(%rsp), %rdx
movl $0x8, %edi
movq %r12, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0xc833e
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r12d, %r12d
jmp 0xf9ac7
xorl %r14d, %r14d
jmp 0xf99e6