// File: /workspace/github_c_makefile_star2_O0/albinoloverats[P]hide/src/jpeg-load.c
// Function: ParseDHT
static int ParseDHT(stJpegData *jdata, const uint8_t *stream)
{
	/*
	 * u8 0xff
	 * u8 0xc4 (type of segment)
	 * u16 be length of segment
	 * 4-bits class (0 is DC, 1 is AC, more on this later)
	 * 4-bits table id
	 * array of 16 u8 number of elements for each of 16 depths
	 * array of u8 elements, in order of depth
	 */

	uint8_t huff_bits[17];

	int length = BYTE_TO_WORD(stream) - 2;
	stream += 2; // Skip length

	while (length > 0)
	{
		int index = *stream++;
		// We need to calculate the number of bytes 'vals' will takes
		huff_bits[0] = 0;
		int count = 0;
		for (int i = 1; i < 17; i++)
		{
			huff_bits[i] = *stream++;
			count += huff_bits[i];
		}
		if (count > 256)
		{
			fprintf(stderr, "Fail @ %s:%d\n", __FILE__, __LINE__);
			exit(-1);
		}
		if ((index & 0xf) >= HUFFMAN_TABLES)
		{
			fprintf(stderr, "Fail @ %s:%d\n", __FILE__, __LINE__);
			exit(-1);
		}
		if (index & 0xf0)
		{
			uint8_t *huffval = jdata->m_HTAC[index & 0xf].m_hufVal;
			for (int i = 0; i < count; i++)
				huffval[i] = *stream++;
			BuildHuffmanTable(huff_bits, &jdata->m_HTAC[index & 0xf]); // AC
		}
		else
		{
			uint8_t *huffval = jdata->m_HTDC[index & 0xf].m_hufVal;
			for (int i = 0; i < count; i++)
				huffval[i] = *stream++;
			BuildHuffmanTable(huff_bits, &jdata->m_HTDC[index & 0xf]); // DC
		}
		length -= 1;
		length -= 16;
		length -= count;
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
shll $0x8, %eax
movq -0x10(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
orl %ecx, %eax
subl $0x2, %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x10(%rbp)
cmpl $0x0, -0x34(%rbp)
jle 0x273a
movq -0x10(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x10(%rbp)
movzbl (%rax), %eax
movl %eax, -0x38(%rbp)
movb $0x0, -0x30(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x1, -0x40(%rbp)
cmpl $0x11, -0x40(%rbp)
jge 0x2590
movq -0x10(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x10(%rbp)
movb (%rax), %cl
movslq -0x40(%rbp), %rax
movb %cl, -0x30(%rbp,%rax)
movslq -0x40(%rbp), %rax
movzbl -0x30(%rbp,%rax), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x2557
cmpl $0x100, -0x3c(%rbp)     # imm = 0x100
jle 0x25c7
movq 0x6a58(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x4b46(%rip), %rsi      # 0x70f0
leaq 0x4b4d(%rip), %rdx      # 0x70fe
movl $0x1ca, %ecx            # imm = 0x1CA
movb $0x0, %al
callq 0x10e0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1150
movl -0x38(%rbp), %eax
andl $0xf, %eax
cmpl $0x4, %eax
jl 0x2600
movq 0x6a1f(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
leaq 0x4b0d(%rip), %rsi      # 0x70f0
leaq 0x4b14(%rip), %rdx      # 0x70fe
movl $0x1cf, %ecx            # imm = 0x1CF
movb $0x0, %al
callq 0x10e0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1150
movl -0x38(%rbp), %eax
andl $0xf0, %eax
cmpl $0x0, %eax
je 0x2697
movq -0x8(%rbp), %rax
addq $0xcf20, %rax           # imm = 0xCF20
movl -0x38(%rbp), %ecx
andl $0xf, %ecx
movslq %ecx, %rcx
imulq $0x3118, %rcx, %rcx     # imm = 0x3118
addq %rcx, %rax
addq $0x11, %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x266c
movq -0x10(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x10(%rbp)
movb (%rax), %dl
movq -0x48(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x263d
leaq -0x30(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0xcf20, %rsi           # imm = 0xCF20
movl -0x38(%rbp), %eax
andl $0xf, %eax
cltq
imulq $0x3118, %rax, %rax     # imm = 0x3118
addq %rax, %rsi
callq 0x2750
jmp 0x2718
movq -0x8(%rbp), %rax
addq $0xac0, %rax            # imm = 0xAC0
movl -0x38(%rbp), %ecx
andl $0xf, %ecx
movslq %ecx, %rcx
imulq $0x3118, %rcx, %rcx     # imm = 0x3118
addq %rcx, %rax
addq $0x11, %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x26f2
movq -0x10(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x10(%rbp)
movb (%rax), %dl
movq -0x58(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x26c3
leaq -0x30(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0xac0, %rsi            # imm = 0xAC0
movl -0x38(%rbp), %eax
andl $0xf, %eax
cltq
imulq $0x3118, %rax, %rax     # imm = 0x3118
addq %rax, %rsi
callq 0x2750
movl -0x34(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
subl $0x10, %eax
movl %eax, -0x34(%rbp)
movl -0x3c(%rbp), %ecx
movl -0x34(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x34(%rbp)
jmp 0x2526
xorl %eax, %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)