// File: /workspace/github_c_makefile_star2_O3/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 $0x88, %rsp
movl %esi, %eax
imulq $0xa8, %rax, %rax
xorps %xmm0, %xmm0
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)
movaps %xmm0, (%rsp)
movl 0x18(%rdi), %r9d
testl %r9d, %r9d
jle 0x2339
movq 0x10(%rdi), %rcx
cmpb $-0x1, (%rcx)
jne 0x2339
cmpb $0x0, 0x1(%rcx)
je 0x2339
addq $0x2, %rcx
movq %rcx, 0x10(%rdi)
xorl %ecx, %ecx
movl %ecx, 0x4ded(%rip)      # 0x7120
movl %ecx, 0x4deb(%rip)      # 0x7124
addq %rdi, %rax
addq $0x20, %rax
movl 0x4ddd(%rip), %r8d      # 0x7124
movl $0x1, %r11d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl 0x4dc7(%rip), %r10d     # 0x7120
cmpl %r11d, %r8d
jae 0x23a6
movq 0x10(%rdi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x10(%rdi)
movzbl (%rcx), %esi
cmpl $0xff, %esi
jne 0x2385
cmpb $0x0, (%rdx)
jne 0x2385
addq $0x2, %rcx
movq %rcx, 0x10(%rdi)
movq %rcx, %rdx
shll $0x8, %r10d
orl %esi, %r10d
addl $0x8, %r8d
movq %rdx, %rcx
cmpl %r11d, %r8d
jb 0x2362
movl %r10d, 0x4d81(%rip)     # 0x7120
movl %r8d, 0x4d7e(%rip)      # 0x7124
movl %r8d, %edx
subl %r11d, %edx
movl %r10d, %esi
movl %edx, %ecx
shrl %cl, %esi
movq 0x18(%rax), %rcx
movslq 0x114(%rcx), %r14
testq %r14, %r14
jle 0x240b
movq %r14, %r15
negq %r15
addq $0x120, %rcx            # imm = 0x120
movb $0x1, %bpl
movl $0x1, %r12d
movzwl (%rcx), %r13d
cmpl %r13d, %esi
jne 0x23e8
cmpl %r11d, -0x4(%rcx)
je 0x2405
cmpq %r14, %r12
setb %bpl
leaq (%r15,%r12), %r13
incq %r13
incq %r12
addq $0xc, %rcx
cmpq $0x1, %r13
jne 0x23d9
jmp 0x240b
testb $0x1, %bpl
jne 0x241f
incl %r11d
decl %ebx
cmpl $0x10, %r11d
jne 0x2359
callq 0x1322
movl -0x8(%rcx), %esi
cmpl %r11d, %r8d
jae 0x2469
movq 0x10(%rdi), %rcx
addl %r8d, %ebx
movl %ebx, %edx
leaq 0x1(%rcx), %rbx
movq %rbx, 0x10(%rdi)
movzbl (%rcx), %ebp
cmpl $0xff, %ebp
jne 0x2453
cmpb $0x0, (%rbx)
jne 0x2453
addq $0x2, %rcx
movq %rcx, 0x10(%rdi)
movq %rcx, %rbx
shll $0x8, %r10d
orl %ebp, %r10d
addl $0x8, %r8d
addl $0x8, %edx
movq %rbx, %rcx
cmpl %r11d, %r8d
jb 0x2430
movl %edx, 0x4cb5(%rip)      # 0x7124
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl %edx, %ecx
shll %cl, %r8d
notl %r8d
andl %r10d, %r8d
movl %r8d, 0x4c99(%rip)      # 0x7120
testq %rsi, %rsi
je 0x2555
testl %r9d, %r9d
jle 0x24b1
movq 0x10(%rdi), %rcx
cmpb $-0x1, (%rcx)
jne 0x24b1
cmpb $0x0, 0x1(%rcx)
je 0x24b1
addq $0x2, %rcx
movq %rcx, 0x10(%rdi)
xorl %r8d, %r8d
xorl %edx, %edx
cmpl %esi, %edx
jae 0x24f0
movq 0x10(%rdi), %rcx
leaq 0x1(%rcx), %r9
movq %r9, 0x10(%rdi)
movzbl (%rcx), %r10d
cmpl $0xff, %r10d
jne 0x24df
cmpb $0x0, (%r9)
jne 0x24df
addq $0x2, %rcx
movq %rcx, 0x10(%rdi)
movq %rcx, %r9
shll $0x8, %r8d
orl %r10d, %r8d
addl $0x8, %edx
movq %r9, %rcx
cmpl %esi, %edx
jb 0x24b9
subl %esi, %edx
movl %r8d, %r9d
movl %edx, %ecx
shrl %cl, %r9d
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
shll %cl, %r10d
movl %edx, 0x4c1b(%rip)      # 0x7124
notl %r10d
leal -0x1(%rsi), %ecx
movl $0x1, %r11d
shll %cl, %r11d
andl %r10d, %r8d
movswl %r9w, %r10d
movq $-0x1, %rbx
movl %esi, %ecx
shlq %cl, %rbx
incl %ebx
xorl %ecx, %ecx
cmpl %r10d, %r11d
cmovgl %ebx, %ecx
movl %r8d, 0x4be4(%rip)      # 0x7120
addl %r9d, %ecx
addw 0xa4(%rax), %cx
movw %cx, (%rsp)
movswl %cx, %ecx
movl %ecx, 0xa4(%rax)
jmp 0x2560
movzwl 0xa4(%rax), %ecx
movw %cx, (%rsp)
movl $0x1, %r11d
xorl %r9d, %r9d
movl $0x1, %r10d
cmpl %r10d, %edx
jae 0x25ba
movq 0x10(%rdi), %rcx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x10(%rdi)
movzbl (%rcx), %ebx
cmpl $0xff, %ebx
jne 0x259b
cmpb $0x0, (%rsi)
jne 0x259b
addq $0x2, %rcx
movq %rcx, 0x10(%rdi)
movq %rcx, %rsi
shll $0x8, %r8d
orl %ebx, %r8d
addl $0x8, %edx
movq %rsi, %rcx
cmpl %r10d, %edx
jb 0x2578
movl %r8d, 0x4b6c(%rip)      # 0x7120
movl %edx, 0x4b6a(%rip)      # 0x7124
movl %edx, %ecx
subl %r10d, %ecx
movl %r8d, %esi
shrl %cl, %esi
movq 0x10(%rax), %rcx
movslq 0x114(%rcx), %rbx
testq %rbx, %rbx
jle 0x261c
movq %rbx, %r14
negq %r14
addq $0x120, %rcx            # imm = 0x120
movb $0x1, %bpl
movl $0x1, %r15d
movzwl (%rcx), %r12d
cmpl %r12d, %esi
jne 0x25f9
cmpl %r10d, -0x4(%rcx)
je 0x2616
cmpq %rbx, %r15
setb %bpl
leaq (%r14,%r15), %r12
incq %r12
incq %r15
addq $0xc, %rcx
cmpq $0x1, %r12
jne 0x25ea
jmp 0x261c
testb $0x1, %bpl
jne 0x2646
incl %r10d
cmpl $0x11, %r10d
jne 0x256f
movl $0x11, %r10d
cmpl $0x11, %r10d
sbbl $-0x1, %r11d
cmpl $0x3f, %r11d
jle 0x2569
jmp 0x277c
movl -0x8(%rcx), %esi
cmpl %r10d, %edx
jae 0x2687
movq 0x10(%rdi), %rcx
leaq 0x1(%rcx), %rbx
movq %rbx, 0x10(%rdi)
movzbl (%rcx), %ebp
cmpl $0xff, %ebp
jne 0x2675
cmpb $0x0, (%rbx)
jne 0x2675
addq $0x2, %rcx
movq %rcx, 0x10(%rdi)
movq %rcx, %rbx
shll $0x8, %r8d
orl %ebp, %r8d
addl $0x8, %edx
movq %rbx, %rcx
cmpl %r10d, %edx
jb 0x2652
movl %r8d, %ebx
subl %r10d, %edx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl %edx, %ecx
shll %cl, %r8d
movl %edx, 0x4a86(%rip)      # 0x7124
notl %r8d
andl %ebx, %r8d
movl %r8d, 0x4a75(%rip)      # 0x7120
movl %esi, %ecx
shrl $0x4, %ecx
andl $0xf, %esi
je 0x2761
movzbl %cl, %ebx
addl %r11d, %ebx
cmpl $0x40, %ebx
jge 0x27a2
cmpl %esi, %edx
jae 0x2705
movq 0x10(%rdi), %rcx
leaq 0x1(%rcx), %r11
movq %r11, 0x10(%rdi)
movzbl (%rcx), %ebp
cmpl $0xff, %ebp
jne 0x26f4
cmpb $0x0, (%r11)
jne 0x26f4
addq $0x2, %rcx
movq %rcx, 0x10(%rdi)
movq %rcx, %r11
shll $0x8, %r8d
orl %ebp, %r8d
addl $0x8, %edx
movq %r11, %rcx
cmpl %esi, %edx
jb 0x26d0
subl %esi, %edx
movl %r8d, %r11d
movl %edx, %ecx
shrl %cl, %r11d
movl %edx, 0x4a0f(%rip)      # 0x7124
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
shll %cl, %ebp
notl %ebp
andl %ebp, %r8d
leal -0x1(%rsi), %ecx
movl $0x1, %ebp
shll %cl, %ebp
movl %r8d, 0x49ee(%rip)      # 0x7120
movswl %r11w, %r14d
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl %esi, %ecx
shll %cl, %r15d
orl $0x1, %r15d
cmpl %r14d, %ebp
cmovlel %r9d, %r15d
addl %r11d, %r15d
movslq %ebx, %rcx
incl %ebx
movw %r15w, (%rsp,%rcx,2)
movl %ebx, %r11d
jmp 0x262f
cmpb $0xf, %cl
je 0x2773
movzbl %cl, %ecx
testl %ecx, %ecx
jne 0x262f
jmp 0x277c
addl $0x10, %r11d
jmp 0x262f
addq $0x20, %rax
movq %rsp, %rsi
movl $0x80, %edx
movq %rax, %rdi
callq 0x1100
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x134f