// File: logicx24[P]RayTracer/lodepng.cpp
// Function: lodepng_convert(unsigned char*, unsigned char const*, LodePNGColorMode*, LodePNGColorMode const*, unsigned int, unsigned int)
unsigned lodepng_convert(unsigned char* out, const unsigned char* in,
                         LodePNGColorMode* mode_out, const LodePNGColorMode* mode_in,
                         unsigned w, unsigned h)
{
  size_t i;
  ColorTree tree;
  size_t numpixels = w * h;

  if(lodepng_color_mode_equal(mode_out, mode_in))
  {
    size_t numbytes = lodepng_get_raw_size(w, h, mode_in);
    for(i = 0; i != numbytes; ++i) out[i] = in[i];
    return 0;
  }

  if(mode_out->colortype == LCT_PALETTE)
  {
    size_t palsize = 1u << mode_out->bitdepth;
    if(mode_out->palettesize < palsize) palsize = mode_out->palettesize;
    color_tree_init(&tree);
    for(i = 0; i != palsize; ++i)
    {
      unsigned char* p = &mode_out->palette[i * 4];
      color_tree_add(&tree, p[0], p[1], p[2], p[3], i);
    }
  }

  if(mode_in->bitdepth == 16 && mode_out->bitdepth == 16)
  {
    for(i = 0; i != numpixels; ++i)
    {
      unsigned short r = 0, g = 0, b = 0, a = 0;
      getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in);
      rgba16ToPixel(out, i, mode_out, r, g, b, a);
    }
  }
  else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGBA)
  {
    getPixelColorsRGBA8(out, numpixels, 1, in, mode_in);
  }
  else if(mode_out->bitdepth == 8 && mode_out->colortype == LCT_RGB)
  {
    getPixelColorsRGBA8(out, numpixels, 0, in, mode_in);
  }
  else
  {
    unsigned char r = 0, g = 0, b = 0, a = 0;
    for(i = 0; i != numpixels; ++i)
    {
      getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in);
      rgba8ToPixel(out, i, mode_out, &tree, r, g, b, a);
    }
  }

  if(mode_out->colortype == LCT_PALETTE)
  {
    color_tree_cleanup(&tree);
  }

  return 0; /*no error (this function currently never has one, but maybe OOM detection added later.)*/
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movl %r9d, -0x30(%rbp)
movl -0x2c(%rbp), %eax
imull -0x30(%rbp), %eax
movl %eax, %eax
movq %rax, -0xc8(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x14b60
cmpl $0x0, %eax
je 0x14850
movl -0x2c(%rbp), %edi
movl -0x30(%rbp), %esi
movq -0x28(%rbp), %rdx
callq 0x13b40
movq %rax, -0xd0(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0xd0(%rbp), %rax
je 0x14844
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
movb (%rax,%rcx), %dl
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x14813
movl $0x0, -0x4(%rbp)
jmp 0x14b49
movq -0x20(%rbp), %rax
cmpl $0x3, (%rax)
jne 0x14928
movq -0x20(%rbp), %rax
movl 0x4(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %eax
movq %rax, -0xd8(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
cmpq -0xd8(%rbp), %rax
jae 0x14894
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xd8(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x14ca0
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0xd8(%rbp), %rax
je 0x14926
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movb (%rax), %sil
movq -0xe0(%rbp), %rax
movb 0x1(%rax), %dl
movq -0xe0(%rbp), %rax
movb 0x2(%rax), %cl
movq -0xe0(%rbp), %rax
movb 0x3(%rax), %al
movq -0x38(%rbp), %rdi
movl %edi, %r9d
leaq -0xc0(%rbp), %rdi
movzbl %sil, %esi
movzbl %dl, %edx
movzbl %cl, %ecx
movzbl %al, %r8d
callq 0x14ce0
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x148a8
jmp 0x14928
movq -0x28(%rbp), %rax
cmpl $0x10, 0x4(%rax)
jne 0x14a04
movq -0x20(%rbp), %rax
cmpl $0x10, 0x4(%rax)
jne 0x14a04
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0xc8(%rbp), %rax
je 0x149ff
movw $0x0, -0xe2(%rbp)
movw $0x0, -0xe4(%rbp)
movw $0x0, -0xe6(%rbp)
movw $0x0, -0xe8(%rbp)
movq -0x18(%rbp), %r8
movq -0x38(%rbp), %r9
movq -0x28(%rbp), %rax
leaq -0xe2(%rbp), %rdi
leaq -0xe4(%rbp), %rsi
leaq -0xe6(%rbp), %rdx
leaq -0xe8(%rbp), %rcx
movq %rax, (%rsp)
callq 0x14dd0
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x20(%rbp), %rdx
movw -0xe2(%rbp), %cx
movw -0xe4(%rbp), %r8w
movw -0xe6(%rbp), %ax
movzwl %cx, %ecx
movzwl %r8w, %r8d
movzwl %ax, %r9d
movzwl -0xe8(%rbp), %eax
movl %eax, (%rsp)
callq 0x15110
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x1494c
jmp 0x14b2d
movq -0x20(%rbp), %rax
cmpl $0x8, 0x4(%rax)
jne 0x14a39
movq -0x20(%rbp), %rax
cmpl $0x6, (%rax)
jne 0x14a39
movq -0x10(%rbp), %rdi
movq -0xc8(%rbp), %rsi
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %r8
movl $0x1, %edx
callq 0x153e0
jmp 0x14b2b
movq -0x20(%rbp), %rax
cmpl $0x8, 0x4(%rax)
jne 0x14a6b
movq -0x20(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x14a6b
movq -0x10(%rbp), %rdi
movq -0xc8(%rbp), %rsi
movq -0x18(%rbp), %rcx
movq -0x28(%rbp), %r8
xorl %edx, %edx
callq 0x153e0
jmp 0x14b29
movb $0x0, -0xe9(%rbp)
movb $0x0, -0xea(%rbp)
movb $0x0, -0xeb(%rbp)
movb $0x0, -0xec(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0xc8(%rbp), %rax
je 0x14b27
movq -0x18(%rbp), %r8
movq -0x38(%rbp), %r9
movq -0x28(%rbp), %rax
leaq -0xe9(%rbp), %rdi
leaq -0xea(%rbp), %rsi
leaq -0xeb(%rbp), %rdx
leaq -0xec(%rbp), %rcx
movq %rax, (%rsp)
callq 0x15c50
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x20(%rbp), %rdx
movb -0xe9(%rbp), %r8b
movb -0xea(%rbp), %r9b
movb -0xeb(%rbp), %al
leaq -0xc0(%rbp), %rcx
movzbl %r8b, %r8d
movzbl %r9b, %r9d
movzbl %al, %eax
movl %eax, (%rsp)
movzbl -0xec(%rbp), %eax
movl %eax, 0x8(%rsp)
callq 0x161b0
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x14a8f
jmp 0x14b29
jmp 0x14b2b
jmp 0x14b2d
movq -0x20(%rbp), %rax
cmpl $0x3, (%rax)
jne 0x14b42
leaq -0xc0(%rbp), %rdi
callq 0x16570
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)