// File: tomfunkhouser[P]gaps/pkgs/jpeg/jmemmgr.c
// Function: alloc_sarray
METHODDEF(JSAMPARRAY)
alloc_sarray (j_common_ptr cinfo, int pool_id,
	      JDIMENSION samplesperrow, JDIMENSION numrows)
/* Allocate a 2-D sample array */
{
  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  JSAMPARRAY result;
  JSAMPROW workspace;
  JDIMENSION rowsperchunk, currow, i;
  long ltemp;

  /* Calculate max # of rows allowed in one allocation chunk */
  ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
	  ((long) samplesperrow * SIZEOF(JSAMPLE));
  if (ltemp <= 0)
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
  if (ltemp < (long) numrows)
    rowsperchunk = (JDIMENSION) ltemp;
  else
    rowsperchunk = numrows;
  mem->last_rowsperchunk = rowsperchunk;

  /* Get space for row pointers (small object) */
  result = (JSAMPARRAY) alloc_small(cinfo, pool_id,
				    (size_t) (numrows * SIZEOF(JSAMPROW)));

  /* Get the rows themselves (large objects) */
  currow = 0;
  while (currow < numrows) {
    rowsperchunk = MIN(rowsperchunk, numrows - currow);
    workspace = (JSAMPROW) alloc_large(cinfo, pool_id,
	(size_t) ((size_t) rowsperchunk * (size_t) samplesperrow
		  * SIZEOF(JSAMPLE)));
    for (i = rowsperchunk; i > 0; i--) {
      result[currow++] = workspace;
      workspace += samplesperrow;
    }
  }

  return result;
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movl %esi, 0x4c(%rsp)
movl %edx, 0x48(%rsp)
movl %ecx, 0x44(%rsp)
movq 0x50(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x38(%rsp)
movl 0x48(%rsp), %eax
movl %eax, %ecx
shlq $0x0, %rcx
movl $0x3b9ac9e8, %eax       # imm = 0x3B9AC9E8
xorl %edx, %edx
divq %rcx
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
jg 0xf9f45
movq 0x50(%rsp), %rax
movq (%rax), %rax
movl $0x46, 0x28(%rax)
movq 0x50(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x50(%rsp), %rdi
callq *%rax
movq 0x10(%rsp), %rax
movl 0x44(%rsp), %ecx
cmpq %rcx, %rax
jge 0xf9f5e
movq 0x10(%rsp), %rax
movl %eax, 0x24(%rsp)
jmp 0xf9f66
movl 0x44(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %ecx
movq 0x38(%rsp), %rax
movl %ecx, 0xa0(%rax)
movq 0x50(%rsp), %rdi
movl 0x4c(%rsp), %esi
movl 0x44(%rsp), %eax
movl %eax, %edx
shlq $0x3, %rdx
callq 0xf9b10
movq %rax, 0x30(%rsp)
movl $0x0, 0x20(%rsp)
movl 0x20(%rsp), %eax
cmpl 0x44(%rsp), %eax
jae 0xfa04c
movl 0x24(%rsp), %eax
movl 0x44(%rsp), %ecx
subl 0x20(%rsp), %ecx
cmpl %ecx, %eax
jae 0xf9fc2
movl 0x24(%rsp), %eax
movl %eax, 0xc(%rsp)
jmp 0xf9fce
movl 0x44(%rsp), %eax
subl 0x20(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0x50(%rsp), %rdi
movl 0x4c(%rsp), %esi
movl 0x24(%rsp), %eax
movl %eax, %edx
movl 0x48(%rsp), %eax
imulq %rax, %rdx
shlq $0x0, %rdx
callq 0xf9d90
movq %rax, 0x28(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x1c(%rsp)
cmpl $0x0, 0x1c(%rsp)
jbe 0xfa047
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rax
movl 0x20(%rsp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, 0x20(%rsp)
movl %ecx, %ecx
movq %rdx, (%rax,%rcx,8)
movl 0x48(%rsp), %ecx
movq 0x28(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x28(%rsp)
movl 0x1c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0xfa003
jmp 0xf9f9a
movq 0x30(%rsp), %rax
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)