// File: /workspace/github_c_makefile_star2_O2/Joy-Division[P]tools-zoe/lodepng/lodepng.c
// Function: lodepng_convert
unsigned lodepng_convert(unsigned char* out, const unsigned char* in,
                         const LodePNGColorMode* mode_out, const LodePNGColorMode* mode_in,
                         unsigned w, unsigned h)
{
  size_t i;
  ColorTree tree;
  size_t numpixels = (size_t)w * (size_t)h;
  unsigned error = 0;

  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 palettesize = mode_out->palettesize;
    const unsigned char* palette = mode_out->palette;
    size_t palsize = (size_t)1u << mode_out->bitdepth;
    /*if the user specified output palette but did not give the values, assume
    they want the values of the input color type (assuming that one is palette).
    Note that we never create a new palette ourselves.*/
    if(palettesize == 0)
    {
      palettesize = mode_in->palettesize;
      palette = mode_in->palette;
      /*if the input was also palette with same bitdepth, then the color types are also
      equal, so copy literally. This to preserve the exact indices that were in the PNG
      even in case there are duplicate colors in the palette.*/
      if (mode_in->colortype == LCT_PALETTE && mode_in->bitdepth == mode_out->bitdepth)
      {
        size_t numbytes = lodepng_get_raw_size(w, h, mode_in);
        for(i = 0; i != numbytes; ++i) out[i] = in[i];
        return 0;
      }
    }
    if(palettesize < palsize) palsize = palettesize;
    color_tree_init(&tree);
    for(i = 0; i != palsize; ++i)
    {
      const unsigned char* p = &palette[i * 4];
      color_tree_add(&tree, p[0], p[1], p[2], p[3], (unsigned)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);
      error = rgba8ToPixel(out, i, mode_out, &tree, r, g, b, a);
      if (error) break;
    }
  }

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

  return error;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movl %r9d, %r12d
