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

	TIFF *tif = TIFFOpen(image_info.file, "w");
	if (!tif)
		return errno;

	TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, image_info.width);
	TIFFSetField(tif, TIFFTAG_IMAGELENGTH, image_info.height);
	TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, image_info.bpp);
	TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);

	TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
	TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_CENTIMETER);
	TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
	TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
	TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);

	TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, image_info.width * image_info.bpp));

	for (uint64_t y = 0; y < image_info.height; y++)
	{
		TIFFWriteScanline(tif, image_info.buffer[y], y, 0);
		free(image_info.buffer[y]);
		if (progress_update)
			progress_update(y, image_info.height);
	}
	free(image_info.buffer);

	TIFFClose(tif);

	return errno;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r14
callq 0x1040
movq %rax, %rbx
andl $0x0, (%rax)
movq 0x50(%rsp), %rdi
leaq 0xc99(%rip), %rsi       # 0x2007
callq 0x1050
testq %rax, %rax
je 0x14af
movq %rax, %r15
movq %rbx, 0x10(%rsp)
leaq 0x50(%rsp), %rax
movq 0x30(%rax), %rbp
movq %rax, %rbx
xorl %r12d, %r12d
movq %r15, %rdi
movl $0x100, %esi            # imm = 0x100
movq %rbp, %rdx
xorl %eax, %eax
callq 0x1080
movq 0x28(%rbx), %r13
movq %r15, %rdi
movl $0x101, %esi            # imm = 0x101
movq %r13, %rdx
xorl %eax, %eax
callq 0x1080
movzwl 0x38(%rbx), %edx
movl %edx, 0xc(%rsp)
movq %r15, %rdi
movl $0x115, %esi            # imm = 0x115
xorl %eax, %eax
callq 0x1080
pushq $0x5
popq %rdx
movq %r15, %rdi
movl $0x103, %esi            # imm = 0x103
xorl %eax, %eax
callq 0x1080
pushq $0x8
popq %rdx
movq %r15, %rdi
movl $0x102, %esi            # imm = 0x102
xorl %eax, %eax
callq 0x1080
pushq $0x3
popq %rdx
movq %r15, %rdi
movl $0x128, %esi            # imm = 0x128
xorl %eax, %eax
callq 0x1080
pushq $0x1
popq %rbx
movq %r15, %rdi
movl $0x112, %esi            # imm = 0x112
movl %ebx, %edx
xorl %eax, %eax
callq 0x1080
movq %r15, %rdi
movl $0x11c, %esi            # imm = 0x11C
movl %ebx, %edx
xorl %eax, %eax
callq 0x1080
pushq $0x2
popq %rdx
movq %r15, %rdi
movl $0x106, %esi            # imm = 0x106
xorl %eax, %eax
callq 0x1080
imull 0xc(%rsp), %ebp
movq %r15, %rdi
movl %ebp, %esi
callq 0x1060
movq %r15, %rdi
movl $0x116, %esi            # imm = 0x116
movl %eax, %edx
xorl %eax, %eax
callq 0x1080
leaq 0x50(%rsp), %rax
movq 0x40(%rax), %rbx
cmpq %r12, %r13
je 0x149a
movq (%rbx,%r12,8), %rsi
movq %r15, %rdi
movl %r12d, %edx
xorl %ecx, %ecx
callq 0x10c0
movq (%rbx,%r12,8), %rdi
callq 0x1030
testq %r14, %r14
je 0x1495
movq %r12, %rdi
movq %r13, %rsi
callq *%r14
incq %r12
jmp 0x1468
movq %rbx, %rdi
callq 0x1030
movq %r15, %rdi
callq 0x10d0
movq 0x10(%rsp), %rbx
movl (%rbx), %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq