// File: OpenFodder[P]SOS_Unpacker/capsimg/Codec/CTRawCodecDecompressor.cpp
// Function: CCTRawCodec::DecompressTrack(int)
int CCTRawCodec::DecompressTrack(int verify)
{
	CapsPack cpk;
	CapsWH cwh;

	// in decompress mode free result buffer first
	if (!verify)
		FreeUncompressedTrack();

	// get track header
	PCAPSPACK pk = GetPackHeader(&cpk, wh.ctbuf, wh.ctlen);
	if (!pk)
		return imgeTrackHeader;

	// check crc on compressed data in verify mode
	if (verify)
		if (pk->ccrc != CalcCRC(wh.ctbuf + sizeof(CapsPack), pk->csize))
			return imgeTrackStream;

	// decompress track data
	PCAPSWH wb = DecompressTrack(&cwh, wh.ctbuf, wh.ctlen);
	int res = imgeOk;

	if (verify) {
		// in verify mode check crc on uncompressed data
		if (pk->ucrc != CalcCRC(wb->rawbuf, wb->rawlen))
			res = imgeTrackData;

		// free track data
		FreeUncompressedTrack(wb);
	} else {
		// in decompress mode save track data
		wh.rawbuf = wb->rawbuf;
		wh.rawlen = wb->rawlen;

		for (int trk = 0; trk < CAPS_MTRS; trk++) {
			wh.trkbuf[trk] = wb->trkbuf[trk];
			wh.trklen[trk] = wb->trklen[trk];
		}

		wh.trkcnt = wb->trkcnt;
	}

	return res;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x108(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x61ef
movq -0x108(%rbp), %rdi
callq 0x5ca0
movq -0x108(%rbp), %rax
movq 0x88(%rax), %rsi
movl 0x90(%rax), %edx
leaq -0x2c(%rbp), %rdi
callq 0x5f80
movq %rax, -0xf0(%rbp)
cmpq $0x0, -0xf0(%rbp)
jne 0x6229
movl $0x8, -0x4(%rbp)
jmp 0x63b0
cmpl $0x0, -0x14(%rbp)
je 0x627a
movq -0x108(%rbp), %rax
movq -0xf0(%rbp), %rcx
movl 0x10(%rcx), %ecx
movl %ecx, -0x10c(%rbp)
movq 0x88(%rax), %rdi
addq $0x18, %rdi
movq -0xf0(%rbp), %rax
movl 0xc(%rax), %esi
callq 0x5890
movl %eax, %ecx
movl -0x10c(%rbp), %eax
cmpl %ecx, %eax
je 0x6278
movl $0x9, -0x4(%rbp)
jmp 0x63b0
jmp 0x627a
movq -0x108(%rbp), %rax
movq 0x88(%rax), %rsi
movl 0x90(%rax), %edx
leaq -0xe8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %ecx
callq 0x6700
movq %rax, -0xf8(%rbp)
movl $0x0, -0xfc(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x6306
movq -0xf0(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x110(%rbp)
movq -0xf8(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0xf8(%rbp), %rax
movl 0x38(%rax), %esi
callq 0x5890
movl %eax, %ecx
movl -0x110(%rbp), %eax
cmpl %ecx, %eax
je 0x62f5
movl $0xa, -0xfc(%rbp)
movq -0xf8(%rbp), %rdi
callq 0x5cc0
jmp 0x63a7
movq -0x108(%rbp), %rax
movq -0xf8(%rbp), %rcx
movq 0x30(%rcx), %rcx
movq %rcx, 0x38(%rax)
movq -0xf8(%rbp), %rcx
movl 0x38(%rcx), %ecx
movl %ecx, 0x40(%rax)
movl $0x0, -0x100(%rbp)
cmpl $0x5, -0x100(%rbp)
jge 0x6390
movq -0x108(%rbp), %rax
movq -0xf8(%rbp), %rcx
movslq -0x100(%rbp), %rdx
movq 0x40(%rcx,%rdx,8), %rdx
movslq -0x100(%rbp), %rcx
movq %rdx, 0x48(%rax,%rcx,8)
movq -0xf8(%rbp), %rcx
movslq -0x100(%rbp), %rdx
movl 0x68(%rcx,%rdx,4), %edx
movslq -0x100(%rbp), %rcx
movl %edx, 0x70(%rax,%rcx,4)
movl -0x100(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x100(%rbp)
jmp 0x6333
movq -0x108(%rbp), %rax
movq -0xf8(%rbp), %rcx
movl 0x7c(%rcx), %ecx
movl %ecx, 0x84(%rax)
movl -0xfc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopl (%rax)