// File: OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImageStd.cpp
// Function: CCapsImageStd::FindLoopPoint(ImageStreamInfo*)
int CCapsImageStd::FindLoopPoint(PIMAGESTREAMINFO pstr)
{
	// no loop point if sample looping is not allowed
	if (!pstr->allowloop) {
		pstr->loopofs=0;
		pstr->loopsize=0;
		pstr->looptype=isiltNone;
		return imgeOk;
	}

	// use a copy of the stream to parse data
	ImageStreamInfo isi=*pstr;

	// no loop point
	uint32_t loopofs=0;
	int loopsize=0, loopfound=0, cnt=0;

	while (!isi.readend) {
		uint32_t ofs=isi.strofs;
		int bc=isi.samplebc;

		switch (isi.strtype) {
			// loop point is the last data in forward gap stream; any data after a loop is invalid
			case isitGap0:
				if (loopfound)
					return imgeGeneric;

				loopofs=ofs;
				loopsize=bc;
				break;

			// loop point is the first data in backward gap stream; any loop after first data is invalid
			case isitGap1:
				if (!cnt) {
					loopofs=ofs;
					loopsize=bc;
				} else
					if (!isi.streambc)
						return imgeGeneric;
				break;
		}

		// only allow 1 loop
		if (!isi.streambc) {
			if (loopfound)
				return imgeGeneric;

			loopfound++;
		}

		cnt++;

		// get next sample
		if (GetSample(&isi) != imgeOk)
			return imgeGeneric;
	}

	// set loop point information
	pstr->loopofs=loopofs;
	pstr->loopsize=loopsize;

	if (loopsize)
		pstr->looptype=loopfound ? isiltStream : isiltAuto;
	else
		pstr->looptype=isiltNone;

	return imgeOk;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x44(%rax)
jne 0x12b0e
movq -0x18(%rbp), %rax
movl $0x0, 0x84(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x88(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x8c(%rax)
movl $0x0, -0x4(%rbp)
jmp 0x12cbc
movq -0x18(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
movl $0xa8, %edx
callq 0x41a0
movl $0x0, -0xc4(%rbp)
movl $0x0, -0xc8(%rbp)
movl $0x0, -0xcc(%rbp)
movl $0x0, -0xd0(%rbp)
cmpl $0x0, -0x84(%rbp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x12b60
jmp 0x12c5c
movl -0xa4(%rbp), %eax
movl %eax, -0xd4(%rbp)
movl -0x70(%rbp), %eax
movl %eax, -0xd8(%rbp)
movl -0xc0(%rbp), %eax
movl %eax, -0xe4(%rbp)
subl $0x1, %eax
je 0x12b95
jmp 0x12b88
movl -0xe4(%rbp), %eax
subl $0x2, %eax
je 0x12bc4
jmp 0x12bfd
cmpl $0x0, -0xcc(%rbp)
je 0x12baa
movl $0x2, -0x4(%rbp)
jmp 0x12cbc
movl -0xd4(%rbp), %eax
movl %eax, -0xc4(%rbp)
movl -0xd8(%rbp), %eax
movl %eax, -0xc8(%rbp)
jmp 0x12bfd
cmpl $0x0, -0xd0(%rbp)
jne 0x12be7
movl -0xd4(%rbp), %eax
movl %eax, -0xc4(%rbp)
movl -0xd8(%rbp), %eax
movl %eax, -0xc8(%rbp)
jmp 0x12bfb
cmpl $0x0, -0x74(%rbp)
jne 0x12bf9
movl $0x2, -0x4(%rbp)
jmp 0x12cbc
jmp 0x12bfb
jmp 0x12bfd
cmpl $0x0, -0x74(%rbp)
jne 0x12c27
cmpl $0x0, -0xcc(%rbp)
je 0x12c18
movl $0x2, -0x4(%rbp)
jmp 0x12cbc
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
movq -0xe0(%rbp), %rdi
movl -0xd0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd0(%rbp)
leaq -0xc0(%rbp), %rsi
callq 0x118b0
cmpl $0x0, %eax
je 0x12c57
movl $0x2, -0x4(%rbp)
jmp 0x12cbc
jmp 0x12b4b
movl -0xc4(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x84(%rax)
movl -0xc8(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x88(%rax)
cmpl $0x0, -0xc8(%rbp)
je 0x12ca7
movl -0xcc(%rbp), %edx
movl $0x1, %ecx
movl $0x2, %eax
cmpl $0x0, %edx
cmovnel %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8c(%rax)
jmp 0x12cb5
movq -0x18(%rbp), %rax
movl $0x0, 0x8c(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
nopl (%rax,%rax)