// File: /workspace/github_c_makefile_star2_O1/Joy-Division[P]tools-mgs/lodepng/lodepng.c
// Function: lodepng_encode
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;

  /*check input values validity*/
  if((state->info_png.color.colortype == LCT_PALETTE || state->encoder.force_palette)
      && (state->info_png.color.palettesize == 0 || state->info_png.color.palettesize > 256))
  {
    CERROR_RETURN_ERROR(state->error, 68); /*invalid palette size, it is only allowed to be 1-256*/
  }
  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(state->info_png.color.colortype, state->info_png.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*/

  /* color convert and compute scanline filter types */
  lodepng_info_init(&info);
  lodepng_info_copy(&info, &state->info_png);
  if(state->encoder.auto_convert)
  {
    state->error = lodepng_auto_choose_color(&info.color, image, w, h, &state->info_raw);
  }
  if (!state->error)
  {
    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);
  }

  /* output all PNG chunks */
  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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %r9, %rbx
movl %r8d, %ebp
movl %ecx, %r12d
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, %r15
xorl %eax, %eax
movq %rax, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movq %rax, (%rdi)
movq %rax, (%rsi)
movl $0x0, 0x198(%r9)
movl 0xc0(%r9), %edi
cmpl $0x3, %edi
je 0x7c65
cmpl $0x0, 0x78(%rbx)
je 0x7c8f
movq $-0x101, %rax           # imm = 0xFEFF
movq 0xd0(%rbx), %rcx
addq %rax, %rcx
cmpq %rax, %rcx
ja 0x7c8f
movl $0x44, 0x198(%rbx)
movl $0x44, %eax
jmp 0x7ef6
cmpl $0x3, 0x30(%rbx)
jb 0x7ca9
movl $0x3d, 0x198(%rbx)
movl $0x3d, %eax
jmp 0x7ef6
cmpl $0x2, 0xb8(%rbx)
jb 0x7cc6
movl $0x47, 0x198(%rbx)
movl $0x47, %eax
jmp 0x7ef6
movl 0xc4(%rbx), %esi
callq 0x6a14
movl %eax, 0x198(%rbx)
testl %eax, %eax
jne 0x7ef6
movl 0x88(%rbx), %edi
movl 0x8c(%rbx), %esi
callq 0x6a14
movl %eax, 0x198(%rbx)
testl %eax, %eax
jne 0x7ef6
leaq 0xb0(%rbx), %rsi
leaq 0x88(%rbx), %rax
movq %rax, 0x28(%rsp)
leaq 0x70(%rsp), %rcx
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rcx)
movabsq $0x800000006, %rax      # imm = 0x800000006
movq %rax, (%rcx)
movups %xmm0, 0x8(%rcx)
xorl %eax, %eax
movl %eax, -0x8(%rcx)
movq $0x0, -0x10(%rcx)
movl %eax, 0x94(%rcx)
movups %xmm0, 0x28(%rcx)
movups %xmm0, 0x38(%rcx)
movups %xmm0, 0x48(%rcx)
movups %xmm0, 0x58(%rcx)
movups %xmm0, 0x68(%rcx)
movl %eax, 0x78(%rcx)
movups %xmm0, 0xa8(%rcx)
movups %xmm0, 0xb8(%rcx)
movups %xmm0, 0xc8(%rcx)
leaq 0x60(%rsp), %rdi
callq 0x4bba
xorl %eax, %eax
cmpl %eax, 0x60(%rbx)
je 0x7d9b
leaq 0x70(%rsp), %rdi
movq %r13, %rsi
movl %r12d, %edx
movl %ebp, %ecx
movq 0x28(%rsp), %r8
callq 0x65b2
movl %eax, 0x198(%rbx)
leaq 0x30(%rbx), %rax
movq %rax, 0x38(%rsp)
cmpl $0x0, 0x198(%rbx)
jne 0x7eb2
movq 0x28(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x52c9
testl %eax, %eax
je 0x7def
movq 0x38(%rsp), %rax
movq %rax, (%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x60(%rsp), %r9
movq %r13, %rdx
movl %r12d, %ecx
movl %ebp, %r8d
callq 0x8270
jmp 0x7eb2
movl %ebp, %eax
imull %r12d, %eax
movl 0x70(%rsp), %edx
movl 0x74(%rsp), %ecx
xorl %esi, %esi
cmpq $0x6, %rdx
ja 0x7e0f
leaq 0x6c18(%rip), %rsi      # 0xea24
movl (%rsi,%rdx,4), %esi
imull %ecx, %esi
imulq %rax, %rsi
movq %rsi, 0x30(%rsp)
leaq 0x7(%rsi), %rdi
shrq $0x3, %rdi
callq 0x10f0
movq %rax, %rdx
testq %rax, %rax
setne %al
cmpq $0x0, 0x30(%rsp)
sete %cl
orb %al, %cl
jne 0x7e48
movl $0x53, 0x198(%rbx)
cmpl $0x0, 0x198(%rbx)
movq 0x28(%rsp), %rcx
jne 0x7e78
movq %rdx, %rdi
movq %r13, %rsi
movq %rdx, %r13
leaq 0x70(%rsp), %rdx
movl %r12d, %r8d
movl %ebp, %r9d
callq 0x4de5
movq %r13, %rdx
movl %eax, 0x198(%rbx)
cmpl $0x0, 0x198(%rbx)
jne 0x7eaa
movq 0x38(%rsp), %rax
movq %rax, (%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x60(%rsp), %r9
movq %rdx, %r13
movl %r12d, %ecx
movl %ebp, %r8d
callq 0x8270
movq %r13, %rdx
movq %rdx, %rdi
callq 0x1030
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
cmpl $0x0, 0x198(%rbx)
je 0x7f08
leaq 0x60(%rsp), %rdi
callq 0x4b69
movq 0x40(%rsp), %rdi
callq 0x1030
movq 0x10(%rsp), %rax
movq %rax, (%r15)
movq 0x18(%rsp), %rax
movq %rax, (%r14)
movl 0x198(%rbx), %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10(%rsp), %r13
movq %r13, %rdi
callq 0x87d0
movl 0x68(%rsp), %r9d
movl 0x70(%rsp), %eax
movl 0x74(%rsp), %r8d
movq %r13, %rdi
movl %r12d, %esi
movl %ebp, %edx
movl %eax, %ebp
movl %eax, %ecx
callq 0x88f4
movq 0x118(%rsp), %rsi
testq %rsi, %rsi
je 0x7f61
movq 0x130(%rsp), %rdx
leaq 0x10(%rsp), %rdi
callq 0x8b07
movl %eax, 0x198(%rbx)
testl %eax, %eax
jne 0x7ecc
cmpl $0x3, %ebp
leaq 0x70(%rsp), %r12
jne 0x7f78
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x8b73
movl %ebp, %eax
andl $-0x5, %eax
cmpl $0x2, %eax
jne 0x7f95
cmpl $0x0, 0x78(%rbx)
je 0x7f95
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x8b73
cmpl $0x3, %ebp
jne 0x7fbd
movq 0x78(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x8c73
testl %eax, %eax
je 0x7fbd
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x8d0f
testl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
jne 0x7fdc
cmpl $0x0, 0x88(%rsp)
je 0x7fdc
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x8d0f
cmpl $0x0, 0x98(%rsp)
je 0x7ff5
leaq 0x10(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x9096
cmpl $0x0, 0x104(%rsp)
je 0x800e
leaq 0x10(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x934e
movq 0x120(%rsp), %rsi
testq %rsi, %rsi
je 0x803b
movq 0x138(%rsp), %rdx
leaq 0x10(%rsp), %rdi
callq 0x8b07
movl %eax, 0x198(%rbx)
testl %eax, %eax
jne 0x7ecc
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
leaq 0x10(%rsp), %rdi
movq 0x38(%rsp), %rcx
callq 0x9429
movl %eax, 0x198(%rbx)
testl %eax, %eax
jne 0x7ecc
cmpl $0x0, 0xe8(%rsp)
je 0x807e
leaq 0xec(%rsp), %rsi
leaq 0x10(%rsp), %rdi
callq 0x94b4
movq 0xa8(%rsp), %rbp
testq %rbp, %rbp
je 0x810f
movq 0xb0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x30(%rsp)
xorl %r12d, %r12d
movq 0x28(%rsp), %rax
movq (%rax,%r12,8), %r13
movq %r13, %rdi
callq 0x1090
cmpq $0x4f, %rax
ja 0x80fd
testq %rax, %rax
je 0x8104
cmpl $0x0, 0x80(%rbx)
movq 0x30(%rsp), %rax
movq (%rax,%r12,8), %rdx
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
je 0x80ee
movq 0x38(%rsp), %rcx
callq 0x953f
jmp 0x80f3
callq 0x97ec
incq %r12
cmpq %r12, %rbp
jne 0x80ac
jmp 0x810f
movl $0x42, %eax
jmp 0x8109
movl $0x43, %eax
movl %eax, 0x198(%rbx)
cmpl $0x0, 0x7c(%rbx)
je 0x815a
testq %rbp, %rbp
je 0x8142
movq 0xb0(%rsp), %r13
xorl %r12d, %r12d
movq (%r13,%r12,8), %rdi
leaq 0x6948(%rip), %rsi      # 0xea79
callq 0x10c0
testl %eax, %eax
je 0x815a
incq %r12
cmpq %r12, %rbp
jne 0x8125
movq 0xbf77(%rip), %rdx      # 0x140c0
leaq 0x6929(%rip), %rsi      # 0xea79
leaq 0x10(%rsp), %rdi
callq 0x97ec
movq 0xc0(%rsp), %rbp
testq %rbp, %rbp
je 0x8211
movq 0xc8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x50(%rsp)
xorl %r12d, %r12d
movq 0x28(%rsp), %rax
movq (%rax,%r12,8), %r13
movq %r13, %rdi
callq 0x1090
cmpq $0x4f, %rax
ja 0x81ff
testq %rax, %rax
je 0x8206
movl 0x80(%rbx), %esi
movq 0x30(%rsp), %rax
movq (%rax,%r12,8), %rcx
movq 0x58(%rsp), %rax
movq (%rax,%r12,8), %r8
movq 0x50(%rsp), %rax
movq (%rax,%r12,8), %r9
movq 0x38(%rsp), %rax
movq %rax, (%rsp)
leaq 0x10(%rsp), %rdi
movq %r13, %rdx
callq 0x99c4
incq %r12
cmpq %r12, %rbp
jne 0x81a2
jmp 0x8211
movl $0x42, %eax
jmp 0x820b
movl $0x43, %eax
movl %eax, 0x198(%rbx)
movq 0x128(%rsp), %rsi
testq %rsi, %rsi
je 0x823e
movq 0x140(%rsp), %rdx
leaq 0x10(%rsp), %rdi
callq 0x8b07
movl %eax, 0x198(%rbx)
testl %eax, %eax
jne 0x7ecc
leaq 0x18(%rsp), %rsi
leaq 0x771a(%rip), %rcx      # 0xf964
leaq 0x10(%rsp), %rdi
xorl %edx, %edx
xorl %r8d, %r8d
callq 0x4357
testl %eax, %eax
jne 0x7ecc
movq 0x18(%rsp), %rax
movq %rax, 0x20(%rsp)
jmp 0x7ecc