// File: /workspace/github_c_makefile_star2_O1/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
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)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movl 0x18(%rdi), %r8d
testl %r8d, %r8d
jle 0x236b
movq 0x10(%rdi), %rcx
cmpb $-0x1, (%rcx)
jne 0x236b
cmpb $0x0, 0x1(%rcx)
je 0x236b
addq $0x2, %rcx
movq %rcx, 0x10(%rdi)
xorl %ecx, %ecx
movl %ecx, 0x4dbb(%rip)      # 0x7120
movl %ecx, 0x4db9(%rip)      # 0x7124
addq %rdi, %rax
addq $0x20, %rax
movl 0x4dac(%rip), %esi      # 0x7124
movl $0x1, %r10d
movl 0x4d9b(%rip), %r11d     # 0x7120
xorl %ebp, %ebp
xorl %edx, %edx
cmpl %r10d, %esi
jae 0x23d6
movq 0x10(%rdi), %rcx
leaq 0x1(%rcx), %rbx
movq %rbx, 0x10(%rdi)
movzbl (%rcx), %r14d
cmpl $0xff, %r14d
jne 0x23b7
cmpb $0x0, (%rbx)
jne 0x23b7
addq $0x2, %rcx
movq %rcx, 0x10(%rdi)
movq %rcx, %rbx
shll $0x8, %r11d
orl %r14d, %r11d
addl $0x8, %esi
movq %rbx, %rcx
cmpl %r10d, %esi
jb 0x2392
movl %r11d, 0x4d50(%rip)     # 0x7120
movl %esi, 0x4d4e(%rip)      # 0x7124
movl %esi, %ecx
subl %r10d, %ecx
movl %r11d, %r14d
shrl %cl, %r14d
movq 0x18(%rax), %rcx
movslq 0x114(%rcx), %r15
testq %r15, %r15
setg %bl
jle 0x2437
movq %r15, %r12
negq %r12
addq $0x120, %rcx            # imm = 0x120
movb $0x1, %bl
movl $0x1, %r13d
movzwl (%rcx), %r9d
cmpl %r9d, %r14d
jne 0x2418
cmpl %r10d, -0x4(%rcx)
je 0x2434
cmpq %r15, %r13
setb %bl
leaq (%r12,%r13), %r9
incq %r9
incq %r13
addq $0xc, %rcx
cmpq $0x1, %r9
jne 0x2409
jmp 0x2437
movl -0x8(%rcx), %edx
testb $0x1, %bl
je 0x25ad
cmpl %r10d, %esi
jae 0x248c
movq 0x10(%rdi), %rcx
leaq 0x1(%rcx), %r14
movq %r14, 0x10(%rdi)
movzbl (%rcx), %ebp
cmpl $0xff, %ebp
jne 0x246d
cmpb $0x0, (%r14)
jne 0x246d
addq $0x2, %rcx
movq %rcx, 0x10(%rdi)
movq %rcx, %r14
shll $0x8, %r11d
orl %ebp, %r11d
addl $0x8, %esi
movq %r14, %rcx
cmpl %r10d, %esi
jb 0x2449
movl %r11d, 0x4c9a(%rip)     # 0x7120
movl %esi, 0x4c98(%rip)      # 0x7124
movl %r11d, %r9d
subl %r10d, %esi
movl %esi, 0x4c8c(%rip)      # 0x7124
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movl %esi, %ecx
shll %cl, %r11d
notl %r11d
andl %r9d, %r11d
movl %r11d, 0x4c70(%rip)     # 0x7120
testl %edx, %edx
je 0x259e
testl %r8d, %r8d
jle 0x24e7
movq 0x10(%rdi), %rcx
cmpb $-0x1, (%rcx)
jne 0x24e7
cmpb $0x0, 0x1(%rcx)
je 0x24e7
addq $0x2, %rcx
movq %rcx, 0x10(%rdi)
xorl %r11d, %r11d
movl %r11d, 0x4c42(%rip)     # 0x7120
movl %r11d, 0x4c3f(%rip)     # 0x7124
xorl %esi, %esi
cmpl %edx, %esi
jae 0x2531
movq 0x10(%rdi), %rcx
leaq 0x1(%rcx), %r14
movq %r14, 0x10(%rdi)
movzbl (%rcx), %ebp
cmpl $0xff, %ebp
jne 0x2513
cmpb $0x0, (%r14)
jne 0x2513
addq $0x2, %rcx
movq %rcx, 0x10(%rdi)
movq %rcx, %r14
shll $0x8, %r11d
orl %ebp, %r11d
addl $0x8, %esi
movq %r14, %rcx
cmpl %edx, %esi
jb 0x24ef
movl %r11d, 0x4bf5(%rip)     # 0x7120
movl %esi, 0x4bf3(%rip)      # 0x7124
subl %edx, %esi
movl %r11d, %ebp
movl %esi, %ecx
shrl %cl, %ebp
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
shll %cl, %r9d
movl %esi, 0x4bdb(%rip)      # 0x7124
notl %r9d
andl %r9d, %r11d
movswl %bp, %r9d
leal -0x1(%rdx), %ecx
movl $0x1, %r14d
shll %cl, %r14d
movq $-0x1, %r15
movl %edx, %ecx
shlq %cl, %r15
incl %r15d
cmpl %r9d, %r14d
movl $0x0, %ecx
cmovlel %ecx, %r15d
movl %r11d, 0x4b9f(%rip)     # 0x7120
addl %ebp, %r15d
addw 0xa4(%rax), %r15w
movw %r15w, 0x10(%rsp)
movswl %r15w, %ecx
movl %ecx, 0xa4(%rax)
jmp 0x25aa
movzwl 0xa4(%rax), %ecx
movw %cx, 0x10(%rsp)
movb $0x1, %bpl
incl %r10d
cmpl $0x10, %r10d
sete %cl
orb %cl, %bl
testb $0x1, %bl
je 0x2389
testb $0x1, %bpl
je 0x2867
movl 0x4b4e(%rip), %ebx      # 0x7120
movl $0x1, %ebp
movl 0x4b47(%rip), %esi      # 0x7124
movl $0x1, %r11d
movl $0x0, 0xc(%rsp)
movl %ebx, %r8d
cmpl %r11d, %esi
jae 0x263d
movq 0x10(%rdi), %rcx
leaq 0x1(%rcx), %r9
movq %r9, 0x10(%rdi)
movzbl (%rcx), %ebx
cmpl $0xff, %ebx
jne 0x261b
cmpb $0x0, (%r9)
jne 0x261b
addq $0x2, %rcx
movq %rcx, 0x10(%rdi)
movq %rcx, %r9
shll $0x8, %r8d
orl %ebx, %r8d
addl $0x8, %esi
movq %r9, %rcx
cmpl %r11d, %esi
jb 0x25f7
movl %r8d, 0x4aec(%rip)      # 0x7120
movl %esi, 0x4aea(%rip)      # 0x7124
movl %r8d, %ebx
movl %esi, %ecx
subl %r11d, %ecx
movl %r8d, %r15d
shrl %cl, %r15d
movq 0x10(%rax), %rcx
movslq 0x114(%rcx), %r12
testq %r12, %r12
setg %r14b
jle 0x26a1
movq %r12, %r13
negq %r13
addq $0x120, %rcx            # imm = 0x120
movb $0x1, %r14b
movl $0x1, %r9d
movzwl (%rcx), %r10d
cmpl %r10d, %r15d
jne 0x2681
cmpl %r11d, -0x4(%rcx)
je 0x269e
cmpq %r12, %r9
setb %r14b
leaq (%r9,%r13), %r10
incq %r10
incq %r9
addq $0xc, %rcx
cmpq $0x1, %r10
jne 0x2672
jmp 0x26a1
movl -0x8(%rcx), %edx
testb $0x1, %r14b
je 0x2801
cmpl %r11d, %esi
jae 0x26f7
movq 0x10(%rdi), %rcx
leaq 0x1(%rcx), %r9
movq %r9, 0x10(%rdi)
movzbl (%rcx), %ebx
cmpl $0xff, %ebx
jne 0x26d8
cmpb $0x0, (%r9)
jne 0x26d8
addq $0x2, %rcx
movq %rcx, 0x10(%rdi)
movq %rcx, %r9
shll $0x8, %r8d
orl %ebx, %r8d
addl $0x8, %esi
movq %r9, %rcx
cmpl %r11d, %esi
jb 0x26b4
movl %r8d, 0x4a2f(%rip)      # 0x7120
movl %esi, 0x4a2d(%rip)      # 0x7124
subl %r11d, %esi
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %esi, %ecx
shll %cl, %ebx
movl %esi, 0x4a1b(%rip)      # 0x7124
notl %ebx
andl %r8d, %ebx
movl %ebx, 0x4a0c(%rip)      # 0x7120
movl %edx, %ecx
shrl $0x4, %ecx
movl %edx, %r8d
andl $0xf, %r8d
je 0x27e7
movzbl %cl, %r15d
addl %ebp, %r15d
cmpl $0x40, %r15d
jge 0x2862
cmpl %r8d, %esi
jae 0x2780
movq 0x10(%rdi), %rcx
leaq 0x1(%rcx), %r9
movq %r9, 0x10(%rdi)
movzbl (%rcx), %ebp
cmpl $0xff, %ebp
jne 0x2764
cmpb $0x0, (%r9)
jne 0x2764
addq $0x2, %rcx
movq %rcx, 0x10(%rdi)
movq %rcx, %r9
shll $0x8, %ebx
orl %ebp, %ebx
addl $0x8, %esi
movq %r9, %rcx
cmpl %r8d, %esi
jb 0x2740
movl %ebx, 0x49a6(%rip)      # 0x7120
movl %esi, 0x49a4(%rip)      # 0x7124
subl %r8d, %esi
movl %ebx, %ebp
movl %esi, %ecx
shrl %cl, %ebp
movl %esi, 0x4995(%rip)      # 0x7124
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
shll %cl, %r9d
notl %r9d
andl %r9d, %ebx
leal -0x1(%r8), %ecx
movl $0x1, %r9d
shll %cl, %r9d
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movl %r8d, %ecx
shll %cl, %r10d
movl %ebx, 0x4963(%rip)      # 0x7120
movswl %bp, %ecx
orl $0x1, %r10d
cmpl %ecx, %r9d
movl $0x0, %ecx
cmovlel %ecx, %r10d
addl %ebp, %r10d
movslq %r15d, %rcx
incl %r15d
movw %r10w, 0x10(%rsp,%rcx,2)
movl %ebx, %r8d
movl %r15d, %ebp
jmp 0x2801
testb %cl, %cl
je 0x27f8
movzbl %cl, %ecx
cmpl $0xf, %ecx
jne 0x27fe
addl $0x10, %ebp
jmp 0x27fe
movb $0x1, %cl
movl %ecx, 0xc(%rsp)
movl %ebx, %r8d
testb $0x1, %r14b
jne 0x281a
incl %r11d
cmpl $0x11, %r11d
jne 0x25ee
movl $0x11, %r11d
cmpl $0x11, %r11d
sbbl $-0x1, %ebp
cmpl $0x3f, %ebp
jg 0x283a
movl $0x1, %r11d
movl %ebx, %r8d
testb $0x1, 0xc(%rsp)
je 0x25ee
addq $0x20, %rax
leaq 0x10(%rsp), %rsi
movl $0x80, %edx
movq %rax, %rdi
callq 0x1100
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x134f
callq 0x1322