// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x108bc6
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x20(%rcx), %rax
ja 0x108bc6
movq -0x10(%rbp), %rax
testb $0x1, 0x28(%rax)
je 0x108bc6
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
subq $0x1, %rcx
xorq $-0x1, %rcx
andq %rcx, %rax
movq %rax, -0x38(%rbp)
jmp 0x108c01
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x1288a0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x108bff
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
subq %rcx, %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x108bff
movq -0x40(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x108c01
cmpq $0x0, -0x38(%rbp)
je 0x108d3a
movq -0x28(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jbe 0x108c5e
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x108e60
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0x9d5e0
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x10cdf0
movq -0x48(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x108e47
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x108cb7
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x30(%rcx), %rcx
movq -0x58(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
ja 0x108ca7
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
jmp 0x108cb7
jmp 0x108ca9
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x108c6e
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x38(%rbp), %rcx
cmpq 0x8(%rcx), %rax
je 0x108d17
movq -0x50(%rbp), %rdi
callq 0x10d7e0
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x9d5e0
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x10cdf0
cmpq $0x0, -0x30(%rbp)
je 0x108d0a
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x60(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x108e47
cmpq $0x0, -0x30(%rbp)
je 0x108d2d
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x108e47
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x20(%rcx), %rax
ja 0x108d8c
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x108e60
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x9d5e0
movq -0x10(%rbp), %rdi
addq $0x40, %rdi
movq -0x18(%rbp), %rsi
callq 0x10eac0
movq -0x68(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x108e47
movq -0x10(%rbp), %rdi
addq $0x40, %rdi
movq -0x18(%rbp), %rsi
callq 0x10eb30
movq %rax, -0x70(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x70(%rbp), %rax
jae 0x108dd7
movq -0x28(%rbp), %rax
movq -0x70(%rbp), %rcx
shrq %rcx
cmpq %rcx, %rax
jb 0x108dd7
cmpq $0x0, -0x30(%rbp)
je 0x108dcd
movq -0x70(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x108e47
movq -0x10(%rbp), %rdi
addq $0x40, %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x10eb90
movq %rax, -0x78(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x70(%rbp), %rax
jae 0x108e07
movq -0x28(%rbp), %rax
movq %rax, -0x88(%rbp)
jmp 0x108e12
movq -0x70(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x80(%rbp), %rdx
callq 0x9d5e0
movq -0x10(%rbp), %rdi
addq $0x40, %rdi
movq -0x18(%rbp), %rsi
callq 0x10eac0
movq -0x78(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)