// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/jpeg/jmemmgr.c
// Function: access_virt_sarray
METHODDEF(JSAMPARRAY)
access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
		    JDIMENSION start_row, JDIMENSION num_rows,
		    jboolean writable)
/* Access the part of a virtual sample array starting at start_row */
/* and extending for num_rows rows.  writable is true if  */
/* caller intends to modify the accessed area. */
{
  JDIMENSION end_row = start_row + num_rows;
  JDIMENSION undef_row;

  /* debugging check */
  if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
      ptr->mem_buffer == NULL)
    ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);

  /* Make the desired part of the virtual array accessible */
  if (start_row < ptr->cur_start_row ||
      end_row > ptr->cur_start_row+ptr->rows_in_mem) {
    if (! ptr->b_s_open)
      ERREXIT(cinfo, JERR_VIRTUAL_BUG);
    /* Flush old buffer contents if necessary */
    if (ptr->dirty) {
      do_sarray_io(cinfo, ptr, TRUE);
      ptr->dirty = FALSE;
    }
    /* Decide what part of virtual array to access.
     * Algorithm: if target address > current window, assume forward scan,
     * load starting at target address.  If target address < current window,
     * assume backward scan, load so that target area is top of window.
     * Note that when switching from forward write to forward read, will have
     * start_row = 0, so the limiting case applies and we load from 0 anyway.
     */
    if (start_row > ptr->cur_start_row) {
      ptr->cur_start_row = start_row;
    } else {
      /* use long arithmetic here to avoid overflow & unsigned problems */
      long ltemp;

      ltemp = (long) end_row - (long) ptr->rows_in_mem;
      if (ltemp < 0)
	ltemp = 0;		/* don't fall off front end of file */
      ptr->cur_start_row = (JDIMENSION) ltemp;
    }
    /* Read in the selected part of the array.
     * During the initial write pass, we will do no actual read
     * because the selected part is all undefined.
     */
    do_sarray_io(cinfo, ptr, FALSE);
  }
  /* Ensure the accessed part of the array is defined; prezero if needed.
   * To improve locality of access, we only prezero the part of the array
   * that the caller is about to access, not the entire in-memory array.
   */
  if (ptr->first_undef_row < end_row) {
    if (ptr->first_undef_row < start_row) {
      if (writable)		/* writer skipped over a section of array */
	ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
      undef_row = start_row;	/* but reader is allowed to read ahead */
    } else {
      undef_row = ptr->first_undef_row;
    }
    if (writable)
      ptr->first_undef_row = end_row;
    if (ptr->pre_zero) {
      size_t bytesperrow = (size_t) ptr->samplesperrow * SIZEOF(JSAMPLE);
      undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
      end_row -= ptr->cur_start_row;
      while (undef_row < end_row) {
	jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
	undef_row++;
      }
    } else {
      if (! writable)		/* reader looking at undefined data */
	ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
    }
  }
  /* Flag the buffer dirty if caller will write in it */
  if (writable)
    ptr->dirty = TRUE;
  /* Return address of proper part of the buffer */
  return ptr->mem_buffer + (start_row - ptr->cur_start_row);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %r8d, %ebp
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
leal (%rcx,%rbx), %r12d
cmpl 0x8(%rsi), %r12d
ja 0x8751b
cmpl %ecx, 0x10(%r14)
jb 0x8751b
cmpq $0x0, (%r14)
jne 0x8752a
movq (%r15), %rax
movl $0x16, 0x28(%rax)
movq %r15, %rdi
callq *(%rax)
movl 0x1c(%r14), %eax
cmpl %ebx, %eax
ja 0x8753b
addl 0x14(%r14), %eax
cmpl %eax, %r12d
jbe 0x8759c
cmpl $0x0, 0x2c(%r14)
jne 0x87551
movq (%r15), %rax
movl $0x45, 0x28(%rax)
movq %r15, %rdi
callq *(%rax)
cmpl $0x0, 0x28(%r14)
je 0x87570
movq %r15, %rdi
movq %r14, %rsi
movl $0x1, %edx
callq 0x87901
movl $0x0, 0x28(%r14)
movl %ebx, %eax
cmpl %ebx, 0x1c(%r14)
jb 0x8758b
movl %r12d, %ecx
movl 0x14(%r14), %eax
subq %rax, %rcx
xorl %eax, %eax
testq %rcx, %rcx
cmovgq %rcx, %rax
movl %eax, 0x1c(%r14)
movq %r15, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x87901
movl 0x20(%r14), %eax
cmpl %r12d, %eax
jae 0x87613
cmpl %ebx, %eax
jae 0x875c0
movl %ebx, %eax
testl %ebp, %ebp
je 0x875c0
movq (%r15), %rax
movl $0x16, 0x28(%rax)
movq %r15, %rdi
callq *(%rax)
movl %ebx, %eax
testl %ebp, %ebp
je 0x875c8
movl %r12d, 0x20(%r14)
cmpl $0x0, 0x24(%r14)
je 0x87600
movl 0x1c(%r14), %ecx
subl %ecx, %eax
subl %ecx, %r12d
cmpl %r12d, %eax
jae 0x87613
movl 0xc(%r14), %r15d
movl %eax, %r13d
movl %r12d, %r12d
movq (%r14), %rax
movq (%rax,%r13,8), %rdi
movq %r15, %rsi
callq 0x86b1f
incq %r13
cmpq %r13, %r12
jne 0x875e7
jmp 0x87613
testl %ebp, %ebp
jne 0x87613
movq (%r15), %rax
movl $0x16, 0x28(%rax)
movq %r15, %rdi
callq *(%rax)
testl %ebp, %ebp
je 0x8761f
movl $0x1, 0x28(%r14)
subl 0x1c(%r14), %ebx
shlq $0x3, %rbx
addq (%r14), %rbx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq