// File: /workspace/github_c_makefile_star2_O0/albinoloverats[P]hide/src/jpeg-save.c
// Function: jpeg_encode_data
extern void jpeg_encode_data(FILE *file, jpeg_message_t *msg, jpeg_image_t *info)
{
	message = msg;

	bitstring fillbits; // filling bitstring for the bit alignment of the EOI marker

	Ximage = info->width;
	Yimage = info->height;

	uint32_t Xdiv8 = (Ximage % 8) ? ((Ximage / 8) << 3) + 8 : Ximage;
	uint32_t Ydiv8 = (Yimage % 8) ? ((Yimage / 8) << 3) + 8 : Yimage;

	// The image we encode shall be filled with the last line and the last column
	// from the original bitmap, until Ximage and Yimage are divisible by 8
	// Load BMP image from disk and complete X
	RGB_buffer = calloc(Xdiv8 * Ydiv8, sizeof (colorRGB));

	//uint8_t nr_fillingbytes = (Ximage % 4) ? 4 - (Ximage % 4) : 0;
	for (uint32_t nrline = 0; nrline < Yimage; nrline++)
		memcpy(RGB_buffer + nrline * Xdiv8, info->rgb[nrline], Ximage * 3);
	Ximage = Xdiv8;
	Yimage = Ydiv8;


	fp_jpeg_stream = file;
	init_all();
	SOF0info.width = info->width;
	SOF0info.height = info->height;
	writeword(0xFFD8); // SOI
	write_APP0info();

	write_DQTinfo();
	write_SOF0info();
	write_DHTinfo();
	write_SOSinfo();

	bytenew = 0;
	bytepos = 7;
	main_encoder();
	// Do the bit alignment of the EOI marker
	if (bytepos >= 0)
	{
		fillbits.length = bytepos + 1;
		fillbits.value = (1 << (bytepos + 1)) - 1;
		writebits(fillbits);
	}
	writeword(0xFFD9); //EOI
	free(RGB_buffer);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, 0x5601(%rip)      # 0x9540
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x55fc(%rip)      # 0x9548
movq -0x18(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, 0x55f3(%rip)      # 0x954c
movl 0x55e9(%rip), %eax      # 0x9548
andl $0x7, %eax
cmpl $0x0, %eax
je 0x3f7b
movl 0x55db(%rip), %eax      # 0x9548
shrl $0x3, %eax
shll $0x3, %eax
addl $0x8, %eax
movl %eax, -0x2c(%rbp)
jmp 0x3f84
movl 0x55c7(%rip), %eax      # 0x9548
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x20(%rbp)
movl 0x55bc(%rip), %eax      # 0x954c
andl $0x7, %eax
cmpl $0x0, %eax
je 0x3fac
movl 0x55ae(%rip), %eax      # 0x954c
shrl $0x3, %eax
shll $0x3, %eax
addl $0x8, %eax
movl %eax, -0x30(%rbp)
jmp 0x3fb5
movl 0x559a(%rip), %eax      # 0x954c
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x20(%rbp), %eax
imull -0x24(%rbp), %eax
movl %eax, %eax
movl %eax, %edi
movl $0x3, %esi
callq 0x10d0
movq %rax, 0x5579(%rip)      # 0x9550
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl 0x5565(%rip), %eax      # 0x954c
jae 0x4029
movq 0x5560(%rip), %rdi      # 0x9550
movl -0x28(%rbp), %eax
imull -0x20(%rbp), %eax
movl %eax, %eax
imulq $0x3, %rax, %rax
addq %rax, %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x28(%rbp), %ecx
movq (%rax,%rcx,8), %rsi
imull $0x3, 0x5533(%rip), %eax # 0x9548
movl %eax, %eax
movl %eax, %edx
callq 0x1110
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x3fde
movl -0x20(%rbp), %eax
movl %eax, 0x5516(%rip)      # 0x9548
movl -0x24(%rbp), %eax
movl %eax, 0x5511(%rip)      # 0x954c
movq -0x8(%rbp), %rax
movq %rax, 0x5512(%rip)      # 0x9558
callq 0x4130
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movw %ax, 0x517f(%rip)       # 0x91d8
movq -0x18(%rbp), %rax
movl 0xc(%rax), %eax
movw %ax, 0x516f(%rip)       # 0x91d6
movq 0x54ea(%rip), %rsi      # 0x9558
movl $0xff, %edi
callq 0x10b0
movq 0x54d9(%rip), %rsi      # 0x9558
movl $0xd8, %edi
callq 0x10b0
callq 0x4160
callq 0x42e0
callq 0x43f0
callq 0x45b0
callq 0x4830
movb $0x0, 0x54b7(%rip)      # 0x9560
movb $0x7, 0x5134(%rip)      # 0x91e4
callq 0x4970
movsbl 0x5128(%rip), %eax      # 0x91e4
cmpl $0x0, %eax
jl 0x40ee
movsbl 0x511c(%rip), %eax      # 0x91e4
addl $0x1, %eax
movb %al, -0x1c(%rbp)
movsbl 0x510f(%rip), %ecx      # 0x91e4
addl $0x1, %ecx
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
movw %ax, -0x1a(%rbp)
movl -0x1c(%rbp), %edi
callq 0x4a60
movq 0x5463(%rip), %rsi      # 0x9558
movl $0xff, %edi
callq 0x10b0
movq 0x5452(%rip), %rsi      # 0x9558
movl $0xd9, %edi
callq 0x10b0
movq 0x5439(%rip), %rdi      # 0x9550
callq 0x1040
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)