// File: /workspace/github_c_makefile_star2_O3/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 0x110981
movq (%rdx), %rax
cmpq 0x20(%r12), %rax
ja 0x110981
cmpb $0x0, 0x28(%r12)
je 0x110981
xorl %ebp, %ebp
subq 0x10(%r12), %rbp
andq %rbx, %rbp
jne 0x1109a3
jmp 0x1109ec
movq 0x38(%r12), %rdi
movq %rbx, %rsi
callq 0x1268f9
testq %rax, %rax
je 0x1109ec
movq %rax, %rbp
movq %rbx, %rax
subq %rbp, %rax
cmpq 0x10(%r12), %rax
jae 0x1109ec
movq 0x8(%rbp), %rax
cmpq %r15, %rax
jae 0x110aad
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x110b36
movq %rax, %r13
movq 0x8(%rbp), %rdx
movq %rax, %rdi
movq %rbx, %rsi
callq 0xbe87d
subq %rbp, %rbx
movq %rbx, %rax
xorl %edx, %edx
divq 0x8(%rbp)
movzwl %ax, %esi
movq %rbp, %rdi
callq 0x11378d
jmp 0x110ae5
cmpq %r15, 0x20(%r12)
jae 0x110a7d
movq 0xb8(%r12), %rdi
movq %rbx, %rsi
callq 0x125444
movq %rax, %r13
testq %rax, %rax
jne 0x110a30
leaq 0x3ca7e(%rip), %rdi     # 0x14d494
leaq 0x3c2d0(%rip), %rsi     # 0x14cced
leaq 0x3ca91(%rip), %rcx     # 0x14d4b5
movl $0x4bb, %edx            # imm = 0x4BB
xorl %eax, %eax
callq 0xbe592
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 0x110ada
addq $0x40, %r12
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x113f29
movq %rax, %r13
cmpq %r15, %rbp
cmovbq %rbp, %r15
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0xbe87d
jmp 0x110aa0
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x110b36
movq %rax, %r13
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0xbe87d
addq $0x40, %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x113ed9
jmp 0x110ae5
movq 0x30(%r12), %rdx
xorl %ecx, %ecx
movq %rcx, %rsi
incq %rcx
cmpq %r15, (%rdx,%rsi,8)
jb 0x110ab4
movq (%r12), %rdx
movq -0x8(%rdx,%rcx,8), %r12
cmpq %rax, 0x8(%r12)
jne 0x110af7
testq %r14, %r14
je 0x110ae2
movq %rax, (%r14)
jmp 0x110ae2
testq %r14, %r14
je 0x110ae2
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 0x113ac5
movq %rax, %r13
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0xbe87d
subq %rbp, %rbx
movq %rbx, %rax
xorl %edx, %edx
divq 0x8(%rbp)
movzwl %ax, %esi
movq %rbp, %rdi
callq 0x11378d
testq %r14, %r14
je 0x110ae5
movq 0x8(%r12), %rax
movq %rax, (%r14)
jmp 0x110ae5