// File: /workspace/github_cpp_makefile_star2_O3/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
movl 0x1c(%rsi), %r15d
movl 0x20(%rsi), %ecx
movl $0xa, %ebp
cmpl %ecx, %r15d
jae 0xbc5e
movq %rsi, %rbx
movq 0x28(%rsi), %r14
movl %r15d, %eax
movl %r15d, %edx
incl %edx
movzbl (%r14,%rax), %r12d
xorl %eax, %eax
cmpl $0x20, %r12d
jae 0xbbb2
movl %edx, %r15d
jmp 0xbbcf
movl %r12d, %esi
shrl $0x5, %esi
leal (%rsi,%rdx), %r15d
cmpl %ecx, %r15d
ja 0xbc5e
movl %edx, %edi
addq %r14, %rdi
callq 0xf1c0
andl $0x1f, %r12d
movl 0x10(%rbx), %edx
leal (,%rax,8), %ecx
testl %edx, %edx
cmovnel %eax, %ecx
cmpl $0x1, %r12d
jne 0xbbf5
movl %ecx, 0x4c(%rbx)
movl 0x20(%rbx), %ecx
cmpl %ecx, %r15d
jb 0xbb98
jmp 0xbc5e
cmpl $0x2, %r12d
je 0xbc27
movl $0x9, %ebp
testl %r12d, %r12d
jne 0xbc5e
movl $0xa, %ebp
testl %ecx, %ecx
jne 0xbc5e
leal 0x7(%rax), %esi
shrl $0x3, %esi
testl %edx, %edx
cmovel %eax, %esi
movl $0x1, 0x3c(%rbx)
addl %r15d, %esi
xorl %ecx, %ecx
jmp 0xbc3e
testl %ecx, %ecx
je 0xbc5e
leal 0x7(%rax), %esi
shrl $0x3, %esi
testl %edx, %edx
cmovel %eax, %esi
addl %r15d, %esi
cmpl 0x20(%rbx), %esi
ja 0xbc5e
movl %esi, 0x1c(%rbx)
movl %r15d, %eax
addq %rax, %r14
movq %r14, 0x68(%rbx)
movl %ecx, 0x50(%rbx)
movl %ecx, 0x58(%rbx)
movl 0x4c(%rbx), %eax
testl %eax, %eax
cmovel %ecx, %eax
movl %eax, 0x54(%rbx)
xorl %ebp, %ebp
movl %ebp, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop