// File: /workspace/github_cpp_makefile_star2_O1/OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImageStd.cpp
// Function: CCapsImageStd::GetSampleGap(ImageStreamInfo*)
int CCapsImageStd::GetSampleGap(PIMAGESTREAMINFO pstr)
{
	// get current ipf encoding
	uint32_t ofs=pstr->strofs;
	uint8_t *buf=pstr->strbase;

	uint32_t bitcount, bytecount;

	while (1) {
		// check offset to be within stream boundaries
		if (ofs >= pstr->strsize)
			return imgeTrackData;

		// get stream code and sizeof(count)
		int code=buf[ofs++];

		int vc=code >> CAPS_SIZE_S;
		code&=CAPS_DATAMASK;
		uint32_t count;

		// read the value of the count parameter
		if (vc) {
			if (ofs+vc > pstr->strsize)
				return imgeTrackData;

			count=ReadValue(buf+ofs, vc);
			ofs+=vc;
		} else
			count=0;

		// support data size in bits or bytes
		if (pstr->sizemodebit) {
			bitcount=count;
			bytecount = trackbuf.CalculateByteSize(bitcount);
		} else {
			bytecount=count;
			bitcount=bytecount << 3;
		}

		switch (code) {
			// stream finished
			case cpgapEnd:
				if (bitcount)
					return imgeTrackData;

				pstr->readend=1;
				break;

			// set total sample size
			case cpgapCount:
				pstr->streambc=bitcount;
				continue;

			// data sample
			case cpgapData:
				if (!bitcount)
					return imgeTrackData;

				if (ofs+bytecount > pstr->strsize)
					return imgeTrackData;
				break;

			// any other data type is an error
			default:
				return imgeTrackStream;
		}

		// stop processing after complete data element or stream end
		break;
	}

	// next offset in ipf stream after the current sample
	pstr->strofs=ofs+bytecount;

	// position of the sample data, left justified, starting at bit#7
	pstr->samplebase=buf+ofs;

	// set all sample counters to a simple countdown for the whole sample
	pstr->samplebc=bitcount;
	pstr->remsamplebc=bitcount;

	// set total data size to sample size if undefined, or use the defined data size
	pstr->remstreambc=pstr->streambc ? pstr->streambc : bitcount;

	return imgeOk;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rsi, %rbx
movl 0x1c(%rsi), %ecx
movq 0x28(%rsi), %r14
movl 0x20(%rbx), %r8d
cmpl %r8d, %ecx
jae 0xbd26
movl %ecx, %eax
incl %ecx
movzbl (%r14,%rax), %r15d
xorl %eax, %eax
cmpl $0x20, %r15d
jb 0xbc8c
movl %r15d, %esi
shrl $0x5, %esi
leal (%rsi,%rcx), %r12d
cmpl %r8d, %r12d
jbe 0xbc7f
movl $0xa, %ebp
movl $0x1, %eax
jmp 0xbcf5
movl %ecx, %edi
addq %r14, %rdi
callq 0xf16e
movl %r12d, %ecx
andl $0x1f, %r15d
leal 0x7(%rax), %edi
shrl $0x3, %edi
leal (,%rax,8), %edx
cmpl $0x0, 0x10(%rbx)
cmovnel %eax, %edx
cmovel %eax, %edi
movl $0x1, %eax
cmpl $0x2, %r15d
je 0xbcd4
cmpl $0x1, %r15d
je 0xbcca
testl %r15d, %r15d
jne 0xbce7
testl %edx, %edx
jne 0xbcf0
movl $0x1, 0x3c(%rbx)
jmp 0xbce0
movl %edx, 0x4c(%rbx)
movl $0x2, %eax
jmp 0xbcf5
testl %edx, %edx
je 0xbcee
leal (%rdi,%rcx), %esi
cmpl 0x20(%rbx), %esi
ja 0xbcf0
movl $0x3, %eax
jmp 0xbcf5
movl $0x9, %ebp
jmp 0xbcf5
xorl %edx, %edx
movl $0xa, %ebp
cmpl $0x2, %eax
je 0xbc46
cmpl $0x3, %eax
jne 0xbd2b
addl %ecx, %edi
movl %edi, 0x1c(%rbx)
movl %ecx, %eax
addq %rax, %r14
movq %r14, 0x68(%rbx)
movl %edx, 0x50(%rbx)
movl %edx, 0x58(%rbx)
movl 0x4c(%rbx), %eax
testl %eax, %eax
cmovel %edx, %eax
movl %eax, 0x54(%rbx)
xorl %ebp, %ebp
jmp 0xbd2b
movl $0xa, %ebp
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq