// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/jpeg/jdhuff.c
// Function: jpeg_fill_bit_buffer
GLOBAL(jboolean)
jpeg_fill_bit_buffer (bitread_working_state * state,
		      register bit_buf_type get_buffer, register int bits_left,
		      int nbits)
/* Load up the bit buffer to a depth of at least nbits */
{
  /* Copy heavily used state fields into locals (hopefully registers) */
  register const JOCTET * next_input_byte = state->next_input_byte;
  register size_t bytes_in_buffer = state->bytes_in_buffer;
  j_decompress_ptr cinfo = state->cinfo;

  /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
  /* (It is assumed that no request will be for more than that many bits.) */
  /* We fail to do so only if we hit a marker or are forced to suspend. */

  if (cinfo->unread_marker == 0) {	/* cannot advance past a marker */
    while (bits_left < MIN_GET_BITS) {
      register int c;

      /* Attempt to read a byte */
      if (bytes_in_buffer == 0) {
	if (! (*cinfo->src->fill_input_buffer) (cinfo))
	  return FALSE;
	next_input_byte = cinfo->src->next_input_byte;
	bytes_in_buffer = cinfo->src->bytes_in_buffer;
      }
      bytes_in_buffer--;
      c = GETJOCTET(*next_input_byte++);

      /* If it's 0xFF, check and discard stuffed zero byte */
      if (c == 0xFF) {
	/* Loop here to discard any padding FF's on terminating marker,
	 * so that we can save a valid unread_marker value.  NOTE: we will
	 * accept multiple FF's followed by a 0 as meaning a single FF data
	 * byte.  This data pattern is not valid according to the standard.
	 */
	do {
	  if (bytes_in_buffer == 0) {
	    if (! (*cinfo->src->fill_input_buffer) (cinfo))
	      return FALSE;
	    next_input_byte = cinfo->src->next_input_byte;
	    bytes_in_buffer = cinfo->src->bytes_in_buffer;
	  }
	  bytes_in_buffer--;
	  c = GETJOCTET(*next_input_byte++);
	} while (c == 0xFF);

	if (c == 0) {
	  /* Found FF/00, which represents an FF data byte */
	  c = 0xFF;
	} else {
	  /* Oops, it's actually a marker indicating end of compressed data.
	   * Save the marker code for later use.
	   * Fine point: it might appear that we should save the marker into
	   * bitread working state, not straight into permanent state.  But
	   * once we have hit a marker, we cannot need to suspend within the
	   * current MCU, because we will read no more bytes from the data
	   * source.  So it is OK to update permanent state right away.
	   */
	  cinfo->unread_marker = c;
	  /* See if we need to insert some fake zero bits. */
	  goto no_more_bytes;
	}
      }

      /* OK, load c into get_buffer */
      get_buffer = (get_buffer << 8) | c;
      bits_left += 8;
    } /* end while */
  } else {
  no_more_bytes:
    /* We get here if we've read the marker that terminates the compressed
     * data segment.  There should be enough bits in the buffer register
     * to satisfy the request; if so, no problem.
     */
    if (nbits > bits_left) {
      /* Uh-oh.  Report corrupted data to user and stuff zeroes into
       * the data stream, so that we can produce some kind of image.
       * We use a nonvolatile flag to ensure that only one warning message
       * appears per data segment.
       */
      if (! cinfo->entropy->insufficient_data) {
	WARNMS(cinfo, JWRN_HIT_MARKER);
	cinfo->entropy->insufficient_data = TRUE;
      }
      /* Fill the buffer with zero bits */
      get_buffer <<= MIN_GET_BITS - bits_left;
      bits_left = MIN_GET_BITS;
    }
  }

  /* Unload the local registers */
  state->next_input_byte = next_input_byte;
  state->bytes_in_buffer = bytes_in_buffer;
  state->get_buffer = get_buffer;
  state->bits_left = bits_left;

  return TRUE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movq (%rdi), %r13
movq 0x8(%rdi), %r12
movq 0x20(%rdi), %r15
cmpl $0x0, 0x21c(%r15)
je 0x7ec6a
cmpl %ecx, %ebp
jge 0x7ec48
movq 0x250(%r15), %rax
cmpl $0x0, 0x10(%rax)
jne 0x7ec3b
movq (%r15), %rax
movl $0x75, 0x28(%rax)
movq %r15, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq *0x8(%rax)
movq 0x250(%r15), %rax
movl $0x1, 0x10(%rax)
movb $0x19, %cl
subb %bpl, %cl
shlq %cl, %r14
movl $0x19, %ebp
movq %r13, (%rbx)
movq %r12, 0x8(%rbx)
movq %r14, 0x10(%rbx)
movl %ebp, 0x18(%rbx)
movl $0x1, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ecx, 0x4(%rsp)
cmpl $0x18, %ebp
jg 0x7ec48
testq %r12, %r12
jne 0x7ec91
movq 0x28(%r15), %rax
movq %r15, %rdi
callq *0x18(%rax)
testl %eax, %eax
je 0x7eceb
movq 0x28(%r15), %rax
movq (%rax), %r13
movq 0x8(%rax), %r12
decq %r12
movzbl (%r13), %eax
incq %r13
cmpq $0xff, %rax
jne 0x7ecdd
testq %r12, %r12
jne 0x7ecc2
movq 0x28(%r15), %rax
movq %r15, %rdi
callq *0x18(%rax)
testl %eax, %eax
je 0x7eceb
movq 0x28(%r15), %rax
movq (%rax), %r13
movq 0x8(%rax), %r12
decq %r12
movzbl (%r13), %eax
incq %r13
cmpl $0xff, %eax
je 0x7eca4
testl %eax, %eax
jne 0x7ecf5
movl $0xff, %eax
shlq $0x8, %r14
orq %rax, %r14
addl $0x8, %ebp
xorl %eax, %eax
jmp 0x7ed01
movl $0x1, %eax
xorl %r12d, %r12d
jmp 0x7ed01
movl %eax, 0x21c(%r15)
movl $0x6, %eax
testl %eax, %eax
je 0x7ec6e
cmpl $0x6, %eax
jne 0x7ed17
movl 0x4(%rsp), %ecx
jmp 0x7ec07
xorl %eax, %eax
jmp 0x7ec5b