// File: 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:
subq $0xf8, %rsp
movq %rdi, 0x28(%rsp)
movq %rdi, %rax
movq %rax, 0x30(%rsp)
movq %rdi, 0xf0(%rsp)
movq %rsi, 0xe8(%rsp)
movq 0xe8(%rsp), %rdi
leaq 0xc00a(%rip), %rsi      # 0x4796d
callq 0x3460
movq %rax, 0xe0(%rsp)
cmpq $0x0, 0xe0(%rsp)
jne 0x3b98a
movq 0x28(%rsp), %rdi
callq 0x1f570
jmp 0x3c0dc
leaq 0xd8(%rsp), %rdi
movq 0xe0(%rsp), %rcx
movl $0x1, %esi
movl $0x8, %edx
callq 0x3550
movl %eax, 0xd4(%rsp)
leaq 0xd8(%rsp), %rdi
movslq 0xd4(%rsp), %rdx
xorl %eax, %eax
movl %eax, %esi
callq 0x3270
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0xd0(%rsp)
cmpl $0x0, 0xd0(%rsp)
jne 0x3ba03
movq 0xe0(%rsp), %rdi
callq 0x3350
movq 0x28(%rsp), %rdi
callq 0x1f570
jmp 0x3c0dc
leaq 0xc64a(%rip), %rdi      # 0x48054
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x3080
movq %rax, 0xc8(%rsp)
cmpq $0x0, 0xc8(%rsp)
jne 0x3ba48
movq 0xe0(%rsp), %rdi
callq 0x3350
movq 0x28(%rsp), %rdi
callq 0x1f570
jmp 0x3c0dc
movq 0xc8(%rsp), %rdi
callq 0x3690
movq %rax, 0xc0(%rsp)
cmpq $0x0, 0xc0(%rsp)
jne 0x3ba98
leaq 0xc8(%rsp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x3210
movq 0xe0(%rsp), %rdi
callq 0x3350
movq 0x28(%rsp), %rdi
callq 0x1f570
jmp 0x3c0dc
movq 0xc8(%rsp), %rdi
movq 0x1e509(%rip), %rsi     # 0x59fb0
movl $0xc8, %edx
callq 0x33d0
movq %rax, %rdi
callq 0x3530
cmpl $0x0, %eax
je 0x3baf3
leaq 0xc8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x3210
movq 0xe0(%rsp), %rdi
callq 0x3350
movq 0x28(%rsp), %rdi
callq 0x1f570
jmp 0x3c0dc
movq 0xc8(%rsp), %rdi
movq 0xe0(%rsp), %rsi
callq 0x36e0
movq 0xc8(%rsp), %rdi
movl $0x8, %esi
callq 0x30f0
movq 0xc8(%rsp), %rdi
movq 0xc0(%rsp), %rsi
callq 0x32f0
movq 0xc8(%rsp), %rdi
movq 0xc0(%rsp), %rsi
callq 0x3260
movl %eax, %eax
movq %rax, 0xb8(%rsp)
movq 0xc8(%rsp), %rdi
movq 0xc0(%rsp), %rsi
callq 0x3710
movl %eax, %eax
movq %rax, 0xb0(%rsp)
movq 0xc8(%rsp), %rdi
movq 0xc0(%rsp), %rsi
callq 0x3050
movzbl %al, %eax
movl %eax, 0xac(%rsp)
movq 0xc8(%rsp), %rdi
movq 0xc0(%rsp), %rsi
callq 0x3510
movzbl %al, %eax
movl %eax, 0xa8(%rsp)
cmpl $0x3, 0xa8(%rsp)
jne 0x3bbc2
movq 0xc8(%rsp), %rdi
callq 0x3590
movq 0xc8(%rsp), %rdi
movq 0xc0(%rsp), %rsi
movl $0x10, %edx
callq 0x35f0
cmpl $0x0, %eax
je 0x3bbee
movq 0xc8(%rsp), %rdi
callq 0x3140
cmpl $0x0, 0xa8(%rsp)
jne 0x3bc0f
cmpl $0x8, 0xac(%rsp)
jge 0x3bc0f
movq 0xc8(%rsp), %rdi
callq 0x3220
cmpl $0x8, 0xac(%rsp)
jge 0x3bc26
movq 0xc8(%rsp), %rdi
callq 0x3200
movq 0xc8(%rsp), %rdi
movq 0xc0(%rsp), %rsi
callq 0x3060
movq 0xc8(%rsp), %rdi
movq 0xc0(%rsp), %rsi
callq 0x3510
movzbl %al, %eax
movl %eax, 0xa8(%rsp)
cmpl $0x0, 0xa8(%rsp)
jne 0x3bc71
movl $0x1, 0xa4(%rsp)
jmp 0x3bcec
cmpl $0x4, 0xa8(%rsp)
jne 0x3bc88
movl $0x2, 0xa4(%rsp)
jmp 0x3bcea
cmpl $0x2, 0xa8(%rsp)
jne 0x3bc9f
movl $0x3, 0xa4(%rsp)
jmp 0x3bce8
cmpl $0x6, 0xa8(%rsp)
jne 0x3bcb6
movl $0x4, 0xa4(%rsp)
jmp 0x3bce6
leaq 0xc8(%rsp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x3210
movq 0xe0(%rsp), %rdi
callq 0x3350
movq 0x28(%rsp), %rdi
callq 0x1f570
jmp 0x3c0dc
jmp 0x3bce8
jmp 0x3bcea
jmp 0x3bcec
movq 0x28(%rsp), %rdi
movl 0xb8(%rsp), %eax
movl %eax, 0xa0(%rsp)
movl 0xb0(%rsp), %eax
movl %eax, 0x9c(%rsp)
movl 0xac(%rsp), %eax
subl $0x9, %eax
movl $0xffff, %ecx           # imm = 0xFFFF
movl $0xff, %eax
cmovgel %ecx, %eax
movl %eax, 0x98(%rsp)
movl 0xac(%rsp), %eax
subl $0x9, %eax
setge %al
movzbl %al, %eax
incl %eax
movl %eax, 0x94(%rsp)
movb $0x0, 0x93(%rsp)
movl 0xa0(%rsp), %esi
movl 0x9c(%rsp), %edx
movl 0x98(%rsp), %ecx
movl 0xa4(%rsp), %r8d
xorl %r9d, %r9d
callq 0x25430
movl 0xa0(%rsp), %eax
movl 0xa4(%rsp), %ecx
imull %ecx, %eax
movl 0x94(%rsp), %ecx
imull %ecx, %eax
movq %rax, 0x18(%rsp)
movb $0x0, 0x77(%rsp)
leaq 0x76(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x29fa0
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rcx
leaq 0x78(%rsp), %rdi
leaq 0x77(%rsp), %rdx
callq 0x452b0
jmp 0x3bdc0
leaq 0x76(%rsp), %rdi
callq 0x15780
leaq 0x78(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x45330
movq %rax, 0x58(%rsp)
cmpl $0x8, 0xac(%rsp)
jg 0x3bf16
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
cmpl 0x9c(%rsp), %eax
jge 0x3bf11
movq 0xc8(%rsp), %rdi
movq 0x58(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x3560
jmp 0x3be1c
movl $0x0, 0x50(%rsp)
movl 0x50(%rsp), %eax
cmpl 0xa0(%rsp), %eax
jge 0x3bf01
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
cmpl 0xa4(%rsp), %eax
jae 0x3beef
movl 0x50(%rsp), %eax
imull 0xa4(%rsp), %eax
addl 0x4c(%rsp), %eax
movl %eax, %eax
movl %eax, %esi
leaq 0x78(%rsp), %rdi
callq 0x45330
movq 0x28(%rsp), %rdi
movzbl (%rax), %eax
movw %ax, 0x16(%rsp)
movslq 0x4c(%rsp), %rsi
callq 0x4ba0
movq %rax, %rdi
movl 0x50(%rsp), %eax
movl 0x54(%rsp), %ecx
imull 0xa0(%rsp), %ecx
addl %ecx, %eax
movslq %eax, %rsi
callq 0x4bc0
movw 0x16(%rsp), %cx
movw %cx, (%rax)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x3be3d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x68(%rsp)
movl %eax, 0x64(%rsp)
leaq 0x76(%rsp), %rdi
callq 0x15780
jmp 0x3c0d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x68(%rsp)
movl %eax, 0x64(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x16e60
jmp 0x3c0d0
jmp 0x3bef1
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x3be24
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x3bdf3
jmp 0x3c067
movl $0x0, 0x48(%rsp)
movl 0x48(%rsp), %eax
cmpl 0x9c(%rsp), %eax
jge 0x3c065
movq 0xc8(%rsp), %rdi
movq 0x58(%rsp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x3560
jmp 0x3bf47
movl $0x0, 0x44(%rsp)
movl $0x0, 0x40(%rsp)
movl 0x40(%rsp), %eax
cmpl 0xa0(%rsp), %eax
jge 0x3c055
movl $0x0, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0xa4(%rsp), %eax
jae 0x3c043
movl 0x44(%rsp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x44(%rsp)
movslq %eax, %rsi
leaq 0x78(%rsp), %rdi
callq 0x45330
movq 0x28(%rsp), %rdi
movzbl (%rax), %eax
shll $0x8, %eax
movw %ax, 0xe(%rsp)
movslq 0x3c(%rsp), %rsi
callq 0x4ba0
movq %rax, %rdi
movl 0x40(%rsp), %eax
movl 0x48(%rsp), %ecx
imull 0xa0(%rsp), %ecx
addl %ecx, %eax
movslq %eax, %rsi
callq 0x4bc0
movw 0xe(%rsp), %cx
movw %cx, (%rax)
movl 0x44(%rsp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x44(%rsp)
movslq %eax, %rsi
leaq 0x78(%rsp), %rdi
callq 0x45330
movq 0x28(%rsp), %rdi
movzbl (%rax), %eax
movl %eax, 0x10(%rsp)
movslq 0x3c(%rsp), %rsi
callq 0x4ba0
movq %rax, %rdi
movl 0x40(%rsp), %eax
movl 0x48(%rsp), %ecx
imull 0xa0(%rsp), %ecx
addl %ecx, %eax
movslq %eax, %rsi
callq 0x4bc0
movl 0x10(%rsp), %edx
movswl (%rax), %ecx
addl %edx, %ecx
movw %cx, (%rax)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x3bf70
jmp 0x3c045
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x3bf57
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x3bf1e
jmp 0x3c067
movq 0xc8(%rsp), %rdi
movq 0xc0(%rsp), %rsi
callq 0x3330
jmp 0x3c07e
xorl %eax, %eax
movl %eax, %edx
leaq 0xc8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x3210
jmp 0x3c099
movq 0xe0(%rsp), %rdi
callq 0x3350
jmp 0x3c0a8
movb $0x1, 0x93(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x16e60
testb $0x1, 0x93(%rsp)
jne 0x3c0ce
movq 0x28(%rsp), %rdi
callq 0x12ab0
jmp 0x3c0dc
movq 0x28(%rsp), %rdi
callq 0x12ab0
jmp 0x3c0e9
movq 0x30(%rsp), %rax
addq $0xf8, %rsp
retq
movq 0x68(%rsp), %rdi
callq 0x36a0
nopw %cs:(%rax,%rax)