// File: /workspace/github_c_makefile_star2_O3/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
movl $0x1, %esi
movq %rdi, %rbx
callq 0x4ad0
movl 0x24(%rbx), %ecx
testl %ecx, %ecx
jle 0x4550
movl 0x20(%rbx), %eax
decl %ecx
movl %eax, %edx
andb $0x1, %al
shrl %edx
movl %edx, 0x20(%rbx)
movl %ecx, 0x24(%rbx)
jmp 0x4552
movb $0x1, %al
movq %rbx, %rdi
movl $0x2, %esi
movb %al, 0x2d(%rbx)
callq 0x4ad0
movl 0x24(%rbx), %eax
cmpl $0x2, %eax
jge 0x457b
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x20(%rbx), %edx
leaq 0x9e97(%rip), %rsi      # 0xe41c
addl $-0x2, %eax
movl %edx, %ecx
shrl $0x2, %edx
andl $0x3, %ecx
movl %edx, 0x20(%rbx)
movl %eax, 0x24(%rbx)
movslq (%rsi,%rcx,4), %rcx
addq %rsi, %rcx
jmpq *%rcx
movl %eax, %ecx
andl $0x7, %ecx
je 0x45b1
shrl %cl, %edx
andl $-0x8, %eax
movl %edx, 0x20(%rbx)
movl %eax, 0x24(%rbx)
movq %rbx, %rdi
movl $0x10, %esi
callq 0x4ad0
movl 0x24(%rbx), %eax
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
cmpl $0x10, %eax
jl 0x45e5
movl 0x20(%rbx), %ecx
addl $-0x10, %eax
movzwl %cx, %r14d
shrl $0x10, %ecx
movl %ecx, 0x20(%rbx)
movl %eax, 0x24(%rbx)
movq %rbx, %rdi
movl $0x10, %esi
callq 0x4ad0
movl 0x24(%rbx), %eax
cmpl $0x10, %eax
jl 0x460d
movl 0x20(%rbx), %ecx
addl $-0x10, %eax
movzwl %cx, %r15d
shrl $0x10, %ecx
movl %ecx, 0x20(%rbx)
movl %eax, 0x24(%rbx)
xorl %r14d, %r15d
cmpl $0xffff, %r15d          # imm = 0xFFFF
jne 0x456a
movl %r14d, 0x30(%rbx)
movb $0x1, 0x2c(%rbx)
jmp 0x46ec
movq $-0x240, %rax           # imm = 0xFDC0
movaps 0x9f18(%rip), %xmm0     # 0xe550
movaps %xmm0, -0x2e0(%rbp,%rax)
addq $0x10, %rax
jne 0x4638
movq $-0x1c0, %rax           # imm = 0xFE40
movaps 0x9f0c(%rip), %xmm1     # 0xe560
movaps %xmm1, -0x120(%rbp,%rax)
addq $0x10, %rax
jne 0x4654
movq $-0x60, %rax
pmovsxbd 0x9f1e(%rip), %xmm1     # 0xe590
movdqa %xmm1, -0xc0(%rbp,%rax)
addq $0x10, %rax
jne 0x4672
leaq -0x520(%rbp), %rdx
leaq 0x8034(%rbx), %rdi
movl $0xf, %esi
movl $0x120, %ecx            # imm = 0x120
movaps %xmm0, 0x460(%rdx)
movaps %xmm0, 0x470(%rdx)
callq 0x4b32
movq $-0x80, %rax
pmovsxbd 0x9ed8(%rip), %xmm0     # 0xe594
movdqa %xmm0, -0x4a0(%rbp,%rax)
addq $0x10, %rax
jne 0x46bc
leaq 0x18034(%rbx), %rdi
leaq -0x520(%rbp), %rdx
movl $0xf, %esi
movl $0x20, %ecx
callq 0x4b32
movb $0x0, 0x2c(%rbx)
movb $0x1, %al
jmp 0x456c
movq %rbx, %rdi
movl $0x5, %esi
callq 0x4ad0
movl 0x24(%rbx), %eax
movl $0x100, %ecx            # imm = 0x100
cmpl $0x5, %eax
movq %rcx, -0x38(%rbp)
jl 0x472f
movl 0x20(%rbx), %ecx
addl $-0x5, %eax
movl %ecx, %edx
shrl $0x5, %ecx
andl $0x1f, %edx
movl %ecx, 0x20(%rbx)
movl %eax, 0x24(%rbx)
addl $0x101, %edx            # imm = 0x101
movq %rdx, -0x38(%rbp)
movq %rbx, %rdi
movl $0x5, %esi
callq 0x4ad0
movl 0x24(%rbx), %eax
movq $0x0, -0x40(%rbp)
cmpl $0x5, %eax
jl 0x4766
movl 0x20(%rbx), %ecx
addl $-0x5, %eax
movl %ecx, %edx
shrl $0x5, %ecx
andl $0x1f, %edx
movl %ecx, 0x20(%rbx)
movl %eax, 0x24(%rbx)
incl %edx
movq %rdx, -0x40(%rbp)
movq %rbx, %rdi
movl $0x4, %esi
callq 0x4ad0
movl 0x24(%rbx), %eax
cmpl $0x3, %eax
jg 0x4783
movl $0x3, %r14d
jmp 0x479d
movl 0x20(%rbx), %ecx
addl $-0x4, %eax
movl %ecx, %r14d
shrl $0x4, %ecx
andl $0xf, %r14d
movl %ecx, 0x20(%rbx)
movl %eax, 0x24(%rbx)
addl $0x4, %r14d
leal (,%r14,4), %r12d
xorl %r13d, %r13d
leaq 0x9d41(%rip), %r15      # 0xe4f0
movq %rbx, %rdi
movl $0x3, %esi
callq 0x4ad0
movl 0x24(%rbx), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x3, %ecx
jl 0x47dd
movl 0x20(%rbx), %edx
addl $-0x3, %ecx
movl %edx, %eax
shrl $0x3, %edx
andl $0x7, %eax
movl %edx, 0x20(%rbx)
movl %ecx, 0x24(%rbx)
movslq (%r13,%r15), %rcx
addq $0x4, %r13
cmpq %r13, %r12
movl %eax, -0xa0(%rbp,%rcx,4)
jne 0x47af
cmpl $0x12, %r14d
jg 0x4823
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
movq %rdx, %rcx
movl $0x0, -0xa0(%rbp,%rsi,4)
jne 0x4806
leaq -0x520(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
movl $0x7, %esi
movl $0x13, %ecx
callq 0x4b32
testb %al, %al
je 0x456a
movq -0x38(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rsp, -0x48(%rbp)
leal (%rcx,%rax), %r15d
movq %rsp, %rcx
leal 0xf(,%r15,4), %eax
andl $-0x10, %eax
subq %rax, %rcx
movq %rcx, %rsp
leaq 0x4(%rcx), %rax
xorl %r14d, %r14d
movq %rcx, -0x30(%rbp)
movq %rax, -0x50(%rbp)
movq %rbx, %rdi
leaq -0x520(%rbp), %rsi
movl $0x7, %edx
callq 0x4a76
testl %eax, %eax
js 0x4a6d
cmpl $0xf, %eax
ja 0x48b1
movslq %r14d, %rcx
incl %r14d
movq -0x30(%rbp), %rdx
movl %eax, (%rdx,%rcx,4)
movl %r14d, %r12d
jmp 0x4a13
movq %rbx, %rdi
cmpl $0x11, %eax
je 0x4997
cmpl $0x10, %eax
jne 0x49c7
movl $0x2, %esi
movl $0x2, %r13d
callq 0x4ad0
movl 0x24(%rbx), %eax
cmpl $0x2, %eax
jl 0x48f8
movl 0x20(%rbx), %ecx
addl $-0x2, %eax
movl %ecx, %r13d
shrl $0x2, %ecx
andl $0x3, %r13d
movl %ecx, 0x20(%rbx)
movl %eax, 0x24(%rbx)
addl $0x3, %r13d
testl %r14d, %r14d
je 0x4a6d
leal (%r13,%r14), %r12d
cmpl %r15d, %r12d
jg 0x4a6d
movl %r13d, %edx
incl %r13d
movslq %r14d, %rcx
movq -0x30(%rbp), %rax
movdqa 0x99ec(%rip), %xmm3     # 0xe310
pmovsxbq 0x9aeb(%rip), %xmm1     # 0xe418
pcmpeqd %xmm4, %xmm4
pmovsxbq 0x9ae0(%rip), %xmm5     # 0xe41a
decq %rdx
movl -0x4(%rax,%rcx,4), %eax
andl $-0x2, %r13d
movq %rdx, %xmm0
movq -0x50(%rbp), %rdx
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
leaq (%rdx,%rcx,4), %rcx
pxor %xmm3, %xmm0
movdqa %xmm1, %xmm2
pxor %xmm3, %xmm2
pcmpgtq %xmm0, %xmm2
movd %xmm2, %edx
notl %edx
testb $0x1, %dl
je 0x4976
movl %eax, -0x4(%rcx)
pxor %xmm4, %xmm2
pextrb $0x8, %xmm2, %edx
testb $0x1, %dl
je 0x4987
movl %eax, (%rcx)
addq $0x8, %rcx
paddq %xmm5, %xmm1
addq $-0x2, %r13
jne 0x495b
jmp 0x4a13
movl $0x3, %esi
callq 0x4ad0
movl 0x24(%rbx), %eax
movl $0x2, %edx
cmpl $0x3, %eax
jl 0x49f5
movl 0x20(%rbx), %ecx
addl $-0x3, %eax
movl %ecx, %edx
shrl $0x3, %ecx
andl $0x7, %edx
movl %ecx, 0x20(%rbx)
movl %eax, 0x24(%rbx)
addl $0x3, %edx
jmp 0x49f5
movl $0x7, %esi
callq 0x4ad0
movl 0x24(%rbx), %eax
movl $0xa, %edx
cmpl $0x7, %eax
jl 0x49f5
movl 0x20(%rbx), %ecx
addl $-0x7, %eax
movl %ecx, %edx
shrl $0x7, %ecx
andl $0x7f, %edx
movl %ecx, 0x20(%rbx)
movl %eax, 0x24(%rbx)
addl $0xb, %edx
leal (%rdx,%r14), %r12d
cmpl %r15d, %r12d
jg 0x4a6d
movslq %r14d, %rax
movq -0x30(%rbp), %rcx
shll $0x2, %edx
xorl %esi, %esi
leaq (%rcx,%rax,4), %rdi
callq 0x10d0
cmpl %r15d, %r12d
movl %r12d, %r14d
jl 0x487b
movq -0x38(%rbp), %rcx
leaq 0x8034(%rbx), %rdi
movl $0xf, %esi
movq -0x30(%rbp), %rdx
callq 0x4b32
testb %al, %al
je 0x4a6d
movl -0x38(%rbp), %eax
movq -0x30(%rbp), %rcx
leaq 0x18034(%rbx), %rdi
movl $0xf, %esi
leaq (%rcx,%rax,4), %rdx
movq -0x40(%rbp), %rcx
callq 0x4b32
testb %al, %al
je 0x4a6d
movb $0x0, 0x2c(%rbx)
movq -0x48(%rbp), %rsp
jmp 0x46ec
movq -0x48(%rbp), %rsp
jmp 0x456a