// File: mhe[P]raytracerframework_cpp/lodepng.cpp
// Function: LodePNG_decode(LodePNG_Decoder*, unsigned char**, unsigned long*, unsigned char const*, unsigned long)
void LodePNG_decode(LodePNG_Decoder* decoder, unsigned char** out, size_t* outsize, const unsigned char* in, size_t insize)
{
  *out = 0;
  *outsize = 0;
  decodeGeneric(decoder, out, outsize, in, insize);
  if(decoder->error) return;
  if(!decoder->settings.color_convert || LodePNG_InfoColor_equal(&decoder->infoRaw.color, &decoder->infoPng.color))
  {
    /*same color type, no copying or converting of data needed*/
    /*store the infoPng color settings on the infoRaw so that the infoRaw still reflects what colorType
    the raw image has to the end user*/
    if(!decoder->settings.color_convert)
    {
      decoder->error = LodePNG_InfoColor_copy(&decoder->infoRaw.color, &decoder->infoPng.color);
      if(decoder->error) return;
    }
  }
  else
  {
    /*color conversion needed; sort of copy of the data*/
    unsigned char* data = *out;

    /*TODO: check if this works according to the statement in the documentation: "The converter can convert from greyscale input color type, to 8-bit greyscale or greyscale with alpha"*/
    if(!(decoder->infoRaw.color.colorType == 2 || decoder->infoRaw.color.colorType == 6) && !(decoder->infoRaw.color.bitDepth == 8)) { decoder->error = 56; return; }

    *outsize = (decoder->infoPng.width * decoder->infoPng.height * LodePNG_InfoColor_getBpp(&decoder->infoRaw.color) + 7) / 8;
    *out = (unsigned char*)malloc(*outsize);
    if(!(*out))
    {
      decoder->error = 9947;
      *outsize = 0;
    }
    else decoder->error = LodePNG_convert(*out, data, &decoder->infoRaw.color, &decoder->infoPng.color, decoder->infoPng.width, decoder->infoPng.height);
    free(data);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movq -0x18(%rbp), %rax
movq $0x0, (%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
callq 0x28460
movq -0x8(%rbp), %rax
cmpl $0x0, 0x120(%rax)
je 0x282ed
jmp 0x28455
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x28315
movq -0x8(%rbp), %rdi
addq $0x18, %rdi
movq -0x8(%rbp), %rsi
addq $0x40, %rsi
addq $0x18, %rsi
callq 0x26350
cmpl $0x0, %eax
je 0x2835d
movq -0x8(%rbp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x28358
movq -0x8(%rbp), %rdi
addq $0x18, %rdi
movq -0x8(%rbp), %rsi
addq $0x40, %rsi
addq $0x18, %rsi
callq 0x26f60
movl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x120(%rax)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x120(%rax)
je 0x28356
jmp 0x28455
jmp 0x28358
jmp 0x28455
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x2, 0x18(%rax)
je 0x28399
movq -0x8(%rbp), %rax
cmpl $0x6, 0x18(%rax)
je 0x28399
movq -0x8(%rbp), %rax
cmpl $0x8, 0x1c(%rax)
je 0x28399
movq -0x8(%rbp), %rax
movl $0x38, 0x120(%rax)
jmp 0x28455
movq -0x8(%rbp), %rax
movl 0x40(%rax), %eax
movq -0x8(%rbp), %rcx
imull 0x44(%rcx), %eax
movl %eax, -0x34(%rbp)
movq -0x8(%rbp), %rdi
addq $0x18, %rdi
callq 0x26220
movl %eax, %ecx
movl -0x34(%rbp), %eax
imull %ecx, %eax
addl $0x7, %eax
shrl $0x3, %eax
movl %eax, %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x5570
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x2840c
movq -0x8(%rbp), %rax
movl $0x26db, 0x120(%rax)    # imm = 0x26DB
movq -0x18(%rbp), %rax
movq $0x0, (%rax)
jmp 0x2844c
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x8(%rbp), %rdx
addq $0x18, %rdx
movq -0x8(%rbp), %rcx
addq $0x40, %rcx
addq $0x18, %rcx
movq -0x8(%rbp), %rax
movl 0x40(%rax), %r8d
movq -0x8(%rbp), %rax
movl 0x44(%rax), %r9d
callq 0x27130
movl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x120(%rax)
movq -0x30(%rbp), %rdi
callq 0x54b0
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)