// File: /workspace/github_c_makefile_star2_O2/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 -0x4f8(%rsp), %rsp
movq %rdi, %rbx
pushq $0x1
popq %rsi
callq 0x3e37
testl %eax, %eax
setne 0x2d(%rbx)
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x3e37
testl %eax, %eax
js 0x41d8
xorl %r14d, %r14d
cmpl $0x2, %eax
je 0x3fcb
cmpl $0x1, %eax
je 0x3f06
testl %eax, %eax
jne 0x41db
movl 0x24(%rbx), %eax
movl %eax, %ecx
andl $0x7, %ecx
je 0x3ece
andl $-0x8, %eax
shrl %cl, 0x20(%rbx)
movl %eax, 0x24(%rbx)
pushq $0x10
popq %r15
movq %rbx, %rdi
movl %r15d, %esi
callq 0x3e37
movq %rbx, %rdi
movl %r15d, %esi
movl %eax, %r14d
callq 0x3e37
xorl %r14d, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x41d8
movl %r14d, 0x30(%rbx)
movb $0x1, 0x2c(%rbx)
jmp 0x3fc3
movq $-0x240, %rax           # imm = 0xFDC0
testq %rax, %rax
je 0x3f23
movl $0x8, -0x2e0(%rbp,%rax)
addq $0x4, %rax
jmp 0x3f0d
movq $-0x1c0, %rax           # imm = 0xFE40
testq %rax, %rax
je 0x3f40
movl $0x9, -0x120(%rbp,%rax)
addq $0x4, %rax
jmp 0x3f2a
pushq $-0x60
popq %rax
testq %rax, %rax
je 0x3f59
movl $0x7, -0xc0(%rbp,%rax)
addq $0x4, %rax
jmp 0x3f43
pushq $-0x20
popq %rax
testq %rax, %rax
je 0x3f72
movl $0x8, -0xa0(%rbp,%rax)
addq $0x4, %rax
jmp 0x3f5c
leaq 0x8034(%rbx), %rdi
pushq $0xf
popq %rsi
leaq -0x520(%rbp), %rdx
movl $0x120, %ecx            # imm = 0x120
callq 0x4299
pushq $-0x80
popq %rax
testq %rax, %rax
je 0x3fa6
movl $0x5, -0x4a0(%rbp,%rax)
addq $0x4, %rax
jmp 0x3f90
leaq 0x18034(%rbx), %rdi
pushq $0xf
popq %rsi
leaq -0x520(%rbp), %rdx
pushq $0x20
popq %rcx
callq 0x4299
movb $0x0, 0x2c(%rbx)
movb $0x1, %r14b
jmp 0x41db
pushq $0x5
popq %r15
movq %rbx, %rdi
movl %r15d, %esi
callq 0x3e37
movq %rbx, %rdi
movl %r15d, %esi
movq %rax, -0x30(%rbp)
callq 0x3e37
movq %rax, -0x38(%rbp)
pushq $0x4
popq %rsi
movq %rbx, %rdi
callq 0x3e37
movl %eax, %r15d
movl $0x0, %r13d
leaq 0x83f8(%rip), %r12      # 0xc400
addl $0x4, %r15d
testl %r15d, %r15d
cmovgl %r15d, %r13d
shlq $0x2, %r13
cmpq %r14, %r13
je 0x4038
movq %rbx, %rdi
pushq $0x3
popq %rsi
callq 0x3e37
movslq (%r14,%r12), %rcx
addq $0x4, %r14
movl %eax, -0xa0(%rbp,%rcx,4)
jmp 0x4017
movq -0x30(%rbp), %r14
movslq %r15d, %rax
addl $0x101, %r14d           # imm = 0x101
leaq (%r12,%rax,4), %rcx
cmpq $0x12, %rax
jg 0x4064
movslq (%rcx), %rdx
incq %rax
addq $0x4, %rcx
andl $0x0, -0xa0(%rbp,%rdx,4)
jmp 0x404a
leaq -0x520(%rbp), %rdi
pushq $0x7
popq %r12
leaq -0xa0(%rbp), %rdx
pushq $0x13
popq %rcx
movl %r12d, %esi
callq 0x4299
testb %al, %al
je 0x41d8
movq -0x38(%rbp), %rax
movq %r14, -0x30(%rbp)
movq %rsp, -0x48(%rbp)
leal 0x1(%rax), %eax
movq %rax, -0x38(%rbp)
addl %r14d, %eax
movq %rsp, %r14
movq %rax, -0x40(%rbp)
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
xorl %r13d, %r13d
cmpl -0x40(%rbp), %r13d
jge 0x418e
movq %rbx, %rdi
leaq -0x520(%rbp), %rsi
movl %r12d, %edx
callq 0x41ed
testl %eax, %eax
js 0x41d4
cmpl $0xf, %eax
movslq %r13d, %r15
ja 0x40f0
incl %r13d
movl %eax, (%r14,%r15,4)
jmp 0x40bb
cmpl $0x11, %eax
pushq $0x3
popq %r12
movl %r12d, %esi
je 0x4155
cmpl $0x10, %eax
jne 0x414e
movq %rbx, %rdi
pushq $0x2
popq %rsi
callq 0x3e37
testl %r13d, %r13d
pushq $0x7
popq %r12
je 0x41d4
addl $0x3, %eax
addl %eax, %r13d
cmpl -0x40(%rbp), %r13d
jg 0x41d4
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
leaq (%r14,%r15,4), %rcx
subq $0x1, %rax
jb 0x40bb
movl -0x4(%r14,%r15,4), %edx
movl %edx, (%rcx)
addq $0x4, %rcx
jmp 0x4137
pushq $0x7
popq %rsi
pushq $0xb
popq %r12
movq %rbx, %rdi
callq 0x3e37
addl %r12d, %eax
addl %eax, %r13d
cmpl -0x40(%rbp), %r13d
pushq $0x7
popq %r12
jg 0x41d4
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
leaq (%r14,%r15,4), %rcx
subq $0x1, %rax
jb 0x40bb
andl $0x0, (%rcx)
addq $0x4, %rcx
jmp 0x417b
leaq 0x8034(%rbx), %rdi
pushq $0xf
popq %rsi
movq -0x30(%rbp), %rcx
movq %r14, %rdx
callq 0x4299
testb %al, %al
je 0x41d4
movslq -0x30(%rbp), %rax
leaq 0x18034(%rbx), %rdi
leaq (%r14,%rax,4), %rdx
pushq $0xf
popq %rsi
movq -0x38(%rbp), %rcx
callq 0x4299
testb %al, %al
je 0x41d4
movb $0x0, 0x2c(%rbx)
movq -0x48(%rbp), %rsp
jmp 0x3fc3
movq -0x48(%rbp), %rsp
xorl %r14d, %r14d
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq