// File: /workspace/github_c_makefile_star2_O3/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 $0x238, %rsp            # imm = 0x238
movl (%r8), %ebp
movq %r8, 0x28(%rsp)
movl 0x3c0(%r8), %r14d
xorl %eax, %eax
movzwl (%rdi,%rax), %r8d
movd %r8d, %xmm0
punpcklbw %xmm0, %xmm0    # xmm0 = xmm0[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
pshuflw $0x60, %xmm0, %xmm0     # xmm0 = xmm0[0,0,2,1,4,5,6,7]
psrad $0x18, %xmm0
cvtdq2pd %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp,%rax,8)
addq $0x2, %rax
cmpq $0x40, %rax
jne 0x3bc9
xorl %eax, %eax
movapd 0x1652(%rip), %xmm0     # 0x5250
movapd 0x165a(%rip), %xmm1     # 0x5260
movapd 0x1662(%rip), %xmm2     # 0x5270
movapd 0x166a(%rip), %xmm3     # 0x5280
movapd 0xa0(%rsp,%rax), %xmm7
movapd 0x90(%rsp,%rax), %xmm9
movapd 0x80(%rsp,%rax), %xmm10
movapd 0x70(%rsp,%rax), %xmm8
movapd 0x30(%rsp,%rax), %xmm5
movapd 0x40(%rsp,%rax), %xmm6
movapd 0x50(%rsp,%rax), %xmm11
movapd 0x60(%rsp,%rax), %xmm12
movapd %xmm5, %xmm4
unpcklpd %xmm8, %xmm4            # xmm4 = xmm4[0],xmm8[0]
unpckhpd %xmm8, %xmm5            # xmm5 = xmm5[1],xmm8[1]
movapd %xmm6, %xmm8
unpcklpd %xmm10, %xmm8           # xmm8 = xmm8[0],xmm10[0]
unpckhpd %xmm10, %xmm6           # xmm6 = xmm6[1],xmm10[1]
movapd %xmm11, %xmm10
unpcklpd %xmm9, %xmm10           # xmm10 = xmm10[0],xmm9[0]
unpckhpd %xmm9, %xmm11           # xmm11 = xmm11[1],xmm9[1]
movapd %xmm12, %xmm9
unpcklpd %xmm7, %xmm9            # xmm9 = xmm9[0],xmm7[0]
unpckhpd %xmm7, %xmm12           # xmm12 = xmm12[1],xmm7[1]
movapd %xmm4, %xmm7
addpd %xmm12, %xmm7
subpd %xmm12, %xmm4
movapd %xmm5, %xmm12
addpd %xmm9, %xmm12
subpd %xmm9, %xmm5
movapd %xmm8, %xmm13
addpd %xmm11, %xmm13
subpd %xmm11, %xmm8
movapd %xmm6, %xmm11
addpd %xmm10, %xmm11
subpd %xmm10, %xmm6
movapd %xmm7, %xmm9
addpd %xmm11, %xmm9
subpd %xmm11, %xmm7
movapd %xmm12, %xmm11
addpd %xmm13, %xmm11
subpd %xmm13, %xmm12
movapd %xmm11, %xmm10
addpd %xmm9, %xmm10
subpd %xmm11, %xmm9
addpd %xmm7, %xmm12
mulpd %xmm0, %xmm12
movapd %xmm7, %xmm11
addpd %xmm12, %xmm11
subpd %xmm12, %xmm7
addpd %xmm8, %xmm6
addpd %xmm5, %xmm8
addpd %xmm4, %xmm5
movapd %xmm6, %xmm12
subpd %xmm5, %xmm12
mulpd %xmm1, %xmm12
mulpd %xmm2, %xmm6
addpd %xmm12, %xmm6
mulpd %xmm3, %xmm5
addpd %xmm12, %xmm5
mulpd %xmm0, %xmm8
movapd %xmm4, %xmm12
addpd %xmm8, %xmm12
subpd %xmm8, %xmm4
movapd %xmm4, %xmm8
addpd %xmm6, %xmm8
subpd %xmm6, %xmm4
movapd %xmm12, %xmm6
addpd %xmm5, %xmm6
subpd %xmm5, %xmm12
movapd %xmm11, %xmm5
unpcklpd %xmm4, %xmm5            # xmm5 = xmm5[0],xmm4[0]
movapd %xmm10, %xmm13
unpcklpd %xmm6, %xmm13           # xmm13 = xmm13[0],xmm6[0]
movapd %xmm7, %xmm14
unpcklpd %xmm12, %xmm14          # xmm14 = xmm14[0],xmm12[0]
movapd %xmm9, %xmm15
unpcklpd %xmm8, %xmm15           # xmm15 = xmm15[0],xmm8[0]
movhlps %xmm11, %xmm4           # xmm4 = xmm11[1],xmm4[1]
movhlps %xmm10, %xmm6           # xmm6 = xmm10[1],xmm6[1]
movhlps %xmm7, %xmm12           # xmm12 = xmm7[1],xmm12[1]
movhlps %xmm9, %xmm8            # xmm8 = xmm9[1],xmm8[1]
movaps %xmm8, 0x90(%rsp,%rax)
movaps %xmm12, 0xa0(%rsp,%rax)
movaps %xmm6, 0x70(%rsp,%rax)
movaps %xmm4, 0x80(%rsp,%rax)
movapd %xmm15, 0x50(%rsp,%rax)
movapd %xmm14, 0x60(%rsp,%rax)
movapd %xmm13, 0x30(%rsp,%rax)
movapd %xmm5, 0x40(%rsp,%rax)
subq $-0x80, %rax
cmpq $0x200, %rax            # imm = 0x200
jne 0x3c16
movl $0x38, %eax
movapd 0x30(%rsp,%rax,8), %xmm9
movapd -0x190(%rsp,%rax,8), %xmm5
movapd -0x150(%rsp,%rax,8), %xmm4
movapd -0x110(%rsp,%rax,8), %xmm7
movapd -0xd0(%rsp,%rax,8), %xmm6
movapd %xmm5, %xmm8
addpd %xmm9, %xmm8
subpd %xmm9, %xmm5
movapd -0x10(%rsp,%rax,8), %xmm10
movapd %xmm4, %xmm9
addpd %xmm10, %xmm9
subpd %xmm10, %xmm4
movapd -0x50(%rsp,%rax,8), %xmm10
movapd %xmm7, %xmm11
addpd %xmm10, %xmm11
subpd %xmm10, %xmm7
movapd -0x90(%rsp,%rax,8), %xmm10
movapd %xmm6, %xmm12
addpd %xmm10, %xmm12
subpd %xmm10, %xmm6
movapd %xmm8, %xmm10
addpd %xmm12, %xmm10
subpd %xmm12, %xmm8
movapd %xmm9, %xmm12
addpd %xmm11, %xmm12
subpd %xmm11, %xmm9
movapd %xmm12, %xmm11
addpd %xmm10, %xmm11
movapd %xmm11, -0x190(%rsp,%rax,8)
subpd %xmm12, %xmm10
movapd %xmm10, -0x90(%rsp,%rax,8)
addpd %xmm8, %xmm9
mulpd %xmm0, %xmm9
movapd %xmm8, %xmm10
addpd %xmm9, %xmm10
movapd %xmm10, -0x110(%rsp,%rax,8)
subpd %xmm9, %xmm8
movapd %xmm8, -0x10(%rsp,%rax,8)
addpd %xmm7, %xmm6
addpd %xmm4, %xmm7
addpd %xmm5, %xmm4
movapd %xmm6, %xmm8
subpd %xmm4, %xmm8
mulpd %xmm1, %xmm8
mulpd %xmm2, %xmm6
addpd %xmm8, %xmm6
mulpd %xmm3, %xmm4
addpd %xmm8, %xmm4
mulpd %xmm0, %xmm7
movapd %xmm5, %xmm8
addpd %xmm7, %xmm8
subpd %xmm7, %xmm5
movapd %xmm5, %xmm7
addpd %xmm6, %xmm7
movapd %xmm7, -0x50(%rsp,%rax,8)
subpd %xmm6, %xmm5
movapd %xmm5, -0xd0(%rsp,%rax,8)
movapd %xmm8, %xmm5
addpd %xmm4, %xmm5
movapd %xmm5, -0x150(%rsp,%rax,8)
subpd %xmm4, %xmm8
movapd %xmm8, 0x30(%rsp,%rax,8)
addq $0x2, %rax
cmpq $0x40, %rax
jne 0x3df5
xorl %edi, %edi
movapd 0x1321(%rip), %xmm0     # 0x5290
movdqa 0x1329(%rip), %xmm1     # 0x52a0
leaq 0x56552(%rip), %rax     # 0x5a4d0
movupd (%rsi,%rdi,4), %xmm2
mulpd 0x30(%rsp,%rdi,4), %xmm2
addpd %xmm0, %xmm2
cvttpd2dq %xmm2, %xmm2
pshuflw $0xe8, %xmm2, %xmm2     # xmm2 = xmm2[0,2,2,3,4,5,6,7]
paddw %xmm1, %xmm2
movd %xmm2, (%rdi,%rax)
addq $0x4, %rdi
cmpq $0x80, %rdi
jne 0x3f7e
xorl %esi, %esi
leaq 0x134b(%rip), %rdi      # 0x5300
leaq 0x56594(%rip), %r15     # 0x5a550
movzwl (%rax), %r8d
movzbl (%rsi,%rdi), %r9d
movw %r8w, (%r15,%r9,2)
incq %rsi
addq $0x2, %rax
cmpq $0x40, %rsi
jne 0x3fbc
movq 0x565f2(%rip), %rax     # 0x5a5d0
movb 0x30e1(%rip), %r9b      # 0x70c6
movq 0x3154(%rip), %rsi      # 0x7140
movq (%rsi), %rdi
addq $0x8, %rdi
xorl %r8d, %r8d
cmpq %rdi, %rax
jae 0x4050
movzwl (%r8,%r15), %r10d
cmpw $0x2, %r10w
jl 0x4043
movq 0x8(%rsi), %r11
testb %r9b, (%r11,%rax)
je 0x4017
orl $0x1, %r10d
jmp 0x401e
andl $0x7ffe, %r10d          # imm = 0x7FFE
movw %r10w, (%r8,%r15)
addb %r9b, %r9b
movb %r9b, 0x3099(%rip)      # 0x70c6
jne 0x4043
movb $0x1, 0x3090(%rip)      # 0x70c6
incq %rax
movq %rax, 0x56590(%rip)     # 0x5a5d0
movb $0x1, %r9b
addq $0x2, %r8
cmpq $0x80, %r8
jne 0x3ff6
movzwl 0x564f9(%rip), %eax     # 0x5a550
movzwl (%rdx), %esi
movw %ax, (%rdx)
cmpw %si, %ax
je 0x4089
subl %esi, %eax
movq 0x13b85(%rip), %rdx     # 0x17bf0
movswq %ax, %rbx
movzbl (%rdx,%rbx), %eax
movl (%rcx,%rax,4), %edi
callq 0x3b11
shlq $0x2, %rbx
addq 0x53b7a(%rip), %rbx     # 0x57c00
movq %rbx, %rcx
movl (%rcx), %edi
callq 0x3b11
movb $0x3f, %al
movzbl %al, %r13d
cmpw $0x0, (%r15,%r13,2)
jne 0x40a7
decb %al
jne 0x4092
jmp 0x418e
movb %al, 0xf(%rsp)
movl %r14d, 0x14(%rsp)
movl %ebp, 0x10(%rsp)
movl $0x1, %ebx
movslq %ebx, %r14
decq %r14
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
movl %ecx, %r12d
movzwl 0x2(%r15,%r14,2), %eax
incq %r14
incl %ebp
cmpq %r13, %r14
jg 0x40e3
leal 0x1(%r12), %ecx
testw %ax, %ax
je 0x40c6
leaq (%r14,%r14), %rdx
movl %ebp, %ecx
cmpl $0x10, %ebp
jl 0x4128
movq %rdx, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
shrl $0x4, %r12d
cmpl $0x1, %r12d
adcl $0x0, %r12d
movl 0x14(%rsp), %ebx
movl %ebx, %edi
callq 0x3b11
decl %r12d
jne 0x4108
movq 0x18(%rsp), %rdx
movzwl (%rdx,%r15), %eax
movl %ebp, %ecx
andl $0xf, %ecx
movq 0x20(%rsp), %rbx
shll $0x4, %ecx
movq %rdx, %r12
movq 0x13abb(%rip), %rdx     # 0x17bf0
movswq %ax, %rax
movzbl (%rdx,%rax), %eax
movslq %ecx, %rcx
addq %rax, %rcx
movq 0x28(%rsp), %rax
movl (%rax,%rcx,4), %edi
callq 0x3b11
movq 0x53aa9(%rip), %rax     # 0x57c00
movswq (%r12,%r15), %rcx
movl (%rax,%rcx,4), %edi
callq 0x3b11
addl %ebp, %ebx
incl %ebx
cmpq %r13, %r14
jl 0x40b9
cmpb $0x3f, 0xf(%rsp)
movl 0x10(%rsp), %ebp
jne 0x418e
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebp, %edi
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3b11