// File: phillip-h[P]fractal-gen/lodepng.cpp
// Function: rgba8ToPixel(unsigned char*, unsigned long, LodePNGColorMode const*, ColorTree*, unsigned char, unsigned char, unsigned char, unsigned char)
static unsigned rgba8ToPixel(unsigned char* out, size_t i,
                             const LodePNGColorMode* mode, ColorTree* tree /*for palette*/,
                             unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
  if(mode->colortype == LCT_GREY)
  {
    unsigned char grey = r; /*((unsigned short)r + g + b) / 3*/;
    if(mode->bitdepth == 8) out[i] = grey;
    else if(mode->bitdepth == 16) out[i * 2 + 0] = out[i * 2 + 1] = grey;
    else
    {
      /*take the most significant bits of grey*/
      grey = (grey >> (8 - mode->bitdepth)) & ((1 << mode->bitdepth) - 1);
      addColorBits(out, i, mode->bitdepth, grey);
    }
  }
  else if(mode->colortype == LCT_RGB)
  {
    if(mode->bitdepth == 8)
    {
      out[i * 3 + 0] = r;
      out[i * 3 + 1] = g;
      out[i * 3 + 2] = b;
    }
    else
    {
      out[i * 6 + 0] = out[i * 6 + 1] = r;
      out[i * 6 + 2] = out[i * 6 + 3] = g;
      out[i * 6 + 4] = out[i * 6 + 5] = b;
    }
  }
  else if(mode->colortype == LCT_PALETTE)
  {
    int index = color_tree_get(tree, r, g, b, a);
    if(index < 0) return 82; /*color not in palette*/
    if(mode->bitdepth == 8) out[i] = index;
    else addColorBits(out, i, mode->bitdepth, (unsigned)index);
  }
  else if(mode->colortype == LCT_GREY_ALPHA)
  {
    unsigned char grey = r; /*((unsigned short)r + g + b) / 3*/;
    if(mode->bitdepth == 8)
    {
      out[i * 2 + 0] = grey;
      out[i * 2 + 1] = a;
    }
    else if(mode->bitdepth == 16)
    {
      out[i * 4 + 0] = out[i * 4 + 1] = grey;
      out[i * 4 + 2] = out[i * 4 + 3] = a;
    }
  }
  else if(mode->colortype == LCT_RGBA)
  {
    if(mode->bitdepth == 8)
    {
      out[i * 4 + 0] = r;
      out[i * 4 + 1] = g;
      out[i * 4 + 2] = b;
      out[i * 4 + 3] = a;
    }
    else
    {
      out[i * 8 + 0] = out[i * 8 + 1] = r;
      out[i * 8 + 2] = out[i * 8 + 3] = g;
      out[i * 8 + 4] = out[i * 8 + 5] = b;
      out[i * 8 + 6] = out[i * 8 + 7] = a;
    }
  }

  return 0; /*no error*/
}

