// File: cphinton42[P]hinton-language/src/pool_allocator.cpp
// Function: pool_alloc_(Pool_Allocator*, unsigned long)
void *pool_alloc_(Pool_Allocator *pool, u64 size)
{
    if(!pool->current_block)
    {
        assert(pool->new_block_size > 0);
        
        Block_Header *block = pool_get_block(pool, 0);
        pool->current_block = block;
        pool->current_point = block->memory;
        pool->current_end = block->memory - sizeof(Block_Header) + block->size;
    }
    
    assert(pool->current_block && pool->current_point && pool->current_end);
    
    u64 available_space = pool->current_end - pool->current_point;
    
    if(size > available_space)
    {
        // Retire the old block
        pool->current_block->next = pool->used_blocks;
        pool->used_blocks = pool->current_block;
        pool->mark += available_space;
        
        // Get a new block
        u64 min_size = size + sizeof(Block_Header);
        
        Block_Header *block = pool_get_block(pool, min_size);
        pool->current_block = block;
        pool->current_point = block->memory;
        pool->current_end = block->memory - sizeof(Block_Header) + block->size;
        
        available_space = pool->current_end - pool->current_point;
    }
    
    assert(size <= available_space);
    
    byte *result = pool->current_point;
    uintptr_t unaligned_point = (uintptr_t)(pool->current_point + size);
    pool->current_point = (byte*)((unaligned_point + 7) & (~7));
    
    pool->mark += (pool->current_point - result);
    
    return (void*)result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, (%rax)
jne 0xce0e
movq -0x8(%rbp), %rax
cmpq $0x0, 0x30(%rax)
jbe 0xcda7
jmp 0xcdc6
leaq 0xd68e(%rip), %rdi      # 0x1a43c
leaq 0xd630(%rip), %rsi      # 0x1a3e5
movl $0x6c, %edx
leaq 0xd694(%rip), %rcx      # 0x1a455
callq 0x3080
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xcfb0
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rcx
addq $0x10, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rcx
addq $0x10, %rcx
addq $-0x10, %rcx
movq -0x18(%rbp), %rax
addq 0x8(%rax), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, (%rcx)
movb %al, -0x41(%rbp)
je 0xce3c
movq -0x8(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, 0x8(%rcx)
movb %al, -0x41(%rbp)
je 0xce3c
movq -0x8(%rbp), %rax
cmpq $0x0, 0x10(%rax)
setne %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0xce45
jmp 0xce47
jmp 0xce66
leaq 0xd630(%rip), %rdi      # 0x1a47e
leaq 0xd590(%rip), %rsi      # 0x1a3e5
movl $0x74, %edx
leaq 0xd5f4(%rip), %rcx      # 0x1a455
callq 0x3080
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rcx
subq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpq -0x20(%rbp), %rax
jbe 0xcf27
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x20(%rbp), %rcx
movq -0x8(%rbp), %rax
addq 0x18(%rax), %rcx
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0xcfb0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rcx
addq $0x10, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rcx
addq $0x10, %rcx
addq $-0x10, %rcx
movq -0x30(%rbp), %rax
addq 0x8(%rax), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rcx
subq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpq -0x20(%rbp), %rax
ja 0xcf33
jmp 0xcf52
leaq 0xd584(%rip), %rdi      # 0x1a4be
leaq 0xd4a4(%rip), %rsi      # 0x1a3e5
movl $0x8a, %edx
leaq 0xd508(%rip), %rcx      # 0x1a455
callq 0x3080
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
addq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rcx
addq $0x7, %rcx
andq $-0x8, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x38(%rbp), %rax
subq %rax, %rcx
movq -0x8(%rbp), %rax
addq 0x18(%rax), %rcx
movq %rcx, 0x18(%rax)
movq -0x38(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)