// File: walker-xian[P]teisye/src/teisye.cpp
// Function: teisye::heap::alloc(unsigned long)
inline void* alloc(size_t size) noexcept
    {
        auto key = _book.key(size + sizeof(memory_unit::header));
        memory_unit* unit{};
        if (key <= _book.key_max_small())
        {
            unit = _thread_cache.alloc(key);
            if (unit)
            {
                unit->_header._idle = false;
                unit->_header._size = static_cast<uint32_t>(size);
                assert(validate(unit));
                return &unit->_next;
            }
        }

        if (key <= _book.key_max_medium())
        {
            unit = _shared_cache.alloc(key);
            if (unit)
            {
                unit->_header._idle = false;
                unit->_header._size = static_cast<uint32_t>(size);
                assert(validate(unit));
                return &unit->_next;
            }

            unit = _slabs.alloc(_book.value(key));
        }
        else if (key <= _book.key_max_large())
        {
            unit = _large.alloc(size);
        }
        else
        {
            size_t adjusted_size = size + sizeof(huge_unit) + sizeof(huge_unit::_raw_ptr);
            void *raw_ptr = ::malloc(adjusted_size);
            if (raw_ptr)
            {
                auto hu = align<huge_unit*>(raw_ptr, sizeof(huge_unit)) - 1;
                assert(hu >= raw_ptr);
                hu->_raw_ptr = raw_ptr;
                unit = reinterpret_cast<memory_unit*>(&hu->_unit);
            }
        }

        assert(unit);
        if (unit)
        {
            unit->_header._size = static_cast<uint32_t>(size);
            unit->_header._signature = _unit_signature;
            unit->_header._key = key;
            unit->_header._idle = false;
            assert(is_aligned(&unit->_next));
            assert(validate(unit));
            return &unit->_next;
        }

        return nullptr;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
leaq 0x20a81(%rip), %rdi     # 0x57818
callq 0x37340
movb %al, -0x19(%rbp)
movq $0x0, -0x28(%rbp)
movsbl -0x19(%rbp), %eax
movl %eax, -0x44(%rbp)
leaq 0x20a63(%rip), %rdi     # 0x57818
callq 0x37770
movb %al, %cl
movl -0x44(%rbp), %eax
movsbl %cl, %ecx
cmpl %ecx, %eax
jg 0x36e3e
callq 0x37790
movq %rax, %rdi
movsbl -0x19(%rbp), %esi
callq 0x377b0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x36e3c
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rax
movb $0x0, 0x3(%rax)
movq -0x18(%rbp), %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x28(%rbp), %rsi
callq 0x372d0
testb $0x1, %al
jne 0x36e0a
jmp 0x36e0c
jmp 0x36e2b
leaq 0x4b35(%rip), %rdi      # 0x3b948
leaq 0x4b3d(%rip), %rsi      # 0x3b957
movl $0x297, %edx            # imm = 0x297
leaq 0x4b40(%rip), %rcx      # 0x3b966
callq 0x52e0
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x8(%rbp)
jmp 0x3708a
jmp 0x36e3e
movsbl -0x19(%rbp), %eax
movl %eax, -0x54(%rbp)
leaq 0x209cc(%rip), %rdi     # 0x57818
callq 0x37810
movb %al, %cl
movl -0x54(%rbp), %eax
movsbl %cl, %ecx
cmpl %ecx, %eax
jg 0x36efb
leaq 0x20b48(%rip), %rdi     # 0x579b0
movsbl -0x19(%rbp), %esi
callq 0x37830
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x36ed6
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rax
movb $0x0, 0x3(%rax)
movq -0x18(%rbp), %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x28(%rbp), %rsi
callq 0x372d0
testb $0x1, %al
jne 0x36ea4
jmp 0x36ea6
jmp 0x36ec5
leaq 0x4a9b(%rip), %rdi      # 0x3b948
leaq 0x4aa3(%rip), %rsi      # 0x3b957
movl $0x2a3, %edx            # imm = 0x2A3
leaq 0x4aa6(%rip), %rcx      # 0x3b966
callq 0x52e0
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x8(%rbp)
jmp 0x3708a
leaq 0x2093b(%rip), %rdi     # 0x57818
movsbl -0x19(%rbp), %esi
callq 0x37960
movq -0x50(%rbp), %rdi
movq %rax, %rsi
callq 0x378a0
movq %rax, -0x28(%rbp)
jmp 0x36fb1
movsbl -0x19(%rbp), %eax
movl %eax, -0x58(%rbp)
leaq 0x2090f(%rip), %rdi     # 0x57818
callq 0x379b0
movb %al, %cl
movl -0x58(%rbp), %eax
movsbl %cl, %ecx
cmpl %ecx, %eax
jg 0x36f31
movq -0x50(%rbp), %rdi
addq $0x8, %rdi
movq -0x18(%rbp), %rsi
callq 0x379d0
movq %rax, -0x28(%rbp)
jmp 0x36faf
movq -0x18(%rbp), %rax
addq $0x10, %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x5730
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x36fad
movq -0x38(%rbp), %rdi
movl $0x10, %esi
callq 0x37a20
addq $-0x10, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x38(%rbp), %rax
jb 0x36f77
jmp 0x36f96
leaq 0x4a0a(%rip), %rdi      # 0x3b988
leaq 0x49d2(%rip), %rsi      # 0x3b957
movl $0x2b4, %edx            # imm = 0x2B4
leaq 0x49d5(%rip), %rcx      # 0x3b966
callq 0x52e0
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, (%rax)
movq -0x40(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
jmp 0x36faf
jmp 0x36fb1
cmpq $0x0, -0x28(%rbp)
je 0x36fba
jmp 0x36fd9
leaq 0x49d5(%rip), %rdi      # 0x3b996
leaq 0x498f(%rip), %rsi      # 0x3b957
movl $0x2ba, %edx            # imm = 0x2BA
leaq 0x4992(%rip), %rcx      # 0x3b966
callq 0x52e0
cmpq $0x0, -0x28(%rbp)
je 0x37082
movq -0x18(%rbp), %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x28(%rbp), %rax
movw $0x6871, (%rax)         # imm = 0x6871
movb -0x19(%rbp), %cl
movq -0x28(%rbp), %rax
movb %cl, 0x2(%rax)
movq -0x28(%rbp), %rax
movb $0x0, 0x3(%rax)
movq -0x28(%rbp), %rdi
addq $0x8, %rdi
callq 0x37a50
testb $0x1, %al
jne 0x3701f
jmp 0x37021
jmp 0x37040
leaq 0x4973(%rip), %rdi      # 0x3b99b
leaq 0x4928(%rip), %rsi      # 0x3b957
movl $0x2c1, %edx            # imm = 0x2C1
leaq 0x492b(%rip), %rcx      # 0x3b966
callq 0x52e0
movq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x372d0
testb $0x1, %al
jne 0x37053
jmp 0x37055
jmp 0x37074
leaq 0x48ec(%rip), %rdi      # 0x3b948
leaq 0x48f4(%rip), %rsi      # 0x3b957
movl $0x2c2, %edx            # imm = 0x2C2
leaq 0x48f7(%rip), %rcx      # 0x3b966
callq 0x52e0
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x8(%rbp)
jmp 0x3708a
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop