// File: /workspace/github_cpp_makefile_star2_O1/mhe[P]raytracerframework_cpp/lodepng.cpp
// Function: LodePNG_encode(LodePNG_Encoder*, unsigned char**, unsigned long*, unsigned char const*, unsigned int, unsigned int)
void LodePNG_encode(LodePNG_Encoder* encoder, unsigned char** out, size_t* outsize, const unsigned char* image, unsigned w, unsigned h)
{
  LodePNG_InfoPng 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;
  encoder->error = 0;
  
  info = encoder->infoPng; /*UNSAFE copy to avoid having to cleanup! but we will only change primitive parameters, and not invoke the cleanup function nor touch the palette's buffer so we use it safely*/
  info.width = w;
  info.height = h;
  
  if(encoder->settings.autoLeaveOutAlphaChannel && isFullyOpaque(image, w, h, &encoder->infoRaw.color))
  {
    /*go to a color type without alpha channel*/
    if(info.color.colorType == 6) info.color.colorType = 2;
    else if(info.color.colorType == 4) info.color.colorType = 0;
  }
  
  if(encoder->settings.zlibsettings.windowSize > 32768) { encoder->error = 60; return; } /*error: windowsize larger than allowed*/
  if(encoder->settings.zlibsettings.btype > 2) { encoder->error = 61; return; } /*error: unexisting btype*/
  if(encoder->infoPng.interlaceMethod > 1) { encoder->error = 71; return; } /*error: unexisting interlace mode*/
  if((encoder->error = checkColorValidity(info.color.colorType, info.color.bitDepth))) return; /*error: unexisting color type given*/
  if((encoder->error = checkColorValidity(encoder->infoRaw.color.colorType, encoder->infoRaw.color.bitDepth))) return; /*error: unexisting color type given*/
  
  if(!LodePNG_InfoColor_equal(&encoder->infoRaw.color, &info.color))
  {
    unsigned char* converted;
    size_t size = (w * h * LodePNG_InfoColor_getBpp(&info.color) + 7) / 8;
    
    if((info.color.colorType != 6 && info.color.colorType != 2) || (info.color.bitDepth != 8)) { encoder->error = 59; return; } /*for the output image, only these types are supported*/
    converted = (unsigned char*)malloc(size);
    if(!converted && size) encoder->error = 9955; /*error: malloc failed*/
    if(!encoder->error) encoder->error = LodePNG_convert(converted, image, &info.color, &encoder->infoRaw.color, w, h);
    if(!encoder->error) preProcessScanlines(&data, &datasize, converted, &info);/*filter(data.data, converted.data, w, h, LodePNG_InfoColor_getBpp(&info.color));*/
    free(converted);
  }
  else preProcessScanlines(&data, &datasize, image, &info);/*filter(data.data, image, w, h, LodePNG_InfoColor_getBpp(&info.color));*/
  
  ucvector_init(&outv);
  while(!encoder->error) /*not really a while loop, this is only used to break out if an error happens to avoid goto's to do the ucvector cleanup*/
  {
#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.bitDepth, info.color.colorType, info.interlaceMethod);
#ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
    /*unknown chunks between IHDR and PLTE*/
    if(info.unknown_chunks.data[0]) { encoder->error = addUnknownChunks(&outv, info.unknown_chunks.data[0], info.unknown_chunks.datasize[0]); if(encoder->error) break; }
#endif /*LODEPNG_COMPILE_UNKNOWN_CHUNKS*/
    /*PLTE*/
    if(info.color.colorType == 3)
    {
      if(info.color.palettesize == 0 || info.color.palettesize > 256) { encoder->error = 68; break; }
      addChunk_PLTE(&outv, &info.color);
    }
    if(encoder->settings.force_palette && (info.color.colorType == 2 || info.color.colorType == 6))
    {
      if(info.color.palettesize == 0 || info.color.palettesize > 256) { encoder->error = 68; break; }
      addChunk_PLTE(&outv, &info.color);
    }
    /*tRNS*/
    if(info.color.colorType == 3 && !isPaletteFullyOpaque(info.color.palette, info.color.palettesize)) addChunk_tRNS(&outv, &info.color);
    if((info.color.colorType == 0 || info.color.colorType == 2) && 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);
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
#ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
    /*unknown chunks between PLTE and IDAT*/
    if(info.unknown_chunks.data[1]) { encoder->error = addUnknownChunks(&outv, info.unknown_chunks.data[1], info.unknown_chunks.datasize[1]); if(encoder->error) break; }
#endif /*LODEPNG_COMPILE_UNKNOWN_CHUNKS*/
    /*IDAT (multiple IDAT chunks must be consecutive)*/
    encoder->error = addChunk_IDAT(&outv, data, datasize, &encoder->settings.zlibsettings);
    if(encoder->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) { encoder->error = 66; break; }
      if(strlen(info.text.keys[i]) < 1) { encoder->error = 67; break; }
      if(encoder->settings.text_compression)
        addChunk_zTXt(&outv, info.text.keys[i], info.text.strings[i], &encoder->settings.zlibsettings);
      else
        addChunk_tEXt(&outv, info.text.keys[i], info.text.strings[i]);
    }
    /*LodePNG version id in text chunk*/
    if(encoder->settings.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) { encoder->error = 66; break; }
      if(strlen(info.itext.keys[i]) < 1) { encoder->error = 67; break; }
      addChunk_iTXt(&outv, encoder->settings.text_compression,
                    info.itext.keys[i], info.itext.langtags[i], info.itext.transkeys[i], info.itext.strings[i], 
                    &encoder->settings.zlibsettings);
    }
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
#ifdef LODEPNG_COMPILE_UNKNOWN_CHUNKS
    /*unknown chunks between IDAT and IEND*/
    if(info.unknown_chunks.data[2]) { encoder->error = addUnknownChunks(&outv, info.unknown_chunks.data[2], info.unknown_chunks.datasize[2]); if(encoder->error) break; }
#endif /*LODEPNG_COMPILE_UNKNOWN_CHUNKS*/
    /*IEND*/
    addChunk_IEND(&outv);
    
    break; /*this isn't really a while loop; no error happened so break out now!*/
  }
  
  free(data);
  /*instead of cleaning the vector up, give it to the output*/
  *out = outv.data;
  *outsize = outv.size;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movl %r9d, %r12d
movl %r8d, %r13d
movq %rcx, %rbx
movq %rdi, %r15
xorl %eax, %eax
movq %rax, -0x58(%rbp)
movq %rax, -0x68(%rbp)
movq %rsi, -0x70(%rbp)
movq %rax, (%rsi)
movq %rdx, -0x78(%rbp)
movq %rax, (%rdx)
movl $0x0, 0x128(%rdi)
leaq 0x20(%rdi), %rsi
leaq -0x158(%rbp), %r14
movl $0xe0, %edx
movq %r14, %rdi
callq 0x5270
movl %r13d, (%r14)
movl %r12d, 0x4(%r14)
cmpl $0x0, 0xc(%r15)
je 0x2046b
movl 0x100(%r15), %ecx
cmpl $0x3, %ecx
je 0x203d6
movl %r12d, %eax
imull %r13d, %eax
cmpl $0x4, %ecx
je 0x203a2
cmpl $0x6, %ecx
jne 0x2046b
cmpl $0x8, 0x104(%r15)
jne 0x20425
testl %eax, %eax
je 0x2044c
movl %eax, %eax
shlq $0x2, %rax
xorl %ecx, %ecx
movl %ecx, %edx
andl $-0x4, %edx
cmpb $-0x1, 0x3(%rbx,%rdx)
jne 0x2046b
addq $0x4, %rcx
cmpq %rcx, %rax
jne 0x20384
jmp 0x2044c
cmpl $0x8, 0x104(%r15)
jne 0x203fc
testl %eax, %eax
je 0x2044c
movl %eax, %eax
addq %rax, %rax
xorl %ecx, %ecx
movl %ecx, %edx
andl $-0x2, %edx
cmpb $-0x1, 0x1(%rbx,%rdx)
jne 0x2046b
addq $0x2, %rcx
cmpq %rcx, %rax
jne 0x203bb
jmp 0x2044c
movq 0x110(%r15), %rax
testq %rax, %rax
je 0x2044c
movq 0x108(%r15), %rcx
xorl %edx, %edx
cmpb $-0x1, 0x3(%rcx,%rdx,4)
jne 0x2046b
incq %rdx
cmpq %rdx, %rax
jne 0x203eb
jmp 0x2044c
testl %eax, %eax
je 0x2044c
movl %eax, %eax
shlq $0x2, %rax
xorl %ecx, %ecx
leal 0x2(%rcx), %edx
cmpb $-0x1, (%rbx,%rdx)
jne 0x2046b
leal 0x3(%rcx), %edx
cmpb $-0x1, (%rbx,%rdx)
jne 0x2046b
addq $0x4, %rcx
cmpq %rcx, %rax
jne 0x20408
jmp 0x2044c
testl %eax, %eax
je 0x2044c
movl %eax, %eax
shlq $0x3, %rax
xorl %ecx, %ecx
leal 0x6(%rcx), %edx
cmpb $-0x1, (%rbx,%rdx)
jne 0x2046b
leal 0x7(%rcx), %edx
cmpb $-0x1, (%rbx,%rdx)
jne 0x2046b
addq $0x8, %rcx
cmpq %rcx, %rax
jne 0x20431
movl -0x140(%rbp), %eax
cmpl $0x6, %eax
je 0x20460
cmpl $0x4, %eax
jne 0x2046b
xorl %eax, %eax
jmp 0x20465
movl $0x2, %eax
movl %eax, -0x140(%rbp)
cmpl $0x8001, 0x8(%r15)      # imm = 0x8001
jb 0x20485
movl $0x3c, 0x128(%r15)
jmp 0x2064c
cmpl $0x3, (%r15)
jb 0x2049b
movl $0x3d, 0x128(%r15)
jmp 0x2064c
cmpl $0x2, 0x30(%r15)
jb 0x204b2
movl $0x47, 0x128(%r15)
jmp 0x2064c
movl -0x140(%rbp), %edi
movl -0x13c(%rbp), %esi
movq %rdi, -0x48(%rbp)
movl %esi, -0x50(%rbp)
callq 0x1ea53
movl %eax, 0x128(%r15)
testl %eax, %eax
jne 0x2064c
movl 0x100(%r15), %r14d
movl 0x104(%r15), %esi
movl %r14d, %edi
callq 0x1ea53
movl %eax, 0x128(%r15)
testl %eax, %eax
jne 0x2064c
movq -0x48(%rbp), %rcx
cmpl %ecx, %r14d
jne 0x20532
movl 0x104(%r15), %eax
cmpl -0x13c(%rbp), %eax
jne 0x20532
leaq -0x58(%rbp), %rdi
leaq -0x68(%rbp), %rsi
leaq -0x158(%rbp), %rcx
movq %rbx, %rdx
callq 0x209cf
jmp 0x20614
xorl %eax, %eax
cmpl $0x6, %ecx
ja 0x20543
leaq 0x1163c(%rip), %rax     # 0x31b7c
movl (%rax,%rcx,4), %eax
andl $-0x5, %ecx
xorl $0x2, %ecx
movl -0x50(%rbp), %edx
xorl $0x8, %edx
orl %ecx, %edx
sete %cl
jne 0x20605
movb %cl, -0x60(%rbp)
movl %r12d, %r14d
imull %r13d, %r14d
imull %eax, %r14d
imull -0x13c(%rbp), %r14d
addl $0x7, %r14d
movl %r14d, %edi
shrl $0x3, %edi
callq 0x5420
movq %rax, %rdx
testq %rax, %rax
setne %al
cmpl $0x8, %r14d
setb %cl
orb %al, %cl
jne 0x2059e
movl $0x26e3, 0x128(%r15) # imm = 0x26E3
cmpl $0x0, 0x128(%r15)
jne 0x205d4
leaq 0x100(%r15), %rcx
movq %rdx, %rdi
movq %rbx, %rsi
movq %rdx, %rbx
leaq -0x140(%rbp), %rdx
movl %r13d, %r8d
movl %r12d, %r9d
callq 0x1dfc6
movq %rbx, %rdx
movl %eax, 0x128(%r15)
cmpl $0x0, 0x128(%r15)
jne 0x205f8
leaq -0x58(%rbp), %rdi
leaq -0x68(%rbp), %rsi
leaq -0x158(%rbp), %rcx
movq %rdx, %rbx
callq 0x209cf
movq %rbx, %rdx
movq %rdx, %rdi
callq 0x5390
movb -0x60(%rbp), %cl
jmp 0x20610
movl $0x3b, 0x128(%r15)
testb %cl, %cl
je 0x2064c
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
cmpl $0x0, 0x128(%r15)
je 0x2065e
movq -0x58(%rbp), %rdi
callq 0x5390
movq -0x40(%rbp), %rax
movq -0x70(%rbp), %rcx
movq %rax, (%rcx)
movq -0x38(%rbp), %rax
movq -0x78(%rbp), %rcx
movq %rax, (%rcx)
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x40(%rbp), %rbx
movq %rbx, %rdi
callq 0x20f6c
movl -0x148(%rbp), %r9d
movq %rbx, %rdi
movl %r13d, %esi
movl %r12d, %edx
movl -0x50(%rbp), %ecx
movq -0x48(%rbp), %r8
callq 0x21071
movq -0xa8(%rbp), %rsi
testq %rsi, %rsi
je 0x206ad
movq -0x90(%rbp), %rdx
leaq -0x40(%rbp), %rdi
callq 0x2129d
movl %eax, 0x128(%r15)
testl %eax, %eax
jne 0x2062d
movq -0x48(%rbp), %rbx
cmpl $0x3, %ebx
jne 0x206dc
movq $-0x101, %rax           # imm = 0xFEFF
movq -0x130(%rbp), %rcx
addq %rax, %rcx
cmpq %rax, %rcx
jbe 0x20703
leaq -0x40(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x2130f
movl %ebx, %eax
andl $-0x5, %eax
cmpl $0x2, %eax
jne 0x20723
cmpl $0x0, 0x10(%r15)
je 0x20723
movq $-0x101, %rax           # imm = 0xFEFF
movq -0x130(%rbp), %rcx
addq %rax, %rcx
cmpq %rax, %rcx
ja 0x20713
movl $0x44, 0x128(%r15)
jmp 0x2062d
leaq -0x40(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x2130f
cmpl $0x3, %ebx
jne 0x2075e
movq -0x130(%rbp), %rax
testq %rax, %rax
je 0x2075e
movq -0x138(%rbp), %rcx
xorl %edx, %edx
cmpb $-0x1, 0x3(%rcx,%rdx,4)
jne 0x2074e
incq %rdx
cmpq %rdx, %rax
jne 0x2073d
jmp 0x2075e
leaq -0x40(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x21407
testl $0xfffffffd, %ebx       # imm = 0xFFFFFFFD
jne 0x2077f
cmpl $0x0, -0x128(%rbp)
je 0x2077f
leaq -0x40(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x21407
cmpl $0x0, -0x118(%rbp)
je 0x20798
leaq -0x40(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x21770
cmpl $0x0, -0xb8(%rbp)
je 0x207b1
leaq -0x40(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x21a7b
movq -0xa0(%rbp), %rsi
testq %rsi, %rsi
je 0x207dc
movq -0x88(%rbp), %rdx
leaq -0x40(%rbp), %rdi
callq 0x2129d
movl %eax, 0x128(%r15)
testl %eax, %eax
jne 0x2062d
movq -0x58(%rbp), %rsi
movq -0x68(%rbp), %rdx
leaq -0x40(%rbp), %rdi
movq %r15, %rcx
callq 0x21b96
movl %eax, 0x128(%r15)
testl %eax, %eax
jne 0x2062d
cmpb $0x0, -0xc8(%rbp)
je 0x20818
leaq -0xc4(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x21c1d
movq -0x108(%rbp), %r14
testq %r14, %r14
je 0x20892
movq -0x100(%rbp), %r13
movq -0xf8(%rbp), %rax
movq %rax, -0x48(%rbp)
xorl %ebx, %ebx
movq (%r13,%rbx,8), %r12
movq %r12, %rdi
callq 0x5120
cmpq $0x4f, %rax
ja 0x2087f
testq %rax, %rax
je 0x20886
cmpl $0x0, 0x18(%r15)
movq -0x48(%rbp), %rax
movq (%rax,%rbx,8), %rdx
leaq -0x40(%rbp), %rdi
movq %r12, %rsi
je 0x20870
movq %r15, %rcx
callq 0x21cb7
jmp 0x20875
callq 0x21efc
incq %rbx
cmpq %rbx, %r14
jne 0x20838
jmp 0x20892
movl $0x42, %eax
jmp 0x2088b
movl $0x43, %eax
movl %eax, 0x128(%r15)
cmpl $0x0, 0x14(%r15)
je 0x208de
testq %r14, %r14
je 0x208c7
movq -0x100(%rbp), %r12
xorl %r13d, %r13d
leaq 0x112e9(%rip), %rbx     # 0x31b98
movq (%r12,%r13,8), %rdi
movq %rbx, %rsi
callq 0x5440
testl %eax, %eax
je 0x208de
incq %r13
cmpq %r13, %r14
jne 0x208af
leaq 0x112ca(%rip), %rsi     # 0x31b98
leaq 0x112cb(%rip), %rdx     # 0x31ba0
leaq -0x40(%rbp), %rdi
callq 0x21efc
movq -0xf0(%rbp), %r14
testq %r14, %r14
je 0x20979
movq -0xe8(%rbp), %r13
movq -0xe0(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x60(%rbp)
xorl %ebx, %ebx
movq (%r13,%rbx,8), %r12
movq %r12, %rdi
callq 0x5120
cmpq $0x4f, %rax
ja 0x20966
testq %rax, %rax
je 0x2096d
movl 0x18(%r15), %esi
movq -0x48(%rbp), %rax
movq (%rax,%rbx,8), %rcx
movq -0x50(%rbp), %rax
movq (%rax,%rbx,8), %r8
movq -0x60(%rbp), %rax
movq (%rax,%rbx,8), %r9
movq %r15, (%rsp)
leaq -0x40(%rbp), %rdi
movq %r12, %rdx
callq 0x2209a
incq %rbx
cmpq %rbx, %r14
jne 0x20918
jmp 0x20979
movl $0x42, %eax
jmp 0x20972
movl $0x43, %eax
movl %eax, 0x128(%r15)
movq -0x98(%rbp), %rsi
testq %rsi, %rsi
je 0x209a1
movq -0x80(%rbp), %rdx
leaq -0x40(%rbp), %rdi
callq 0x2129d
movl %eax, 0x128(%r15)
testl %eax, %eax
jne 0x2062d
leaq -0x38(%rbp), %rsi
leaq 0x11208(%rip), %rcx     # 0x31bb4
leaq -0x40(%rbp), %rdi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x1d440
testl %eax, %eax
jne 0x2062d
movq -0x38(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x2062d