// File: Lut99[P]RayTracer/src/lib/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*/
    unsigned char* data = *out;
    size_t outsize;

    /*TODO: check if this works according to the statement in the documentation: "The converter can convert
    from grayscale input color type, to 8-bit grayscale or grayscale 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 0x1463d0
movq 0x20(%rsp), %rax
cmpl $0x0, 0x1f0(%rax)
je 0x146233
movq 0x20(%rsp), %rax
movl 0x1f0(%rax), %eax
movl %eax, 0x44(%rsp)
jmp 0x1463c1
movq 0x20(%rsp), %rax
cmpl $0x0, 0x2c(%rax)
je 0x146264
movq 0x20(%rsp), %rdi
addq $0x90, %rdi
movq 0x20(%rsp), %rsi
addq $0xb8, %rsi
addq $0x10, %rsi
callq 0x141150
cmpl $0x0, %eax
je 0x1462c6
movq 0x20(%rsp), %rax
cmpl $0x0, 0x2c(%rax)
jne 0x1462c1
movq 0x20(%rsp), %rdi
addq $0x90, %rdi
movq 0x20(%rsp), %rsi
addq $0xb8, %rsi
addq $0x10, %rsi
callq 0x13f920
movl %eax, %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x1f0(%rax)
movq 0x20(%rsp), %rax
cmpl $0x0, 0x1f0(%rax)
je 0x1462bf
movq 0x20(%rsp), %rax
movl 0x1f0(%rax), %eax
movl %eax, 0x44(%rsp)
jmp 0x1463c1
jmp 0x1462c1
jmp 0x1463b2
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rax
cmpl $0x2, 0x90(%rax)
je 0x14630a
movq 0x20(%rsp), %rax
cmpl $0x6, 0x90(%rax)
je 0x14630a
movq 0x20(%rsp), %rax
cmpl $0x8, 0x94(%rax)
je 0x14630a
movl $0x38, 0x44(%rsp)
jmp 0x1463c1
movq 0x30(%rsp), %rax
movl (%rax), %edi
movq 0x28(%rsp), %rax
movl (%rax), %esi
movq 0x20(%rsp), %rdx
addq $0x90, %rdx
callq 0x13fd60
movq %rax, (%rsp)
movq (%rsp), %rdi
callq 0x13ce80
movq %rax, %rcx
movq 0x38(%rsp), %rax
movq %rcx, (%rax)
movq 0x38(%rsp), %rax
cmpq $0x0, (%rax)
jne 0x14635d
movq 0x20(%rsp), %rax
movl $0x53, 0x1f0(%rax)
jmp 0x1463a8
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 0x140bc0
movl %eax, %ecx
movq 0x20(%rsp), %rax
movl %ecx, 0x1f0(%rax)
movq 0x8(%rsp), %rdi
callq 0x13e2d0
movq 0x20(%rsp), %rax
movl 0x1f0(%rax), %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nopw (%rax,%rax)