// File: /workspace/github_c_makefile_star2_O2/albinoloverats[P]hide/src/jpeg-load.c
// Function: ProcessHuffmanDataUnit
static void ProcessHuffmanDataUnit(stJpegData *jdata, int indx)
{
	stComponent *c = &jdata->m_component_info[indx];

	// Start Huffman decoding
	int16_t DCT_tcoeff[64] = { 0x0 };

	bool found = false;
	int decodedValue = 0;

	// Scan Decode Resync
	if (jdata->m_restart_interval > 0)
		if (jdata->m_stream[0] == 0xff && jdata->m_stream[1] != 0x00)
		{
			// Something might be wrong, we should have had an interval marker set
			jdata->m_stream += 2;
			g_reservoir = 0;
			g_nbits_in_reservoir = 0;

			// The value in the interval marker determines what number it will count
			// upto before looping back...i.e, for an interval marker of 4, we will
			// have 0xFFD0, 0xFFD1, 0xFFD2, 0xFFD3, 0xFFD4, 0xFFD0, 0xFFD1..etc..looping
			// so each time we get a resync, it will count from 0 to 4 then restart :)
		}

	// First thing is get the 1 DC coefficient at the start of our 64 element block
	for (int k = 1; k < 16; k++)
	{
		// Keep grabbing one bit at a time till we find one thats a huffman code
		int code = LookNBits(&jdata->m_stream, k);

		// Check if its one of our huffman codes
		if (IsInHuffmanCodes(code, k, c->m_dcTable->m_numBlocks, c->m_dcTable->m_blocks, &decodedValue))
		{
			// Skip over the rest of the bits now.
			SkipNBits(&jdata->m_stream, k);

			found = true;

			// The decoded value is the number of bits we have to read in next
			int numDataBits = decodedValue;
			// We know the next k bits are for the actual data
			if (numDataBits == 0)
				DCT_tcoeff[0] = c->m_previousDC;
			else
			{
				if (jdata->m_restart_interval > 0)
					if (jdata->m_stream[0] == 0xff && jdata->m_stream[1] != 0x00)
					{
						jdata->m_stream += 2;
						g_reservoir = 0;
						g_nbits_in_reservoir = 0;
					}

				int16_t data = GetNBits(&jdata->m_stream, numDataBits);
				data = (int16_t)DetermineSign(data, numDataBits);
				DCT_tcoeff[0] = data + c->m_previousDC;
				c->m_previousDC = DCT_tcoeff[0];
			}
			// Found so we can exit out
			break;
		}
	}

	if (!found)
	{
		fprintf(stderr, "Fail @ %s:%d\n", __FILE__, __LINE__);
		exit(-1);
	}

	// Second, the 63 AC coefficient
	int nr = 1;
	bool EOB_found = false;
	while ((nr <= 63) && (!EOB_found))
	{
		int k = 0;
		for (k = 1; k <= 16; k++)
		{
			// Keep grabbing one bit at a time till we find one thats a huffman code
			int code = LookNBits(&jdata->m_stream, k);

			// Check if its one of our huffman codes
			if (IsInHuffmanCodes(code, k, c->m_acTable->m_numBlocks, c->m_acTable->m_blocks, &decodedValue))
			{
				// Skip over k bits, since we found the huffman value
				SkipNBits(&jdata->m_stream, k);

				// Our decoded value is broken down into 2 parts, repeating RLE, and then
				// the number of bits that make up the actual value next
				int valCode = decodedValue;

				uint8_t size_val = valCode & 0xF; // Number of bits for our data
				uint8_t count_0 = valCode >> 4; // Number RunLengthZeros

				if (size_val == 0)
				{ // RLE
					if (count_0 == 0)
						EOB_found = true; // EOB found, go out
					else if (count_0 == 0xF)
						nr += 16; // skip 16 zeros
				}
				else
				{
					nr += count_0; //skip count_0 zeroes
					if (nr > 63)
					{
						fprintf(stderr, "Fail @ %s:%d\n", __FILE__, __LINE__);
						exit(-1);
					}

					int16_t data = GetNBits(&jdata->m_stream, size_val);
					data = (int16_t)DetermineSign(data, size_val);
					DCT_tcoeff[nr++] = data;
				}
				break;
			}
		}

		if (k > 16)
			nr++;
	}

	// We've decoded a block of data, so copy it across to our buffer
	for (int j = 0; j < 64; j++)
		c->m_DCT[j] = DCT_tcoeff[j];
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %r14
movl %esi, %eax
imulq $0xa8, %rax, %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
andl $0x0, 0xc(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
cmpl $0x0, 0x18(%rdi)
jle 0x2126
movq 0x10(%r14), %rcx
cmpb $-0x1, (%rcx)
jne 0x2126
cmpb $0x0, 0x1(%rcx)
je 0x2126
addq $0x2, %rcx
movq %rcx, 0x10(%r14)
andl $0x0, 0x4001(%rip)      # 0x6120
andl $0x0, 0x3ffe(%rip)      # 0x6124
leaq (%r14,%rax), %r12
addq $0x20, %r12
leaq 0x10(%r14), %rbx
pushq $0x1
popq %rbp
leaq 0xc(%rsp), %r15
cmpl $0x10, %ebp
je 0x23e5
movq %rbx, %rdi
movl %ebp, %esi
callq 0x27f3
movq 0x18(%r12), %rcx
movl 0x114(%rcx), %edx
addq $0x118, %rcx            # imm = 0x118
movl %eax, %edi
movl %ebp, %esi
movq %r15, %r8
callq 0x2841
testb %al, %al
jne 0x2173
incl %ebp
jmp 0x213a
movl 0x3fab(%rip), %edx      # 0x6124
movl 0x3fa1(%rip), %eax      # 0x6120
movl %edx, %ecx
subl %ebp, %ecx
cmpl %ebp, %edx
jae 0x21c2
movq (%rbx), %rdi
leaq 0x1(%rdi), %r8
movq %r8, (%rbx)
movzbl (%rdi), %esi
cmpl $0xff, %esi
jne 0x21a9
cmpb $0x0, (%r8)
jne 0x21a9
addq $0x2, %rdi
movq %rdi, (%rbx)
shll $0x8, %eax
orl %esi, %eax
movl %eax, 0x3f6c(%rip)      # 0x6120
addl $0x8, %edx
movl %edx, 0x3f67(%rip)      # 0x6124
addl $0x8, %ecx
jmp 0x2183
movl %ecx, 0x3f5c(%rip)      # 0x6124
pushq $-0x1
popq %r13
movl %r13d, %edx
shll %cl, %edx
notl %edx
andl %edx, %eax
movl %eax, 0x3f45(%rip)      # 0x6120
movl 0xc(%rsp), %r15d
testq %r15, %r15
je 0x2250
cmpl $0x0, 0x18(%r14)
jle 0x220f
movq (%rbx), %rax
cmpb $-0x1, (%rax)
jne 0x220f
cmpb $0x0, 0x1(%rax)
je 0x220f
addq $0x2, %rax
movq %rax, (%rbx)
andl $0x0, 0x3f18(%rip)      # 0x6120
andl $0x0, 0x3f15(%rip)      # 0x6124
movq %rbx, %rdi
movl %r15d, %esi
callq 0x287f
leal -0x1(%r15), %ecx
pushq $0x1
popq %rdx
shll %cl, %edx
pushq $-0x1
popq %rsi
movl %r15d, %ecx
shlq %cl, %rsi
movswl %ax, %edi
incl %esi
xorl %ecx, %ecx
cmpl %edi, %edx
cmovgl %esi, %ecx
addl %eax, %ecx
addw 0xa4(%r12), %cx
movswl %cx, %eax
movl %eax, 0xa4(%r12)
jmp 0x2259
movzwl 0xa4(%r12), %ecx
movw %cx, 0x10(%rsp)
pushq $0x1
popq %rax
xorl %ecx, %ecx
leaq 0xc(%rsp), %r14
cmpl $0x3f, %eax
jg 0x23bb
testb $0x1, %cl
jne 0x23bb
movl %eax, 0x8(%rsp)
movl %r13d, %r15d
pushq $0x1
popq %rbp
cmpl $0x11, %ebp
je 0x22bc
movq %rbx, %rdi
movl %ebp, %esi
callq 0x27f3
movq 0x10(%r12), %rcx
movl 0x114(%rcx), %edx
addq $0x118, %rcx            # imm = 0x118
movl %eax, %edi
movl %ebp, %esi
movq %r14, %r8
callq 0x2841
testb %al, %al
jne 0x22c7
incl %ebp
decl %r15d
jmp 0x2284
xorl %ecx, %ecx
movl 0x8(%rsp), %eax
jmp 0x23a9
movl 0x3e57(%rip), %ecx      # 0x6124
movl 0x3e4d(%rip), %eax      # 0x6120
addl %ecx, %r15d
cmpl %ebp, %ecx
jae 0x2315
movq (%rbx), %rsi
leaq 0x1(%rsi), %rdi
movq %rdi, (%rbx)
movzbl (%rsi), %edx
cmpl $0xff, %edx
jne 0x22fb
cmpb $0x0, (%rdi)
jne 0x22fb
addq $0x2, %rsi
movq %rsi, (%rbx)
shll $0x8, %eax
orl %edx, %eax
movl %eax, 0x3e1a(%rip)      # 0x6120
addl $0x8, %ecx
movl %ecx, 0x3e15(%rip)      # 0x6124
addl $0x8, %r15d
jmp 0x22d6
movl %r13d, %edx
movl %r15d, %ecx
shll %cl, %edx
movl %r15d, 0x3e00(%rip)     # 0x6124
notl %edx
andl %edx, %eax
movl %eax, 0x3df2(%rip)      # 0x6120
movl 0xc(%rsp), %r15d
movl %r15d, %eax
shrl $0x4, %eax
andl $0xf, %r15d
je 0x2394
movzbl %al, %r13d
addl 0x8(%rsp), %r13d
cmpl $0x40, %r13d
jge 0x2404
movq %rbx, %rdi
movl %r15d, %esi
callq 0x287f
leal -0x1(%r15), %ecx
pushq $0x1
popq %rdx
shll %cl, %edx
pushq $-0x1
popq %r8
movl %r8d, %esi
movl %r15d, %ecx
shll %cl, %esi
movswl %ax, %edi
orl $0x1, %esi
xorl %ecx, %ecx
cmpl %edi, %edx
cmovlel %ecx, %esi
addl %eax, %esi
movslq %r13d, %rax
incl %r13d
movw %si, 0x10(%rsp,%rax,2)
movl %r13d, %eax
movl %r8d, %r13d
jmp 0x23a9
testb %al, %al
je 0x23b4
movzbl %al, %eax
cmpl $0xf, %eax
movl 0x8(%rsp), %eax
jne 0x23a7
addl $0x10, %eax
xorl %ecx, %ecx
cmpl $0x11, %ebp
sbbl $-0x1, %eax
jmp 0x2268
movb $0x1, %cl
jmp 0x22be
xorl %eax, %eax
cmpq $0x40, %rax
je 0x23d3
movzwl 0x10(%rsp,%rax,2), %ecx
movw %cx, 0x20(%r12,%rax,2)
incq %rax
jmp 0x23bd
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3c0c(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1d9a(%rip), %rsi      # 0x4190
leaq 0x1da1(%rip), %rdx      # 0x419e
movl $0x2cd, %ecx            # imm = 0x2CD
jmp 0x2421
movq 0x3bed(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1d7b(%rip), %rsi      # 0x4190
leaq 0x1d82(%rip), %rdx      # 0x419e
movl $0x2f5, %ecx            # imm = 0x2F5
xorl %eax, %eax
callq 0x10d0
pushq $-0x1
popq %rdi
callq 0x1150