// Assembly:
subq $0x58, %rsp
movl %r8d, 0x4(%rsp)
movq %rcx, %rax
movl 0x4(%rsp), %ecx
movq %rax, 0x8(%rsp)
movq %rdx, %rax
movq 0x8(%rsp), %rdx
movq %rax, 0x10(%rsp)
movq %rsi, %rax
movq 0x10(%rsp), %rsi
movq %rax, 0x18(%rsp)
movq %rdi, %r8
movq 0x18(%rsp), %rdi
movb %r9b, %al
movb 0x68(%rsp), %r9b
movb 0x60(%rsp), %r9b
movq %r8, 0x48(%rsp)
movq %rdi, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
movb %cl, 0x2f(%rsp)
movb %al, 0x2e(%rsp)
movq 0x38(%rsp), %rax
cmpl $0x0, (%rax)
jne 0xbb07
movb 0x2f(%rsp), %al
movb %al, 0x2d(%rsp)
movq 0x38(%rsp), %rax
cmpl $0x8, 0x4(%rax)
jne 0xba84
movb 0x2d(%rsp), %dl
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
movb %dl, (%rax,%rcx)
jmp 0xbb02
movq 0x38(%rsp), %rax
cmpl $0x10, 0x4(%rax)
jne 0xbab6
movb 0x2d(%rsp), %dl
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq %rcx
movb %dl, 0x1(%rax,%rcx)
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq %rcx
movb %dl, (%rax,%rcx)
jmp 0xbb00
movzbl 0x2d(%rsp), %eax
movq 0x38(%rsp), %rdx
movl $0x8, %ecx
subl 0x4(%rdx), %ecx
sarl %cl, %eax
movq 0x38(%rsp), %rcx
movl 0x4(%rcx), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
subl $0x1, %ecx
andl %ecx, %eax
movb %al, 0x2d(%rsp)
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x38(%rsp), %rax
movl 0x4(%rax), %edx
movzbl 0x2d(%rsp), %ecx
callq 0x16030
jmp 0xbb02
jmp 0xbe16
movq 0x38(%rsp), %rax
cmpl $0x2, (%rax)
jne 0xbbc4
movq 0x38(%rsp), %rax
cmpl $0x8, 0x4(%rax)
jne 0xbb5a
movb 0x2f(%rsp), %dl
movq 0x48(%rsp), %rax
imulq $0x3, 0x40(%rsp), %rcx
movb %dl, (%rax,%rcx)
movb 0x2e(%rsp), %dl
movq 0x48(%rsp), %rax
imulq $0x3, 0x40(%rsp), %rcx
movb %dl, 0x1(%rax,%rcx)
movb 0x60(%rsp), %dl
movq 0x48(%rsp), %rax
imulq $0x3, 0x40(%rsp), %rcx
movb %dl, 0x2(%rax,%rcx)
jmp 0xbbbf
movb 0x2f(%rsp), %dl
movq 0x48(%rsp), %rax
imulq $0x6, 0x40(%rsp), %rcx
movb %dl, 0x1(%rax,%rcx)
movq 0x48(%rsp), %rax
imulq $0x6, 0x40(%rsp), %rcx
movb %dl, (%rax,%rcx)
movb 0x2e(%rsp), %dl
movq 0x48(%rsp), %rax
imulq $0x6, 0x40(%rsp), %rcx
movb %dl, 0x3(%rax,%rcx)
movq 0x48(%rsp), %rax
imulq $0x6, 0x40(%rsp), %rcx
movb %dl, 0x2(%rax,%rcx)
movb 0x60(%rsp), %dl
movq 0x48(%rsp), %rax
imulq $0x6, 0x40(%rsp), %rcx
movb %dl, 0x5(%rax,%rcx)
movq 0x48(%rsp), %rax
imulq $0x6, 0x40(%rsp), %rcx
movb %dl, 0x4(%rax,%rcx)
jmp 0xbe14
movq 0x38(%rsp), %rax
cmpl $0x3, (%rax)
jne 0xbc4b
movq 0x30(%rsp), %rdi
movb 0x2f(%rsp), %dl
movb 0x2e(%rsp), %cl
movb 0x60(%rsp), %al
movzbl %dl, %esi
movzbl %cl, %edx
movzbl %al, %ecx
movzbl 0x68(%rsp), %r8d
callq 0x16100
movl %eax, 0x28(%rsp)
cmpl $0x0, 0x28(%rsp)
jge 0xbc0b
movl $0x52, 0x54(%rsp)
jmp 0xbe1e
movq 0x38(%rsp), %rax
cmpl $0x8, 0x4(%rax)
jne 0xbc2b
movl 0x28(%rsp), %eax
movb %al, %dl
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
movb %dl, (%rax,%rcx)
jmp 0xbc46
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x38(%rsp), %rax
movl 0x4(%rax), %edx
movl 0x28(%rsp), %ecx
callq 0x16030
jmp 0xbe12
movq 0x38(%rsp), %rax
cmpl $0x4, (%rax)
jne 0xbcf8
movb 0x2f(%rsp), %al
movb %al, 0x27(%rsp)
movq 0x38(%rsp), %rax
cmpl $0x8, 0x4(%rax)
jne 0xbc97
movb 0x27(%rsp), %dl
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq %rcx
movb %dl, (%rax,%rcx)
movb 0x68(%rsp), %dl
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq %rcx
movb %dl, 0x1(%rax,%rcx)
jmp 0xbcf3
movq 0x38(%rsp), %rax
cmpl $0x10, 0x4(%rax)
jne 0xbcf1
movb 0x27(%rsp), %dl
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq $0x2, %rcx
movb %dl, 0x1(%rax,%rcx)
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq $0x2, %rcx
movb %dl, (%rax,%rcx)
movb 0x68(%rsp), %dl
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq $0x2, %rcx
movb %dl, 0x3(%rax,%rcx)
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq $0x2, %rcx
movb %dl, 0x2(%rax,%rcx)
jmp 0xbcf3
jmp 0xbe10
movq 0x38(%rsp), %rax
cmpl $0x6, (%rax)
jne 0xbe0e
movq 0x38(%rsp), %rax
cmpl $0x8, 0x4(%rax)
jne 0xbd6d
movb 0x2f(%rsp), %dl
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq $0x2, %rcx
movb %dl, (%rax,%rcx)
movb 0x2e(%rsp), %dl
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq $0x2, %rcx
movb %dl, 0x1(%rax,%rcx)
movb 0x60(%rsp), %dl
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq $0x2, %rcx
movb %dl, 0x2(%rax,%rcx)
movb 0x68(%rsp), %dl
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq $0x2, %rcx
movb %dl, 0x3(%rax,%rcx)
jmp 0xbe0c
movb 0x2f(%rsp), %dl
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq $0x3, %rcx
movb %dl, 0x1(%rax,%rcx)
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq $0x3, %rcx
movb %dl, (%rax,%rcx)
movb 0x2e(%rsp), %dl
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq $0x3, %rcx
movb %dl, 0x3(%rax,%rcx)
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq $0x3, %rcx
movb %dl, 0x2(%rax,%rcx)
movb 0x60(%rsp), %dl
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq $0x3, %rcx
movb %dl, 0x5(%rax,%rcx)
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq $0x3, %rcx
movb %dl, 0x4(%rax,%rcx)
movb 0x68(%rsp), %dl
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq $0x3, %rcx
movb %dl, 0x7(%rax,%rcx)
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
shlq $0x3, %rcx
movb %dl, 0x6(%rax,%rcx)
jmp 0xbe0e
jmp 0xbe10
jmp 0xbe12
jmp 0xbe14
jmp 0xbe16
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nopw (%rax,%rax)