// File: /workspace/github_cpp_makefile_star2_O3/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 0x4260
movl %r13d, (%r14)
movl %r12d, 0x4(%r14)
cmpl $0x0, 0xc(%r15)
je 0x1f1c9
movl 0x100(%r15), %ecx
cmpl $0x3, %ecx
je 0x1f134
movl %r12d, %eax
imull %r13d, %eax
cmpl $0x4, %ecx
je 0x1f100
cmpl $0x6, %ecx
jne 0x1f1c9
cmpl $0x8, 0x104(%r15)
jne 0x1f183
testl %eax, %eax
je 0x1f1aa
movl %eax, %eax
shlq $0x2, %rax
xorl %ecx, %ecx
movl %ecx, %edx
andl $-0x4, %edx
cmpb $-0x1, 0x3(%rbx,%rdx)
jne 0x1f1c9
addq $0x4, %rcx
cmpq %rcx, %rax
jne 0x1f0e2
jmp 0x1f1aa
cmpl $0x8, 0x104(%r15)
jne 0x1f15a
testl %eax, %eax
je 0x1f1aa
movl %eax, %eax
addq %rax, %rax
xorl %ecx, %ecx
movl %ecx, %edx
andl $-0x2, %edx
cmpb $-0x1, 0x1(%rbx,%rdx)
jne 0x1f1c9
addq $0x2, %rcx
cmpq %rcx, %rax
jne 0x1f119
jmp 0x1f1aa
movq 0x110(%r15), %rax
testq %rax, %rax
je 0x1f1aa
movq 0x108(%r15), %rcx
xorl %edx, %edx
cmpb $-0x1, 0x3(%rcx,%rdx,4)
jne 0x1f1c9
incq %rdx
cmpq %rdx, %rax
jne 0x1f149
jmp 0x1f1aa
testl %eax, %eax
je 0x1f1aa
movl %eax, %eax
shlq $0x2, %rax
xorl %ecx, %ecx
leal 0x2(%rcx), %edx
cmpb $-0x1, (%rbx,%rdx)
jne 0x1f1c9
leal 0x3(%rcx), %edx
cmpb $-0x1, (%rbx,%rdx)
jne 0x1f1c9
addq $0x4, %rcx
cmpq %rcx, %rax
jne 0x1f166
jmp 0x1f1aa
testl %eax, %eax
je 0x1f1aa
movl %eax, %eax
shlq $0x3, %rax
xorl %ecx, %ecx
leal 0x6(%rcx), %edx
cmpb $-0x1, (%rbx,%rdx)
jne 0x1f1c9
leal 0x7(%rcx), %edx
cmpb $-0x1, (%rbx,%rdx)
jne 0x1f1c9
addq $0x8, %rcx
cmpq %rcx, %rax
jne 0x1f18f
movl -0x140(%rbp), %eax
cmpl $0x6, %eax
je 0x1f1be
cmpl $0x4, %eax
jne 0x1f1c9
xorl %eax, %eax
jmp 0x1f1c3
movl $0x2, %eax
movl %eax, -0x140(%rbp)
cmpl $0x8001, 0x8(%r15)      # imm = 0x8001
jb 0x1f1e3
movl $0x3c, 0x128(%r15)
jmp 0x1f399
cmpl $0x3, (%r15)
jb 0x1f1f9
movl $0x3d, 0x128(%r15)
jmp 0x1f399
cmpl $0x2, 0x30(%r15)
jb 0x1f210
movl $0x47, 0x128(%r15)
jmp 0x1f399
movl -0x140(%rbp), %edi
movl -0x13c(%rbp), %esi
movl %edi, -0x30(%rbp)
movl %esi, -0x38(%rbp)
callq 0x1d9bd
movl %eax, 0x128(%r15)
testl %eax, %eax
jne 0x1f399
movl 0x100(%r15), %edi
movl 0x104(%r15), %r14d
movl %edi, -0x60(%rbp)
movl %r14d, %esi
callq 0x1d9bd
movl %eax, 0x128(%r15)
testl %eax, %eax
jne 0x1f399
movl -0x30(%rbp), %edx
movl -0x60(%rbp), %eax
xorl %edx, %eax
xorl -0x38(%rbp), %r14d
orl %eax, %r14d
jne 0x1f28b
leaq -0x58(%rbp), %rdi
leaq -0x68(%rbp), %rsi
leaq -0x158(%rbp), %rcx
movq %rbx, %rdx
callq 0x1f71b
jmp 0x1f361
cmpl $0x6, %edx
ja 0x1f2a9
movl %edx, %eax
leaq 0x115b7(%rip), %rcx     # 0x30850
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x1, %eax
jmp 0x1f2b9
xorl %eax, %eax
jmp 0x1f2b9
movl $0x3, %eax
jmp 0x1f2b9
movl $0x4, %eax
movl %edx, %ecx
andl $-0x5, %ecx
xorl $0x2, %ecx
movl -0x38(%rbp), %edx
xorl $0x8, %edx
orl %ecx, %edx
jne 0x1f305
movl %r12d, %r14d
imull %r13d, %r14d
imull %eax, %r14d
andl $0x1fffffff, %r14d      # imm = 0x1FFFFFFF
movq %r14, %rdi
callq 0x4410
movq %rax, %rdi
testq %rax, %rax
setne %al
testl %r14d, %r14d
sete %cl
orb %al, %cl
jne 0x1f315
movl $0x26e3, 0x128(%r15) # imm = 0x26E3
jmp 0x1f35c
movl $0x3b, 0x128(%r15)
jmp 0x1f399
leaq 0x100(%r15), %rcx
movq %rbx, %rsi
movq %rdi, %rbx
leaq -0x140(%rbp), %rdx
movl %r13d, %r8d
movl %r12d, %r9d
callq 0x1cf0b
movq %rbx, %rdi
movl %eax, 0x128(%r15)
testl %eax, %eax
jne 0x1f35c
leaq -0x58(%rbp), %rdi
leaq -0x68(%rbp), %rsi
leaq -0x158(%rbp), %rcx
movq %rbx, %rdx
callq 0x1f71b
movq %rbx, %rdi
callq 0x4380
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
cmpl $0x0, 0x128(%r15)
je 0x1f3ab
movq -0x58(%rbp), %rdi
callq 0x4380
movq -0x50(%rbp), %rax
movq -0x70(%rbp), %rcx
movq %rax, (%rcx)
movq -0x48(%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 -0x50(%rbp), %rbx
movq %rbx, %rdi
callq 0x1fcde
movl -0x148(%rbp), %r9d
movq %rbx, %rdi
movl %r13d, %esi
movl %r12d, %edx
movl -0x38(%rbp), %ecx
movl -0x30(%rbp), %r8d
callq 0x1fde3
movq -0xa8(%rbp), %rsi
testq %rsi, %rsi
je 0x1f3fa
movq -0x90(%rbp), %rdx
leaq -0x50(%rbp), %rdi
callq 0x20016
movl %eax, 0x128(%r15)
testl %eax, %eax
jne 0x1f37a
movl -0x30(%rbp), %esi
cmpl $0x3, %esi
jne 0x1f455
movq $-0x101, %rax           # imm = 0xFEFF
addq -0x130(%rbp), %rax
cmpq $-0x100, %rax
jb 0x1f47c
leaq -0x50(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x2007e
movq -0x130(%rbp), %rax
testq %rax, %rax
je 0x1f4d2
movq -0x138(%rbp), %rcx
xorl %edx, %edx
movl -0x30(%rbp), %esi
cmpb $-0x1, 0x3(%rcx,%rdx,4)
jne 0x1f49e
incq %rdx
cmpq %rdx, %rax
jne 0x1f444
jmp 0x1f4b1
movl %esi, %eax
andl $-0x5, %eax
cmpl $0x2, %eax
jne 0x1f4b1
cmpl $0x0, 0x10(%r15)
je 0x1f4b1
movq $-0x101, %rax           # imm = 0xFEFF
addq -0x130(%rbp), %rax
cmpq $-0x100, %rax
jae 0x1f48c
movl $0x44, 0x128(%r15)
jmp 0x1f37a
leaq -0x50(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x2007e
jmp 0x1f4ae
leaq -0x50(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x2016f
movl -0x30(%rbp), %esi
testl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
jne 0x1f4d2
cmpl $0x0, -0x128(%rbp)
je 0x1f4d2
leaq -0x50(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x2016f
cmpl $0x0, -0x118(%rbp)
je 0x1f4eb
leaq -0x50(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x204ba
cmpl $0x0, -0xb8(%rbp)
je 0x1f504
leaq -0x50(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x20788
movq -0xa0(%rbp), %rsi
testq %rsi, %rsi
je 0x1f52f
movq -0x88(%rbp), %rdx
leaq -0x50(%rbp), %rdi
callq 0x20016
movl %eax, 0x128(%r15)
testl %eax, %eax
jne 0x1f37a
movq -0x58(%rbp), %rsi
movq -0x68(%rbp), %rdx
leaq -0x50(%rbp), %rdi
movq %r15, %rcx
callq 0x2088d
movl %eax, 0x128(%r15)
testl %eax, %eax
jne 0x1f37a
cmpb $0x0, -0xc8(%rbp)
je 0x1f56b
leaq -0xc4(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x2090f
movq -0x108(%rbp), %r14
testq %r14, %r14
je 0x1f5d2
movq -0x100(%rbp), %r13
movq -0xf8(%rbp), %rax
movq %rax, -0x38(%rbp)
xorl %ebx, %ebx
movq (%r13,%rbx,8), %r12
movq %r12, %rdi
callq 0x4120
cmpq $0x4f, %rax
ja 0x1f5db
testq %rax, %rax
je 0x1f5e2
cmpl $0x0, 0x18(%r15)
movq -0x38(%rbp), %rax
movq (%rax,%rbx,8), %rdx
leaq -0x50(%rbp), %rdi
movq %r12, %rsi
je 0x1f5c3
movq %r15, %rcx
callq 0x209a9
jmp 0x1f5c8
callq 0x20bfd
incq %rbx
cmpq %rbx, %r14
jne 0x1f58b
jmp 0x1f5ee
cmpl $0x0, 0x14(%r15)
jne 0x1f623
jmp 0x1f63a
movl $0x42, %eax
jmp 0x1f5e7
movl $0x43, %eax
movl %eax, 0x128(%r15)
cmpl $0x0, 0x14(%r15)
je 0x1f63a
testq %r14, %r14
je 0x1f623
movq -0x100(%rbp), %r12
xorl %r13d, %r13d
leaq 0x1159d(%rip), %rbx     # 0x30ba8
movq (%r12,%r13,8), %rdi
movq %rbx, %rsi
callq 0x4430
testl %eax, %eax
je 0x1f63a
incq %r13
cmpq %r13, %r14
jne 0x1f60b
leaq 0x1157e(%rip), %rsi     # 0x30ba8
leaq 0x1157f(%rip), %rdx     # 0x30bb0
leaq -0x50(%rbp), %rdi
callq 0x20bfd
movq -0xf0(%rbp), %r14
testq %r14, %r14
je 0x1f6d5
movq -0xe8(%rbp), %r13
movq -0xe0(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x60(%rbp)
xorl %ebx, %ebx
movq (%r13,%rbx,8), %r12
movq %r12, %rdi
callq 0x4120
cmpq $0x4f, %rax
ja 0x1f6c2
testq %rax, %rax
je 0x1f6c9
movl 0x18(%r15), %esi
movq -0x38(%rbp), %rax
movq (%rax,%rbx,8), %rcx
movq -0x30(%rbp), %rax
movq (%rax,%rbx,8), %r8
movq -0x60(%rbp), %rax
movq (%rax,%rbx,8), %r9
movq %r15, (%rsp)
leaq -0x50(%rbp), %rdi
movq %r12, %rdx
callq 0x20d8e
incq %rbx
cmpq %rbx, %r14
jne 0x1f674
jmp 0x1f6d5
movl $0x42, %eax
jmp 0x1f6ce
movl $0x43, %eax
movl %eax, 0x128(%r15)
movq -0x98(%rbp), %rsi
testq %rsi, %rsi
je 0x1f6fd
movq -0x80(%rbp), %rdx
leaq -0x50(%rbp), %rdi
callq 0x20016
movl %eax, 0x128(%r15)
testl %eax, %eax
jne 0x1f37a
leaq -0x48(%rbp), %rsi
leaq 0x114bc(%rip), %rcx     # 0x30bc4
leaq -0x50(%rbp), %rdi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x1c3c3
jmp 0x1f37a