// File: phillip-h[P]fractal-gen/lodepng.cpp
// Function: lodepng_decode(unsigned char**, unsigned int*, unsigned int*, LodePNGState*, unsigned char const*, unsigned long)
unsigned lodepng_decode(unsigned char** out, unsigned* w, unsigned* h,
                        LodePNGState* state,
                        const unsigned char* in, size_t insize)
{
  *out = 0;
  decodeGeneric(out, w, h, state, in, insize);
  if(state->error) return state->error;
  if(!state->decoder.color_convert || lodepng_color_mode_equal(&state->info_raw, &state->info_png.color))
  {
    /*same color type, no copying or converting of data needed*/
    /*store the info_png color settings on the info_raw so that the info_raw still reflects what colortype
    the raw image has to the end user*/
    if(!state->decoder.color_convert)
    {
      state->error = lodepng_color_mode_copy(&state->info_raw, &state->info_png.color);
      if(state->error) return state->error;
    }
  }
  else
  {
    /*color conversion needed; sort of copy of the data*/
    unsigned char* data = *out;
    size_t outsize;

    /*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(!(state->info_raw.colortype == LCT_RGB || state->info_raw.colortype == LCT_RGBA)
       && !(state->info_raw.bitdepth == 8))
    {
      return 56; /*unsupported color mode conversion*/
    }

    outsize = lodepng_get_raw_size(*w, *h, &state->info_raw);
    *out = (unsigned char*)lodepng_malloc(outsize);
    if(!(*out))
    {
      state->error = 83; /*alloc fail*/
    }
    else state->error = lodepng_convert(*out, data, &state->info_raw,
                                        &state->info_png.color, *w, *h);
    lodepng_free(data);
  }
  return state->error;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
movq %r8, 0x18(%rsp)
movq %r9, 0x10(%rsp)
movq 0x38(%rsp), %rax
movq $0x0, (%rax)
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %r8
movq 0x10(%rsp), %r9
callq 0xd590
movq 0x20(%rsp), %rax
cmpl $0x0, 0x1a0(%rax)
je 0xd3f3
movq 0x20(%rsp), %rax
movl 0x1a0(%rax), %eax
movl %eax, 0x44(%rsp)
jmp 0xd581
movq 0x20(%rsp), %rax
cmpl $0x0, 0x2c(%rax)
je 0xd424
movq 0x20(%rsp), %rdi
addq $0x90, %rdi
movq 0x20(%rsp), %rsi
addq $0xb8, %rsi
addq $0x10, %rsi
callq 0xa0e0
cmpl $0x0, %eax
je 0xd486
movq 0x20(%rsp), %rax
cmpl $0x0, 0x2c(%rax)
jne 0xd481
movq 0x20(%rsp), %rdi
addq $0x90, %rdi
movq 0x20(%rsp), %rsi
addq $0xb8, %rsi
addq $0x10, %rsi
callq 0x8af0
movl %eax, %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x1a0(%rax)
movq 0x20(%rsp), %rax
cmpl $0x0, 0x1a0(%rax)
je 0xd47f
movq 0x20(%rsp), %rax
movl 0x1a0(%rax), %eax
movl %eax, 0x44(%rsp)
jmp 0xd581
jmp 0xd481
jmp 0xd572
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rax
cmpl $0x2, 0x90(%rax)
je 0xd4ca
movq 0x20(%rsp), %rax
cmpl $0x6, 0x90(%rax)
je 0xd4ca
movq 0x20(%rsp), %rax
cmpl $0x8, 0x94(%rax)
je 0xd4ca
movl $0x38, 0x44(%rsp)
jmp 0xd581
movq 0x30(%rsp), %rax
movl (%rax), %edi
movq 0x28(%rsp), %rax
movl (%rax), %esi
movq 0x20(%rsp), %rdx
addq $0x90, %rdx
callq 0x8ee0
movq %rax, (%rsp)
movq (%rsp), %rdi
callq 0x6cd0
movq %rax, %rcx
movq 0x38(%rsp), %rax
movq %rcx, (%rax)
movq 0x38(%rsp), %rax
cmpq $0x0, (%rax)
jne 0xd51d
movq 0x20(%rsp), %rax
movl $0x53, 0x1a0(%rax)
jmp 0xd568
movq 0x38(%rsp), %rax
movq (%rax), %rdi
movq 0x8(%rsp), %rsi
movq 0x20(%rsp), %rdx
addq $0x90, %rdx
movq 0x20(%rsp), %rcx
addq $0xb8, %rcx
addq $0x10, %rcx
movq 0x30(%rsp), %rax
movl (%rax), %r8d
movq 0x28(%rsp), %rax
movl (%rax), %r9d
callq 0x9c30
movl %eax, %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x1a0(%rax)
movq 0x8(%rsp), %rdi
callq 0x78b0
movq 0x20(%rsp), %rax
movl 0x1a0(%rax), %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nopw (%rax,%rax)