movl %r8d, %r13d
movq %rcx, %r15
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %rbp
movq %rdx, %rdi
movq %rcx, %rsi
callq 0x417e
testl %eax, %eax
je 0x3d4f
movl %r13d, %edi
movl %r12d, %esi
movq %r15, %rdx
callq 0x36a4
xorl %r15d, %r15d
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x3f7b
movb (%r14,%rcx), %dl
movb %dl, (%rbp,%rcx)
incq %rcx
jmp 0x3d39
movl %r13d, 0x24(%rsp)
movl %r12d, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movq %rbx, 0x28(%rsp)
cmpl $0x3, (%rbx)
jne 0x3e10
movq 0x28(%rsp), %rdx
movq 0x10(%rdx), %rax
movl 0x4(%rdx), %ecx
pushq $0x1
popq %rbx
shlq %cl, %rbx
testq %rax, %rax
je 0x3d89
movq 0x8(%rdx), %r15
jmp 0x3dcd
movq 0x18(%rsp), %rdx
movq 0x10(%rdx), %rax
movq 0x8(%rdx), %r15
cmpl $0x3, (%rdx)
jne 0x3dcd
cmpl %ecx, 0x4(%rdx)
jne 0x3dcd
movl 0x24(%rsp), %edi
movl 0x10(%rsp), %esi
movq 0x18(%rsp), %rdx
callq 0x36a4
xorl %r15d, %r15d
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x3f7b
movb (%r14,%rcx), %dl
movb %dl, (%rbp,%rcx)
incq %rcx
jmp 0x3db7
cmpq %rbx, %rax
cmovbq %rax, %rbx
leaq 0x30(%rsp), %r12
movq %r12, %rdi
callq 0x41ea
xorl %r13d, %r13d
cmpq %r13, %rbx
je 0x3e10
movzbl (%r15,%r13,4), %esi
movzbl 0x1(%r15,%r13,4), %edx
movzbl 0x2(%r15,%r13,4), %ecx
movzbl 0x3(%r15,%r13,4), %r8d
movq %r12, %rdi
movl %r13d, %r9d
callq 0x4204
incq %r13
jmp 0x3de4
movl 0x24(%rsp), %eax
movl 0x10(%rsp), %ecx
imulq %rax, %rcx
movq %rcx, 0x10(%rsp)
movq 0x18(%rsp), %r8
movl 0x4(%r8), %ecx
xorl $0x10, %ecx
movq 0x28(%rsp), %rbx
movl 0x4(%rbx), %eax
movl %eax, %edx
xorl $0x10, %edx
orl %ecx, %edx
jne 0x3f41
movq %rbp, %r12
leaq 0x7220(%rip), %r15      # 0xb06c
xorl %edi, %edi
xorl %ebp, %ebp
cmpq %rbp, 0x10(%rsp)
je 0x3f69
andw $0x0, 0xe(%rsp)
andw $0x0, 0xc(%rsp)
andw $0x0, 0xa(%rsp)
andw $0x0, 0x8(%rsp)
movq 0x18(%rsp), %rax
movq %rax, (%rsp)
movq %rdi, %r13
leaq 0xe(%rsp), %rdi
leaq 0xc(%rsp), %rsi
leaq 0xa(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movq %r14, %r8
movq %rbp, %r9
callq 0x42a5
movq %r13, %rdi
movl (%rbx), %esi
cmpq $0x6, %rsi
ja 0x3f30
movzwl 0xe(%rsp), %ebx
movzwl 0xc(%rsp), %edx
movzwl 0xa(%rsp), %eax
movzwl 0x8(%rsp), %ecx
movslq (%r15,%rsi,4), %rsi
addq %r15, %rsi
jmpq *%rsi
rolw $0x8, %bx
movw %bx, (%r12,%rbp,2)
jmp 0x3f30
movq %r12, %rax
movb %bh, (%rax,%rbp,4)
movb %bl, 0x1(%r12,%rbp,4)
movb %ch, 0x2(%rax,%rbp,4)
movb %cl, 0x3(%r12,%rbp,4)
jmp 0x3f30
movq %r12, %rcx
movb %bh, (%rcx,%rdi,2)
movb %bl, 0x1(%r12,%rdi,2)
movb %dh, 0x2(%rcx,%rdi,2)
movb %dl, 0x3(%r12,%rdi,2)
movb %ah, 0x4(%rcx,%rdi,2)
movb %al, 0x5(%r12,%rdi,2)
jmp 0x3f30
movq %r12, %rsi
movb %bh, (%rsi,%rbp,8)
movb %bl, 0x1(%r12,%rbp,8)
movb %dh, 0x2(%rsi,%rbp,8)
movb %dl, 0x3(%r12,%rbp,8)
movb %ah, 0x4(%rsi,%rbp,8)
movb %al, 0x5(%r12,%rbp,8)
movb %ch, 0x6(%rsi,%rbp,8)
movb %cl, 0x7(%r12,%rbp,8)
incq %rbp
addq $0x3, %rdi
movq 0x28(%rsp), %rbx
jmp 0x3e50
cmpl $0x8, %eax
jne 0x3f90
movl (%rbx), %eax
cmpl $0x2, %eax
je 0x4159
cmpl $0x6, %eax
jne 0x3f90
pushq $0x1
popq %rdx
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rcx
callq 0x441d
xorl %r15d, %r15d
cmpl $0x3, (%rbx)
jne 0x3f7b
leaq 0x30(%rsp), %rdi
callq 0x4ac9
movl %r15d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
leaq 0xe(%rsp), %rcx
movb %al, (%rcx)
leaq 0xc(%rsp), %rcx
movb %al, (%rcx)
leaq 0xa(%rsp), %rcx
movb %al, (%rcx)
leaq 0x8(%rsp), %rcx
movb %al, (%rcx)
xorl %r15d, %r15d
xorl %r12d, %r12d
cmpq %r12, 0x10(%rsp)
je 0x3f69
movq 0x18(%rsp), %rax
movq %rax, (%rsp)
leaq 0xe(%rsp), %rdi
leaq 0xc(%rsp), %rsi
leaq 0xa(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movq %r14, %r8
movq %r12, %r9
callq 0x4806
movl (%rbx), %r8d
cmpq $0x6, %r8
ja 0x414d
movb 0xe(%rsp), %al
movzbl %al, %esi
movb 0xc(%rsp), %dl
movb 0xa(%rsp), %cl
movb 0x8(%rsp), %dil
leaq 0x7045(%rip), %r9       # 0xb050
movslq (%r9,%r8,4), %r8
addq %r9, %r8
jmpq *%r8
movl 0x4(%rbx), %edx
cmpl $0x10, %edx
je 0x410d
cmpl $0x8, %edx
jne 0x412f
movb %al, (%rbp,%r12)
jmp 0x414d
movzbl %dl, %edx
movzbl %cl, %ecx
movzbl %dil, %r8d
leaq 0x30(%rsp), %rdi
callq 0x9037
testl %eax, %eax
js 0x4173
movl 0x4(%rbx), %edx
cmpl $0x8, %edx
jne 0x4140
movb %al, (%rbp,%r12)
jmp 0x414d
cmpl $0x8, 0x4(%rbx)
jne 0x40c3
movb %al, (%rbp,%r15)
movb %dl, 0x1(%rbp,%r15)
movb %cl, 0x2(%rbp,%r15)
jmp 0x414d
movl 0x4(%rbx), %ecx
cmpl $0x10, %ecx
je 0x4119
cmpl $0x8, %ecx
jne 0x414d
movb %al, (%rbp,%r12,2)
movb %dil, 0x1(%rbp,%r12,2)
jmp 0x414d
cmpl $0x8, 0x4(%rbx)
jne 0x40e3
movb %al, (%rbp,%r12,4)
movb %dl, 0x1(%rbp,%r12,4)
movb %cl, 0x2(%rbp,%r12,4)
movb %dil, 0x3(%rbp,%r12,4)
jmp 0x414d
movb %al, 0x1(%rbp,%r15,2)
movb %al, (%rbp,%r15,2)
movb %dl, 0x3(%rbp,%r15,2)
movb %dl, 0x2(%rbp,%r15,2)
movb %cl, 0x5(%rbp,%r15,2)
movb %cl, 0x4(%rbp,%r15,2)
jmp 0x414d
movb %al, 0x1(%rbp,%r12,8)
movb %al, (%rbp,%r12,8)
movb %dl, 0x3(%rbp,%r12,8)
movb %dl, 0x2(%rbp,%r12,8)
movb %cl, 0x5(%rbp,%r12,8)
movb %cl, 0x4(%rbp,%r12,8)
movb %dil, 0x7(%rbp,%r12,8)
movb %dil, 0x6(%rbp,%r12,8)
jmp 0x414d
movb %al, 0x1(%rbp,%r12,2)
movb %al, (%rbp,%r12,2)
jmp 0x414d
movb %al, 0x1(%rbp,%r12,4)
movb %al, (%rbp,%r12,4)
movb %dil, 0x3(%rbp,%r12,4)
movb %dil, 0x2(%rbp,%r12,4)
jmp 0x414d
movb $0x8, %cl
subb %dl, %cl
shrl %cl, %esi
pushq $-0x1
popq %rax
movl %edx, %ecx
shll %cl, %eax
notl %eax
andl %esi, %eax
movq %rbp, %rdi
movq %r12, %rsi
movl %eax, %ecx
callq 0x8fe3
incq %r12
addq $0x3, %r15
jmp 0x3fb4
xorl %r15d, %r15d
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
xorl %edx, %edx
movq %r14, %rcx
callq 0x441d
jmp 0x3f6c
pushq $0x52
popq %rax
movl %eax, %r15d
jmp 0x3f6c