// File: /workspace/github_c_makefile_star2_O1/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 0x11bbf8
movq (%rdx), %rax
cmpq 0x20(%r12), %rax
ja 0x11bbf8
cmpb $0x1, 0x28(%r12)
jne 0x11bbf8
xorl %ebp, %ebp
subq 0x10(%r12), %rbp
andq %rbx, %rbp
jmp 0x11bc1f
movq 0x38(%r12), %rdi
movq %rbx, %rsi
callq 0x131e3e
testq %rax, %rax
je 0x11bc1d
movq %rbx, %rcx
subq %rax, %rcx
xorl %ebp, %ebp
cmpq 0x10(%r12), %rcx
cmovbq %rax, %rbp
jmp 0x11bc1f
xorl %ebp, %ebp
testq %rbp, %rbp
je 0x11bc6d
movq 0x8(%rbp), %rax
cmpq %r15, %rax
jae 0x11bcfe
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x11bdb7
movq %rax, %r13
movq 0x8(%rbp), %rdx
movq %rax, %rdi
movq %rbx, %rsi
callq 0xc83f1
subq %rbp, %rbx
movq %rbx, %rax
xorl %edx, %edx
divq 0x8(%rbp)
movzwl %ax, %esi
movq %rbp, %rdi
callq 0x11ea7f
jmp 0x11bda5
cmpq %r15, 0x20(%r12)
jae 0x11bd2b
movq 0xb8(%r12), %rdi
movq %rbx, %rsi
callq 0x130a3e
movq %rax, %r13
testq %rax, %rax
jne 0x11bcb1
leaq 0x3c7a5(%rip), %rdi     # 0x15843c
leaq 0x3bff7(%rip), %rsi     # 0x157c95
leaq 0x3c7b8(%rip), %rcx     # 0x15845d
movl $0x4bb, %edx            # imm = 0x4BB
xorl %eax, %eax
callq 0xc80f6
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 0x11bd9a
addq $0x40, %r12
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x11f177
movq %rax, %r13
cmpq %r15, %rbp
cmovbq %rbp, %r15
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0xc83f1
jmp 0x11bd4e
movq 0x30(%r12), %rdx
xorl %ecx, %ecx
movq %rcx, %rsi
incq %rcx
cmpq %r15, (%rdx,%rsi,8)
jb 0x11bd05
movq (%r12), %rdx
movq -0x8(%rdx,%rcx,8), %r12
cmpq %rax, 0x8(%r12)
jne 0x11bd5b
testq %r14, %r14
je 0x11bda2
movq %rax, (%r14)
jmp 0x11bda2
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x11bdb7
movq %rax, %r13
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0xc83f1
addq $0x40, %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x11f127
jmp 0x11bda5
movq %r12, %rdi
callq 0x11edc1
movq %rax, %r13
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0xc83f1
subq %rbp, %rbx
movq %rbx, %rax
xorl %edx, %edx
divq 0x8(%rbp)
movzwl %ax, %esi
movq %rbp, %rdi
callq 0x11ea7f
testq %r14, %r14
je 0x11bda5
movq 0x8(%r12), %rax
movq %rax, (%r14)
jmp 0x11bda5
testq %r14, %r14
je 0x11bda2
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