// File: walker-xian[P]teisye/src/teisye.cpp
// Function: teisye::heap_large::large_slab::alloc(unsigned long)
inline unit_t*  alloc(size_t size) noexcept
        {
            unit_t *chunk{};
            size_t aligned_size = align(size + sizeof(*chunk));

            if (aligned_size > _free_size || _busy.test_and_set()) return nullptr;

            for (auto cur = _first; cur < _last; cur = cur->_right)
            {
                // the unit is not free
                if (cur->_owner) continue;

                // merge free units
                for (auto right = cur->_right; right < _last && !right->_owner; right = right->_right)
                {
                    cur->_right = right->_right;
                }
                assert(cur->_right > _first && cur->_right <= _last);

                auto free_size = pointer_diff(cur->_right, cur);
                if (aligned_size > free_size) continue;

                // if the remaining size is equal or greater than 128 bytes, split it
                if (free_size - aligned_size >= 128)
                {
                    auto split = reinterpret_cast<unit_t*>(reinterpret_cast<size_t>(cur) + aligned_size);
                    split->_owner = nullptr;
                    split->_right = cur->_right;
                    cur->_right = split;
                }

                chunk = cur;
                chunk->_owner = this;

                assert(pointer_diff(chunk->_right, chunk) >= size + sizeof(*chunk));
                
                _free_size.fetch_sub(pointer_diff(chunk->_right, chunk));
                break;
            }

            _busy.clear();
            
            return chunk;
        }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x58(%rbp)
movq %rsi, -0x60(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x60(%rbp), %rdi
addq $0x18, %rdi
callq 0x38650
movq -0xa0(%rbp), %rdi
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x98(%rbp)
addq $0x18, %rdi
callq 0x39430
movq %rax, %rcx
movq -0x98(%rbp), %rax
cmpq %rcx, %rax
ja 0x38ee7
movq -0xa0(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x40(%rbp)
movl $0x5, -0x44(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl -0x44(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0xa8(%rbp)
subl $0x4, %eax
ja 0x38e80
movq -0xa8(%rbp), %rax
leaq 0x28d9(%rip), %rcx      # 0x3b750
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0xb0(%rbp), %rcx
movb $0x1, %al
xchgb %al, (%rcx)
movb %al, -0xb1(%rbp)
jmp 0x38edd
movq -0xb0(%rbp), %rcx
movb $0x1, %al
xchgb %al, (%rcx)
movb %al, -0xb1(%rbp)
jmp 0x38edd
movq -0xb0(%rbp), %rcx
movb $0x1, %al
xchgb %al, (%rcx)
movb %al, -0xb1(%rbp)
jmp 0x38edd
movq -0xb0(%rbp), %rcx
movb $0x1, %al
xchgb %al, (%rcx)
movb %al, -0xb1(%rbp)
jmp 0x38edd
movq -0xb0(%rbp), %rcx
movb $0x1, %al
xchgb %al, (%rcx)
movb %al, -0xb1(%rbp)
movb -0xb1(%rbp), %al
cmpb $0x0, %al
je 0x38ef4
movq $0x0, -0x50(%rbp)
jmp 0x3922e
movq -0xa0(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x78(%rbp)
movq -0xa0(%rbp), %rcx
movq -0x78(%rbp), %rax
cmpq 0x8(%rcx), %rax
jae 0x391b5
movq -0x78(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x38f27
jmp 0x391a5
movq -0x78(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x80(%rbp)
movq -0xa0(%rbp), %rdx
movq -0x80(%rbp), %rcx
xorl %eax, %eax
cmpq 0x8(%rdx), %rcx
movb %al, -0xb2(%rbp)
jae 0x38f5f
movq -0x80(%rbp), %rax
cmpq $0x0, 0x8(%rax)
setne %al
xorb $-0x1, %al
movb %al, -0xb2(%rbp)
movb -0xb2(%rbp), %al
testb $0x1, %al
jne 0x38f6b
jmp 0x38f86
movq -0x80(%rbp), %rax
movq (%rax), %rcx
movq -0x78(%rbp), %rax
movq %rcx, (%rax)
movq -0x80(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x80(%rbp)
jmp 0x38f32
movq -0xa0(%rbp), %rdx
movq -0x78(%rbp), %rax
movq (%rax), %rcx
xorl %eax, %eax
cmpq (%rdx), %rcx
movb %al, -0xb3(%rbp)
jbe 0x38fbc
movq -0xa0(%rbp), %rcx
movq -0x78(%rbp), %rax
movq (%rax), %rax
cmpq 0x8(%rcx), %rax
setbe %al
movb %al, -0xb3(%rbp)
movb -0xb3(%rbp), %al
testb $0x1, %al
jne 0x38fc8
jmp 0x38fca
jmp 0x38fe9
leaq 0x2b3b(%rip), %rdi      # 0x3bb0c
leaq 0x297f(%rip), %rsi      # 0x3b957
movl $0x1f5, %edx            # imm = 0x1F5
leaq 0x2b55(%rip), %rcx      # 0x3bb39
callq 0x52e0
movq -0x78(%rbp), %rax
movq (%rax), %rdi
movq -0x78(%rbp), %rsi
callq 0x394b0
movq %rax, -0x88(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x88(%rbp), %rax
jbe 0x39012
jmp 0x391a5
movq -0x88(%rbp), %rax
subq -0x70(%rbp), %rax
cmpq $0x80, %rax
jb 0x39062
movq -0x78(%rbp), %rax
addq -0x70(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x78(%rbp), %rax
movq (%rax), %rcx
movq -0x90(%rbp), %rax
movq %rcx, (%rax)
movq -0x90(%rbp), %rcx
movq -0x78(%rbp), %rax
movq %rcx, (%rax)
movq -0xa0(%rbp), %rcx
movq -0x78(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x68(%rbp), %rax
movq (%rax), %rdi
movq -0x68(%rbp), %rsi
callq 0x394b0
movq -0x60(%rbp), %rcx
addq $0x18, %rcx
cmpq %rcx, %rax
jb 0x39098
jmp 0x390b7
leaq 0x2ad0(%rip), %rdi      # 0x3bb6f
leaq 0x28b1(%rip), %rsi      # 0x3b957
movl $0x206, %edx            # imm = 0x206
leaq 0x2a87(%rip), %rcx      # 0x3bb39
callq 0x52e0
movq -0xa0(%rbp), %rax
addq $0x18, %rax
movq %rax, -0xd0(%rbp)
movq -0x68(%rbp), %rsi
movq (%rsi), %rdi
callq 0x394b0
movq -0xd0(%rbp), %rcx
movq %rcx, -0x8(%rbp)
movq %rax, -0x10(%rbp)
movl $0x5, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
movq %rcx, -0x20(%rbp)
decl %eax
movl %eax, %ecx
movq %rcx, -0xc0(%rbp)
subl $0x4, %eax
ja 0x39128
movq -0xc0(%rbp), %rax
leaq 0x2645(%rip), %rcx      # 0x3b764
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0xc8(%rbp), %rcx
movq -0x20(%rbp), %rax
negq %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x28(%rbp)
jmp 0x391a3
movq -0xc8(%rbp), %rcx
movq -0x20(%rbp), %rax
negq %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x28(%rbp)
jmp 0x391a3
movq -0xc8(%rbp), %rcx
movq -0x20(%rbp), %rax
negq %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x28(%rbp)
jmp 0x391a3
movq -0xc8(%rbp), %rcx
movq -0x20(%rbp), %rax
negq %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x28(%rbp)
jmp 0x391a3
movq -0xc8(%rbp), %rcx
movq -0x20(%rbp), %rax
negq %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x28(%rbp)
jmp 0x391b5
movq -0x78(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x78(%rbp)
jmp 0x38f02
movq -0xa0(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x30(%rbp)
movl $0x5, -0x34(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0xe0(%rbp)
movl -0x34(%rbp), %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x37750
movl %eax, -0x38(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0xd4(%rbp)
subl $0x3, %eax
je 0x3920f
jmp 0x391f6
movl -0xd4(%rbp), %eax
subl $0x5, %eax
je 0x3921b
jmp 0x39203
movq -0xe0(%rbp), %rax
movb $0x0, (%rax)
jmp 0x39226
movq -0xe0(%rbp), %rax
movb $0x0, (%rax)
jmp 0x39226
movq -0xe0(%rbp), %rcx
xorl %eax, %eax
xchgb %al, (%rcx)
movq -0x68(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax,%rax)