// File: /workspace/github_c_makefile_star2_O2/albinoloverats[P]hide/src/png.c
// Function: read_png
static int read_png(image_info_t *image_info, void (*progress_update)(uint64_t, uint64_t))
{
	errno = EXIT_SUCCESS;

	FILE *fp = fopen(image_info->file, "rb");
	if (!fp)
		return errno;

	uint8_t header[8];
	fread(header, 1, sizeof header, fp);

	/* initialize stuff */
	png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
	if (!png_ptr)
		goto cf;

	png_infop info_ptr = png_create_info_struct(png_ptr);
	if (!info_ptr)
		goto cleanup;

	/*
	 * libpng uses setjmp as a replacement for exceptions; so “catch”
	 * them and handle them our own way
	 */
	if (setjmp(png_jmpbuf(png_ptr)))
		goto cleanup;

	png_init_io(png_ptr, fp);
	png_set_sig_bytes(png_ptr, 8);

	png_read_info(png_ptr, info_ptr);

	image_info->width = png_get_image_width(png_ptr, info_ptr);
	image_info->height = png_get_image_height(png_ptr, info_ptr);
	png_byte bit_depth = png_get_bit_depth(png_ptr, info_ptr);
	image_info->extra = malloc(sizeof bit_depth);
	memcpy(image_info->extra, &bit_depth, sizeof bit_depth);

	switch (png_get_color_type(png_ptr, info_ptr))
	{
		case PNG_COLOR_TYPE_RGB:
			image_info->bpp = 3;
			break;
		case PNG_COLOR_TYPE_RGBA:
			image_info->bpp = 4;
			break;
		default:
			errno = ENOTSUP;
			goto cleanup;
	}

	//number_of_passes = png_set_interlace_handling(png_ptr);
	png_read_update_info(png_ptr, info_ptr);

	/* read file */
	if (setjmp(png_jmpbuf(png_ptr)))
		goto cleanup;

	image_info->buffer = malloc(sizeof (uint8_t *) * image_info->height);
	for (uint64_t y = 0; y < image_info->height; y++)
	{
		image_info->buffer[y] = malloc(image_info->width * image_info->bpp);
		if (progress_update)
			progress_update(y, image_info->height);
	}

	png_read_image(png_ptr, image_info->buffer);

cleanup:
	png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
cf:
	fclose(fp);

	return errno;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r15
movq %rdi, %r14
callq 0x1060
andl $0x0, (%rax)
movq %rax, %rbp
movq (%r14), %rdi
leaq 0xc64(%rip), %rsi       # 0x2004
callq 0x1110
testq %rax, %rax
je 0x1437
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x8
popq %rdx
movq %rax, %rcx
callq 0x10a0
leaq 0xc3c(%rip), %rdi       # 0x2007
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x11e0
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x142f
movq %rax, %r12
movq %rax, %rdi
callq 0x1120
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x141c
movq %rax, %r13
movq 0x2be5(%rip), %rsi      # 0x3fe8
movl $0xc8, %edx
movq %r12, %rdi
callq 0x10d0
movq %rax, %rdi
callq 0x10f0
testl %eax, %eax
je 0x1449
xorl %eax, %eax
movl %eax, %edx
leaq 0x18(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1160
movq %rbx, %rdi
callq 0x10b0
movl (%rbp), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, 0x8(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x1170
pushq $0x8
popq %rsi
movq %r12, %rdi
callq 0x11a0
movq %r12, %rdi
movq %r13, %rsi
callq 0x1150
movq %r12, %rdi
movq %r13, %rsi
callq 0x1180
movl %eax, %eax
movq %rax, 0x30(%r14)
movq %r12, %rdi
movq %r13, %rsi
callq 0x11c0
movl %eax, %eax
movq %rax, 0x28(%r14)
movq %r12, %rdi
movq %r13, %rsi
callq 0x1050
movl %eax, %ebp
pushq $0x1
popq %rdi
callq 0x1140
movq %rax, 0x48(%r14)
movb %bpl, (%rax)
movq %r12, %rdi
movq %r13, %rsi
callq 0x1080
cmpb $0x2, %al
je 0x14ce
movzbl %al, %eax
cmpl $0x6, %eax
jne 0x155a
movw $0x4, %ax
jmp 0x14d2
movw $0x3, %ax
movq 0x8(%rsp), %rbp
movw %ax, 0x38(%r14)
movq %r12, %rdi
movq %r13, %rsi
callq 0x11b0
movq 0x2afa(%rip), %rsi      # 0x3fe8
movl $0xc8, %edx
movq %r12, %rdi
callq 0x10d0
movq %rax, %rdi
callq 0x10f0
testl %eax, %eax
jne 0x141c
movq 0x28(%r14), %rbp
leaq (,%rbp,8), %rdi
callq 0x1140
movq %rax, 0x40(%r14)
xorl %eax, %eax
movl %eax, %r13d
cmpq %rbp, %r13
jae 0x156b
movzwl 0x38(%r14), %eax
movl %eax, %edi
imulq 0x30(%r14), %rdi
callq 0x1140
movq 0x40(%r14), %rcx
movq %rax, (%rcx,%r13,8)
testq %r15, %r15
je 0x1555
movq %r13, %rdi
movq %rbp, %rsi
callq *%r15
movq 0x28(%r14), %rbp
incq %r13
jmp 0x1525
movq 0x8(%rsp), %rbp
movl $0x5f, (%rbp)
jmp 0x141c
movq 0x40(%r14), %rsi
movq %r12, %rdi
callq 0x1070
movq 0x8(%rsp), %rbp
jmp 0x141c