// File: drummyfish[P]flowchart/src/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 != LAC_NO)
  {
    state->error = doAutoChooseColor(&info.color, image, w, h, &state->info_raw,
                                     state->encoder.auto_convert);
  }
  if(state->error) return state->error;

  if(state->encoder.zlibsettings.windowsize > 32768)
  {
    CERROR_RETURN_ERROR(state->error, 60); /*error: windowsize larger than allowed*/
  }
  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 * lodepng_get_bpp(&info.color) + 7) / 8;

    converted = (unsigned char*)mymalloc(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);
    myfree(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", 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*/
    /*IEND*/
    addChunk_IEND(&outv);

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

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

  return state->error;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl %r8d, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0x138(%rbp)
movq $0x0, -0x140(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movq -0x18(%rbp), %rax
movq $0x0, (%rax)
movq -0x30(%rbp), %rax
movl $0x0, 0x190(%rax)
leaq -0x118(%rbp), %rdi
callq 0x5b90
movq -0x30(%rbp), %rsi
addq $0xa8, %rsi
leaq -0x118(%rbp), %rdi
callq 0x5da0
cmpl $0x3, -0x108(%rbp)
je 0x9822
movq -0x30(%rbp), %rax
cmpl $0x0, 0x70(%rax)
je 0x9859
cmpq $0x0, -0xf8(%rbp)
je 0x9839
cmpq $0x100, -0xf8(%rbp) # imm = 0x100
jbe 0x9859
movq -0x30(%rbp), %rax
movl $0x44, 0x190(%rax)
movq -0x30(%rbp), %rax
movl 0x190(%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0xa04e
movq -0x30(%rbp), %rax
cmpl $0x0, 0x60(%rax)
je 0x989c
leaq -0x118(%rbp), %rdi
addq $0x10, %rdi
movq -0x20(%rbp), %rsi
movl -0x24(%rbp), %edx
movl -0x28(%rbp), %ecx
movq -0x30(%rbp), %r8
addq $0x80, %r8
movq -0x30(%rbp), %rax
movl 0x60(%rax), %r9d
callq 0xa060
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x190(%rax)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x190(%rax)
je 0x98bb
movq -0x30(%rbp), %rax
movl 0x190(%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0xa04e
movq -0x30(%rbp), %rax
cmpl $0x8000, 0x40(%rax)     # imm = 0x8000
jbe 0x98e2
movq -0x30(%rbp), %rax
movl $0x3c, 0x190(%rax)
movl $0x3c, -0x4(%rbp)
jmp 0xa04e
movq -0x30(%rbp), %rax
cmpl $0x2, 0x38(%rax)
jbe 0x9906
movq -0x30(%rbp), %rax
movl $0x3d, 0x190(%rax)
movl $0x3d, -0x4(%rbp)
jmp 0xa04e
movq -0x30(%rbp), %rax
cmpl $0x1, 0xb0(%rax)
jbe 0x992d
movq -0x30(%rbp), %rax
movl $0x47, 0x190(%rax)
movl $0x47, -0x4(%rbp)
jmp 0xa04e
movl -0x108(%rbp), %edi
movl -0x104(%rbp), %esi
callq 0x7a50
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x190(%rax)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x190(%rax)
je 0x9969
movq -0x30(%rbp), %rax
movl 0x190(%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0xa04e
movq -0x30(%rbp), %rax
movl 0x80(%rax), %edi
movq -0x30(%rbp), %rax
movl 0x84(%rax), %esi
callq 0x7a50
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x190(%rax)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x190(%rax)
je 0x99ad
movq -0x30(%rbp), %rax
movl 0x190(%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0xa04e
movq -0x30(%rbp), %rdi
addq $0x80, %rdi
leaq -0x118(%rbp), %rsi
addq $0x10, %rsi
callq 0x64f0
cmpl $0x0, %eax
jne 0x9ad3
movl -0x24(%rbp), %eax
imull -0x28(%rbp), %eax
movl %eax, -0x160(%rbp)
leaq -0x118(%rbp), %rdi
addq $0x10, %rdi
callq 0x5320
movl %eax, %ecx
movl -0x160(%rbp), %eax
imull %ecx, %eax
addl $0x7, %eax
shrl $0x3, %eax
movl %eax, %eax
movq %rax, -0x150(%rbp)
movq -0x150(%rbp), %rdi
callq 0x3840
movq %rax, -0x148(%rbp)
cmpq $0x0, -0x148(%rbp)
jne 0x9a3d
cmpq $0x0, -0x150(%rbp)
je 0x9a3d
movq -0x30(%rbp), %rax
movl $0x53, 0x190(%rax)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x190(%rax)
jne 0x9a84
movq -0x148(%rbp), %rdi
movq -0x20(%rbp), %rsi
leaq -0x118(%rbp), %rdx
addq $0x10, %rdx
movq -0x30(%rbp), %rcx
addq $0x80, %rcx
movl -0x24(%rbp), %r8d
movl -0x28(%rbp), %r9d
callq 0x61a0
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x190(%rax)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x190(%rax)
jne 0x9ac5
movq -0x148(%rbp), %rdx
movl -0x24(%rbp), %ecx
movl -0x28(%rbp), %r8d
movq -0x30(%rbp), %rax
addq $0x38, %rax
leaq -0x138(%rbp), %rdi
leaq -0x140(%rbp), %rsi
leaq -0x118(%rbp), %r9
movq %rax, (%rsp)
callq 0xa540
movq -0x148(%rbp), %rdi
callq 0x4050
jmp 0x9b04
movq -0x20(%rbp), %rdx
movl -0x24(%rbp), %ecx
movl -0x28(%rbp), %r8d
movq -0x30(%rbp), %rax
addq $0x38, %rax
leaq -0x138(%rbp), %rdi
leaq -0x140(%rbp), %rsi
leaq -0x118(%rbp), %r9
movq %rax, (%rsp)
callq 0xa540
leaq -0x130(%rbp), %rdi
callq 0xa980
movq -0x30(%rbp), %rax
cmpl $0x0, 0x190(%rax)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x9b29
jmp 0xa00d
leaq -0x130(%rbp), %rdi
callq 0xa9b0
movl -0x24(%rbp), %esi
movl -0x28(%rbp), %edx
movl -0x108(%rbp), %ecx
movl -0x104(%rbp), %r8d
movl -0x110(%rbp), %r9d
leaq -0x130(%rbp), %rdi
callq 0xaa40
cmpq $0x0, -0x60(%rbp)
je 0x9b96
movq -0x60(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq -0x130(%rbp), %rdi
callq 0xab00
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x190(%rax)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x190(%rax)
je 0x9b94
jmp 0xa00d
jmp 0x9b96
cmpl $0x3, -0x108(%rbp)
jne 0x9bb6
leaq -0x118(%rbp), %rsi
addq $0x10, %rsi
leaq -0x130(%rbp), %rdi
callq 0xab90
movq -0x30(%rbp), %rax
cmpl $0x0, 0x70(%rax)
je 0x9be9
cmpl $0x2, -0x108(%rbp)
je 0x9bd2
cmpl $0x6, -0x108(%rbp)
jne 0x9be9
leaq -0x118(%rbp), %rsi
addq $0x10, %rsi
leaq -0x130(%rbp), %rdi
callq 0xab90
cmpl $0x3, -0x108(%rbp)
jne 0x9c21
movq -0x100(%rbp), %rdi
movq -0xf8(%rbp), %rsi
callq 0xac40
cmpl $0x0, %eax
je 0x9c21
leaq -0x118(%rbp), %rsi
addq $0x10, %rsi
leaq -0x130(%rbp), %rdi
callq 0xad90
cmpl $0x0, -0x108(%rbp)
je 0x9c33
cmpl $0x2, -0x108(%rbp)
jne 0x9c53
cmpl $0x0, -0xf0(%rbp)
je 0x9c53
leaq -0x118(%rbp), %rsi
addq $0x10, %rsi
leaq -0x130(%rbp), %rdi
callq 0xad90
cmpl $0x0, -0xe0(%rbp)
je 0x9c6f
leaq -0x130(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0xaf80
cmpl $0x0, -0x74(%rbp)
je 0x9c88
leaq -0x130(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0xb0e0
cmpq $0x0, -0x58(%rbp)
je 0x9cc3
movq -0x58(%rbp), %rsi
movq -0x40(%rbp), %rdx
leaq -0x130(%rbp), %rdi
callq 0xab00
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x190(%rax)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x190(%rax)
je 0x9cc1
jmp 0xa00d
jmp 0x9cc3
movq -0x138(%rbp), %rsi
movq -0x140(%rbp), %rdx
movq -0x30(%rbp), %rcx
addq $0x38, %rcx
leaq -0x130(%rbp), %rdi
callq 0xb170
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x190(%rax)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x190(%rax)
je 0x9d03
jmp 0xa00d
cmpl $0x0, -0x90(%rbp)
je 0x9d26
leaq -0x118(%rbp), %rsi
addq $0x8c, %rsi
leaq -0x130(%rbp), %rdi
callq 0xb1f0
movq $0x0, -0x158(%rbp)
movq -0x158(%rbp), %rax
cmpq -0xd0(%rbp), %rax
jae 0x9e32
movq -0xc8(%rbp), %rax
movq -0x158(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x3100
cmpq $0x4f, %rax
jbe 0x9d75
movq -0x30(%rbp), %rax
movl $0x42, 0x190(%rax)
jmp 0x9e32
movq -0xc8(%rbp), %rax
movq -0x158(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x3100
cmpq $0x1, %rax
jae 0x9da5
movq -0x30(%rbp), %rax
movl $0x43, 0x190(%rax)
jmp 0x9e32
movq -0x30(%rbp), %rax
cmpl $0x0, 0x78(%rax)
je 0x9de9
movq -0xc8(%rbp), %rax
movq -0x158(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0xc0(%rbp), %rax
movq -0x158(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x30(%rbp), %rcx
addq $0x38, %rcx
leaq -0x130(%rbp), %rdi
callq 0xb2e0
jmp 0x9e19
movq -0xc8(%rbp), %rax
movq -0x158(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0xc0(%rbp), %rax
movq -0x158(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
leaq -0x130(%rbp), %rdi
callq 0xb410
jmp 0x9e1b
movq -0x158(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x158(%rbp)
jmp 0x9d31
movq -0x30(%rbp), %rax
cmpl $0x0, 0x74(%rax)
je 0x9ecf
movl $0x0, -0x15c(%rbp)
movq $0x0, -0x158(%rbp)
movq -0x158(%rbp), %rax
cmpq -0xd0(%rbp), %rax
jae 0x9eaa
movq -0xc8(%rbp), %rax
movq -0x158(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x14684(%rip), %rsi     # 0x1e502
callq 0x3390
cmpl $0x0, %eax
jne 0x9e94
movl $0x1, -0x15c(%rbp)
jmp 0x9eaa
jmp 0x9e96
movq -0x158(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x158(%rbp)
jmp 0x9e55
cmpl $0x0, -0x15c(%rbp)
jne 0x9ecd
leaq -0x130(%rbp), %rdi
leaq 0x14641(%rip), %rsi     # 0x1e502
leaq 0x14642(%rip), %rdx     # 0x1e50a
callq 0xb410
jmp 0x9ecf
movq $0x0, -0x158(%rbp)
movq -0x158(%rbp), %rax
cmpq -0xb8(%rbp), %rax
jae 0x9fc9
movq -0xb0(%rbp), %rax
movq -0x158(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x3100
cmpq $0x4f, %rax
jbe 0x9f1e
movq -0x30(%rbp), %rax
movl $0x42, 0x190(%rax)
jmp 0x9fc9
movq -0xb0(%rbp), %rax
movq -0x158(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x3100
cmpq $0x1, %rax
jae 0x9f4b
movq -0x30(%rbp), %rax
movl $0x43, 0x190(%rax)
jmp 0x9fc9
movq -0x30(%rbp), %rax
movl 0x78(%rax), %esi
movq -0xb0(%rbp), %rax
movq -0x158(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0xa8(%rbp), %rax
movq -0x158(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq -0xa0(%rbp), %rax
movq -0x158(%rbp), %rdi
movq (%rax,%rdi,8), %r8
movq -0x98(%rbp), %rax
movq -0x158(%rbp), %rdi
movq (%rax,%rdi,8), %r9
movq -0x30(%rbp), %rax
addq $0x38, %rax
leaq -0x130(%rbp), %rdi
movq %rax, (%rsp)
callq 0xb4f0
movq -0x158(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x158(%rbp)
jmp 0x9eda
cmpq $0x0, -0x50(%rbp)
je 0xa001
movq -0x50(%rbp), %rsi
movq -0x38(%rbp), %rdx
leaq -0x130(%rbp), %rdi
callq 0xab00
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x190(%rax)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x190(%rax)
je 0x9fff
jmp 0xa00d
jmp 0xa001
leaq -0x130(%rbp), %rdi
callq 0xb720
leaq -0x118(%rbp), %rdi
callq 0x5d20
movq -0x138(%rbp), %rdi
callq 0x4050
movq -0x130(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x128(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movl 0x190(%rax), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
nopw (%rax,%rax)