// File: /workspace/github_cpp_makefile_star2_O3/OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImageStd.cpp
// Function: CCapsImageStd::ScanImage()
int CCapsImageStd::ScanImage()
{
	vector <ScanInfo> si;
	si.reserve(DEF_SCANINFO);
	ScanInfo empty_si = { 0, 0 };
	DiskTrackInfo dt;

	// process complete file
	for (int run = 1; run;) {
		int pos = loader.GetPosition();
		int type = loader.ReadChunk();
		PCAPSCHUNK pc = loader.GetChunk();
		int did = 0;

		switch (type) {
			// end of file
			case CCapsLoader::ccidEof:
				run = 0;
				continue;

			// remember data position
			case CCapsLoader::ccidData:
				did = pc->cg.mod.data.did;
				if ((unsigned)did >= si.size())
					si.resize(did + 1, empty_si);
				si[did].data = pos;
				loader.SkipData();
				break;

			// remember track header position
			case CCapsLoader::ccidTrck:
				did = pc->cg.mod.trck.did;
				if ((unsigned)did >= si.size())
					si.resize(did + 1, empty_si);
				si[did].track = pos;
				break;

			// handle file/format errors
			case CCapsLoader::ccidErrFile:
			case CCapsLoader::ccidErrType:
			case CCapsLoader::ccidErrShort:
			case CCapsLoader::ccidErrHeader:
			case CCapsLoader::ccidErrData:
				return imgeGeneric;

			// image info
			case CCapsLoader::ccidInfo:
				dii.ci = pc->cg.mod.info;
				dii.civalid = 1;
				continue;

			// remember track image descriptor
			case CCapsLoader::ccidImge:
				did = pc->cg.mod.imge.did;
				if ((unsigned)did >= si.size())
					si.resize(did + 1, empty_si);
				si[did].track = pos;
				break;

			// ignore other data
			default:
				continue;
		}

		// find highest data id
		if (did > dii.didmax)
			dii.didmax = did;

		// if did found, process if both header and data available
		if (did && (unsigned)did < si.size()) {
			if (si[did].track && si[did].data) {
				// reset track info
				memset(&dt, 0, sizeof(dt));

				// save image position
				int actpos = loader.GetPosition();

				// create track info
				loader.SetPosition(si[did].track);
				type = loader.ReadChunk();
				pc = loader.GetChunk();

				// set track by type
				switch (type) {
					case CCapsLoader::ccidTrck:
						dt.type = dtitCapsDump;
						dt.cylinder = pc->cg.mod.trck.cyl;
						dt.head = pc->cg.mod.trck.head;
						dii.dmpcount++;
						break;

					case CCapsLoader::ccidImge:
						dt.type = dtitCapsImage;
						dt.cylinder = pc->cg.mod.imge.cylinder;
						dt.head = pc->cg.mod.imge.head;
						dt.sectorcnt = pc->cg.mod.imge.blkcnt;
						dt.ci = pc->cg.mod.imge;
						dii.relcount++;
						break;

					default:
						return imgeGeneric;
				}

				// remember file positions
				dt.headerpos = si[did].track;
				dt.datapos = si[did].data;

				// create data info
				loader.SetPosition(si[did].data);
				loader.ReadChunk();
				dt.datasize = loader.GetDataSize();

				// write info
				int res = AddTrack(&dt);
				if (res != imgeOk)
					return res;

				// save to image is not allowed
				dii.modimage = 1;

				// restore image position
				loader.SetPosition(actpos);
			}
		}
	}

	return imgeOk;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rdi, %rbx
xorps %xmm0, %xmm0
movq %rsp, %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movl $0xc8, %esi
callq 0xc92a
leaq 0x18(%rsp), %rax
movq $0x0, (%rax)
leaq 0xd0(%rbx), %r15
leaq 0x164(%rbx), %rax
movq %rax, 0x20(%rsp)
leaq 0x4c(%rbx), %rax
movq %rax, 0x28(%rsp)
leaq 0x8948(%rip), %r13      # 0x12204
movq %r15, %rdi
callq 0xd29c
movl %eax, %ebp
movq %r15, %rdi
xorl %esi, %esi
callq 0xcf7a
decl %eax
cmpl $0xc, %eax
ja 0x98bc
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
movl 0x170(%rbx), %r14d
movslq %r14d, %r12
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
ja 0x9916
leaq 0x1(%r12), %rsi
movq %rsp, %rdi
leaq 0x18(%rsp), %rdx
callq 0xc9c6
movq (%rsp), %rax
movl %ebp, 0x4(%rax,%r12,8)
movq %r15, %rdi
callq 0xd114
jmp 0x99af
movl $0x15, %ecx
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rsi
rep  movsl (%rsi), %es:(%rdi)
movl $0x1, 0x48(%rbx)
jmp 0x98bc
movl 0x170(%rbx), %r14d
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
ja 0x99a9
movslq %r14d, %rsi
incq %rsi
movq %rsp, %rdi
leaq 0x18(%rsp), %rdx
callq 0xc9c6
jmp 0x99a5
movl 0x198(%rbx), %r14d
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r14, %rcx
ja 0x99a9
movslq %r14d, %rsi
incq %rsi
movq %rsp, %rdi
leaq 0x18(%rsp), %rdx
callq 0xc9c6
movq (%rsp), %rax
movslq %r14d, %rcx
movl %ebp, (%rax,%rcx,8)
cmpl 0x38(%rbx), %r14d
jle 0x99b9
movl %r14d, 0x38(%rbx)
testl %r14d, %r14d
je 0x98bc
movl %r14d, %ecx
movq (%rsp), %rax
movq 0x8(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rcx, %rdx
jbe 0x98bc
movslq %r14d, %r14
cmpl $0x0, (%rax,%r14,8)
je 0x98bc
cmpl $0x0, 0x4(%rax,%r14,8)
je 0x98bc
movl $0x148, %edx            # imm = 0x148
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x4140
movq %r15, %rdi
callq 0xd29c
movl %eax, %ebp
movq (%rsp), %rax
movl (%rax,%r14,8), %esi
movq %r15, %rdi
callq 0xd2be
movq %r15, %rdi
xorl %esi, %esi
callq 0xcf7a
cmpl $0xb, %eax
je 0x9a72
cmpl $0xd, %eax
jne 0x9ae6
movl $0x3, 0x30(%rsp)
movq 0x164(%rbx), %rax
movq %rax, 0x40(%rsp)
movl 0x18c(%rbx), %eax
movl %eax, 0x48(%rsp)
movl $0x11, %ecx
leaq 0x130(%rsp), %rdi
movq 0x20(%rsp), %rsi
rep  movsl (%rsi), %es:(%rdi)
incl 0x34(%rbx)
jmp 0x9a89
movl $0x2, 0x30(%rsp)
movq 0x168(%rbx), %rax
movq %rax, 0x40(%rsp)
incl 0x30(%rbx)
movq (%rsp), %rax
movl (%rax,%r14,8), %ecx
movl %ecx, 0x4c(%rsp)
movl 0x4(%rax,%r14,8), %esi
movl %esi, 0x50(%rsp)
movq %r15, %rdi
callq 0xd2be
movq %r15, %rdi
xorl %esi, %esi
callq 0xcf7a
movq %r15, %rdi
callq 0xd274
movl %eax, 0x54(%rsp)
movq %rbx, %rdi
leaq 0x30(%rsp), %rsi
callq 0xe96c
movl %eax, %r14d
testl %eax, %eax
jne 0x9aec
movl $0x1, 0x18(%rbx)
movq %r15, %rdi
movl %ebp, %esi
callq 0xd2be
jmp 0x98bc
movl $0x2, %r14d
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x9b02
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x4210
movl %r14d, %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r14d, %r14d
jmp 0x9aec
jmp 0x9b26
jmp 0x9b26
jmp 0x9b26
jmp 0x9b26
jmp 0x9b26
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x9b3f
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x4210
movq %rbx, %rdi
callq 0x43a0
nop