// File: /workspace/github_cpp_makefile_star2_O2/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 0x79554
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 0x79572
movq (%r14), %rax
movl $0xe, 0x28(%rax)
movl %ebp, 0x2c(%rax)
movq (%r14), %rax
movq %r14, %rdi
callq *(%rax)
andq $-0x8, %rbx
movslq %ebp, %rcx
leaq (%r15,%rcx,8), %rdx
addq $0x68, %rdx
xorl %ebp, %ebp
movq %rdx, %rax
movq (%rax), %rax
testq %rax, %rax
je 0x7959c
movq %rax, %rbp
cmpq %rbx, 0x10(%rax)
jb 0x79586
jmp 0x7963c
movq %rdx, 0x8(%rsp)
movq %r15, 0x10(%rsp)
testq %rbp, %rbp
leaq 0x23750(%rip), %rax     # 0x9cd00
leaq 0x23759(%rip), %rdx     # 0x9cd10
cmoveq %rax, %rdx
leaq 0x18(%rbx), %r12
movq (%rdx,%rcx,8), %rax
movl $0x3b9ac9e8, %r13d      # imm = 0x3B9AC9E8
subq %rbx, %r13
cmpq %r13, %rax
cmovbq %rax, %r13
leaq (%r12,%r13), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x7a110
testq %rax, %rax
jne 0x79614
movq %r13, %r15
shrq %r15
cmpq $0x63, %r13
movq %r15, %r13
ja 0x795d3
movq (%r14), %rax
movabsq $0x200000036, %rcx      # imm = 0x200000036
movq %rcx, 0x28(%rax)
movq (%r14), %rax
movq %r14, %rdi
callq *(%rax)
movq %r15, %r13
jmp 0x795d3
movq 0x10(%rsp), %rcx
addq %r15, 0x98(%rcx)
addq %rbx, %r13
testq %rbp, %rbp
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %r13, 0x10(%rax)
movq 0x8(%rsp), %rcx
cmovneq %rbp, %rcx
movq %rax, (%rcx)
movq 0x8(%rax), %rdx
leaq (%rax,%rdx), %rcx
addq $0x18, %rcx
addq %rbx, %rdx
movq %rdx, 0x8(%rax)
subq %rbx, 0x10(%rax)
movq %rcx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq