// File: tomfunkhouser[P]gaps/pkgs/jpeg/jcprepct.c
// Function: create_context_buffer
LOCAL(void)
create_context_buffer (j_compress_ptr cinfo)
{
  my_prep_ptr prep = (my_prep_ptr) cinfo->prep;
  int rgroup_height = cinfo->max_v_samp_factor;
  int ci, i;
  jpeg_component_info * compptr;
  JSAMPARRAY true_buffer, fake_buffer;

  /* Grab enough space for fake row pointers for all the components;
   * we need five row groups' worth of pointers for each component.
   */
  fake_buffer = (JSAMPARRAY)
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
				(cinfo->num_components * 5 * rgroup_height) *
				SIZEOF(JSAMPROW));

  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
       ci++, compptr++) {
    /* Allocate the actual buffer space (3 row groups) for this component.
     * We make the buffer wide enough to allow the downsampler to edge-expand
     * horizontally within the buffer, if it so chooses.
     */
    true_buffer = (*cinfo->mem->alloc_sarray)
      ((j_common_ptr) cinfo, JPOOL_IMAGE,
       (JDIMENSION) (((long) compptr->width_in_blocks * DCTSIZE *
		      cinfo->max_h_samp_factor) / compptr->h_samp_factor),
       (JDIMENSION) (3 * rgroup_height));
    /* Copy true buffer row pointers into the middle of the fake row array */
    MEMCOPY(fake_buffer + rgroup_height, true_buffer,
	    3 * rgroup_height * SIZEOF(JSAMPROW));
    /* Fill in the above and below wraparound pointers */
    for (i = 0; i < rgroup_height; i++) {
      fake_buffer[i] = true_buffer[2 * rgroup_height + i];
      fake_buffer[4 * rgroup_height + i] = true_buffer[i];
    }
    prep->color_buf[ci] = fake_buffer + rgroup_height;
    fake_buffer += 5 * rgroup_height; /* point to space for next component */
  }
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq 0x40(%rsp), %rax
movq 0x1c0(%rax), %rax
movq %rax, 0x38(%rsp)
movq 0x40(%rsp), %rax
movl 0x13c(%rax), %eax
movl %eax, 0x34(%rsp)
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x40(%rsp), %rdi
movq 0x40(%rsp), %rcx
imull $0x5, 0x4c(%rcx), %ecx
imull 0x34(%rsp), %ecx
movslq %ecx, %rdx
shlq $0x3, %rdx
movl $0x1, %esi
callq *%rax
movq %rax, 0x10(%rsp)
movl $0x0, 0x30(%rsp)
movq 0x40(%rsp), %rax
movq 0x58(%rax), %rax
movq %rax, 0x20(%rsp)
movl 0x30(%rsp), %eax
movq 0x40(%rsp), %rcx
cmpl 0x4c(%rcx), %eax
jge 0xda475
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x40(%rsp), %rdi
movq 0x20(%rsp), %rax
movl 0x1c(%rax), %eax
shlq $0x3, %rax
movq 0x40(%rsp), %rcx
movslq 0x138(%rcx), %rcx
imulq %rcx, %rax
movq 0x20(%rsp), %rcx
movslq 0x8(%rcx), %rcx
cqto
idivq %rcx
movq %rax, %rcx
movq 0x8(%rsp), %rax
movl %ecx, %edx
imull $0x3, 0x34(%rsp), %ecx
movl $0x1, %esi
callq *%rax
movq %rax, 0x18(%rsp)
movq 0x10(%rsp), %rdi
movslq 0x34(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq 0x18(%rsp), %rsi
imull $0x3, 0x34(%rsp), %eax
movslq %eax, %rdx
shlq $0x3, %rdx
callq 0xf3d0
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x34(%rsp), %eax
jge 0xda41e
movq 0x18(%rsp), %rax
movl 0x34(%rsp), %ecx
shll %ecx
addl 0x2c(%rsp), %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdx
movq 0x10(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x18(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
movq 0x10(%rsp), %rax
movl 0x34(%rsp), %ecx
shll $0x2, %ecx
addl 0x2c(%rsp), %ecx
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0xda3be
movq 0x10(%rsp), %rdx
movslq 0x34(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rdx
movq 0x38(%rsp), %rax
movslq 0x30(%rsp), %rcx
movq %rdx, 0x10(%rax,%rcx,8)
imull $0x5, 0x34(%rsp), %ecx
movq 0x10(%rsp), %rax
movslq %ecx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
movq 0x20(%rsp), %rax
addq $0x60, %rax
movq %rax, 0x20(%rsp)
jmp 0xda321
addq $0x48, %rsp
retq
nopw (%rax,%rax)