// File: /workspace/github_c_makefile_star2_O1/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, %ebp
movl %r8d, %r13d
movq %rcx, %r12
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
movq %rdx, %rdi
movq %rcx, %rsi
callq 0x52ae
testl %eax, %eax
je 0x4e10
movl (%r12), %edx
movl 0x4(%r12), %ecx
movl %r13d, %edi
movl %ebp, %esi
callq 0x4633
testq %rax, %rax
je 0x4e98
xorl %r12d, %r12d
xorl %ecx, %ecx
movb (%r14,%rcx), %dl
movb %dl, (%rbx,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x4dfc
jmp 0x5299
movl %ebp, 0x10(%rsp)
movq %r12, 0x28(%rsp)
movq %rbx, 0x18(%rsp)
movq %r15, 0x20(%rsp)
cmpl $0x3, (%r15)
jne 0x4f1e
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rsi
movl 0x4(%rax), %ecx
movl $0x1, %ebx
shlq %cl, %rbx
testq %rsi, %rsi
je 0x4e4c
movq 0x8(%rax), %rbp
jmp 0x4ea0
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rsi
movq 0x8(%rax), %rbp
movl (%rax), %edx
cmpl $0x3, %edx
jne 0x4ea0
movl 0x4(%rax), %eax
cmpl %ecx, %eax
jne 0x4ea0
movl %r13d, %edi
movl 0x10(%rsp), %esi
movl %eax, %ecx
callq 0x4633
testq %rax, %rax
movq 0x18(%rsp), %rsi
je 0x4e98
xorl %r12d, %r12d
xorl %ecx, %ecx
movb (%r14,%rcx), %dl
movb %dl, (%rsi,%rcx)
incq %rcx
cmpq %rcx, %rax
jne 0x4e84
jmp 0x5299
xorl %r12d, %r12d
jmp 0x5299
cmpq %rbx, %rsi
cmovbq %rsi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movl $0xffffffff, 0xb0(%rsp) # imm = 0xFFFFFFFF
testq %rsi, %rsi
je 0x4f1e
xorl %r15d, %r15d
leaq 0x30(%rsp), %r12
movzbl (%rbp,%r15,4), %esi
movzbl 0x1(%rbp,%r15,4), %edx
movzbl 0x2(%rbp,%r15,4), %ecx
movzbl 0x3(%rbp,%r15,4), %r8d
movq %r12, %rdi
movl %r15d, %r9d
callq 0x531b
incq %r15
cmpq %r15, %rbx
jne 0x4ef3
movl %r13d, %eax
movl 0x10(%rsp), %ecx
imulq %rax, %rcx
movq %rcx, 0x10(%rsp)
movq 0x28(%rsp), %r8
cmpl $0x10, 0x4(%r8)
movq 0x20(%rsp), %rbp
jne 0x5062
cmpl $0x10, 0x4(%rbp)
jne 0x5062
cmpq $0x0, 0x10(%rsp)
je 0x5286
xorl %r15d, %r15d
leaq 0x8(%rsp), %r13
leaq 0x90e4(%rip), %r12      # 0xe04c
xorl %ebx, %ebx
xorl %ebp, %ebp
movw %r15w, 0xe(%rsp)
movw %r15w, 0xc(%rsp)
movw %r15w, 0xa(%rsp)
movw %r15w, 0x8(%rsp)
movq 0x28(%rsp), %rax
movq %rax, (%rsp)
leaq 0xe(%rsp), %rdi
leaq 0xc(%rsp), %rsi
leaq 0xa(%rsp), %rdx
movq %r13, %rcx
movq %r14, %r8
movq %rbp, %r9
callq 0x53d7
movq 0x20(%rsp), %rax
movl (%rax), %esi
cmpq $0x6, %rsi
ja 0x5043
movzwl 0xe(%rsp), %eax
movzwl 0xc(%rsp), %edx
movzwl 0xa(%rsp), %edi
movzwl 0x8(%rsp), %ecx
movslq (%r12,%rsi,4), %rsi
addq %r12, %rsi
jmpq *%rsi
rolw $0x8, %ax
movq 0x18(%rsp), %rcx
movw %ax, (%rcx,%rbp,2)
jmp 0x5043
movq 0x18(%rsp), %rdx
movb %ah, (%rdx,%rbp,4)
movb %al, 0x1(%rdx,%rbp,4)
movb %ch, 0x2(%rdx,%rbp,4)
movb %cl, 0x3(%rdx,%rbp,4)
jmp 0x5043
movq 0x18(%rsp), %rcx
movb %ah, (%rcx,%rbx,2)
movb %al, 0x1(%rcx,%rbx,2)
movb %dh, 0x2(%rcx,%rbx,2)
movb %dl, 0x3(%rcx,%rbx,2)
movl %edi, %eax
movb %ah, 0x4(%rcx,%rbx,2)
movb %al, 0x5(%rcx,%rbx,2)
jmp 0x5043
movq 0x18(%rsp), %rsi
movb %ah, (%rsi,%rbp,8)
movb %al, 0x1(%rsi,%rbp,8)
movb %dh, 0x2(%rsi,%rbp,8)
movb %dl, 0x3(%rsi,%rbp,8)
movl %edi, %eax
movb %ah, 0x4(%rsi,%rbp,8)
movb %al, 0x5(%rsi,%rbp,8)
movb %ch, 0x6(%rsi,%rbp,8)
movb %cl, 0x7(%rsi,%rbp,8)
incq %rbp
addq $0x3, %rbx
cmpq %rbp, 0x10(%rsp)
jne 0x4f6c
xorl %r12d, %r12d
movq 0x20(%rsp), %rbp
jmp 0x5289
movl 0x4(%rbp), %eax
cmpl $0x8, %eax
movq 0x18(%rsp), %rbx
jne 0x50b4
cmpl $0x6, (%rbp)
jne 0x508f
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
movl $0x1, %edx
movq %r14, %rcx
callq 0x5556
jmp 0x5286
cmpl $0x8, %eax
jne 0x50b4
cmpl $0x2, (%rbp)
jne 0x50b4
xorl %r12d, %r12d
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
xorl %edx, %edx
movq %r14, %rcx
callq 0x5556
jmp 0x5289
xorl %r12d, %r12d
movb %r12b, 0xe(%rsp)
movb %r12b, 0xc(%rsp)
movb %r12b, 0xa(%rsp)
movb %r12b, 0x8(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x5289
xorl %r13d, %r13d
movq 0x28(%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 %r13, %r9
callq 0x59c8
movl (%rbp), %r8d
movb $0x1, %r15b
cmpq $0x6, %r8
ja 0x5270
movb 0xe(%rsp), %al
movzbl %al, %esi
movb 0xc(%rsp), %dl
movb 0xa(%rsp), %cl
movb 0x8(%rsp), %dil
leaq 0x8f02(%rip), %r9       # 0xe030
movslq (%r9,%r8,4), %r8
addq %r9, %r8
jmpq *%r8
movl 0x4(%rbp), %edx
cmpl $0x10, %edx
je 0x5230
cmpl $0x8, %edx
jne 0x5250
movb %al, (%rbx,%r13)
jmp 0x5270
movzbl %dl, %edx
movzbl %cl, %ecx
movzbl %dil, %r8d
leaq 0x30(%rsp), %rdi
callq 0xb1ab
testl %eax, %eax
js 0x5225
movl 0x4(%rbp), %edx
cmpl $0x8, %edx
jne 0x5263
movb %al, (%rbx,%r13)
jmp 0x5270
cmpl $0x8, 0x4(%rbp)
leaq (%r13,%r13,2), %rsi
jne 0x51e3
movb %al, (%rbx,%rsi)
movb %dl, 0x1(%rbx,%rsi)
movb %cl, 0x2(%rbx,%rsi)
jmp 0x5270
movl 0x4(%rbp), %ecx
cmpl $0x10, %ecx
je 0x523b
cmpl $0x8, %ecx
jne 0x5270
movb %al, (%rbx,%r13,2)
movb %dil, 0x1(%rbx,%r13,2)
jmp 0x5270
cmpl $0x8, 0x4(%rbp)
jne 0x51fc
movb %al, (%rbx,%r13,4)
movb %dl, 0x1(%rbx,%r13,4)
movb %cl, 0x2(%rbx,%r13,4)
movb %dil, 0x3(%rbx,%r13,4)
jmp 0x5270
movb %al, 0x1(%rbx,%rsi,2)
movb %al, (%rbx,%rsi,2)
movb %dl, 0x3(%rbx,%rsi,2)
movb %dl, 0x2(%rbx,%rsi,2)
movb %cl, 0x5(%rbx,%rsi,2)
movb %cl, 0x4(%rbx,%rsi,2)
jmp 0x5270
movb %al, 0x1(%rbx,%r13,8)
movb %al, (%rbx,%r13,8)
movb %dl, 0x3(%rbx,%r13,8)
movb %dl, 0x2(%rbx,%r13,8)
movb %cl, 0x5(%rbx,%r13,8)
movb %cl, 0x4(%rbx,%r13,8)
movb %dil, 0x7(%rbx,%r13,8)
movb %dil, 0x6(%rbx,%r13,8)
jmp 0x5270
movl $0x52, %r12d
xorl %r15d, %r15d
jmp 0x5273
movb %al, 0x1(%rbx,%r13,2)
movb %al, (%rbx,%r13,2)
jmp 0x5270
movb %al, 0x1(%rbx,%r13,4)
movb %al, (%rbx,%r13,4)
movb %dil, 0x3(%rbx,%r13,4)
movb %dil, 0x2(%rbx,%r13,4)
jmp 0x5270
movb $0x8, %cl
subb %dl, %cl
shrl %cl, %esi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %edx, %ecx
shll %cl, %eax
notl %eax
andl %esi, %eax
movq %rbx, %rdi
movq %r13, %rsi
movl %eax, %ecx
callq 0xb153
xorl %r12d, %r12d
testb %r15b, %r15b
je 0x5289
incq %r13
cmpq 0x10(%rsp), %r13
jne 0x50da
xorl %r12d, %r12d
cmpl $0x3, (%rbp)
jne 0x5299
leaq 0x30(%rsp), %rdi
callq 0x5cb0
movl %r12d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq