// File: phillip-h[P]fractal-gen/lodepng.cpp
// Function: lodepng_encode(unsigned char**, unsigned long*, unsigned char const*, unsigned int, unsigned int, LodePNGState*)
unsigned lodepng_encode(unsigned char** out, size_t* outsize,
                        const unsigned char* image, unsigned w, unsigned h,
                        LodePNGState* state)
{
  LodePNGInfo info;
  ucvector outv;
  unsigned char* data = 0; /*uncompressed version of the IDAT chunk data*/
  size_t datasize = 0;

  /*provide some proper output values if error will happen*/
  *out = 0;
  *outsize = 0;
  state->error = 0;

  lodepng_info_init(&info);
  lodepng_info_copy(&info, &state->info_png);

  if((info.color.colortype == LCT_PALETTE || state->encoder.force_palette)
      && (info.color.palettesize == 0 || info.color.palettesize > 256))
  {
    state->error = 68; /*invalid palette size, it is only allowed to be 1-256*/
    return state->error;
  }

  if(state->encoder.auto_convert)
  {
    state->error = lodepng_auto_choose_color(&info.color, image, w, h, &state->info_raw);
  }
  if(state->error) return state->error;

  if(state->encoder.zlibsettings.btype > 2)
  {
    CERROR_RETURN_ERROR(state->error, 61); /*error: unexisting btype*/
  }
  if(state->info_png.interlace_method > 1)
  {
    CERROR_RETURN_ERROR(state->error, 71); /*error: unexisting interlace mode*/
  }

  state->error = checkColorValidity(info.color.colortype, info.color.bitdepth);
  if(state->error) return state->error; /*error: unexisting color type given*/
  state->error = checkColorValidity(state->info_raw.colortype, state->info_raw.bitdepth);
  if(state->error) return state->error; /*error: unexisting color type given*/

  if(!lodepng_color_mode_equal(&state->info_raw, &info.color))
  {
    unsigned char* converted;
    size_t size = (w * h * (size_t)lodepng_get_bpp(&info.color) + 7) / 8;

    converted = (unsigned char*)lodepng_malloc(size);
    if(!converted && size) state->error = 83; /*alloc fail*/
    if(!state->error)
    {
      state->error = lodepng_convert(converted, image, &info.color, &state->info_raw, w, h);
    }
    if(!state->error) preProcessScanlines(&data, &datasize, converted, w, h, &info, &state->encoder);
    lodepng_free(converted);
  }
  else preProcessScanlines(&data, &datasize, image, w, h, &info, &state->encoder);

  ucvector_init(&outv);
  while(!state->error) /*while only executed once, to break on error*/
  {
#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
    size_t i;
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
    /*write signature and chunks*/
    writeSignature(&outv);
    /*IHDR*/
    addChunk_IHDR(&outv, w, h, info.color.colortype, info.color.bitdepth, info.interlace_method);
#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
    /*unknown chunks between IHDR and PLTE*/
    if(info.unknown_chunks_data[0])
    {
      state->error = addUnknownChunks(&outv, info.unknown_chunks_data[0], info.unknown_chunks_size[0]);
      if(state->error) break;
    }
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
    /*PLTE*/
    if(info.color.colortype == LCT_PALETTE)
    {
      addChunk_PLTE(&outv, &info.color);
    }
    if(state->encoder.force_palette && (info.color.colortype == LCT_RGB || info.color.colortype == LCT_RGBA))
    {
      addChunk_PLTE(&outv, &info.color);
    }
    /*tRNS*/
    if(info.color.colortype == LCT_PALETTE && getPaletteTranslucency(info.color.palette, info.color.palettesize) != 0)
    {
      addChunk_tRNS(&outv, &info.color);
    }
    if((info.color.colortype == LCT_GREY || info.color.colortype == LCT_RGB) && info.color.key_defined)
    {
      addChunk_tRNS(&outv, &info.color);
    }
#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
    /*bKGD (must come between PLTE and the IDAt chunks*/
    if(info.background_defined) addChunk_bKGD(&outv, &info);
    /*pHYs (must come before the IDAT chunks)*/
    if(info.phys_defined) addChunk_pHYs(&outv, &info);

    /*unknown chunks between PLTE and IDAT*/
    if(info.unknown_chunks_data[1])
    {
      state->error = addUnknownChunks(&outv, info.unknown_chunks_data[1], info.unknown_chunks_size[1]);
      if(state->error) break;
    }
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
    /*IDAT (multiple IDAT chunks must be consecutive)*/
    state->error = addChunk_IDAT(&outv, data, datasize, &state->encoder.zlibsettings);
    if(state->error) break;
#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
    /*tIME*/
    if(info.time_defined) addChunk_tIME(&outv, &info.time);
    /*tEXt and/or zTXt*/
    for(i = 0; i != info.text_num; ++i)
    {
      if(strlen(info.text_keys[i]) > 79)
      {
        state->error = 66; /*text chunk too large*/
        break;
      }
      if(strlen(info.text_keys[i]) < 1)
      {
        state->error = 67; /*text chunk too small*/
        break;
      }
      if(state->encoder.text_compression)
      {
        addChunk_zTXt(&outv, info.text_keys[i], info.text_strings[i], &state->encoder.zlibsettings);
      }
      else
      {
        addChunk_tEXt(&outv, info.text_keys[i], info.text_strings[i]);
      }
    }
    /*LodePNG version id in text chunk*/
    if(state->encoder.add_id)
    {
      unsigned alread_added_id_text = 0;
      for(i = 0; i != info.text_num; ++i)
      {
        if(!strcmp(info.text_keys[i], "LodePNG"))
        {
          alread_added_id_text = 1;
          break;
        }
      }
      if(alread_added_id_text == 0)
      {
        addChunk_tEXt(&outv, "LodePNG", LODEPNG_VERSION_STRING); /*it's shorter as tEXt than as zTXt chunk*/
      }
    }
    /*iTXt*/
    for(i = 0; i != info.itext_num; ++i)
    {
      if(strlen(info.itext_keys[i]) > 79)
      {
        state->error = 66; /*text chunk too large*/
        break;
      }
      if(strlen(info.itext_keys[i]) < 1)
      {
        state->error = 67; /*text chunk too small*/
        break;
      }
      addChunk_iTXt(&outv, state->encoder.text_compression,
                    info.itext_keys[i], info.itext_langtags[i], info.itext_transkeys[i], info.itext_strings[i],
                    &state->encoder.zlibsettings);
    }

    /*unknown chunks between IDAT and IEND*/
    if(info.unknown_chunks_data[2])
    {
      state->error = addUnknownChunks(&outv, info.unknown_chunks_data[2], info.unknown_chunks_size[2]);
      if(state->error) break;
    }
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
    addChunk_IEND(&outv);

    break; /*this isn't really a while loop; no error happened so break out now!*/
  }

  lodepng_info_cleanup(&info);
  lodepng_free(data);
  /*instead of cleaning the vector up, give it to the output*/
  *out = outv.data;
  *outsize = outv.size;

  return state->error;
}

// Assembly:
subq $0x178, %rsp            # imm = 0x178
movq %rdi, 0x168(%rsp)
movq %rsi, 0x160(%rsp)
movq %rdx, 0x158(%rsp)
movl %ecx, 0x154(%rsp)
movl %r8d, 0x150(%rsp)
movq %r9, 0x148(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x38(%rsp)
movq 0x168(%rsp), %rax
movq $0x0, (%rax)
movq 0x160(%rsp), %rax
movq $0x0, (%rax)
movq 0x148(%rsp), %rax
movl $0x0, 0x1a0(%rax)
leaq 0x60(%rsp), %rdi
callq 0x95d0
movq 0x148(%rsp), %rsi
addq $0xb8, %rsi
leaq 0x60(%rsp), %rdi
callq 0x97d0
cmpl $0x3, 0x70(%rsp)
je 0xe7a3
movq 0x148(%rsp), %rax
cmpl $0x0, 0x80(%rax)
je 0xe7e8
cmpq $0x0, 0x80(%rsp)
je 0xe7bc
cmpq $0x100, 0x80(%rsp) # imm = 0x100
jbe 0xe7e8
movq 0x148(%rsp), %rax
movl $0x44, 0x1a0(%rax)
movq 0x148(%rsp), %rax
movl 0x1a0(%rax), %eax
movl %eax, 0x174(%rsp)
jmp 0xf055
movq 0x148(%rsp), %rax
cmpl $0x0, 0x68(%rax)
je 0xe839
leaq 0x60(%rsp), %rdi
addq $0x10, %rdi
movq 0x158(%rsp), %rsi
movl 0x154(%rsp), %edx
movl 0x150(%rsp), %ecx
movq 0x148(%rsp), %r8
addq $0x90, %r8
callq 0xcb40
movl %eax, %ecx
movq 0x148(%rsp), %rax
movl %ecx, 0x1a0(%rax)
movq 0x148(%rsp), %rax
cmpl $0x0, 0x1a0(%rax)
je 0xe864
movq 0x148(%rsp), %rax
movl 0x1a0(%rax), %eax
movl %eax, 0x174(%rsp)
jmp 0xf055
movq 0x148(%rsp), %rax
cmpl $0x2, 0x38(%rax)
jbe 0xe894
movq 0x148(%rsp), %rax
movl $0x3d, 0x1a0(%rax)
movl $0x3d, 0x174(%rsp)
jmp 0xf055
movq 0x148(%rsp), %rax
cmpl $0x1, 0xc0(%rax)
jbe 0xe8c7
movq 0x148(%rsp), %rax
movl $0x47, 0x1a0(%rax)
movl $0x47, 0x174(%rsp)
jmp 0xf055
movl 0x70(%rsp), %edi
movl 0x74(%rsp), %esi
callq 0xd280
movl %eax, %ecx
movq 0x148(%rsp), %rax
movl %ecx, 0x1a0(%rax)
movq 0x148(%rsp), %rax
cmpl $0x0, 0x1a0(%rax)
je 0xe90f
movq 0x148(%rsp), %rax
movl 0x1a0(%rax), %eax
movl %eax, 0x174(%rsp)
jmp 0xf055
movq 0x148(%rsp), %rax
movl 0x90(%rax), %edi
movq 0x148(%rsp), %rax
movl 0x94(%rax), %esi
callq 0xd280
movl %eax, %ecx
movq 0x148(%rsp), %rax
movl %ecx, 0x1a0(%rax)
movq 0x148(%rsp), %rax
cmpl $0x0, 0x1a0(%rax)
je 0xe96b
movq 0x148(%rsp), %rax
movl 0x1a0(%rax), %eax
movl %eax, 0x174(%rsp)
jmp 0xf055
movq 0x148(%rsp), %rdi
addq $0x90, %rdi
leaq 0x60(%rsp), %rsi
addq $0x10, %rsi
callq 0xa0e0
cmpl $0x0, %eax
jne 0xeab0
movl 0x154(%rsp), %eax
imull 0x150(%rsp), %eax
movl %eax, %eax
movq %rax, 0x10(%rsp)
leaq 0x60(%rsp), %rdi
addq $0x10, %rdi
callq 0x8cd0
movl %eax, %ecx
movq 0x10(%rsp), %rax
movl %ecx, %ecx
imulq %rcx, %rax
addq $0x7, %rax
shrq $0x3, %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rdi
callq 0x6cd0
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x30(%rsp)
jne 0xea00
cmpq $0x0, 0x28(%rsp)
je 0xea00
movq 0x148(%rsp), %rax
movl $0x53, 0x1a0(%rax)
movq 0x148(%rsp), %rax
cmpl $0x0, 0x1a0(%rax)
jne 0xea5b
movq 0x30(%rsp), %rdi
movq 0x158(%rsp), %rsi
leaq 0x60(%rsp), %rdx
addq $0x10, %rdx
movq 0x148(%rsp), %rcx
addq $0x90, %rcx
movl 0x154(%rsp), %r8d
movl 0x150(%rsp), %r9d
callq 0x9c30
movl %eax, %ecx
movq 0x148(%rsp), %rax
movl %ecx, 0x1a0(%rax)
movq 0x148(%rsp), %rax
cmpl $0x0, 0x1a0(%rax)
jne 0xeaa4
movq 0x30(%rsp), %rdx
movl 0x154(%rsp), %ecx
movl 0x150(%rsp), %r8d
movq 0x148(%rsp), %rax
addq $0x38, %rax
leaq 0x40(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x60(%rsp), %r9
movq %rax, (%rsp)
callq 0xf070
movq 0x30(%rsp), %rdi
callq 0x78b0
jmp 0xeaeb
movq 0x158(%rsp), %rdx
movl 0x154(%rsp), %ecx
movl 0x150(%rsp), %r8d
movq 0x148(%rsp), %rax
addq $0x38, %rax
leaq 0x40(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x60(%rsp), %r9
movq %rax, (%rsp)
callq 0xf070
leaq 0x48(%rsp), %rdi
callq 0xf580
movq 0x148(%rsp), %rax
cmpl $0x0, 0x1a0(%rax)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xeb12
jmp 0xf00c
leaq 0x48(%rsp), %rdi
callq 0xf5b0
movl 0x154(%rsp), %esi
movl 0x150(%rsp), %edx
movl 0x70(%rsp), %ecx
movl 0x74(%rsp), %r8d
movl 0x68(%rsp), %r9d
leaq 0x48(%rsp), %rdi
callq 0xf630
cmpq $0x0, 0x118(%rsp)
je 0xeb8f
movq 0x118(%rsp), %rsi
movq 0x130(%rsp), %rdx
leaq 0x48(%rsp), %rdi
callq 0xf6f0
movl %eax, %ecx
movq 0x148(%rsp), %rax
movl %ecx, 0x1a0(%rax)
movq 0x148(%rsp), %rax
cmpl $0x0, 0x1a0(%rax)
je 0xeb8d
jmp 0xf00c
jmp 0xeb8f
cmpl $0x3, 0x70(%rsp)
jne 0xeba9
leaq 0x60(%rsp), %rsi
addq $0x10, %rsi
leaq 0x48(%rsp), %rdi
callq 0xf790
movq 0x148(%rsp), %rax
cmpl $0x0, 0x80(%rax)
je 0xebdb
cmpl $0x2, 0x70(%rsp)
je 0xebc8
cmpl $0x6, 0x70(%rsp)
jne 0xebdb
leaq 0x60(%rsp), %rsi
addq $0x10, %rsi
leaq 0x48(%rsp), %rdi
callq 0xf790
cmpl $0x3, 0x70(%rsp)
jne 0xec0c
movq 0x78(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0xf840
cmpl $0x0, %eax
je 0xec0c
leaq 0x60(%rsp), %rsi
addq $0x10, %rsi
leaq 0x48(%rsp), %rdi
callq 0xf9b0
cmpl $0x0, 0x70(%rsp)
je 0xec1a
cmpl $0x2, 0x70(%rsp)
jne 0xec37
cmpl $0x0, 0x88(%rsp)
je 0xec37
leaq 0x60(%rsp), %rsi
addq $0x10, %rsi
leaq 0x48(%rsp), %rdi
callq 0xf9b0
cmpl $0x0, 0x98(%rsp)
je 0xec50
leaq 0x48(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xfbd0
cmpl $0x0, 0x104(%rsp)
je 0xec69
leaq 0x48(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xfd50
cmpq $0x0, 0x120(%rsp)
je 0xecb6
movq 0x120(%rsp), %rsi
movq 0x138(%rsp), %rdx
leaq 0x48(%rsp), %rdi
callq 0xf6f0
movl %eax, %ecx
movq 0x148(%rsp), %rax
movl %ecx, 0x1a0(%rax)
movq 0x148(%rsp), %rax
cmpl $0x0, 0x1a0(%rax)
je 0xecb4
jmp 0xf00c
jmp 0xecb6
movq 0x40(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x148(%rsp), %rcx
addq $0x38, %rcx
leaq 0x48(%rsp), %rdi
callq 0xfdf0
movl %eax, %ecx
movq 0x148(%rsp), %rax
movl %ecx, 0x1a0(%rax)
movq 0x148(%rsp), %rax
cmpl $0x0, 0x1a0(%rax)
je 0xecfc
jmp 0xf00c
cmpl $0x0, 0xe8(%rsp)
je 0xed1c
leaq 0x60(%rsp), %rsi
addq $0x8c, %rsi
leaq 0x48(%rsp), %rdi
callq 0xfe80
movq $0x0, 0x20(%rsp)
movq 0x20(%rsp), %rax
cmpq 0xa8(%rsp), %rax
je 0xee2a
movq 0xb0(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x30d0
cmpq $0x4f, %rax
jbe 0xed6b
movq 0x148(%rsp), %rax
movl $0x42, 0x1a0(%rax)
jmp 0xee2a
movq 0xb0(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x30d0
cmpq $0x1, %rax
jae 0xed9e
movq 0x148(%rsp), %rax
movl $0x43, 0x1a0(%rax)
jmp 0xee2a
movq 0x148(%rsp), %rax
cmpl $0x0, 0x88(%rax)
je 0xede9
movq 0xb0(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
movq 0xb8(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
movq 0x148(%rsp), %rcx
addq $0x38, %rcx
leaq 0x48(%rsp), %rdi
callq 0xff70
jmp 0xee15
movq 0xb0(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
movq 0xb8(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
leaq 0x48(%rsp), %rdi
callq 0x100e0
jmp 0xee17
movq 0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x20(%rsp)
jmp 0xed25
movq 0x148(%rsp), %rax
cmpl $0x0, 0x84(%rax)
je 0xeeba
movl $0x0, 0x1c(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x20(%rsp), %rax
cmpq 0xa8(%rsp), %rax
je 0xee99
movq 0xb0(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xda0a(%rip), %rsi      # 0x1c87d
callq 0x32d0
cmpl $0x0, %eax
jne 0xee87
movl $0x1, 0x1c(%rsp)
jmp 0xee99
jmp 0xee89
movq 0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x20(%rsp)
jmp 0xee4c
cmpl $0x0, 0x1c(%rsp)
jne 0xeeb8
movq 0x13389(%rip), %rdx     # 0x22230
leaq 0x48(%rsp), %rdi
leaq 0xd9ca(%rip), %rsi      # 0x1c87d
callq 0x100e0
jmp 0xeeba
movq $0x0, 0x20(%rsp)
movq 0x20(%rsp), %rax
cmpq 0xc0(%rsp), %rax
je 0xefb8
movq 0xc8(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x30d0
cmpq $0x4f, %rax
jbe 0xef09
movq 0x148(%rsp), %rax
movl $0x42, 0x1a0(%rax)
jmp 0xefb8
movq 0xc8(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x30d0
cmpq $0x1, %rax
jae 0xef39
movq 0x148(%rsp), %rax
movl $0x43, 0x1a0(%rax)
jmp 0xefb8
movq 0x148(%rsp), %rax
movl 0x88(%rax), %esi
movq 0xc8(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
movq 0xd0(%rsp), %rax
movq 0x20(%rsp), %rcx
movq (%rax,%rcx,8), %rcx
movq 0xd8(%rsp), %rax
movq 0x20(%rsp), %rdi
movq (%rax,%rdi,8), %r8
movq 0xe0(%rsp), %rax
movq 0x20(%rsp), %rdi
movq (%rax,%rdi,8), %r9
movq 0x148(%rsp), %rax
addq $0x38, %rax
leaq 0x48(%rsp), %rdi
movq %rax, (%rsp)
callq 0x101f0
movq 0x20(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x20(%rsp)
jmp 0xeec3
cmpq $0x0, 0x128(%rsp)
je 0xf002
movq 0x128(%rsp), %rsi
movq 0x140(%rsp), %rdx
leaq 0x48(%rsp), %rdi
callq 0xf6f0
movl %eax, %ecx
movq 0x148(%rsp), %rax
movl %ecx, 0x1a0(%rax)
movq 0x148(%rsp), %rax
cmpl $0x0, 0x1a0(%rax)
je 0xf000
jmp 0xf00c
jmp 0xf002
leaq 0x48(%rsp), %rdi
callq 0x104a0
leaq 0x60(%rsp), %rdi
callq 0x9760
movq 0x40(%rsp), %rdi
callq 0x78b0
movq 0x48(%rsp), %rcx
movq 0x168(%rsp), %rax
movq %rcx, (%rax)
movq 0x50(%rsp), %rcx
movq 0x160(%rsp), %rax
movq %rcx, (%rax)
movq 0x148(%rsp), %rax
movl 0x1a0(%rax), %eax
movl %eax, 0x174(%rsp)
movl 0x174(%rsp), %eax
addq $0x178, %rsp            # imm = 0x178
retq
nopw %cs:(%rax,%rax)