// File: OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImageStd.cpp
// Function: CCapsImageStd::LoadTrack(DiskTrackInfo*, unsigned int)
int CCapsImageStd::LoadTrack(PDISKTRACKINFO pti, UDWORD flag)
{
	// set lock flags
	di.pdt=pti;
	di.flag=flag;

	int res = imgeOk;

	// update the track image if already available
	if (pti->trackcnt) {
		switch (pti->type) {
			case dtitCapsDump:
				res = UpdateDump();
				break;

			case dtitCapsImage:
				res = UpdateImage(0);
				break;

			default:
				res = imgeGeneric;
				break;
		}

		return res;
	}

	// process stored track data or generate data
	if (pti->datasize) {
		// load data, check data header
		loader.SetPosition(pti->datapos);
		if (loader.ReadChunk() == CCapsLoader::ccidData) {
			// allocate data buffer if needed
			AllocDiskData(pti->datasize);
			di.datacount=pti->datasize;

			// load and decode data
			if (loader.ReadData(di.data) == pti->datasize) {
				switch (pti->type) {
					case dtitCapsDump:
						res=DecompressDump();
						break;

					case dtitCapsImage:
						res=DecodeImage();
						break;

					default:
						res=imgeGeneric;
						break;
				}
			} else
				res=imgeGeneric;
		} else
			res=imgeGeneric;
	} else {
		// generate data
		switch (pti->type) {
			case dtitCapsImage:
				di.datacount=0;
				res=DecodeImage();
				break;

			default:
				res=imgeGeneric;
				break;
		}
	}

	return res;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rcx
movq %rcx, 0x210(%rax)
movl -0x1c(%rbp), %ecx
movl %ecx, 0x208(%rax)
movl $0x0, -0x20(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x28(%rax)
je 0xed0b
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
subl $0x2, %eax
je 0xecda
jmp 0xecd0
movl -0x2c(%rbp), %eax
subl $0x3, %eax
je 0xece9
jmp 0xecf9
movq -0x28(%rbp), %rdi
movq (%rdi), %rax
callq *0x40(%rax)
movl %eax, -0x20(%rbp)
jmp 0xed00
movq -0x28(%rbp), %rdi
xorl %esi, %esi
callq 0xee20
movl %eax, -0x20(%rbp)
jmp 0xed00
movl $0x2, -0x20(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xee11
movq -0x18(%rbp), %rax
cmpl $0x0, 0x24(%rax)
je 0xeddd
movq -0x28(%rbp), %rdi
addq $0xd0, %rdi
movq -0x18(%rbp), %rax
movl 0x20(%rax), %esi
callq 0x15970
movq -0x28(%rbp), %rdi
addq $0xd0, %rdi
xorl %esi, %esi
callq 0x15380
cmpl $0xa, %eax
jne 0xedd4
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x24(%rax), %esi
callq 0xef30
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x24(%rcx), %ecx
movl %ecx, 0x1f4(%rax)
movq %rax, %rdi
addq $0xd0, %rdi
movq 0x1e8(%rax), %rsi
callq 0x15790
movq -0x18(%rbp), %rcx
cmpl 0x24(%rcx), %eax
jne 0xedcb
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x30(%rbp)
subl $0x2, %eax
je 0xeda5
jmp 0xed9b
movl -0x30(%rbp), %eax
subl $0x3, %eax
je 0xedb4
jmp 0xedc2
movq -0x28(%rbp), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movl %eax, -0x20(%rbp)
jmp 0xedc9
movq -0x28(%rbp), %rdi
callq 0xef90
movl %eax, -0x20(%rbp)
jmp 0xedc9
movl $0x2, -0x20(%rbp)
jmp 0xedd2
movl $0x2, -0x20(%rbp)
jmp 0xeddb
movl $0x2, -0x20(%rbp)
jmp 0xee0b
movq -0x18(%rbp), %rax
movl (%rax), %eax
subl $0x3, %eax
jne 0xee02
jmp 0xedea
movq -0x28(%rbp), %rdi
movl $0x0, 0x1f4(%rdi)
callq 0xef90
movl %eax, -0x20(%rbp)
jmp 0xee09
movl $0x2, -0x20(%rbp)
jmp 0xee0b
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)