// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
movq %rsi, %r15
movq %rdi, %r14
callq 0x1060
movq %rax, %rbp
movl $0x0, (%rax)
movq (%r14), %rdi
leaq 0xc68(%rip), %rsi       # 0x2004
callq 0x1110
testq %rax, %rax
je 0x142f
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
movl $0x1, %esi
movl $0x8, %edx
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, (%rsp)
testq %rax, %rax
je 0x1427
movq %rax, %rdi
callq 0x1120
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x1416
movq (%rsp), %rdi
movq 0x2be8(%rip), %rsi      # 0x3fe8
movl $0xc8, %edx
callq 0x10d0
movq %rax, %rdi
callq 0x10f0
testl %eax, %eax
je 0x1441
xorl %eax, %eax
movl %eax, %edx
movq %rsp, %rdi
leaq 0x8(%rsp), %rsi
callq 0x1160
movq %rbx, %rdi
callq 0x10b0
movl (%rbp), %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsp), %rdi
movq %rbx, %rsi
callq 0x1170
movq (%rsp), %rdi
movl $0x8, %esi
callq 0x11a0
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x1150
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x1180
movl %eax, %eax
movq %rax, 0x30(%r14)
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x11c0
movl %eax, %eax
movq %rax, 0x28(%r14)
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x1050
movl %eax, %r12d
movl $0x1, %edi
callq 0x1140
movq %rax, 0x48(%r14)
movb %r12b, (%rax)
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x1080
cmpb $0x2, %al
je 0x14d7
movzbl %al, %eax
cmpl $0x6, %eax
jne 0x1580
movw $0x4, %ax
jmp 0x14db
movw $0x3, %ax
movw %ax, 0x38(%r14)
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x11b0
movq (%rsp), %rdi
movq 0x2aef(%rip), %rsi      # 0x3fe8
movl $0xc8, %edx
callq 0x10d0
movq %rax, %rdi
callq 0x10f0
testl %eax, %eax
jne 0x1416
movq 0x28(%r14), %r13
leaq (,%r13,8), %rdi
callq 0x1140
movq %rax, 0x40(%r14)
testq %r13, %r13
je 0x156e
xorl %eax, %eax
movq %rax, %r12
movzwl 0x38(%r14), %eax
movl %eax, %edi
imulq 0x30(%r14), %rdi
callq 0x1140
movq 0x40(%r14), %rcx
movq %rax, (%rcx,%r12,8)
testq %r15, %r15
je 0x1559
movq %r12, %rdi
movq %r13, %rsi
callq *%r15
movq %r12, %rax
incq %rax
movq 0x28(%r14), %rcx
movq %rcx, %r13
movq %rax, %r12
cmpq %rcx, %rax
jb 0x1532
movq (%rsp), %rdi
movq 0x40(%r14), %rsi
callq 0x1070
jmp 0x1416
movl $0x5f, (%rbp)
jmp 0x1416