// File: Lut99[P]RayTracer/src/lib/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 gray = r; /*((unsigned short)r + g + b) / 3u;*/
    if(mode->bitdepth == 8) out[i] = gray;
    else if(mode->bitdepth == 16) out[i * 2 + 0] = out[i * 2 + 1] = gray;
    else {
      /*take the most significant bits of gray*/
      gray = ((unsigned)gray >> (8u - mode->bitdepth)) & ((1u << mode->bitdepth) - 1u);
      addColorBits(out, i, mode->bitdepth, gray);
    }
  } 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 gray = r; /*((unsigned short)r + g + b) / 3u;*/
    if(mode->bitdepth == 8) {
      out[i * 2 + 0] = gray;
      out[i * 2 + 1] = a;
    } else if(mode->bitdepth == 16) {
      out[i * 4 + 0] = out[i * 4 + 1] = gray;
      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 0x142f77
movb 0x2f(%rsp), %al
movb %al, 0x2d(%rsp)
movq 0x38(%rsp), %rax
cmpl $0x8, 0x4(%rax)
jne 0x142ef4
movb 0x2d(%rsp), %dl
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
movb %dl, (%rax,%rcx)
jmp 0x142f72
movq 0x38(%rsp), %rax
cmpl $0x10, 0x4(%rax)
jne 0x142f26
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 0x142f70
movzbl 0x2d(%rsp), %eax
movq 0x38(%rsp), %rdx
movl $0x8, %ecx
subl 0x4(%rdx), %ecx
shrl %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 0x14fd50
jmp 0x142f72
jmp 0x143286
movq 0x38(%rsp), %rax
cmpl $0x2, (%rax)
jne 0x143034
movq 0x38(%rsp), %rax
cmpl $0x8, 0x4(%rax)
jne 0x142fca
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 0x14302f
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 0x143284
movq 0x38(%rsp), %rax
cmpl $0x3, (%rax)
jne 0x1430bb
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 0x14fe20
movl %eax, 0x28(%rsp)
cmpl $0x0, 0x28(%rsp)
jge 0x14307b
movl $0x52, 0x54(%rsp)
jmp 0x14328e
movq 0x38(%rsp), %rax
cmpl $0x8, 0x4(%rax)
jne 0x14309b
movl 0x28(%rsp), %eax
movb %al, %dl
movq 0x48(%rsp), %rax
movq 0x40(%rsp), %rcx
movb %dl, (%rax,%rcx)
jmp 0x1430b6
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rsi
movq 0x38(%rsp), %rax
movl 0x4(%rax), %edx
movl 0x28(%rsp), %ecx
callq 0x14fd50
jmp 0x143282
movq 0x38(%rsp), %rax
cmpl $0x4, (%rax)
jne 0x143168
movb 0x2f(%rsp), %al
movb %al, 0x27(%rsp)
movq 0x38(%rsp), %rax
cmpl $0x8, 0x4(%rax)
jne 0x143107
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 0x143163
movq 0x38(%rsp), %rax
cmpl $0x10, 0x4(%rax)
jne 0x143161
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 0x143163
jmp 0x143280
movq 0x38(%rsp), %rax
cmpl $0x6, (%rax)
jne 0x14327e
movq 0x38(%rsp), %rax
cmpl $0x8, 0x4(%rax)
jne 0x1431dd
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 0x14327c
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 0x14327e
jmp 0x143280
jmp 0x143282
jmp 0x143284
jmp 0x143286
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nopw (%rax,%rax)