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

	/* create file */
	FILE *fp = fopen(image_info.file, "wb");
	if (!fp)
		return errno;

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

	png_set_compression_level(png_ptr, 9/*Z_BEST_COMPRESSION*/);

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

	if (setjmp(png_jmpbuf(png_ptr)))
		goto cleanup;

	png_init_io(png_ptr, fp);

	/* write header */
	if (setjmp(png_jmpbuf(png_ptr)))
		goto cleanup;

	png_byte color_type;
	switch (image_info.bpp)
	{
		case 3:
			color_type = PNG_COLOR_TYPE_RGB;
			break;
		case 4:
			color_type = PNG_COLOR_TYPE_RGBA;
			break;
		default:
			goto cleanup;
	}

	png_byte bit_depth;
	memcpy(&bit_depth, image_info.extra, sizeof bit_depth);
	png_set_IHDR(png_ptr, info_ptr, image_info.width, image_info.height, bit_depth,
			 color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
	free(image_info.extra);

	png_write_info(png_ptr, info_ptr);

	/* write bytes */
	if (setjmp(png_jmpbuf(png_ptr)))
		goto cleanup;

	png_write_image(png_ptr, image_info.buffer);

	/* end write */
	if (setjmp(png_jmpbuf(png_ptr)))
		goto cleanup;

	png_write_end(png_ptr, NULL);

	/* clean up heap allocation */
	for (uint64_t y = 0; y < image_info.height; y++)
	{
		free(image_info.buffer[y]);
		if (progress_update)
			progress_update(y, image_info.height);
	}
	free(image_info.buffer);

cleanup:
	png_destroy_write_struct(&png_ptr, &info_ptr);
cf:
	fclose(fp);
	return errno;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
leaq 0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
movq %rdi, -0x10(%rbp)
callq 0x1060
movq %rax, %rcx
movq -0x40(%rbp), %rax
movl $0x0, (%rcx)
movq (%rax), %rdi
leaq 0x98e(%rip), %rsi       # 0x200e
callq 0x1110
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x169f
callq 0x1060
movl (%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x18c1
leaq 0x961(%rip), %rdi       # 0x2007
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x1190
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x16c5
jmp 0x18ae
movq -0x20(%rbp), %rdi
movl $0x9, %esi
callq 0x1090
movq -0x20(%rbp), %rdi
callq 0x1120
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x16ec
jmp 0x18a1
movq -0x20(%rbp), %rdi
movq 0x28f1(%rip), %rsi      # 0x3fe8
movl $0xc8, %edx
callq 0x10d0
movq %rax, %rdi
callq 0x10f0
cmpl $0x0, %eax
je 0x1713
jmp 0x18a1
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1170
movq -0x20(%rbp), %rdi
movq 0x28bd(%rip), %rsi      # 0x3fe8
movl $0xc8, %edx
callq 0x10d0
movq %rax, %rdi
callq 0x10f0
cmpl $0x0, %eax
je 0x1747
jmp 0x18a1
movq -0x40(%rbp), %rax
movzwl 0x38(%rax), %eax
movl %eax, -0x44(%rbp)
subl $0x3, %eax
je 0x1763
jmp 0x1759
movl -0x44(%rbp), %eax
subl $0x4, %eax
je 0x1769
jmp 0x176f
movb $0x2, -0x29(%rbp)
jmp 0x1774
movb $0x6, -0x29(%rbp)
jmp 0x1774
jmp 0x18a1
movq -0x40(%rbp), %rax
movq 0x48(%rax), %rcx
movb (%rcx), %cl
movb %cl, -0x2a(%rbp)
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq 0x30(%rax), %rcx
movl %ecx, %edx
movq 0x28(%rax), %rax
movl %eax, %ecx
movzbl -0x2a(%rbp), %r8d
movzbl -0x29(%rbp), %r9d
xorl %eax, %eax
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
callq 0x11d0
movq -0x40(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x1040
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x1030
movq -0x20(%rbp), %rdi
movq 0x2806(%rip), %rsi      # 0x3fe8
movl $0xc8, %edx
callq 0x10d0
movq %rax, %rdi
callq 0x10f0
cmpl $0x0, %eax
je 0x17fe
jmp 0x18a1
movq -0x40(%rbp), %rax
movq -0x20(%rbp), %rdi
movq 0x40(%rax), %rsi
callq 0x10c0
movq -0x20(%rbp), %rdi
movq 0x27ce(%rip), %rsi      # 0x3fe8
movl $0xc8, %edx
callq 0x10d0
movq %rax, %rdi
callq 0x10f0
cmpl $0x0, %eax
je 0x1833
jmp 0x18a1
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10e0
movq $0x0, -0x38(%rbp)
movq -0x40(%rbp), %rcx
movq -0x38(%rbp), %rax
cmpq 0x28(%rcx), %rax
jae 0x1894
movq -0x40(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1040
cmpq $0x0, -0x10(%rbp)
je 0x1884
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rdi
movq 0x28(%rcx), %rsi
callq *%rax
jmp 0x1886
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x1848
movq -0x40(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x1040
leaq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1130
movq -0x18(%rbp), %rdi
callq 0x10b0
callq 0x1060
movl (%rax), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)