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

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

	jpeg_message_t msg = { image_info.width, NULL };
	msg.data = malloc(msg.size); // total capacity

	/* retrieve the image data */
	jpeg_image_t *image = image_info.extra;

	/* copy message from image_info.buffer to message.data */
	for (uint64_t x = 0, i = 0; x < image_info.width * image_info.bpp; x += image_info.bpp, i++)
	{
		msg.data[i]  = (image_info.buffer[0][x + 0] & 0x07) << 5;
		msg.data[i] |= (image_info.buffer[0][x + 1] & 0x03) << 3;
		msg.data[i] |= (image_info.buffer[0][x + 2] & 0x07);
		if (progress_update)
			progress_update(x, image_info.width);
	}
	free(image_info.buffer[0]);
	free(image_info.buffer);

	/* get actual message length from data */
	memcpy(&msg.size, msg.data, sizeof msg.size);
	msg.size = ntohll(msg.size);

	/* write the message to the image */
	jpeg_encode_data(fp, &msg, image);

	free(msg.data);
	for (uint64_t i = 0; i < image->height; i++)
		free(image->rgb[i]);
	free(image->rgb);
	free(image);

	fclose(fp);

	return errno;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
leaq 0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq %rdi, -0x10(%rbp)
callq 0x1050
movq %rax, %rcx
movq -0x50(%rbp), %rax
movl $0x0, (%rcx)
movq (%rax), %rdi
leaq 0x58f8(%rip), %rsi      # 0x7008
callq 0x1100
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x172f
callq 0x1050
movl (%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x1980
movq -0x50(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x28(%rbp), %rdi
callq 0x1120
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, -0x20(%rbp)
movq 0x48(%rax), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x50(%rbp), %rdx
movq -0x38(%rbp), %rax
movq 0x30(%rdx), %rcx
movzwl 0x38(%rdx), %edx
imulq %rdx, %rcx
cmpq %rcx, %rax
jae 0x183c
movq -0x50(%rbp), %rax
movq 0x40(%rax), %rcx
movq (%rcx), %rcx
movq -0x38(%rbp), %rdx
movzbl (%rcx,%rdx), %ecx
andl $0x7, %ecx
shll $0x5, %ecx
movb %cl, %sil
movq -0x20(%rbp), %rcx
movq -0x40(%rbp), %rdx
movb %sil, (%rcx,%rdx)
movq 0x40(%rax), %rcx
movq (%rcx), %rcx
movq -0x38(%rbp), %rdx
movzbl 0x1(%rcx,%rdx), %edi
andl $0x3, %edi
shll $0x3, %edi
movq -0x20(%rbp), %rcx
movq -0x40(%rbp), %rdx
movzbl (%rcx,%rdx), %esi
orl %edi, %esi
movb %sil, (%rcx,%rdx)
movq 0x40(%rax), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movzbl 0x2(%rax,%rcx), %esi
andl $0x7, %esi
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
cmpq $0x0, -0x10(%rbp)
je 0x1819
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rdi
movq 0x30(%rcx), %rsi
callq *%rax
jmp 0x181b
movq -0x50(%rbp), %rax
movzwl 0x38(%rax), %eax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x176f
movq -0x50(%rbp), %rax
movq 0x40(%rax), %rax
movq (%rax), %rdi
callq 0x1040
movq -0x50(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x1040
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movabsq $-0x100000000000000, %rax # imm = 0xFF00000000000000
andq -0x28(%rbp), %rax
shrq $0x38, %rax
movabsq $0xff000000000000, %rcx # imm = 0xFF000000000000
andq -0x28(%rbp), %rcx
shrq $0x28, %rcx
orq %rcx, %rax
movabsq $0xff0000000000, %rcx   # imm = 0xFF0000000000
andq -0x28(%rbp), %rcx
shrq $0x18, %rcx
orq %rcx, %rax
movabsq $0xff00000000, %rcx     # imm = 0xFF00000000
andq -0x28(%rbp), %rcx
shrq $0x8, %rcx
orq %rcx, %rax
movabsq $0xff000000, %rcx       # imm = 0xFF000000
andq -0x28(%rbp), %rcx
shlq $0x8, %rcx
orq %rcx, %rax
movq -0x28(%rbp), %rcx
andq $0xff0000, %rcx         # imm = 0xFF0000
shlq $0x18, %rcx
orq %rcx, %rax
movq -0x28(%rbp), %rcx
andq $0xff00, %rcx           # imm = 0xFF00
shlq $0x28, %rcx
orq %rcx, %rax
movq -0x28(%rbp), %rcx
andq $0xff, %rcx
shlq $0x38, %rcx
orq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq -0x28(%rbp), %rsi
callq 0x1130
movq -0x20(%rbp), %rdi
callq 0x1040
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x30(%rbp), %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jae 0x1958
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1040
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x1926
movq -0x30(%rbp), %rax
movq (%rax), %rdi
callq 0x1040
movq -0x30(%rbp), %rdi
callq 0x1040
movq -0x18(%rbp), %rdi
callq 0x1070
callq 0x1050
movl (%rax), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)