// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
addq $0x20, %rax
movslq -0xc(%rbp), %rcx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
leaq -0xa0(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x1090
movb $0x0, -0xa1(%rbp)
movl $0x0, -0xa8(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jle 0x2cb3
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movzbl (%rax), %eax
cmpl $0xff, %eax
jne 0x2cb1
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movzbl 0x1(%rax), %eax
cmpl $0x0, %eax
je 0x2cb1
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x2, %rcx
movq %rcx, 0x10(%rax)
movl $0x0, 0x6879(%rip)      # 0x9520
movl $0x0, 0x6873(%rip)      # 0x9524
jmp 0x2cb3
movl $0x1, -0xac(%rbp)
cmpl $0x10, -0xac(%rbp)
jge 0x2f2b
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
movl -0xac(%rbp), %esi
callq 0x3950
movl %eax, -0xb0(%rbp)
movl -0xb0(%rbp), %edi
movl -0xac(%rbp), %esi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x114(%rax), %edx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rcx
addq $0x118, %rcx            # imm = 0x118
leaq -0xa8(%rbp), %r8
callq 0x39f0
testb $0x1, %al
jne 0x2d21
jmp 0x2f15
jmp 0x2d23
jmp 0x2d25
jmp 0x2d27
movl 0x67f7(%rip), %eax      # 0x9524
cmpl -0xac(%rbp), %eax
jae 0x2db4
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x10(%rcx)
movb (%rax), %al
movb %al, -0xb1(%rbp)
movl 0x67ca(%rip), %eax      # 0x9520
shll $0x8, %eax
movl %eax, 0x67c1(%rip)      # 0x9520
movzbl -0xb1(%rbp), %eax
cmpl $0xff, %eax
jne 0x2d8d
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movzbl (%rax), %eax
cmpl $0x0, %eax
jne 0x2d8d
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
movzbl -0xb1(%rbp), %eax
orl 0x6786(%rip), %eax      # 0x9520
movl %eax, 0x6780(%rip)      # 0x9520
movl 0x677e(%rip), %eax      # 0x9524
addl $0x8, %eax
movl %eax, 0x6775(%rip)      # 0x9524
jmp 0x2d27
jmp 0x2db6
jmp 0x2db8
movl -0xac(%rbp), %ecx
movl 0x6760(%rip), %eax      # 0x9524
subl %ecx, %eax
movl %eax, 0x6758(%rip)      # 0x9524
movl 0x6752(%rip), %ecx      # 0x9524
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
andl 0x673e(%rip), %eax      # 0x9520
movl %eax, 0x6738(%rip)      # 0x9520
jmp 0x2dea
movb $0x1, -0xa1(%rbp)
movl -0xa8(%rbp), %eax
movl %eax, -0xb8(%rbp)
cmpl $0x0, -0xb8(%rbp)
jne 0x2e1c
movq -0x18(%rbp), %rax
movl 0xa4(%rax), %eax
movw %ax, -0xa0(%rbp)
jmp 0x2f13
movq -0x8(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jle 0x2e6f
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movzbl (%rax), %eax
cmpl $0xff, %eax
jne 0x2e6d
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movzbl 0x1(%rax), %eax
cmpl $0x0, %eax
je 0x2e6d
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x2, %rcx
movq %rcx, 0x10(%rax)
movl $0x0, 0x66bd(%rip)      # 0x9520
movl $0x0, 0x66b7(%rip)      # 0x9524
jmp 0x2e6f
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
movl -0xb8(%rbp), %esi
callq 0x3a80
movw %ax, -0xba(%rbp)
movswl -0xba(%rbp), %eax
movl -0xb8(%rbp), %ecx
subl $0x1, %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
cmpl %ecx, %eax
jge 0x2ed0
movswq -0xba(%rbp), %rax
movl -0xb8(%rbp), %ecx
movq $-0x1, %rdx
shlq %cl, %rdx
movq %rdx, %rcx
addq %rcx, %rax
addq $0x1, %rax
movl %eax, -0xe0(%rbp)
jmp 0x2edd
movswl -0xba(%rbp), %eax
movl %eax, -0xe0(%rbp)
movl -0xe0(%rbp), %eax
movw %ax, -0xba(%rbp)
movswl -0xba(%rbp), %eax
movq -0x18(%rbp), %rcx
addl 0xa4(%rcx), %eax
movw %ax, -0xa0(%rbp)
movswl -0xa0(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0xa4(%rax)
jmp 0x2f2b
jmp 0x2f17
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0x2cbd
testb $0x1, -0xa1(%rbp)
jne 0x2f62
movq 0x60bd(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x41ab(%rip), %rsi      # 0x70f0
leaq 0x41b2(%rip), %rdx      # 0x70fe
movl $0x2cd, %ecx            # imm = 0x2CD
movb $0x0, %al
callq 0x10e0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1150
movl $0x1, -0xc0(%rbp)
movb $0x0, -0xc1(%rbp)
xorl %eax, %eax
cmpl $0x3f, -0xc0(%rbp)
movb %al, -0xe1(%rbp)
jg 0x2f92
movb -0xc1(%rbp), %al
xorb $-0x1, %al
movb %al, -0xe1(%rbp)
movb -0xe1(%rbp), %al
testb $0x1, %al
jne 0x2fa1
jmp 0x3270
movl $0x0, -0xc8(%rbp)
movl $0x1, -0xc8(%rbp)
cmpl $0x10, -0xc8(%rbp)
jg 0x3253
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
movl -0xc8(%rbp), %esi
callq 0x3950
movl %eax, -0xcc(%rbp)
movl -0xcc(%rbp), %edi
movl -0xc8(%rbp), %esi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x114(%rax), %edx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x118, %rcx            # imm = 0x118
leaq -0xa8(%rbp), %r8
callq 0x39f0
testb $0x1, %al
jne 0x3019
jmp 0x323d
jmp 0x301b
jmp 0x301d
jmp 0x301f
movl 0x64ff(%rip), %eax      # 0x9524
cmpl -0xc8(%rbp), %eax
jae 0x30ac
movq -0x8(%rbp), %rcx
movq 0x10(%rcx), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x10(%rcx)
movb (%rax), %al
movb %al, -0xcd(%rbp)
movl 0x64d2(%rip), %eax      # 0x9520
shll $0x8, %eax
movl %eax, 0x64c9(%rip)      # 0x9520
movzbl -0xcd(%rbp), %eax
cmpl $0xff, %eax
jne 0x3085
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movzbl (%rax), %eax
cmpl $0x0, %eax
jne 0x3085
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
movzbl -0xcd(%rbp), %eax
orl 0x648e(%rip), %eax      # 0x9520
movl %eax, 0x6488(%rip)      # 0x9520
movl 0x6486(%rip), %eax      # 0x9524
addl $0x8, %eax
movl %eax, 0x647d(%rip)      # 0x9524
jmp 0x301f
jmp 0x30ae
jmp 0x30b0
movl -0xc8(%rbp), %ecx
movl 0x6468(%rip), %eax      # 0x9524
subl %ecx, %eax
movl %eax, 0x6460(%rip)      # 0x9524
movl 0x645a(%rip), %ecx      # 0x9524
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
andl 0x6446(%rip), %eax      # 0x9520
movl %eax, 0x6440(%rip)      # 0x9520
jmp 0x30e2
movl -0xa8(%rbp), %eax
movl %eax, -0xd4(%rbp)
movl -0xd4(%rbp), %eax
andl $0xf, %eax
movb %al, -0xd5(%rbp)
movl -0xd4(%rbp), %eax
sarl $0x4, %eax
movb %al, -0xd6(%rbp)
movzbl -0xd5(%rbp), %eax
cmpl $0x0, %eax
jne 0x314f
movzbl -0xd6(%rbp), %eax
cmpl $0x0, %eax
jne 0x312d
movb $0x1, -0xc1(%rbp)
jmp 0x314a
movzbl -0xd6(%rbp), %eax
cmpl $0xf, %eax
jne 0x3148
movl -0xc0(%rbp), %eax
addl $0x10, %eax
movl %eax, -0xc0(%rbp)
jmp 0x314a
jmp 0x323b
movzbl -0xd6(%rbp), %eax
addl -0xc0(%rbp), %eax
movl %eax, -0xc0(%rbp)
cmpl $0x3f, -0xc0(%rbp)
jle 0x3199
movq 0x5e86(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x3f74(%rip), %rsi      # 0x70f0
leaq 0x3f7b(%rip), %rdx      # 0x70fe
movl $0x2f5, %ecx            # imm = 0x2F5
movb $0x0, %al
callq 0x10e0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1150
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
movzbl -0xd5(%rbp), %esi
callq 0x3a80
movw %ax, -0xd8(%rbp)
movswl -0xd8(%rbp), %eax
movzbl -0xd5(%rbp), %ecx
subl $0x1, %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
cmpl %ecx, %eax
jge 0x31ff
movswq -0xd8(%rbp), %rax
movzbl -0xd5(%rbp), %ecx
movl %ecx, %ecx
movq $-0x1, %rdx
shlq %cl, %rdx
movq %rdx, %rcx
addq %rcx, %rax
addq $0x1, %rax
movl %eax, -0xe8(%rbp)
jmp 0x320c
movswl -0xd8(%rbp), %eax
movl %eax, -0xe8(%rbp)
movl -0xe8(%rbp), %eax
movw %ax, -0xd8(%rbp)
movw -0xd8(%rbp), %cx
movl -0xc0(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0xc0(%rbp)
cltq
movw %cx, -0xa0(%rbp,%rax,2)
jmp 0x3253
jmp 0x323f
movl -0xc8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc8(%rbp)
jmp 0x2fb5
cmpl $0x10, -0xc8(%rbp)
jle 0x326b
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x2f73
movl $0x0, -0xdc(%rbp)
cmpl $0x40, -0xdc(%rbp)
jge 0x32b3
movslq -0xdc(%rbp), %rax
movw -0xa0(%rbp,%rax,2), %dx
movq -0x18(%rbp), %rax
movslq -0xdc(%rbp), %rcx
movw %dx, 0x20(%rax,%rcx,2)
movl -0xdc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xdc(%rbp)
jmp 0x327a
addq $0xf0, %rsp
popq %rbp
retq
nopl (%rax)