// File: /workspace/github_c_makefile_star2_O3/afi-lambda[P]a_little_smalltalk/src/memory.c
// Function: allocObject
object allocObject(int memorySize)
{
    int i;
    register int position;
    boolean done;

    if (memorySize >= FREELISTMAX)
    {
        fprintf(stderr, "size %d\n", memorySize);
        sysError("allocation bigger than permitted", "allocObject");
    }

    /* first try the free lists, this is fastest */
    if ((position = objectFreeList[memorySize]) != 0)
    {
        objectFreeList[memorySize] = objectTable[position].STclass;
    }

    /* if not there, next try making a size zero object and
     making it bigger */
    else if ((position = objectFreeList[0]) != 0)
    {
        objectFreeList[0] = objectTable[position].STclass;
        objectTable[position].size = memorySize;
        objectTable[position].memory = mBlockAlloc(memorySize);
    }

    else   /* not found, must work a bit harder */
    {
        done = false;

        /* first try making a bigger object smaller */
        for (i = memorySize + 1; i < FREELISTMAX; i++)
            if ((position = objectFreeList[i]) != 0)
            {
                objectFreeList[i] = objectTable[position].STclass;
                /* just trim it a bit */
                objectTable[position].size = memorySize;
                done = true;
                break;
            }

        /* next try making a smaller object bigger */
        if (!done)
            for (i = 1; i < memorySize; i++)
                if ((position = objectFreeList[i]) != 0)
                {
                    objectFreeList[i] = objectTable[position].STclass;
                    objectTable[position].size = memorySize;
# ifdef mBlockAlloc
                    free(objectTable[position].memory);
# endif
                    objectTable[position].memory = mBlockAlloc(memorySize);
                    done = true;
                    break;
                }

        /* if we STILL don't have it then there is nothing */
        /* more we can do */
        if (!done)
            sysError("out of objects", "alloc");
    }

    /* set class and type */
    objectTable[position].referenceCount = 0;
    objectTable[position].STclass = nilobj;
    objectTable[position].size = memorySize;
    return (position << 1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movl %edi, %ebx
cmpl $0x7d0, %edi            # imm = 0x7D0
jge 0x2e1b
movslq %ebx, %rcx
leaq 0xa578(%rip), %rax      # 0xd2a0
movswl (%rax,%rcx,2), %ebp
testl %ebp, %ebp
je 0x2d4b
movslq %ebp, %rdx
shlq $0x4, %rdx
leaq 0xb502(%rip), %rsi      # 0xe240
movzwl (%rdx,%rsi), %edx
movw %dx, (%rax,%rcx,2)
jmp 0x2df1
movswl 0xa54e(%rip), %ebp      # 0xd2a0
testl %ebp, %ebp
je 0x2d84
movslq %ebp, %r14
shlq $0x4, %r14
leaq 0xb4dc(%rip), %r15      # 0xe240
movzwl (%r14,%r15), %eax
movw %ax, 0xa530(%rip)       # 0xd2a0
movw %bx, 0x4(%r14,%r15)
movl %ebx, %edi
callq 0x2ca2
movq %rax, 0x8(%r14,%r15)
jmp 0x2df1
cmpl $0x7d0, %ebx            # imm = 0x7D0
movl $0x7cf, %edx            # imm = 0x7CF
cmovgel %ebx, %edx
cmpq %rcx, %rdx
je 0x2dc0
movswl 0x2(%rax,%rcx,2), %ebp
incq %rcx
testl %ebp, %ebp
je 0x2d92
movslq %ebp, %rdx
shlq $0x4, %rdx
leaq 0xb48f(%rip), %rsi      # 0xe240
movzwl (%rdx,%rsi), %edi
movw %di, (%rax,%rcx,2)
movw %bx, 0x4(%rdx,%rsi)
jmp 0x2df1
cmpl $0x2, %ebx
jl 0x2ddc
movl %ebx, %edx
movl $0x1, %ecx
movswl (%rax,%rcx,2), %ebp
testl %ebp, %ebp
jne 0x2e27
incq %rcx
cmpq %rcx, %rdx
jne 0x2dcc
leaq 0x6396(%rip), %rdi      # 0x9179
leaq 0x639e(%rip), %rsi      # 0x9188
callq 0x4dec
xorl %ebp, %ebp
movslq %ebp, %rax
movq %rax, %rcx
shlq $0x4, %rcx
leaq 0xb43e(%rip), %rdx      # 0xe240
movl $0x0, (%rcx,%rdx)
movw %bx, 0x4(%rcx,%rdx)
addl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movl %ebx, %edi
callq 0x2556
jmp 0x2d1e
movslq %ebp, %r14
shlq $0x4, %r14
leaq 0xb40b(%rip), %r15      # 0xe240
movzwl (%r14,%r15), %edx
movw %dx, (%rax,%rcx,2)
jmp 0x2d70