// File: /workspace/github_c_makefile_star2_O1/albinoloverats[P]hide/src/jpeg-load.c
// Function: DecodeSingleBlock
static void DecodeSingleBlock(stComponent *comp, uint8_t *outputBuf, int stride)
{
	int16_t *inptr = comp->m_DCT;
	double *quantptr = comp->m_qTable;

	// Create a temp 8x8, i.e. 64 array for the data
	int data[64] = { 0x0 };

	// Copy our data into the temp array
	for (int i = 0; i < 64; i++)
		data[i] = inptr[i];


	static uint64_t offset = 0;
	static uint8_t bit = 1;
	static bool aloc = false;

	for (int i = 0; i < 64 && !(aloc && offset >= message->size + sizeof message->size); i++)
		if (data[i] > 1)
		{
			switch (action)
			{
				case JPEG_LOAD_FIND:
				{
					uint8_t *ptr = (aloc ? message->data : (uint8_t *)&message->size) + offset;
					if (data[i] & 0x01)
						*ptr |= (0xFF & bit);
					bit <<= 1;
					if (!bit)
					{
						bit = 1;
						offset++;
					}
					if (offset >= sizeof message->size && !aloc)
					{
						message->size = fill_size ? : ntohll(message->size);
						message->data = calloc(message->size + sizeof message->size, sizeof (uint8_t));
						aloc = true;
					}
					break;
				}
				case JPEG_LOAD_READ:
					message->size++;
					break;
			}
		}


	// De-Quantize
	DequantizeBlock(data, quantptr);

	// De-Zig-Zag
	int block[64] = { 0x0 };
	DeZigZag(block, data);

	// Create an 8x8 array
	int arrayBlock[8][8] = { { 0x0 } };
	TransformArray(arrayBlock, block);

	// Inverse DCT
	int val[8][8] = { { 0x0 } };
	PerformIDCT(val, arrayBlock);

	// Level Shift each element (i.e. add 128), and copy to our output
	uint8_t *outptr = outputBuf;
	for (int y = 0; y < 8; y++)
	{
		for (int x = 0; x < 8; x++)
		{
			val[x][y] += 128;
			outptr[x] = byte_limit(val[x][y], 0);
		}
		outptr += stride;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x448, %rsp            # imm = 0x448
movl %edx, 0xc(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %r14
movq 0x8(%rdi), %r15
leaq 0x40(%rsp), %rdi
xorl %ebx, %ebx
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x1090
movswl 0x20(%r14,%rbx,2), %eax
movl %eax, 0x40(%rsp,%rbx,4)
incq %rbx
cmpq $0x40, %rbx
jne 0x28a0
movb 0x4877(%rip), %al       # 0x7130
movq 0x4868(%rip), %r14      # 0x7128
movb 0x47e9(%rip), %r13b     # 0x70b0
movl 0x4842(%rip), %r12d     # 0x7110
movq 0x4843(%rip), %rcx      # 0x7118
movq %rcx, (%rsp)
xorl %ebp, %ebp
testb $0x1, %al
je 0x28f6
movq 0x4822(%rip), %rcx      # 0x7108
movq (%rcx), %rcx
addq $0x8, %rcx
cmpq %rcx, %r14
jae 0x29bf
movl 0x40(%rsp,%rbp,4), %ecx
cmpl $0x2, %ecx
jl 0x29b2
testl %r12d, %r12d
je 0x29a8
cmpl $0x1, %r12d
jne 0x29b2
testb $0x1, %cl
je 0x293a
movq 0x47e6(%rip), %rcx      # 0x7108
addq $0x8, %rcx
testb $0x1, %al
leaq 0x47d9(%rip), %rdx      # 0x7108
cmoveq %rdx, %rcx
movq (%rcx), %rcx
orb %r13b, (%rcx,%r14)
addb %r13b, %r13b
movb %r13b, 0x476c(%rip)     # 0x70b0
jne 0x295a
movb $0x1, 0x4763(%rip)      # 0x70b0
incq %r14
movq %r14, 0x47d1(%rip)      # 0x7128
movb $0x1, %r13b
cmpq $0x8, %r14
setb %cl
orb %al, %cl
testb $0x1, %cl
jne 0x29b2
movq (%rsp), %rax
movq %rax, %rdi
testq %rax, %rax
jne 0x2981
movq 0x478d(%rip), %rax      # 0x7108
movq (%rax), %rdi
bswapq %rdi
movq 0x4780(%rip), %rbx      # 0x7108
movq %rdi, (%rbx)
addq $0x8, %rdi
movl $0x1, %esi
callq 0x10c0
movq %rax, 0x8(%rbx)
movb $0x1, 0x478c(%rip)      # 0x7130
movb $0x1, %al
jmp 0x29b2
movq 0x4759(%rip), %rcx      # 0x7108
incq (%rcx)
incq %rbp
cmpq $0x40, %rbp
jne 0x28db
xorl %eax, %eax
xorps %xmm0, %xmm0
cvtsi2sdl 0x40(%rsp,%rax,4), %xmm0
mulsd (%r15,%rax,8), %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0x40(%rsp,%rax,4)
incq %rax
cmpq $0x40, %rax
jne 0x29c1
leaq 0x240(%rsp), %rdi
xorl %ebx, %ebx
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x1090
leaq 0x2682(%rip), %rax      # 0x5080
movslq (%rbx,%rax), %rcx
movl 0x40(%rsp,%rcx,4), %ecx
movl %ecx, 0x240(%rsp,%rbx)
addq $0x4, %rbx
cmpq $0x100, %rbx            # imm = 0x100
jne 0x29fe
leaq 0x340(%rsp), %r14
xorl %ebx, %ebx
movl $0x100, %edx            # imm = 0x100
movq %r14, %rdi
xorl %esi, %esi
callq 0x1090
xorl %eax, %eax
cltq
xorl %ecx, %ecx
movl 0x240(%rsp,%rax,4), %edx
movl %edx, (%r14,%rcx,8)
incq %rax
addq $0x4, %rcx
cmpq $0x20, %rcx
jne 0x2a39
incq %rbx
addq $0x4, %r14
cmpq $0x8, %rbx
jne 0x2a35
leaq 0x140(%rsp), %rdi
xorl %ebx, %ebx
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x1090
leaq 0x2705(%rip), %r13      # 0x5180
leal 0x1(,%rbx,2), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x25c7(%rip), %xmm0     # 0x5058
movsd %xmm0, 0x28(%rsp)
xorl %ebp, %ebp
movq %rbx, 0x18(%rsp)
leal 0x1(,%rbp,2), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x25a4(%rip), %xmm0     # 0x5058
movsd %xmm0, 0x20(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
leaq 0x340(%rsp), %r12
xorl %ebx, %ebx
xorl %r14d, %r14d
testq %rbx, %rbx
sete %r14b
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
mulsd 0x20(%rsp), %xmm0
callq 0x10a0
movsd %xmm0, 0x38(%rsp)
movsd (%r13,%r14,8), %xmm0
movsd %xmm0, 0x30(%rsp)
xorl %r14d, %r14d
movsd (%rsp), %xmm0
movsd %xmm0, (%rsp)
xorl %r15d, %r15d
testq %r14, %r14
sete %r15b
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
mulsd 0x28(%rsp), %xmm0
callq 0x10a0
movsd (%r13,%r15,8), %xmm1
mulsd 0x30(%rsp), %xmm1
xorps %xmm2, %xmm2
cvtsi2sdl (%r12,%r14,4), %xmm2
mulsd %xmm1, %xmm2
mulsd 0x38(%rsp), %xmm2
mulsd %xmm0, %xmm2
movsd (%rsp), %xmm0
addsd %xmm2, %xmm0
incq %r14
cmpq $0x8, %r14
jne 0x2b04
movsd %xmm0, (%rsp)
incq %rbx
addq $0x20, %r12
cmpq $0x8, %rbx
jne 0x2acd
cvttsd2si (%rsp), %eax
sarl $0x2, %eax
movq %rbp, %rcx
shlq $0x5, %rcx
addq %rsp, %rcx
addq $0x140, %rcx            # imm = 0x140
movq 0x18(%rsp), %rbx
movl %eax, (%rcx,%rbx,4)
incq %rbp
cmpq $0x8, %rbp
jne 0x2a9e
incq %rbx
cmpq $0x8, %rbx
jne 0x2a7b
movslq 0xc(%rsp), %rax
xorl %ecx, %ecx
movl $0xff, %edx
xorl %esi, %esi
movq 0x10(%rsp), %r11
leaq 0x140(%rsp), %rbx
movq %rbx, %rdi
xorl %r8d, %r8d
movl (%rdi), %r9d
leal 0x80(%r9), %r10d
cmpl $0x7f, %r9d
cmovgl %edx, %r10d
subl $-0x80, %r9d
movl %r9d, (%rdi)
cmovll %ecx, %r10d
movb %r10b, (%r11,%r8)
incq %r8
addq $0x20, %rdi
cmpq $0x8, %r8
jne 0x2bce
addq %rax, %r11
incq %rsi
addq $0x4, %rbx
cmpq $0x8, %rsi
jne 0x2bc8
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop