// File: /workspace/github_c_makefile_star2_O1/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 0x2e11
movslq %ebx, %rcx
leaq 0xa56c(%rip), %rax      # 0xd2a0
movswl (%rax,%rcx,2), %ebp
testl %ebp, %ebp
je 0x2d57
movslq %ebp, %rdx
shlq $0x4, %rdx
leaq 0xb4f6(%rip), %rsi      # 0xe240
movzwl (%rdx,%rsi), %edx
movw %dx, (%rax,%rcx,2)
jmp 0x2e5f
movswl 0xa542(%rip), %ebp      # 0xd2a0
testl %ebp, %ebp
je 0x2d93
movslq %ebp, %r14
shlq $0x4, %r14
leaq 0xb4d0(%rip), %r15      # 0xe240
movzwl (%r14,%r15), %eax
movw %ax, 0xa524(%rip)       # 0xd2a0
movw %bx, 0x4(%r14,%r15)
movl %ebx, %edi
callq 0x2cb0
movq %rax, 0x8(%r14,%r15)
jmp 0x2e5f
cmpl $0x7d0, %ebx            # imm = 0x7D0
movl $0x7cf, %edx            # imm = 0x7CF
cmovgel %ebx, %edx
cmpq %rcx, %rdx
je 0x2ddf
movswl 0x2(%rax,%rcx,2), %ebp
incq %rcx
testl %ebp, %ebp
je 0x2da1
leaq -0x1(%rcx), %rdx
cmpq $0x7cf, %rdx            # imm = 0x7CF
setge %dl
movslq %ebp, %rsi
shlq $0x4, %rsi
leaq 0xb472(%rip), %rdi      # 0xe240
movzwl (%rsi,%rdi), %r8d
movw %r8w, (%rax,%rcx,2)
movw %bx, 0x4(%rsi,%rdi)
jmp 0x2deb
cmpq $0x7cf, %rcx            # imm = 0x7CF
setge %dl
xorl %ebp, %ebp
testb %dl, %dl
je 0x2e46
movb $0x1, %cl
cmpl $0x2, %ebx
jl 0x2e48
movl %ebx, %esi
movl $0x1, %edx
movswl (%rax,%rdx,2), %ebp
testl %ebp, %ebp
jne 0x2e1d
incq %rdx
cmpq %rdx, %rsi
jne 0x2dfd
xorl %ebp, %ebp
jmp 0x2e48
movl %ebx, %edi
callq 0x2556
jmp 0x2d2a
movslq %ebp, %r14
shlq $0x4, %r14
leaq 0xb415(%rip), %r15      # 0xe240
movzwl (%r14,%r15), %ecx
movw %cx, (%rax,%rdx,2)
movw %bx, 0x4(%r14,%r15)
movl %ebx, %edi
callq 0x2cb0
movq %rax, 0x8(%r14,%r15)
xorl %ecx, %ecx
testb %cl, %cl
je 0x2e5f
leaq 0x6310(%rip), %rdi      # 0x9163
leaq 0x6318(%rip), %rsi      # 0x9172
callq 0x4dfc
movslq %ebp, %rax
movq %rax, %rcx
shlq $0x4, %rcx
leaq 0xb3d0(%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