// File: tomfunkhouser[P]gaps/pkgs/png/pngrutil.c
// Function: png_handle_bKGD
void /* PRIVATE */
png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
{
   png_size_t truelen;
   png_byte buf[6];

   png_debug(1, "in png_handle_bKGD");

   if (!(png_ptr->mode & PNG_HAVE_IHDR))
      png_error(png_ptr, "Missing IHDR before bKGD");
   else if (png_ptr->mode & PNG_HAVE_IDAT)
   {
      png_warning(png_ptr, "Invalid bKGD after IDAT");
      png_crc_finish(png_ptr, length);
      return;
   }
   else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
            !(png_ptr->mode & PNG_HAVE_PLTE))
   {
      png_warning(png_ptr, "Missing PLTE before bKGD");
      png_crc_finish(png_ptr, length);
      return;
   }
   else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
   {
      png_warning(png_ptr, "Duplicate bKGD chunk");
      png_crc_finish(png_ptr, length);
      return;
   }

   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
      truelen = 1;
   else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
      truelen = 6;
   else
      truelen = 2;

   if (length != truelen)
   {
      png_warning(png_ptr, "Incorrect bKGD chunk length");
      png_crc_finish(png_ptr, length);
      return;
   }

   png_crc_read(png_ptr, buf, truelen);
   if (png_crc_finish(png_ptr, 0))
      return;

   /* We convert the index value into RGB components so that we can allow
    * arbitrary RGB values for background when we have transparency, and
    * so it is easy to determine the RGB values of the background color
    * from the info_ptr struct. */
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
   {
      png_ptr->background.index = buf[0];
      if (info_ptr && info_ptr->num_palette)
      {
          if (buf[0] >= info_ptr->num_palette)
          {
             png_warning(png_ptr, "Incorrect bKGD chunk index value");
             return;
          }
          png_ptr->background.red =
             (png_uint_16)png_ptr->palette[buf[0]].red;
          png_ptr->background.green =
             (png_uint_16)png_ptr->palette[buf[0]].green;
          png_ptr->background.blue =
             (png_uint_16)png_ptr->palette[buf[0]].blue;
      }
   }
   else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
   {
      png_ptr->background.red =
      png_ptr->background.green =
      png_ptr->background.blue =
      png_ptr->background.gray = png_get_uint_16(buf);
   }
   else
   {
      png_ptr->background.red = png_get_uint_16(buf);
      png_ptr->background.green = png_get_uint_16(buf + 2);
      png_ptr->background.blue = png_get_uint_16(buf + 4);
   }

   png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background));
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movl %edx, 0x14(%rsp)
movq 0x20(%rsp), %rax
movl 0x11c(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x12557b
movq 0x20(%rsp), %rdi
leaq 0x1a366(%rip), %rsi     # 0x13f8d7
callq 0x113860
jmp 0x12563c
movq 0x20(%rsp), %rax
movl 0x11c(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x1255b2
movq 0x20(%rsp), %rdi
leaq 0x1a356(%rip), %rsi     # 0x13f8f0
callq 0x113940
movq 0x20(%rsp), %rdi
movl 0x14(%rsp), %esi
callq 0x122b90
jmp 0x125899
movq 0x20(%rsp), %rax
movzbl 0x24e(%rax), %eax
cmpl $0x3, %eax
jne 0x1255fa
movq 0x20(%rsp), %rax
movl 0x11c(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x1255fa
movq 0x20(%rsp), %rdi
leaq 0x1a326(%rip), %rsi     # 0x13f908
callq 0x113940
movq 0x20(%rsp), %rdi
movl 0x14(%rsp), %esi
callq 0x122b90
jmp 0x125899
cmpq $0x0, 0x18(%rsp)
je 0x125636
movq 0x18(%rsp), %rax
movl 0x8(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x125636
movq 0x20(%rsp), %rdi
leaq 0x1a303(%rip), %rsi     # 0x13f921
callq 0x113940
movq 0x20(%rsp), %rdi
movl 0x14(%rsp), %esi
callq 0x122b90
jmp 0x125899
jmp 0x125638
jmp 0x12563a
jmp 0x12563c
movq 0x20(%rsp), %rax
movzbl 0x24e(%rax), %eax
cmpl $0x3, %eax
jne 0x125658
movq $0x1, 0x8(%rsp)
jmp 0x125682
movq 0x20(%rsp), %rax
movzbl 0x24e(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x125677
movq $0x6, 0x8(%rsp)
jmp 0x125680
movq $0x2, 0x8(%rsp)
jmp 0x125682
movl 0x14(%rsp), %eax
cmpq 0x8(%rsp), %rax
je 0x1256b1
movq 0x20(%rsp), %rdi
leaq 0x1a29d(%rip), %rsi     # 0x13f936
callq 0x113940
movq 0x20(%rsp), %rdi
movl 0x14(%rsp), %esi
callq 0x122b90
jmp 0x125899
movq 0x20(%rsp), %rdi
leaq 0x2(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x122b40
movq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x122b90
cmpl $0x0, %eax
je 0x1256db
jmp 0x125899
movq 0x20(%rsp), %rax
movzbl 0x24e(%rax), %eax
cmpl $0x3, %eax
jne 0x1257ca
movb 0x2(%rsp), %cl
movq 0x20(%rsp), %rax
movb %cl, 0x260(%rax)
cmpq $0x0, 0x18(%rsp)
je 0x1257c5
movq 0x18(%rsp), %rax
movzwl 0x20(%rax), %eax
cmpl $0x0, %eax
je 0x1257c5
movzbl 0x2(%rsp), %eax
movq 0x18(%rsp), %rcx
movzwl 0x20(%rcx), %ecx
cmpl %ecx, %eax
jl 0x125745
movq 0x20(%rsp), %rdi
leaq 0x1a217(%rip), %rsi     # 0x13f952
callq 0x113940
jmp 0x125899
movq 0x20(%rsp), %rax
movq 0x238(%rax), %rax
movzbl 0x2(%rsp), %ecx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movzbl (%rax), %eax
movw %ax, %cx
movq 0x20(%rsp), %rax
movw %cx, 0x262(%rax)
movq 0x20(%rsp), %rax
movq 0x238(%rax), %rax
movzbl 0x2(%rsp), %ecx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movzbl 0x1(%rax), %eax
movw %ax, %cx
movq 0x20(%rsp), %rax
movw %cx, 0x264(%rax)
movq 0x20(%rsp), %rax
movq 0x238(%rax), %rax
movzbl 0x2(%rsp), %ecx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movzbl 0x2(%rax), %eax
movw %ax, %cx
movq 0x20(%rsp), %rax
movw %cx, 0x266(%rax)
jmp 0x12587e
movq 0x20(%rsp), %rax
movzbl 0x24e(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x125822
movzbl 0x2(%rsp), %eax
shll $0x8, %eax
movzbl 0x3(%rsp), %ecx
addl %ecx, %eax
movw %ax, %cx
movq 0x20(%rsp), %rax
movw %cx, 0x268(%rax)
movq 0x20(%rsp), %rax
movw %cx, 0x266(%rax)
movq 0x20(%rsp), %rax
movw %cx, 0x264(%rax)
movq 0x20(%rsp), %rax
movw %cx, 0x262(%rax)
jmp 0x12587c
movzbl 0x2(%rsp), %eax
shll $0x8, %eax
movzbl 0x3(%rsp), %ecx
addl %ecx, %eax
movw %ax, %cx
movq 0x20(%rsp), %rax
movw %cx, 0x262(%rax)
movzbl 0x4(%rsp), %eax
shll $0x8, %eax
movzbl 0x5(%rsp), %ecx
addl %ecx, %eax
movw %ax, %cx
movq 0x20(%rsp), %rax
movw %cx, 0x264(%rax)
movzbl 0x6(%rsp), %eax
shll $0x8, %eax
movzbl 0x7(%rsp), %ecx
addl %ecx, %eax
movw %ax, %cx
movq 0x20(%rsp), %rax
movw %cx, 0x266(%rax)
jmp 0x12587e
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
addq $0x260, %rdx            # imm = 0x260
callq 0xfcb80
addq $0x28, %rsp
retq
nop