// File: /workspace/github_c_makefile_star2_O2/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 0x2a44
movslq %ebx, %rcx
leaq 0xa90d(%rip), %rax      # 0xd290
movswl (%rax,%rcx,2), %ebp
testl %ebp, %ebp
je 0x29a6
movslq %ebp, %rdx
shlq $0x4, %rdx
leaq 0xb897(%rip), %rsi      # 0xe230
movzwl (%rdx,%rsi), %edx
movw %dx, (%rax,%rcx,2)
jmp 0x2a8b
movswl 0xa8e3(%rip), %ebp      # 0xd290
testl %ebp, %ebp
je 0x29e2
movslq %ebp, %r14
shlq $0x4, %r14
leaq 0xb871(%rip), %r15      # 0xe230
movzwl (%r14,%r15), %eax
movw %ax, 0xa8c5(%rip)       # 0xd290
movw %bx, 0x4(%r14,%r15)
movl %ebx, %edi
callq 0x28ff
movq %rax, 0x8(%r14,%r15)
jmp 0x2a8b
movq %rcx, %rdx
cmpq $0x7cf, %rdx            # imm = 0x7CF
jge 0x2a17
movswl 0x2(%rax,%rdx,2), %ebp
incq %rdx
testl %ebp, %ebp
je 0x29e5
movslq %ebp, %rcx
shlq $0x4, %rcx
leaq 0xb828(%rip), %rsi      # 0xe230
movzwl (%rcx,%rsi), %edi
movw %di, (%rax,%rdx,2)
movw %bx, 0x4(%rcx,%rsi)
jmp 0x2a8b
pushq $0x1
popq %rdx
cmpq %rcx, %rdx
jge 0x2a76
movswl (%rax,%rdx,2), %ebp
incq %rdx
testl %ebp, %ebp
je 0x2a1a
movslq %ebp, %r14
shlq $0x4, %r14
leaq 0xb7f8(%rip), %r15      # 0xe230
movzwl (%r14,%r15), %ecx
movw %cx, -0x2(%rax,%rdx,2)
jmp 0x29cb
movq 0xa5a5(%rip), %rax      # 0xcff0
movq (%rax), %rdi
leaq 0x676b(%rip), %rsi      # 0x91c0
movl %ebx, %edx
xorl %eax, %eax
callq 0x2170
leaq 0x66d1(%rip), %rdi      # 0x9136
leaq 0x66eb(%rip), %rsi      # 0x9157
callq 0x4938
jmp 0x2979
leaq 0x66e6(%rip), %rdi      # 0x9163
leaq 0x66ee(%rip), %rsi      # 0x9172
callq 0x4938
xorl %ebp, %ebp
movslq %ebp, %rax
movq %rax, %rcx
shlq $0x4, %rcx
leaq 0xb794(%rip), %rdx      # 0xe230
andl $0x0, (%rcx,%rdx)
movw %bx, 0x4(%rcx,%rdx)
addl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq