// File: /workspace/github_cpp_makefile_star2_O1/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 $0x188, %rsp            # imm = 0x188
movq %rdi, %rbx
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movl $0xc8, %esi
callq 0xca16
leaq 0x28(%rsp), %rax
movq $0x0, (%rax)
leaq 0xd0(%rbx), %r15
leaq 0x164(%rbx), %rax
movq %rax, 0x30(%rsp)
leaq 0x4c(%rbx), %rax
movq %rax, 0x38(%rsp)
leaq 0x86a2(%rip), %r13      # 0x121e4
movq %r15, %rdi
callq 0xd1c2
movl %eax, %r14d
movq %r15, %rdi
xorl %esi, %esi
callq 0xce9a
movl $0x3, %edx
decl %eax
cmpl $0xc, %eax
ja 0x9b7a
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
movl $0x2, 0xc(%rsp)
movl $0x1, %edx
xorl %ecx, %ecx
jmp 0x9dc9
movl 0x170(%rbx), %r12d
movslq %r12d, %rbp
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x3, %rax
cmpq %r12, %rax
ja 0x9bb1
leaq 0x1(%rbp), %rsi
leaq 0x10(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0xcaac
movq 0x10(%rsp), %rax
movl %r14d, 0x4(%rax,%rbp,8)
movq %r15, %rdi
callq 0xd03a
jmp 0x9c34
movl 0x170(%rbx), %r12d
movslq %r12d, %rbp
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x3, %rax
cmpq %r12, %rax
ja 0x9c27
leaq 0x1(%rbp), %rsi
leaq 0x10(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0xcaac
jmp 0x9c27
movl 0x198(%rbx), %r12d
movslq %r12d, %rbp
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x3, %rax
cmpq %r12, %rax
ja 0x9c27
leaq 0x1(%rbp), %rsi
leaq 0x10(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0xcaac
shlq $0x3, %rbp
addq 0x10(%rsp), %rbp
movl %r14d, (%rbp)
cmpl 0x38(%rbx), %r12d
jle 0x9c3e
movl %r12d, 0x38(%rbx)
testl %r12d, %r12d
je 0x9d04
movl %r12d, %edx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rsi
subq %rax, %rsi
sarq $0x3, %rsi
xorl %ecx, %ecx
cmpq %rdx, %rsi
jbe 0x9d06
movslq %r12d, %r12
cmpl $0x0, (%rax,%r12,8)
je 0x9d04
cmpl $0x0, 0x4(%rax,%r12,8)
je 0x9d04
movl $0x148, %edx            # imm = 0x148
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0x4140
movq %r15, %rdi
callq 0xd1c2
movl %eax, %r14d
movq 0x10(%rsp), %rax
movl (%rax,%r12,8), %esi
movq %r15, %rdi
callq 0xd1e4
movq %r15, %rdi
xorl %esi, %esi
callq 0xce9a
cmpl $0xb, %eax
je 0x9d31
cmpl $0xd, %eax
jne 0x9db4
movl $0x3, 0x40(%rsp)
movl 0x164(%rbx), %eax
movl %eax, 0x50(%rsp)
movl 0x168(%rbx), %eax
movl %eax, 0x54(%rsp)
movl 0x18c(%rbx), %eax
movl %eax, 0x58(%rsp)
movl $0x11, %ecx
leaq 0x140(%rsp), %rdi
movq 0x30(%rsp), %rsi
rep  movsl (%rsi), %es:(%rdi)
incl 0x34(%rbx)
jmp 0x9d50
xorl %ecx, %ecx
xorl %edx, %edx
jmp 0x9dc9
movl $0x15, %ecx
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %rsi
rep  movsl (%rsi), %es:(%rdi)
movl $0x1, 0x48(%rbx)
jmp 0x9b7a
movb $0x1, %cl
jmp 0x9dc9
movl $0x2, 0x40(%rsp)
movl 0x168(%rbx), %eax
movl %eax, 0x50(%rsp)
movl 0x16c(%rbx), %eax
movl %eax, 0x54(%rsp)
incl 0x30(%rbx)
movq 0x10(%rsp), %rax
movl (%rax,%r12,8), %ecx
movl %ecx, 0x5c(%rsp)
movl 0x4(%rax,%r12,8), %esi
movl %esi, 0x60(%rsp)
movq %r15, %rdi
callq 0xd1e4
movq %r15, %rdi
xorl %esi, %esi
callq 0xce9a
movq %r15, %rdi
callq 0xd19a
movl %eax, 0x64(%rsp)
movq %rbx, %rdi
leaq 0x40(%rsp), %rsi
callq 0xe950
testl %eax, %eax
sete %bpl
jne 0x9dbb
movl $0x1, 0x18(%rbx)
movb $0x1, %bpl
movq %r15, %rdi
movl %r14d, %esi
callq 0xd1e4
movl 0xc(%rsp), %eax
jmp 0x9dbb
movl $0x2, %eax
xorl %ebp, %ebp
xorb $0x1, %bpl
movzbl %bpl, %edx
xorl %ecx, %ecx
movl %eax, 0xc(%rsp)
cmpl $0x3, %edx
je 0x9dd2
testl %edx, %edx
jne 0x9dde
testb %cl, %cl
je 0x9b42
xorl %ebx, %ebx
jmp 0x9de2
movl 0xc(%rsp), %ebx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x9df9
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x4210
movl %ebx, %eax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9e17
jmp 0x9e17
jmp 0x9e17
jmp 0x9e17
jmp 0x9e17
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x9e31
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x4210
movq %rbx, %rdi
callq 0x43a0
nop