// File: /workspace/github_c_makefile_star2_O1/DavidSM64[P]DKR-Decompressor/GECompression.cpp
// Function: GECompression::OutputDecompressedBuffer(int&, int&)
unsigned char* GECompression::OutputDecompressedBuffer(int& fileSize, int& compressedSize)
{
	compressedSize = 0;
	if ((compressedBuffer == NULL) || (compressedBufferSize < 3))
		return NULL;


	if ((((compressedBuffer[0] << 8) | compressedBuffer[1]) != 0x1172) && (((compressedBuffer[0] << 8) | compressedBuffer[1]) != 0x1173) && (((compressedBuffer[0] << 8) | compressedBuffer[1]) != 0x68DE)&& (((compressedBuffer[0] << 8) | compressedBuffer[1]) != 0x789C) && (((compressedBuffer[0] << 8) | compressedBuffer[1]) != 0x78DA) && (((compressedBuffer[0] << 24) | (compressedBuffer[1]<<16) | (compressedBuffer[2]<<8) | (compressedBuffer[3])) != 0x1F8B0800) && (((compressedBuffer[0] << 24) | (compressedBuffer[1]<<16) | (compressedBuffer[2]<<8) | (compressedBuffer[3])) != 0x1F8B0808) && (game != BLASTCORPS) && (game != BANJOTOOIE) && (game != CONKER)  && (game != TOPGEARRALLY) && (game != JFG) && (game != DKR) && (game != JFGKIOSK) && (game != MICKEYSPEEDWAY) && (game != ZLB))
	{
		//MessageBox(NULL, "Not 1172/1173/1F8B0800/1F8B0808 Compressed", "Error", NULL);
        std::cout << "Error: Not 1172/1173/1F8B0800/1F8B0808 Compressed" << std::endl;
		return NULL;
	}

	bitsCache = 0;
	bitsRemain = 0;
	if ((game == GOLDENEYE) || (game == KILLERINSTINCT) || (game == MILO) || (game == STUNTRACER64) || (game == RESIDENTEVIL2))
		bytesIndex = 2;
	else if (game == PD)
		bytesIndex = 5;
	else if (game == BANJOKAZOOIE)
		bytesIndex = 6;
	else if ((game == DONKEYKONG64) || (game == DONKEYKONG64KIOSK) || (game == BLASTCORPS))
		bytesIndex = 0xA;
	else if ((game == BANJOTOOIE))
		bytesIndex = 0x2;
	else if (game == CONKER)
		bytesIndex = 4;
	else if (game == TOPGEARRALLY)
		bytesIndex = 0xE;
	else if ((game == JFG) || (game == JFGKIOSK) || (game == DKR) || (game == MICKEYSPEEDWAY))
		bytesIndex = 0x5;
	else if (game == ZLB)
		bytesIndex = 0x12;


	if (((game == DONKEYKONG64) || (game == DONKEYKONG64KIOSK) || (game == BLASTCORPS)) && (((compressedBuffer[0] << 24) | (compressedBuffer[1]<<16) | (compressedBuffer[2]<<8) | (compressedBuffer[3])) == 0x1F8B0808))
	{
		while (compressedBuffer[bytesIndex] != 0)
			bytesIndex++;
		bytesIndex++;
	}
	unpackTableIndex = 0;
	for (int x = 0; x < 0xFFFF; x++)
	{
		unpackTable[x].bits = 0;
		unpackTable[x].flags = 0;
		unpackTable[x].nextIndex = 0;
		unpackTable[x].wordValue = 0;
	}

	unsigned char* returnBuffer;
	returnBuffer = new unsigned char[maxByteSize];


	bool done = false;
	unsigned char compressionType;
	fileSize = 0;

	iterationCounter = 0;

	do
	{
		if (iterationCounter > 268435455)
		{
			fileSize = 0;
			if (returnBuffer != NULL)
				delete [] returnBuffer;
			return NULL;
		}
		iterationCounter++;
		done = GetNBits(1);
		if (bytesIndex >= maxByteSize)
		{
			fileSize = 0;
			if (returnBuffer != NULL)
				delete [] returnBuffer;
			return NULL;
		}
		compressionType = GetNBits(2);
		if (compressionType == 0)
		{
			if (!UncompressType0(returnBuffer, fileSize))
			{
				fileSize = 0;
				if (returnBuffer != NULL)
					delete [] returnBuffer;
				return NULL;
			}
		}
		else if (compressionType == 1)
		{
			if (!UncompressType1(returnBuffer, fileSize))
			{
				fileSize = 0;
				if (returnBuffer != NULL)
					delete [] returnBuffer;
				return NULL;
			}
		}
		else if (compressionType == 2)
		{
			if (!UncompressType2(returnBuffer, fileSize))
			{
				fileSize = 0;
				if (returnBuffer != NULL)
					delete [] returnBuffer;
				return NULL;
			}
		}
		else
		{
			fileSize = 0;
				if (returnBuffer != NULL)
					delete [] returnBuffer;
				return NULL;
		}
	} while (done != 1);

	compressedSize = bytesIndex;
	return returnBuffer;	
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl $0x0, (%rdx)
movq 0x50(%rdi), %rax
xorl %r12d, %r12d
testq %rax, %rax
je 0x3c51
movq %rdi, %r15
cmpl $0x3, 0x58(%rdi)
jl 0x3c51
movq %rdx, %r14
movq %rsi, %rbx
movzbl (%rax), %edx
movl %edx, %esi
shll $0x8, %esi
movzbl 0x1(%rax), %ecx
orl %ecx, %esi
cmpl $0x789b, %esi           # imm = 0x789B
jg 0x3a5c
leal -0x1172(%rsi), %edi
cmpl $0x2, %edi
jb 0x3aa8
cmpl $0x68de, %esi           # imm = 0x68DE
je 0x3aa8
jmp 0x3a6c
cmpl $0x789c, %esi           # imm = 0x789C
je 0x3aa8
cmpl $0x78da, %esi           # imm = 0x78DA
je 0x3aa8
shll $0x18, %edx
shll $0x10, %ecx
orl %edx, %ecx
movzbl 0x2(%rax), %edx
shll $0x8, %edx
orl %ecx, %edx
movzbl 0x3(%rax), %ecx
orl %edx, %ecx
orl $0x8, %ecx
cmpl $0x1f8b0808, %ecx       # imm = 0x1F8B0808
je 0x3aa8
movl (%r15), %ecx
cmpl $0x11, %ecx
ja 0x3c63
movl $0x27b60, %edx          # imm = 0x27B60
btl %ecx, %edx
jae 0x3c63
movq $0x0, 0x38(%r15)
movl $0x0, 0x40(%r15)
movl (%r15), %ecx
movl $0x2, %edx
cmpq $0xa, %rcx
ja 0x3add
leaq 0x3703(%rip), %rsi      # 0x71d0
movslq (%rsi,%rcx,4), %rdi
addq %rsi, %rdi
jmpq *%rdi
movl $0x5, %edx
jmp 0x3b0d
cmpl $0x10, %ecx
je 0x3b0d
cmpl $0x12, %ecx
je 0x3b0d
leal -0x4(%rcx), %edx
cmpl $0xd, %edx
ja 0x3b54
movl $0x27bf, %esi           # imm = 0x27BF
btl %edx, %esi
jae 0x3b54
movl %edx, %edx
leaq 0x371e(%rip), %rsi      # 0x7220
movq (%rsi,%rdx,8), %rdx
jmp 0x3b0d
movl $0x6, %edx
movq %rdx, 0x48(%r15)
cmpl $0x7, %ecx
ja 0x3b54
movl $0xb0, %edx
btl %ecx, %edx
jae 0x3b54
movl (%rax), %ecx
bswapl %ecx
cmpl $0x1f8b0808, %ecx       # imm = 0x1F8B0808
jne 0x3b54
movq 0x48(%r15), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x3b4a
leaq 0x1(%rcx), %rdx
movq %rdx, 0x48(%r15)
cmpb $0x0, 0x1(%rax,%rcx)
movq %rdx, %rcx
jne 0x3b36
jmp 0x3b4d
movq %rcx, %rdx
incq %rdx
movq %rdx, 0x48(%r15)
movl $0x0, 0x30(%r15)
movq 0x28(%r15), %rax
movl $0x10, %ecx
xorl %edx, %edx
movq %rdx, -0x10(%rax,%rcx)
movb $0x0, -0x8(%rax,%rcx)
movl $0x0, -0x4(%rax,%rcx)
movq %rdx, (%rax,%rcx)
addq $0x18, %rcx
cmpq $0x17fff8, %rcx         # imm = 0x17FFF8
jne 0x3b67
movl $0x800000, %edi         # imm = 0x800000
callq 0x2030
movq %rax, %r12
movl $0x0, (%rbx)
movq $0x0, 0x80(%r15)
movq 0x80(%r15), %rax
cmpq $0x10000000, %rax       # imm = 0x10000000
jae 0x3c40
incq %rax
movq %rax, 0x80(%r15)
movq %r15, %rdi
movl $0x1, %esi
callq 0x3928
cmpq $0x800000, 0x48(%r15)   # imm = 0x800000
jae 0x3c40
movq %rax, %r13
movq %r15, %rdi
movl $0x2, %esi
callq 0x3928
cmpb $0x2, %al
je 0x3c1c
movzbl %al, %eax
cmpl $0x1, %eax
je 0x3c0c
testl %eax, %eax
jne 0x3c40
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x3c88
jmp 0x3c2a
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x3d5c
jmp 0x3c2a
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x3e06
testb %al, %al
je 0x3c40
testq %r13, %r13
je 0x3ba8
movl 0x48(%r15), %eax
movl %eax, (%r14)
jmp 0x3c51
movl $0x0, (%rbx)
movq %r12, %rdi
callq 0x21e0
xorl %r12d, %r12d
movq %r12, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x636e(%rip), %rbx      # 0x9fd8
leaq 0x361f(%rip), %rsi      # 0x7290
movl $0x31, %edx
movq %rbx, %rdi
callq 0x21c0
movq %rbx, %rdi
callq 0x20d0
jmp 0x3c4e