// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/jpeg/jmemmgr.c
// Function: realize_virt_arrays
METHODDEF(void)
realize_virt_arrays (j_common_ptr cinfo)
/* Allocate the in-memory buffers for any unrealized virtual arrays */
{
  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  long space_per_minheight, maximum_space, avail_mem;
  long minheights, max_minheights;
  jvirt_sarray_ptr sptr;
  jvirt_barray_ptr bptr;

  /* Compute the minimum space needed (maxaccess rows in each buffer)
   * and the maximum space needed (full image height in each buffer).
   * These may be of use to the system-dependent jpeg_mem_available routine.
   */
  space_per_minheight = 0;
  maximum_space = 0;
  for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
    if (sptr->mem_buffer == NULL) { /* if not realized yet */
      space_per_minheight += (long) sptr->maxaccess *
			     (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
      maximum_space += (long) sptr->rows_in_array *
		       (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
    }
  }
  for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
    if (bptr->mem_buffer == NULL) { /* if not realized yet */
      space_per_minheight += (long) bptr->maxaccess *
			     (long) bptr->blocksperrow * SIZEOF(JBLOCK);
      maximum_space += (long) bptr->rows_in_array *
		       (long) bptr->blocksperrow * SIZEOF(JBLOCK);
    }
  }

  if (space_per_minheight <= 0)
    return;			/* no unrealized arrays, no work */

  /* Determine amount of memory to actually use; this is system-dependent. */
  avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,
				 mem->total_space_allocated);

  /* If the maximum space needed is available, make all the buffers full
   * height; otherwise parcel it out with the same number of minheights
   * in each buffer.
   */
  if (avail_mem >= maximum_space)
    max_minheights = 1000000000L;
  else {
    max_minheights = avail_mem / space_per_minheight;
    /* If there doesn't seem to be enough space, try to get the minimum
     * anyway.  This allows a "stub" implementation of jpeg_mem_available().
     */
    if (max_minheights <= 0)
      max_minheights = 1;
  }

  /* Allocate the in-memory buffers and initialize backing store as needed. */

  for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
    if (sptr->mem_buffer == NULL) { /* if not realized yet */
      minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;
      if (minheights <= max_minheights) {
	/* This buffer fits in memory */
	sptr->rows_in_mem = sptr->rows_in_array;
      } else {
	/* It doesn't fit in memory, create backing store. */
	sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess);
	jpeg_open_backing_store(cinfo, & sptr->b_s_info,
				(long) sptr->rows_in_array *
				(long) sptr->samplesperrow *
				(long) SIZEOF(JSAMPLE));
	sptr->b_s_open = TRUE;
      }
      sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE,
				      sptr->samplesperrow, sptr->rows_in_mem);
      sptr->rowsperchunk = mem->last_rowsperchunk;
      sptr->cur_start_row = 0;
      sptr->first_undef_row = 0;
      sptr->dirty = FALSE;
    }
  }

  for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
    if (bptr->mem_buffer == NULL) { /* if not realized yet */
      minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;
      if (minheights <= max_minheights) {
	/* This buffer fits in memory */
	bptr->rows_in_mem = bptr->rows_in_array;
      } else {
	/* It doesn't fit in memory, create backing store. */
	bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess);
	jpeg_open_backing_store(cinfo, & bptr->b_s_info,
				(long) bptr->rows_in_array *
				(long) bptr->blocksperrow *
				(long) SIZEOF(JBLOCK));
	bptr->b_s_open = TRUE;
      }
      bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE,
				      bptr->blocksperrow, bptr->rows_in_mem);
      bptr->rowsperchunk = mem->last_rowsperchunk;
      bptr->cur_start_row = 0;
      bptr->first_undef_row = 0;
      bptr->dirty = FALSE;
    }
  }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdi, %rbx
movq 0x8(%rdi), %r12
movq 0x88(%r12), %rax
testq %rax, %rax
je 0x87334
xorl %r14d, %r14d
xorl %r15d, %r15d
cmpq $0x0, (%rax)
jne 0x87329
movl 0x10(%rax), %ecx
movl 0x8(%rax), %edx
movl 0xc(%rax), %esi
imulq %rsi, %rcx
addq %rcx, %r14
imulq %rsi, %rdx
addq %rdx, %r15
movq 0x30(%rax), %rax
testq %rax, %rax
jne 0x8730c
jmp 0x8733a
xorl %r15d, %r15d
xorl %r14d, %r14d
movq 0x90(%r12), %rax
testq %rax, %rax
je 0x87372
cmpq $0x0, (%rax)
jne 0x8736c
movl 0x10(%rax), %ecx
movl 0x8(%rax), %edx
movl 0xc(%rax), %esi
imulq %rsi, %rcx
shlq $0x7, %rcx
addq %rcx, %r14
imulq %rsi, %rdx
shlq $0x7, %rdx
addq %rdx, %r15
movq 0x30(%rax), %rax
jmp 0x87342
testq %r14, %r14
jle 0x874e5
movq 0x98(%r12), %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x87a98
movl $0x3b9aca00, %r13d      # imm = 0x3B9ACA00
cmpq %r15, %rax
jge 0x873af
cqto
idivq %r14
cmpq $0x2, %rax
movl $0x1, %r13d
cmovgeq %rax, %r13
movq 0x88(%r12), %r14
testq %r14, %r14
je 0x87448
cmpq $0x0, (%r14)
jne 0x8743f
movl 0x8(%r14), %ecx
movl 0x10(%r14), %esi
leaq -0x1(%rcx), %rax
cqto
idivq %rsi
movl $0x14, %edx
cmpq %r13, %rax
jl 0x87407
imull %r13d, %esi
movl %esi, 0x14(%r14)
leaq 0x38(%r14), %rsi
movl 0xc(%r14), %edx
imulq %rcx, %rdx
movq %rbx, %rdi
callq 0x87a9c
movl $0x1, %ecx
movl $0x2c, %edx
movl %ecx, (%r14,%rdx)
movl 0xc(%r14), %edx
movl 0x14(%r14), %ecx
movq %rbx, %rdi
movl $0x1, %esi
callq 0x8704f
movq %rax, (%r14)
movl 0xa0(%r12), %eax
movl %eax, 0x18(%r14)
movq $0x0, 0x1c(%r14)
movl $0x0, 0x28(%r14)
movq 0x30(%r14), %r14
jmp 0x873b7
movq 0x90(%r12), %r14
testq %r14, %r14
je 0x874e5
cmpq $0x0, (%r14)
jne 0x874dc
movl 0x8(%r14), %ecx
movl 0x10(%r14), %esi
leaq -0x1(%rcx), %rax
cqto
idivq %rsi
movl $0x14, %edx
cmpq %r13, %rax
jl 0x874a4
imull %r13d, %esi
movl %esi, 0x14(%r14)
leaq 0x38(%r14), %rsi
movl 0xc(%r14), %edx
imulq %rcx, %rdx
shlq $0x7, %rdx
movq %rbx, %rdi
callq 0x87a9c
movl $0x1, %ecx
movl $0x2c, %edx
movl %ecx, (%r14,%rdx)
movl 0xc(%r14), %edx
movl 0x14(%r14), %ecx
movq %rbx, %rdi
movl $0x1, %esi
callq 0x8710a
movq %rax, (%r14)
movl 0xa0(%r12), %eax
movl %eax, 0x18(%r14)
movq $0x0, 0x1c(%r14)
movl $0x0, 0x28(%r14)
movq 0x30(%r14), %r14
jmp 0x87450
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq