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

  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 error;
  }

  if(mode_out->colortype == LCT_PALETTE)
  {
    size_t palsize = 1 << mode_out->bitdepth;
    if(mode_out->palettesize < palsize) palsize = mode_out->palettesize;
    color_tree_init(&tree, 1);
    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)
  {
    size_t numpixels = w * h;
    for(i = 0; i < numpixels; i++)
    {
      unsigned short r = 0, g = 0, b = 0, a = 0;
      error = getPixelColorRGBA16(&r, &g, &b, &a, in, i, mode_in);
      if(error) return error;
      error = rgba16ToPixel(out, i, mode_out, r, g, b, a);
      if(error) return error;
    }
  }
  else
  {
    size_t numpixels = w * h;
    for(i = 0; i < numpixels; i++)
    {
      unsigned char r = 0, g = 0, b = 0, a = 0;
      error = getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in);
      if(error) return error;
      error = rgba8ToPixel(out, i, mode_out, &tree, r, g, b, a);
      if(error) return error;
    }
  }

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

  return error;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
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 $0x0, -0x34(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x64f0
cmpl $0x0, %eax
je 0x6230
movl -0x2c(%rbp), %edi
movl -0x30(%rbp), %esi
movq -0x28(%rbp), %rdx
callq 0x5520
movq %rax, -0x58(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x58(%rbp), %rax
jae 0x6225
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
movb (%rax,%rcx), %dl
movq -0x10(%rbp), %rax
movq -0x40(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x61f7
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x64dc
movq -0x20(%rbp), %rax
cmpl $0x3, (%rax)
jne 0x62ec
movq -0x20(%rbp), %rax
movl 0x4(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
cltq
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
cmpq -0x60(%rbp), %rax
jae 0x626b
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x60(%rbp)
leaq -0x50(%rbp), %rdi
movl $0x1, %esi
callq 0x6630
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x60(%rbp), %rax
jae 0x62ea
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x40(%rbp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movb (%rax), %sil
movq -0x68(%rbp), %rax
movb 0x1(%rax), %dl
movq -0x68(%rbp), %rax
movb 0x2(%rax), %cl
movq -0x68(%rbp), %rax
movb 0x3(%rax), %al
movq -0x40(%rbp), %rdi
movl %edi, %r9d
leaq -0x50(%rbp), %rdi
movzbl %sil, %esi
movzbl %dl, %edx
movzbl %cl, %ecx
movzbl %al, %r8d
callq 0x6670
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x6281
jmp 0x62ec
movq -0x28(%rbp), %rax
cmpl $0x10, 0x4(%rax)
jne 0x63d8
movq -0x20(%rbp), %rax
cmpl $0x10, 0x4(%rax)
jne 0x63d8
movl -0x2c(%rbp), %eax
imull -0x30(%rbp), %eax
movl %eax, %eax
movq %rax, -0x70(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x70(%rbp), %rax
jae 0x63d3
movw $0x0, -0x72(%rbp)
movw $0x0, -0x74(%rbp)
movw $0x0, -0x76(%rbp)
movw $0x0, -0x78(%rbp)
movq -0x18(%rbp), %r8
movq -0x40(%rbp), %r9
movq -0x28(%rbp), %rax
leaq -0x72(%rbp), %rdi
leaq -0x74(%rbp), %rsi
leaq -0x76(%rbp), %rdx
leaq -0x78(%rbp), %rcx
movq %rax, (%rsp)
callq 0x6740
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x637c
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x64dc
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x20(%rbp), %rdx
movw -0x72(%rbp), %cx
movw -0x74(%rbp), %r8w
movw -0x76(%rbp), %ax
movzwl %cx, %ecx
movzwl %r8w, %r8d
movzwl %ax, %r9d
movzwl -0x78(%rbp), %eax
movl %eax, (%rsp)
callq 0x6ab0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x63c0
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x64dc
jmp 0x63c2
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x631d
jmp 0x64c4
movl -0x2c(%rbp), %eax
imull -0x30(%rbp), %eax
movl %eax, %eax
movq %rax, -0x80(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x80(%rbp), %rax
jae 0x64c2
movb $0x0, -0x81(%rbp)
movb $0x0, -0x82(%rbp)
movb $0x0, -0x83(%rbp)
movb $0x0, -0x84(%rbp)
movq -0x18(%rbp), %r8
movq -0x40(%rbp), %r9
movq -0x28(%rbp), %rax
leaq -0x81(%rbp), %rdi
leaq -0x82(%rbp), %rsi
leaq -0x83(%rbp), %rdx
leaq -0x84(%rbp), %rcx
movq %rax, (%rsp)
callq 0x6da0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x645c
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x64dc
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x20(%rbp), %rdx
movb -0x81(%rbp), %r8b
movb -0x82(%rbp), %r9b
movb -0x83(%rbp), %al
leaq -0x50(%rbp), %rcx
movzbl %r8b, %r8d
movzbl %r9b, %r9d
movzbl %al, %eax
movl %eax, (%rsp)
movzbl -0x84(%rbp), %eax
movl %eax, 0x8(%rsp)
callq 0x7300
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x64af
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x64dc
jmp 0x64b1
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x63ed
jmp 0x64c4
movq -0x20(%rbp), %rax
cmpl $0x3, (%rax)
jne 0x64d6
leaq -0x50(%rbp), %rdi
callq 0x76c0
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)