// File: /workspace/github_c_makefile_star2_O2/albinoloverats[P]hide/src/jpeg-save.c
// Function: process_DU
static void process_DU(int8_t *ComponentDU, double *fdtbl, int16_t *DC, bitstring *HTDC, bitstring *HTAC)
{
	bitstring EOB = HTAC[0x00];
	bitstring M16zeroes = HTAC[0xF0];
	uint8_t end0pos;

	fdct_and_quantization(ComponentDU, fdtbl, DU_DCT);
	// zigzag reorder
	for (int i = 0; i <= 63; i++)
		DU[zigzag[i]] = DU_DCT[i];


	static uint64_t offset = 0;
	static uint8_t bit = 1;

	for (uint64_t i = 0; i < 64 && offset < message->size + sizeof message->size; i++)
		if (DU[i] > 1)
		{
			uint8_t v = (*(message->data + offset) & bit);
			if (v)
				DU[i] |= 0x0001;
			else
				DU[i] &= 0xFFFE;
			bit <<= 1;
			if (!bit)
			{
				bit = 1;
				offset++;
			}
		}


	int16_t diff = DU[0] - *DC;
	*DC = DU[0];
	// Encode DC
	if (diff == 0)
		writebits(HTDC[0]); // diff might be 0
	else
	{
		writebits(HTDC[category[diff]]);
		writebits(bitcode[diff]);
	}
	// Encode ACs
	for (end0pos = 63; (end0pos > 0) && (DU[end0pos] == 0); end0pos--);
	// end0pos = first element in reverse order !=0
	if (end0pos == 0)
	{
		writebits(EOB);
		return;
	}

	for (int i = 1; i <= end0pos; i++)
	{
		int startpos = i;
		for (; (DU[i] == 0) && (i <= end0pos); i++);
		int nrzeroes = i - startpos;
		if (nrzeroes >= 16)
		{
			for (int nrmarker = 1; nrmarker <= (nrzeroes >> 4); nrmarker++)
				writebits(M16zeroes);
			nrzeroes %= 16;
		}
		writebits(HTAC[(nrzeroes << 4) + category[DU[i]]]);
		writebits(bitcode[DU[i]]);
	}
	if (end0pos != 63)
		writebits(EOB);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movl (%r8), %eax
movl %eax, 0x8(%rsp)
movq %r8, 0x10(%rsp)
movl 0x3c0(%r8), %eax
movl %eax, 0xc(%rsp)
xorl %eax, %eax
cmpq $0x40, %rax
je 0x37fc
movsbl (%rdi,%rax), %r8d
cvtsi2sd %r8d, %xmm0
movsd %xmm0, 0x20(%rsp,%rax,8)
incq %rax
jmp 0x37e1
pushq $0x7
popq %rax
leaq 0x20(%rsp), %rdi
movsd 0x9ec(%rip), %xmm0      # 0x41f8
movsd 0x9ec(%rip), %xmm1      # 0x4200
movsd 0x9ec(%rip), %xmm2      # 0x4208
movsd 0x9ec(%rip), %xmm3      # 0x4210
testl %eax, %eax
js 0x3963
movsd 0x38(%rdi), %xmm6
movsd (%rdi), %xmm5
movsd 0x8(%rdi), %xmm4
movapd %xmm5, %xmm8
addsd %xmm6, %xmm8
subsd %xmm6, %xmm5
movsd 0x30(%rdi), %xmm6
movapd %xmm4, %xmm9
addsd %xmm6, %xmm9
subsd %xmm6, %xmm4
movsd 0x10(%rdi), %xmm6
movsd 0x28(%rdi), %xmm7
movapd %xmm6, %xmm10
addsd %xmm7, %xmm10
subsd %xmm7, %xmm6
movsd 0x18(%rdi), %xmm7
movsd 0x20(%rdi), %xmm11
movapd %xmm7, %xmm12
addsd %xmm11, %xmm12
subsd %xmm11, %xmm7
movapd %xmm8, %xmm11
addsd %xmm12, %xmm11
subsd %xmm12, %xmm8
movapd %xmm9, %xmm12
addsd %xmm10, %xmm12
subsd %xmm10, %xmm9
movapd %xmm12, %xmm10
addsd %xmm11, %xmm10
movsd %xmm10, (%rdi)
subsd %xmm12, %xmm11
movsd %xmm11, 0x20(%rdi)
addsd %xmm8, %xmm9
mulsd %xmm0, %xmm9
movapd %xmm8, %xmm10
addsd %xmm9, %xmm10
movsd %xmm10, 0x10(%rdi)
subsd %xmm9, %xmm8
movsd %xmm8, 0x30(%rdi)
addsd %xmm6, %xmm7
addsd %xmm4, %xmm6
addsd %xmm5, %xmm4
movapd %xmm7, %xmm8
subsd %xmm4, %xmm8
mulsd %xmm1, %xmm8
mulsd %xmm2, %xmm7
addsd %xmm8, %xmm7
mulsd %xmm3, %xmm4
addsd %xmm8, %xmm4
mulsd %xmm0, %xmm6
movapd %xmm5, %xmm8
addsd %xmm6, %xmm8
subsd %xmm6, %xmm5
movapd %xmm5, %xmm6
addsd %xmm7, %xmm6
movsd %xmm6, 0x28(%rdi)
subsd %xmm7, %xmm5
movsd %xmm5, 0x18(%rdi)
movapd %xmm8, %xmm5
addsd %xmm4, %xmm5
movsd %xmm5, 0x8(%rdi)
subsd %xmm4, %xmm8
movsd %xmm8, 0x38(%rdi)
addq $0x40, %rdi
decl %eax
jmp 0x3824
pushq $0x7
popq %rax
leaq 0x20(%rsp), %rdi
testl %eax, %eax
js 0x3ace
movsd 0x1c0(%rdi), %xmm6
movsd (%rdi), %xmm5
movsd 0x40(%rdi), %xmm4
movapd %xmm5, %xmm8
addsd %xmm6, %xmm8
subsd %xmm6, %xmm5
movsd 0x180(%rdi), %xmm6
movapd %xmm4, %xmm9
addsd %xmm6, %xmm9
subsd %xmm6, %xmm4
movsd 0x80(%rdi), %xmm6
movsd 0x140(%rdi), %xmm7
movapd %xmm6, %xmm10
addsd %xmm7, %xmm10
subsd %xmm7, %xmm6
movsd 0xc0(%rdi), %xmm7
movsd 0x100(%rdi), %xmm11
movapd %xmm7, %xmm12
addsd %xmm11, %xmm12
subsd %xmm11, %xmm7
movapd %xmm8, %xmm11
addsd %xmm12, %xmm11
subsd %xmm12, %xmm8
movapd %xmm9, %xmm12
addsd %xmm10, %xmm12
subsd %xmm10, %xmm9
movapd %xmm12, %xmm10
addsd %xmm11, %xmm10
movsd %xmm10, (%rdi)
subsd %xmm12, %xmm11
movsd %xmm11, 0x100(%rdi)
addsd %xmm8, %xmm9
mulsd %xmm0, %xmm9
movapd %xmm8, %xmm10
addsd %xmm9, %xmm10
movsd %xmm10, 0x80(%rdi)
subsd %xmm9, %xmm8
movsd %xmm8, 0x180(%rdi)
addsd %xmm6, %xmm7
addsd %xmm4, %xmm6
addsd %xmm5, %xmm4
movapd %xmm7, %xmm8
subsd %xmm4, %xmm8
mulsd %xmm1, %xmm8
mulsd %xmm2, %xmm7
addsd %xmm8, %xmm7
mulsd %xmm3, %xmm4
addsd %xmm8, %xmm4
mulsd %xmm0, %xmm6
movapd %xmm5, %xmm8
addsd %xmm6, %xmm8
subsd %xmm6, %xmm5
movapd %xmm5, %xmm6
addsd %xmm7, %xmm6
movsd %xmm6, 0x140(%rdi)
subsd %xmm7, %xmm5
movsd %xmm5, 0xc0(%rdi)
movapd %xmm8, %xmm5
addsd %xmm4, %xmm5
movsd %xmm5, 0x40(%rdi)
subsd %xmm4, %xmm8
movsd %xmm8, 0x1c0(%rdi)
addq $0x8, %rdi
decl %eax
jmp 0x396b
xorl %edi, %edi
movsd 0x718(%rip), %xmm0      # 0x41f0
leaq 0x559f1(%rip), %rax     # 0x594d0
cmpq $0x80, %rdi
je 0x3b0e
movsd 0x20(%rsp,%rdi,4), %xmm1
mulsd (%rsi,%rdi,4), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r8d
addl $0xffffc000, %r8d       # imm = 0xFFFFC000
movw %r8w, (%rdi,%rax)
addq $0x2, %rdi
jmp 0x3adf
xorl %esi, %esi
leaq 0x749(%rip), %rdi       # 0x4260
leaq 0x55a32(%rip), %r15     # 0x59550
cmpq $0x40, %rsi
je 0x3b3b
movzwl (%rax), %r8d
movzbl (%rsi,%rdi), %r9d
movw %r8w, (%r15,%r9,2)
incq %rsi
addq $0x2, %rax
jmp 0x3b1e
movq 0x55a8e(%rip), %rax     # 0x595d0
movb 0x2585(%rip), %r8b      # 0x60ce
movq 0x25f0(%rip), %rsi      # 0x6140
xorl %edi, %edi
cmpq $0x80, %rdi
je 0x3bb5
movq (%rsi), %r9
addq $0x8, %r9
cmpq %r9, %rax
jae 0x3bb5
movzwl (%rdi,%r15), %r9d
cmpw $0x2, %r9w
jl 0x3baf
movq 0x8(%rsi), %r10
testb %r8b, (%r10,%rax)
je 0x3b83
orl $0x1, %r9d
jmp 0x3b8a
andl $0x7ffe, %r9d           # imm = 0x7FFE
movw %r9w, (%rdi,%r15)
addb %r8b, %r8b
movb %r8b, 0x2535(%rip)      # 0x60ce
jne 0x3baf
movb $0x1, 0x252c(%rip)      # 0x60ce
incq %rax
movq %rax, 0x55a24(%rip)     # 0x595d0
movb $0x1, %r8b
addq $0x2, %rdi
jmp 0x3b52
movzwl 0x55994(%rip), %eax     # 0x59550
movzwl (%rdx), %esi
movw %ax, (%rdx)
cmpw %si, %ax
je 0x3bee
subl %esi, %eax
movq 0x13020(%rip), %rdx     # 0x16bf0
movswq %ax, %rbx
movzbl (%rdx,%rbx), %eax
movl (%rcx,%rax,4), %edi
callq 0x3732
shlq $0x2, %rbx
addq 0x53015(%rip), %rbx     # 0x56c00
movq %rbx, %rcx
movl (%rcx), %edi
callq 0x3732
pushq $0x3f
popq %r13
xorl %ecx, %ecx
testb %r13b, %r13b
je 0x3cd4
movzbl %r13b, %eax
cmpw $0x0, (%r15,%rax,2)
jne 0x3c17
decl %r13d
incb %cl
jmp 0x3bfb
movl %ecx, 0x4(%rsp)
movzbl %r13b, %ebx
pushq $0x1
popq %rax
cmpl %r13d, %eax
jg 0x3ccd
movq %rax, 0x18(%rsp)
movslq %eax, %rbp
xorl %ecx, %ecx
pushq $-0x1
popq %rax
movl %eax, %r12d
movq %rbp, %rdx
movl %ecx, %r14d
movzwl (%r15,%rbp,2), %eax
incq %rbp
incl %r12d
cmpq %rbx, %rdx
jg 0x3c5a
leal 0x1(%r14), %ecx
testw %ax, %ax
je 0x3c3b
movl %r12d, %ecx
cmpl $0x10, %r12d
jl 0x3c84
shrl $0x4, %r14d
subl $0x1, %r14d
jb 0x3c78
movl 0xc(%rsp), %edi
callq 0x3732
jmp 0x3c67
movl %r12d, %ecx
andl $0xf, %ecx
movzwl -0x2(%r15,%rbp,2), %eax
shll $0x4, %ecx
movq 0x12f62(%rip), %rdx     # 0x16bf0
movswq %ax, %rax
movzbl (%rdx,%rax), %eax
movslq %ecx, %rcx
addq %rax, %rcx
movq 0x10(%rsp), %rax
movl (%rax,%rcx,4), %edi
callq 0x3732
movq 0x52f50(%rip), %rax     # 0x56c00
movswq -0x2(%r15,%rbp,2), %rcx
movl (%rax,%rcx,4), %edi
callq 0x3732
movq 0x18(%rsp), %rax
addl %r12d, %eax
incl %eax
jmp 0x3c22
cmpb $0x0, 0x4(%rsp)
je 0x3cee
movl 0x8(%rsp), %edi
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3732
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq