// File: /workspace/github_c_makefile_star2_O2/johsteffens[P]beth/lib/bcore/bcore_tbman.c
// Function: tbman_s_mem_realloc
static vd_t tbman_s_mem_realloc( bcore_tbman_s* o, vd_t current_ptr, const uz_t* current_bytes, uz_t requested_bytes, uz_t* granted_bytes )
{
    token_manager_s* token_manager = NULL;
    if( current_bytes && *current_bytes <= o->max_block_size && o->aligned )
    {
        token_manager = ( token_manager_s* )( ( intptr_t )current_ptr & ~( intptr_t )( o->pool_size - 1 ) );
    }
    else
    {
        vd_t block_ptr = bcore_btree_vd_s_largest_below_equal( o->internal_btree, current_ptr );
        if( block_ptr && ( ( (u0_t*)current_ptr - (u0_t*)block_ptr ) < o->pool_size ) ) token_manager = block_ptr;
    }

    if( token_manager )
    {
        if( requested_bytes > token_manager->block_size )
        {
            vd_t reserved_ptr = tbman_s_mem_alloc( o, requested_bytes, granted_bytes );
            bcore_memcpy( reserved_ptr, current_ptr, token_manager->block_size );
            token_manager_s_free( token_manager, current_ptr );
            return reserved_ptr;
        }
        else // size reduction
        {
            block_manager_s* block_manager = NULL;
            for( uz_t i = 0; i < o->size; i++ )
            {
                if( requested_bytes <= o->block_size_array[ i ] )
                {
                    block_manager = o->data[ i ];
                    break;
                }
            }

            if( block_manager->block_size != token_manager->block_size )
            {
                vd_t reserved_ptr = block_manager_s_alloc( block_manager );
                bcore_memcpy( reserved_ptr, current_ptr, requested_bytes );
                token_manager_s_free( token_manager, current_ptr );
                if( granted_bytes ) *granted_bytes = block_manager->block_size;
                return reserved_ptr;
            }
            else
            {
                // same block-size: keep current location
                if( granted_bytes ) *granted_bytes = token_manager->block_size;
                return current_ptr;
            }
        }
    }
    else
    {
        if( requested_bytes <= o->max_block_size ) // new size fits into manager, old size was outside manager
        {
            vd_t reserved_ptr = tbman_s_mem_alloc( o, requested_bytes, granted_bytes );
            bcore_memcpy( reserved_ptr, current_ptr, requested_bytes );
            external_manager_s_free( &o->external_manager, current_ptr );
            return reserved_ptr;
        }
        else // neither old nor new size handled by this manager
        {

            uz_t current_ext_bytes = external_manager_s_granted_bytes( &o->external_manager, current_ptr );

            // if requested bytes is less but not significantly less than current bytes, keep current memory
            if( ( requested_bytes < current_ext_bytes ) && ( requested_bytes >= ( current_ext_bytes >> 1 ) ) )
            {
                if( granted_bytes ) *granted_bytes = current_ext_bytes;
                return current_ptr;
            }

            vd_t reserved_ptr = external_manager_s_malloc( &o->external_manager, requested_bytes, granted_bytes );
            uz_t copy_bytes = ( requested_bytes < current_ext_bytes ) ? requested_bytes : current_ext_bytes;
            bcore_memcpy( reserved_ptr, current_ptr, copy_bytes );
            external_manager_s_free( &o->external_manager, current_ptr );
            return reserved_ptr;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %r8, %r14
movq %rcx, %r15
movq %rsi, %rbx
movq %rdi, %r12
testq %rdx, %rdx
je 0x94b25
movq (%rdx), %rax
cmpq 0x20(%r12), %rax
ja 0x94b25
cmpb $0x0, 0x28(%r12)
je 0x94b25
xorl %ebp, %ebp
subq 0x10(%r12), %rbp
andq %rbx, %rbp
jne 0x94b47
jmp 0x94b84
movq 0x38(%r12), %rdi
movq %rbx, %rsi
callq 0xa4867
testq %rax, %rax
je 0x94b84
movq %rax, %rbp
movq %rbx, %rax
subq %rbp, %rax
cmpq 0x10(%r12), %rax
jae 0x94b84
movq 0x8(%rbp), %rax
cmpq %r15, %rax
jae 0x94c45
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x94cc2
movq %rax, %r13
movq 0x8(%rbp), %rdx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x60300
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x96f6d
jmp 0x94c7d
cmpq %r15, 0x20(%r12)
jae 0x94c15
movq 0xb8(%r12), %rdi
movq %rbx, %rsi
callq 0xa34a6
movq %rax, %r13
testq %rax, %rax
jne 0x94bc8
leaq 0x36916(%rip), %rdi     # 0xcb4c4
leaq 0x36168(%rip), %rsi     # 0xcad1d
leaq 0x36929(%rip), %rcx     # 0xcb4e5
movl $0x4bb, %edx            # imm = 0x4BB
xorl %eax, %eax
callq 0x60031
movq (%r13), %rax
movq (%rax), %rbp
cmpq %r15, %rbp
setbe %al
movq %rbp, %rcx
shrq %rcx
cmpq %r15, %rcx
seta %cl
orb %al, %cl
je 0x94c72
addq $0x40, %r12
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x976fe
movq %rax, %r13
cmpq %r15, %rbp
cmovbq %rbp, %r15
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x60300
jmp 0x94c38
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x94cc2
movq %rax, %r13
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x60300
addq $0x40, %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x976ae
jmp 0x94c7d
movq 0x30(%r12), %rdx
xorl %ecx, %ecx
movq %rcx, %rsi
incq %rcx
cmpq %r15, (%rdx,%rsi,8)
jb 0x94c4c
movq (%r12), %rdx
movq -0x8(%rdx,%rcx,8), %r12
cmpq %rax, 0x8(%r12)
jne 0x94c8f
testq %r14, %r14
je 0x94c7a
movq %rax, (%r14)
jmp 0x94c7a
testq %r14, %r14
je 0x94c7a
movq %rbp, (%r14)
movq %rbx, %r13
movq %r13, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x972b8
movq %rax, %r13
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x60300
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x96f6d
testq %r14, %r14
je 0x94c7d
movq 0x8(%r12), %rax
movq %rax, (%r14)
jmp 0x94c7d