// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/jpeg/jmemmgr.c
// Function: alloc_small
METHODDEF(void *)
alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
/* Allocate a "small" object */
{
  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  small_pool_ptr hdr_ptr, prev_hdr_ptr;
  char * data_ptr;
  size_t odd_bytes, min_request, slop;

  /* Check for unsatisfiable request (do now to ensure no overflow below) */
  if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(small_pool_hdr)))
    out_of_memory(cinfo, 1);	/* request exceeds malloc's ability */

  /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
  odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
  if (odd_bytes > 0)
    sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;

  /* See if space is available in any existing pool */
  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);	/* safety check */
  prev_hdr_ptr = NULL;
  hdr_ptr = mem->small_list[pool_id];
  while (hdr_ptr != NULL) {
    if (hdr_ptr->hdr.bytes_left >= sizeofobject)
      break;			/* found pool with enough space */
    prev_hdr_ptr = hdr_ptr;
    hdr_ptr = hdr_ptr->hdr.next;
  }

  /* Time to make a new pool? */
  if (hdr_ptr == NULL) {
    /* min_request is what we need now, slop is what will be leftover */
    min_request = sizeofobject + SIZEOF(small_pool_hdr);
    if (prev_hdr_ptr == NULL)	/* first pool in class? */
      slop = first_pool_slop[pool_id];
    else
      slop = extra_pool_slop[pool_id];
    /* Don't ask for more than MAX_ALLOC_CHUNK */
    if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request))
      slop = (size_t) (MAX_ALLOC_CHUNK-min_request);
    /* Try to get space, if fail reduce slop and try again */
    for (;;) {
      hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop);
      if (hdr_ptr != NULL)
	break;
      slop /= 2;
      if (slop < MIN_SLOP)	/* give up when it gets real small */
	out_of_memory(cinfo, 2); /* jpeg_get_small failed */
    }
    mem->total_space_allocated += min_request + slop;
    /* Success, initialize the new pool header and add to end of list */
    hdr_ptr->hdr.next = NULL;
    hdr_ptr->hdr.bytes_used = 0;
    hdr_ptr->hdr.bytes_left = sizeofobject + slop;
    if (prev_hdr_ptr == NULL)	/* first pool in class? */
      mem->small_list[pool_id] = hdr_ptr;
    else
      prev_hdr_ptr->hdr.next = hdr_ptr;
  }

  /* OK, allocate the object from the current pool */
  data_ptr = (char *) (hdr_ptr + 1); /* point to first data byte in pool */
  data_ptr += hdr_ptr->hdr.bytes_used; /* point to place for object */
  hdr_ptr->hdr.bytes_used += sizeofobject;
  hdr_ptr->hdr.bytes_left -= sizeofobject;

  return (void *) data_ptr;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %rbx
movl %esi, %ebp
movq %rdi, %r14
movq 0x8(%rdi), %r15
cmpq $0x3b9ac9e9, %rdx       # imm = 0x3B9AC9E9
jb 0x86062
movq (%r14), %rax
movabsq $0x100000036, %rcx      # imm = 0x100000036
movq %rcx, 0x28(%rax)
movq (%r14), %rax
movq %r14, %rdi
callq *(%rax)
addq $0x7, %rbx
cmpl $0x2, %ebp
jb 0x86080
movq (%r14), %rax
movl $0xe, 0x28(%rax)
movl %ebp, 0x2c(%rax)
movq (%r14), %rax
movq %r14, %rdi
callq *(%rax)
andq $-0x8, %rbx
movslq %ebp, %rax
movq 0x68(%r15,%rax,8), %rcx
testq %rcx, %rcx
je 0x860a9
movq %rcx, %r13
cmpq %rbx, 0x10(%rcx)
jae 0x86165
movq (%r13), %rcx
testq %rcx, %rcx
jne 0x86091
jmp 0x860ac
xorl %r13d, %r13d
testq %r13, %r13
leaq 0x25f2a(%rip), %rcx     # 0xabfe0
leaq 0x25f33(%rip), %rdx     # 0xabff0
cmoveq %rcx, %rdx
movq %r15, 0x10(%rsp)
leaq (%r15,%rax,8), %rbp
addq $0x68, %rbp
movq (%rdx,%rax,8), %rax
movl $0x3b9ac9e8, %r12d      # imm = 0x3B9AC9E8
subq %rbx, %r12
cmpq %r12, %rax
cmovbq %rax, %r12
leaq (%rbx,%r12), %r15
addq $0x18, %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x86ca8
testq %rax, %rax
jne 0x8613e
movq %rbp, 0x8(%rsp)
leaq 0x18(%rbx), %rbp
cmpq $0x63, %r12
ja 0x86122
movq (%r14), %rax
movabsq $0x200000036, %rcx      # imm = 0x200000036
movq %rcx, 0x28(%rax)
movq (%r14), %rax
movq %r14, %rdi
callq *(%rax)
shrq %r12
leaq (%r12,%rbp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x86ca8
testq %rax, %rax
je 0x86103
movq 0x8(%rsp), %rbp
movq 0x10(%rsp), %rcx
addq %r15, 0x98(%rcx)
addq %rbx, %r12
testq %r13, %r13
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %r12, 0x10(%rax)
cmovneq %r13, %rbp
movq %rax, (%rbp)
movq %rax, %r13
movq 0x8(%r13), %rcx
leaq (%rcx,%r13), %rax
addq $0x18, %rax
addq %rbx, %rcx
movq %rcx, 0x8(%r13)
subq %rbx, 0x10(%r13)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq