// File: /workspace/github_cpp_makefile_star2_O1/cloudinary[P]fuif/./import/read_png.h
// Function: read_PNG_file(char const*)
Image read_PNG_file(const char *input_image) {

  FILE *fp = fopen(input_image,"rb");
  if (!fp) return Image();

  png_byte header[8];
  int rr = fread(header,1,8,fp);
  int is_png = !png_sig_cmp(header,0,rr);
  if (!is_png) {
    fclose(fp);
    return Image();
  }
  png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,(png_voidp) NULL,NULL,NULL);

  if (!png_ptr) {
    fclose(fp);
    return Image();
  }
  png_infop info_ptr = png_create_info_struct(png_ptr);
  if (!info_ptr) {
    png_destroy_read_struct(&png_ptr,(png_infopp) NULL,(png_infopp) NULL);
    fclose(fp);
    return Image();
  }
  if (setjmp(png_jmpbuf(png_ptr))) {
       png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
       fclose(fp);
       return Image();
  }
  png_init_io(png_ptr,fp);
  png_set_sig_bytes(png_ptr,8);


//  int png_flags = PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_GRAY_TO_RGB;
//  png_read_png(png_ptr,info_ptr, png_flags, NULL);
  png_read_info(png_ptr,info_ptr);

  size_t width = png_get_image_width(png_ptr,info_ptr);
  size_t height = png_get_image_height(png_ptr,info_ptr);
  int bit_depth = png_get_bit_depth(png_ptr,info_ptr);
  int color_type = png_get_color_type(png_ptr,info_ptr);

  unsigned int nbplanes;

  if (color_type == PNG_COLOR_TYPE_PALETTE)
        png_set_palette_to_rgb(png_ptr);

  if (png_get_valid(png_ptr, info_ptr,
        PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png_ptr);

  if (color_type == PNG_COLOR_TYPE_GRAY &&
        bit_depth < 8) png_set_expand_gray_1_2_4_to_8(png_ptr);
/*
  if (bit_depth == 16)
#if PNG_LIBPNG_VER >= 10504
       png_set_scale_16(png_ptr);
#else
       png_set_strip_16(png_ptr);
#endif
*/
  if (bit_depth < 8)
       png_set_packing(png_ptr);
//  if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
//       png_set_gray_to_rgb(png_ptr);
  png_read_update_info(png_ptr, info_ptr);
  color_type = png_get_color_type(png_ptr,info_ptr);


  if (color_type == PNG_COLOR_TYPE_GRAY) nbplanes=1;
  else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) nbplanes=2;
  else if (color_type == PNG_COLOR_TYPE_RGB) nbplanes=3;
  else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) nbplanes=4;
  else { png_destroy_read_struct(&png_ptr,(png_infopp) NULL,(png_infopp) NULL); fclose(fp); return Image(); }

  int w = width, h = height, maxval = (bit_depth > 8 ? 65535 : 255);

  int bytes_per_pixel = (bit_depth > 8 ? 2 : 1);
  Image image(w,h,maxval,nbplanes);

  std::vector<unsigned char> one_row(w*nbplanes*bytes_per_pixel,0);
  png_bytep rowptr = &(one_row[0]);
  if (bit_depth <= 8) {
    int y=0;
    while (y < h) {
      png_read_row(png_ptr, rowptr, NULL);
      for(int x=0; x < w ; x++) {
       for (int k=0; k<nbplanes; k++) {
        image.channel[k].data[x+y*w] = one_row[x*nbplanes+k];
       }
      }
      y++;
    }
  } else {
    int y=0;
    while (y < h) {
      png_read_row(png_ptr, rowptr, NULL);
      int index=0;
      for(int x=0; x < w ; x++) {
       for (int k=0; k<nbplanes; k++) {
        image.channel[k].data[x+y*w] = (one_row[index++] << 8);
        image.channel[k].data[x+y*w] += one_row[index++];
       }
      }
      y++;
    }
  }
  png_read_end(png_ptr, info_ptr);
  png_destroy_read_struct(&png_ptr,&info_ptr,(png_infopp) NULL);
  fclose(fp);
  return image;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %rax
movq %rdi, %rbx
leaq 0x86f0(%rip), %rsi      # 0x26a71
movq %rax, %rdi
callq 0x3370
testq %rax, %rax
je 0x1e3d6
movq %rax, %r14
leaq 0x60(%rsp), %r15
movl $0x1, %esi
movl $0x8, %edx
movq %r15, %rdi
movq %rax, %rcx
callq 0x3450
movslq %eax, %rdx
xorl %eax, %eax
movl %eax, %esi
movq %r15, %rdi
callq 0x31f0
testl %eax, %eax
je 0x1e42f
movq %r14, %rdi
callq 0x32a0
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movups %xmm0, 0x10(%rbx)
movups %xmm0, 0x20(%rbx)
jmp 0x1e3e4
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rbx)
movups %xmm0, 0x10(%rbx)
movups %xmm0, (%rbx)
xorl %eax, %eax
movq %rax, 0x30(%rbx)
movabsq $0xa00000001, %rcx      # imm = 0xA00000001
movq %rcx, 0x38(%rbx)
movups %xmm0, 0x40(%rbx)
movups %xmm0, 0x50(%rbx)
movl $0xff, 0x60(%rbx)
movups %xmm0, 0x78(%rbx)
movq %rax, 0x88(%rbx)
movups %xmm0, 0x64(%rbx)
movb $0x1, 0xa8(%rbx)
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8d2a(%rip), %rdi      # 0x27160
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x3070
movq %rax, (%rsp)
testq %rax, %rax
je 0x1e3be
movq %rax, %rdi
callq 0x3590
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x1e6b6
movq (%rsp), %rdi
movq 0x11b3d(%rip), %rsi     # 0x2ffb0
movl $0xc8, %edx
callq 0x3300
movq %rax, %rdi
callq 0x3430
testl %eax, %eax
je 0x1e49b
xorl %eax, %eax
movl %eax, %r15d
movq %rsp, %rdi
leaq 0x8(%rsp), %rsi
jmp 0x1e6c1
movq (%rsp), %rdi
movq %r14, %rsi
callq 0x35d0
movq (%rsp), %rdi
movl $0x8, %esi
callq 0x30c0
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x3250
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x31e0
movl %eax, 0x14(%rsp)
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x3600
movl %eax, %r12d
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x3050
movl %eax, %r13d
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x3410
movl %eax, %r15d
cmpb $0x3, %al
jne 0x1e515
movq (%rsp), %rdi
callq 0x3490
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
movl $0x10, %edx
callq 0x34f0
testl %eax, %eax
je 0x1e535
movq (%rsp), %rdi
callq 0x3110
testb %r15b, %r15b
sete %al
cmpb $0x8, %r13b
setb %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x1e552
movq (%rsp), %rdi
callq 0x31b0
cmpb $0x7, %r13b
ja 0x1e561
movq (%rsp), %rdi
callq 0x3190
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x3060
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x3410
cmpb $0x7, %al
jae 0x1e6b6
movzbl %al, %eax
movl $0x55, %ecx
btl %eax, %ecx
jae 0x1e6b6
movl 0x14(%rsp), %ebp
movl %ebp, %esi
movl %r12d, 0x1c(%rsp)
movl %r12d, %edx
leaq 0x8b65(%rip), %rcx      # 0x27110
movl (%rcx,%rax,4), %r15d
cmpb $0x9, %r13b
setae %r12b
movl $0xffff, %eax           # imm = 0xFFFF
movl $0xff, %ecx
cmovael %eax, %ecx
movq %rbx, %rdi
movl %r15d, %r8d
xorl %r9d, %r9d
callq 0x15138
movl %r15d, %eax
imull %ebp, %eax
movl %r12d, %ecx
shll %cl, %eax
movl %eax, %esi
leaq 0x1b(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x28(%rsp), %rdi
leaq 0x1a(%rsp), %rcx
callq 0x23f7a
movq %r14, 0x58(%rsp)
movl %r15d, %r12d
movq 0x28(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpb $0x9, %r13b
jae 0x1e710
movl 0x1c(%rsp), %ecx
testl %ecx, %ecx
jle 0x1e7bf
movq %r12, 0x48(%rsp)
movl 0x14(%rsp), %eax
movl %eax, %ebp
movl %ecx, %eax
movq %rax, 0x40(%rsp)
xorl %eax, %eax
movl %eax, %r15d
movq (%rsp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq 0x20(%rsp), %rsi
callq 0x3460
cmpl $0x0, 0x14(%rsp)
jle 0x1e6a1
movq 0x28(%rsp), %rax
movq (%rbx), %rcx
movq %r15, %rdx
imulq %rbp, %rdx
xorl %esi, %esi
movl %esi, %edi
leaq (%rdi,%rdi), %r8
movq 0x48(%rsp), %r10
movl %esi, %r14d
movq %rcx, %r11
movl %r14d, %r9d
movzbl (%rax,%r9), %r9d
movq (%r11), %r13
addq %r8, %r13
movw %r9w, (%r13,%rdx,2)
addq $0x40, %r11
incl %r14d
decq %r10
jne 0x1e670
movq %rdi, %r9
incq %r9
addl %r12d, %esi
movq %r9, %rdi
cmpq %rbp, %r9
jne 0x1e661
movq %r15, %rax
incq %rax
movq %rax, %r15
cmpq 0x40(%rsp), %rax
jne 0x1e635
jmp 0x1e7bf
xorl %eax, %eax
movl %eax, %r15d
movq %rsp, %rdi
movq %r15, %rsi
movq %r15, %rdx
callq 0x31a0
movq %r14, %rdi
callq 0x32a0
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movups %xmm0, 0x10(%rbx)
movups %xmm0, 0x20(%rbx)
movq %r15, 0x30(%rbx)
movabsq $0xa00000001, %rax      # imm = 0xA00000001
movq %rax, 0x38(%rbx)
movups %xmm0, 0x40(%rbx)
movups %xmm0, 0x50(%rbx)
movl $0xff, 0x60(%rbx)
movups %xmm0, 0x78(%rbx)
movq %r15, 0x88(%rbx)
jmp 0x1e412
movl 0x1c(%rsp), %ecx
testl %ecx, %ecx
jle 0x1e7bf
movl 0x14(%rsp), %eax
movl %eax, %r13d
movl %ecx, %eax
movq %rax, 0x50(%rsp)
xorl %eax, %eax
movl %eax, %ebp
movq (%rsp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq 0x20(%rsp), %rsi
callq 0x3460
cmpl $0x0, 0x14(%rsp)
jle 0x1e7ab
movq 0x28(%rsp), %rax
movq (%rbx), %rcx
movq %rbp, %rdx
imulq %r13, %rdx
xorl %esi, %esi
movq %rsi, %r8
movslq %r8d, %r11
leaq (%rsi,%rsi), %rdi
movq %r12, %r10
movq %rcx, %r9
movq %r11, %r8
movzbl (%rax,%r11), %r11d
shll $0x8, %r11d
movq (%r9), %r15
addq %rdi, %r15
movw %r11w, (%r15,%rdx,2)
movzbl 0x1(%rax,%r8), %r14d
addq $0x2, %r8
orl %r11d, %r14d
movw %r14w, (%r15,%rdx,2)
addq $0x40, %r9
movq %r8, %r11
decq %r10
jne 0x1e768
movq %rsi, %rdi
incq %rdi
movq %rdi, %rsi
cmpq %r13, %rdi
jne 0x1e75b
movq %rbp, %rax
incq %rax
movq %rax, %rbp
cmpq 0x50(%rsp), %rax
jne 0x1e72e
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x3280
xorl %eax, %eax
movl %eax, %edx
movq %rsp, %rdi
leaq 0x8(%rsp), %rsi
callq 0x31a0
movq 0x58(%rsp), %rdi
callq 0x32a0
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x1e41d
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
jmp 0x1e41d
movq %rax, %r14
jmp 0x1e82b
jmp 0x1e811
jmp 0x1e811
movq %rax, %r14
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x1e82b
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x32c0
movq %rbx, %rdi
callq 0xded8
movq %r14, %rdi
callq 0x35a0