// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
callq 0x1060
movl $0x0, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq 0xc2b(%rip), %rsi       # 0x2004
callq 0x1110
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x13f8
callq 0x1060
movl (%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x163c
leaq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rcx
movl $0x1, %esi
movl $0x8, %edx
callq 0x10a0
leaq 0xbf1(%rip), %rdi       # 0x2007
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x11e0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x1435
jmp 0x1629
movq -0x30(%rbp), %rdi
callq 0x1120
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x144e
jmp 0x1618
movq -0x30(%rbp), %rdi
movq 0x2b8f(%rip), %rsi      # 0x3fe8
movl $0xc8, %edx
callq 0x10d0
movq %rax, %rdi
callq 0x10f0
cmpl $0x0, %eax
je 0x1475
jmp 0x1618
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x1170
movq -0x30(%rbp), %rdi
movl $0x8, %esi
callq 0x11a0
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x1150
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x1180
movl %eax, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x11c0
movl %eax, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x1050
movb %al, -0x39(%rbp)
movl $0x1, %edi
callq 0x1140
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movb -0x39(%rbp), %cl
movb %cl, (%rax)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x1080
movzbl %al, %ecx
movl %ecx, -0x4c(%rbp)
subb $0x2, %al
je 0x1524
jmp 0x151a
movl -0x4c(%rbp), %eax
subl $0x6, %eax
je 0x1530
jmp 0x153c
movq -0x10(%rbp), %rax
movw $0x3, 0x38(%rax)
jmp 0x154c
movq -0x10(%rbp), %rax
movw $0x4, 0x38(%rax)
jmp 0x154c
callq 0x1060
movl $0x5f, (%rax)
jmp 0x1618
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x11b0
movq -0x30(%rbp), %rdi
movq 0x2a84(%rip), %rsi      # 0x3fe8
movl $0xc8, %edx
callq 0x10d0
movq %rax, %rdi
callq 0x10f0
cmpl $0x0, %eax
je 0x1580
jmp 0x1618
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
shlq $0x3, %rdi
callq 0x1140
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jae 0x1607
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0x10(%rbp), %rax
movzwl 0x38(%rax), %eax
imulq %rax, %rdi
callq 0x1140
movq %rax, %rdx
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x48(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
cmpq $0x0, -0x18(%rbp)
je 0x15f7
movq -0x18(%rbp), %rax
movq -0x48(%rbp), %rdi
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rsi
callq *%rax
jmp 0x15f9
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x15a4
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rsi
callq 0x1070
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x1160
movq -0x20(%rbp), %rdi
callq 0x10b0
callq 0x1060
movl (%rax), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)