// File: /workspace/github_c_makefile_star2_O1/halcy[P]quasi_crystals_bot/Engine/../Graphics/Tools/DeflateReader.h
// Function: ReadDeflateBlockHeader
static bool ReadDeflateBlockHeader(DeflateReader *self)
{
	self->islastblock=ReadBitString(&self->bitstream,1);

	int type=ReadBitString(&self->bitstream,2);
	if(type<0) return false;

	switch(type)
	{
		case 0: // Stored.
		{
			FlushBitStream(&self->bitstream);

			int count=ReadBitString(&self->bitstream,16);
			int count2=ReadBitString(&self->bitstream,16);
			if(count!=(count2^0xffff)) return false;

			self->storedbytesleft=count;
			self->isstoredblock=true;
		}
		break;

		case 1: // Fixed Huffman.
			InitializeDeflateFixedLiteralCode(self->literalcode);
			InitializeDeflateFixedDistanceCode(self->distancecode);
			self->isstoredblock=false;
		break;

		case 2: // Dynamic Huffman.
		{
			int numliterals=ReadBitString(&self->bitstream,5)+257;
			int numdistances=ReadBitString(&self->bitstream,5)+1;
			int nummetas=ReadBitString(&self->bitstream,4)+4;

			uint16_t metacode[128];
			if(!ReadDeflateMetaCode(self,metacode,nummetas)) return false;

			int total=numliterals+numdistances;
			int lengths[total];
			int i=0;
			while(i<total)
			{
				int val=ReadHuffmanCode(&self->bitstream,metacode,7);
				if(val<0) return false;

				if(val<16)
				{
					lengths[i++]=val;
				}
				else if(val==16)
				{
					int repeats=ReadBitString(&self->bitstream,2)+3;

					if(i==0||i+repeats>total) return false;

					for(int j=0;j<repeats;j++) lengths[i+j]=lengths[i-1];
					i+=repeats;
				}
				else
				{
					int repeats;
					if(val==17) repeats=ReadBitString(&self->bitstream,3)+3;
					else repeats=ReadBitString(&self->bitstream,7)+11;

					if(i+repeats>total) return false;

					for(int j=0;j<repeats;j++) lengths[i+j]=0;
					i+=repeats;
				}
			}

			if(!BuildHuffmanTableFromLengths(self->literalcode,15,lengths,numliterals)) return false;
			if(!BuildHuffmanTableFromLengths(self->distancecode,15,lengths+numliterals,numdistances)) return false;

			self->isstoredblock=false;
		}
		break;

		default: return false;
	}

	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
leaq -0x4e8(%rsp), %rsp
movq %rdi, %rbx
movl $0x1, %esi
callq 0x47f9
movl 0x24(%rbx), %ecx
testl %ecx, %ecx
jle 0x42c6
movl 0x20(%rbx), %eax
movl %eax, %edx
shrl %edx
movl %edx, 0x20(%rbx)
decl %ecx
movl %ecx, 0x24(%rbx)
andb $0x1, %al
jmp 0x42c8
movb $0x1, %al
movb %al, 0x2d(%rbx)
movq %rbx, %rdi
movl $0x2, %esi
callq 0x47f9
movl 0x24(%rbx), %ecx
cmpl $0x2, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jl 0x42f9
movl 0x20(%rbx), %edx
movl %edx, %eax
andl $0x3, %eax
shrl $0x2, %edx
addl $-0x2, %ecx
movl %edx, 0x20(%rbx)
movl %ecx, 0x24(%rbx)
testl %eax, %eax
js 0x478e
cmpl $0x2, %eax
je 0x4464
cmpl $0x1, %eax
je 0x43a7
testl %eax, %eax
jne 0x478e
movl 0x24(%rbx), %eax
movl %eax, %ecx
andl $0x7, %ecx
je 0x432e
andl $-0x8, %eax
shrl %cl, 0x20(%rbx)
movl %eax, 0x24(%rbx)
movq %rbx, %rdi
movl $0x10, %esi
callq 0x47f9
movl 0x24(%rbx), %eax
cmpl $0x10, %eax
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jl 0x4362
movl 0x20(%rbx), %ecx
movzwl %cx, %r14d
shrl $0x10, %ecx
addl $-0x10, %eax
movl %ecx, 0x20(%rbx)
movl %eax, 0x24(%rbx)
movq %rbx, %rdi
movl $0x10, %esi
callq 0x47f9
movl 0x24(%rbx), %eax
cmpl $0x10, %eax
jl 0x438a
movl 0x20(%rbx), %ecx
movzwl %cx, %r15d
shrl $0x10, %ecx
addl $-0x10, %eax
movl %ecx, 0x20(%rbx)
movl %eax, 0x24(%rbx)
xorl %r14d, %r15d
cmpl $0xffff, %r15d          # imm = 0xFFFF
jne 0x478e
movl %r14d, 0x30(%rbx)
movb $0x1, 0x2c(%rbx)
jmp 0x445d
movq $-0x240, %rax           # imm = 0xFDC0
movl $0x8, -0x2d0(%rbp,%rax)
addq $0x4, %rax
jne 0x43ae
movq $-0x1c0, %rax           # imm = 0xFE40
movl $0x9, -0x110(%rbp,%rax)
addq $0x4, %rax
jne 0x43c6
movq $-0x60, %rax
movl $0x7, -0xb0(%rbp,%rax)
addq $0x4, %rax
jne 0x43de
movq $-0x20, %rax
movl $0x8, -0x90(%rbp,%rax)
addq $0x4, %rax
jne 0x43f6
leaq 0x8034(%rbx), %rdi
leaq -0x510(%rbp), %rdx
movl $0xf, %esi
movl $0x120, %ecx            # imm = 0x120
callq 0x4859
movq $-0x80, %rax
movl $0x5, -0x490(%rbp,%rax)
addq $0x4, %rax
jne 0x442b
leaq 0x18034(%rbx), %rdi
leaq -0x510(%rbp), %rdx
movl $0xf, %esi
movl $0x20, %ecx
callq 0x4859
movb $0x0, 0x2c(%rbx)
movb $0x1, %al
jmp 0x4790
movq %rbx, %rdi
movl $0x5, %esi
callq 0x47f9
movl 0x24(%rbx), %eax
cmpl $0x5, %eax
movl $0x100, %ecx            # imm = 0x100
movq %rcx, -0x30(%rbp)
jl 0x44a0
movl 0x20(%rbx), %ecx
movl %ecx, %edx
andl $0x1f, %edx
shrl $0x5, %ecx
movl %ecx, 0x20(%rbx)
addl $-0x5, %eax
movl %eax, 0x24(%rbx)
addl $0x101, %edx            # imm = 0x101
movq %rdx, -0x30(%rbp)
movq %rbx, %rdi
movl $0x5, %esi
callq 0x47f9
movl 0x24(%rbx), %eax
movq $0x0, -0x38(%rbp)
cmpl $0x5, %eax
jl 0x44d7
movl 0x20(%rbx), %ecx
movl %ecx, %edx
andl $0x1f, %edx
shrl $0x5, %ecx
movl %ecx, 0x20(%rbx)
addl $-0x5, %eax
movl %eax, 0x24(%rbx)
incl %edx
movq %rdx, -0x38(%rbp)
movq %rbx, %rdi
movl $0x4, %esi
callq 0x47f9
movl 0x24(%rbx), %eax
cmpl $0x4, %eax
movl $0x3, %r14d
jl 0x450c
movl 0x20(%rbx), %ecx
movl %ecx, %r14d
andl $0xf, %r14d
shrl $0x4, %ecx
movl %ecx, 0x20(%rbx)
addl $-0x4, %eax
movl %eax, 0x24(%rbx)
addl $0x4, %r14d
leal (,%r14,4), %r12d
xorl %r13d, %r13d
leaq 0x9ee2(%rip), %r15      # 0xe400
movq %rbx, %rdi
movl $0x3, %esi
callq 0x47f9
movl 0x24(%rbx), %ecx
cmpl $0x3, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jl 0x454c
movl 0x20(%rbx), %edx
movl %edx, %eax
andl $0x7, %eax
shrl $0x3, %edx
addl $-0x3, %ecx
movl %edx, 0x20(%rbx)
movl %ecx, 0x24(%rbx)
movslq (%r13,%r15), %rcx
addq $0x4, %r13
cmpq %r13, %r12
movl %eax, -0x90(%rbp,%rcx,4)
jne 0x451e
cmpl $0x12, %r14d
jg 0x4592
leal (%r14d), %ecx
leaq (%r15,%rcx,4), %rax
incq %rcx
movq %rcx, %rdx
movslq (%rax), %rsi
incq %rdx
leaq 0x4(%rax), %rax
cmpl $0x13, %ecx
movl $0x0, -0x90(%rbp,%rsi,4)
movq %rdx, %rcx
jne 0x4575
leaq -0x510(%rbp), %rdi
leaq -0x90(%rbp), %rdx
movl $0x7, %esi
movl $0x13, %ecx
callq 0x4859
testb %al, %al
je 0x478e
movq -0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
leal (%rcx,%rax), %r15d
leal 0xf(,%r15,4), %eax
movq %rsp, -0x40(%rbp)
andl $-0x10, %eax
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
xorl %r14d, %r14d
leaq -0x510(%rbp), %r13
cmpl %r15d, %r14d
jge 0x4741
movq %rbx, %rdi
movq %r13, %rsi
movl $0x7, %edx
callq 0x479f
testl %eax, %eax
js 0x4735
cmpl $0xf, %eax
jg 0x461a
movslq %r14d, %rcx
incl %r14d
movl %eax, (%r12,%rcx,4)
jmp 0x4731
movq %rbx, %rdi
cmpl $0x11, %eax
je 0x4673
cmpl $0x10, %eax
jne 0x46a3
movl $0x2, %esi
movl $0x2, %r13d
callq 0x47f9
movl 0x24(%rbx), %eax
cmpl $0x2, %eax
jl 0x4659
movl 0x20(%rbx), %ecx
movl %ecx, %r13d
andl $0x3, %r13d
shrl $0x2, %ecx
movl %ecx, 0x20(%rbx)
addl $-0x2, %eax
movl %eax, 0x24(%rbx)
addl $0x3, %r13d
testl %r14d, %r14d
je 0x466c
leal (%r13,%r14), %eax
cmpl %r15d, %eax
jle 0x4707
xorl %ecx, %ecx
jmp 0x4726
movl $0x3, %esi
callq 0x47f9
movl 0x24(%rbx), %eax
cmpl $0x3, %eax
movl $0x2, %edx
jl 0x46d1
movl 0x20(%rbx), %ecx
movl %ecx, %edx
andl $0x7, %edx
shrl $0x3, %ecx
movl %ecx, 0x20(%rbx)
addl $-0x3, %eax
movl %eax, 0x24(%rbx)
addl $0x3, %edx
jmp 0x46d1
movl $0x7, %esi
callq 0x47f9
movl 0x24(%rbx), %eax
cmpl $0x7, %eax
movl $0xa, %edx
jl 0x46d1
movl 0x20(%rbx), %ecx
movl %ecx, %edx
andl $0x7f, %edx
shrl $0x7, %ecx
movl %ecx, 0x20(%rbx)
addl $-0x7, %eax
movl %eax, 0x24(%rbx)
addl $0xb, %edx
leal (%rdx,%r14), %r13d
cmpl %r15d, %r13d
jg 0x46ee
movslq %r14d, %rax
leaq (%r12,%rax,4), %rdi
shll $0x2, %edx
xorl %esi, %esi
callq 0x10d0
movl %r13d, %r14d
cmpl %r15d, %r13d
jle 0x46fe
xorl %eax, %eax
leaq -0x510(%rbp), %r13
jmp 0x4737
leaq -0x510(%rbp), %r13
jmp 0x4731
movslq %r14d, %rcx
movl %r13d, %edx
leaq (%r12,%rcx,4), %rsi
movl -0x4(%r12,%rcx,4), %edi
movl %edi, (%rsi)
leaq 0x4(%rsi), %rsi
decq %rdx
jne 0x4711
movb $0x1, %cl
movl %eax, %r14d
leaq -0x510(%rbp), %r13
testb %cl, %cl
je 0x4735
movb $0x1, %al
jmp 0x4737
xorl %eax, %eax
testb %al, %al
jne 0x45e5
jmp 0x478a
leaq 0x8034(%rbx), %rdi
movl $0xf, %esi
movq %r12, %rdx
movq -0x30(%rbp), %rcx
callq 0x4859
testb %al, %al
je 0x478a
movl -0x30(%rbp), %eax
leaq 0x18034(%rbx), %rdi
leaq (%r12,%rax,4), %rdx
movl $0xf, %esi
movq -0x38(%rbp), %rcx
callq 0x4859
testb %al, %al
je 0x478a
movb $0x0, 0x2c(%rbx)
movq -0x40(%rbp), %rsp
jmp 0x445d
movq -0x40(%rbp), %rsp
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq