// File: OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImageStd.cpp
// Function: CCapsImageStd::ProcessBlockGap(ImageStreamInfo*, int)
int CCapsImageStd::ProcessBlockGap(PIMAGESTREAMINFO pg, int gapsize)
{
	// stop if no gap area
	if (!gapsize)
		return imgeOk;

	int skip=0;

	if (pg->esfixbc >= gapsize) {
		// fixed size is enough to fill the gap; disable loop
		SetLoop(pg, 0);

		// if backwards stream, skip unneeded bits
		if (pg->strtype == isitGap1)
			skip=pg->esfixbc-gapsize;
	} else {
		// loop required to fill the gap; error if loop point is not found
		if (!pg->esloopbc)
			return imgeGeneric;

		// number of bits to be generated using the loop
		int miss=gapsize-pg->esfixbc;

		// number of new samples required to generate the bits
		int scnt=miss/pg->esloopbc;

		// partial new sample size, 0 if all samples are complete
		int smod=miss%pg->esloopbc;

		// if partial sample is required
		if (smod) {
			// add another sample
			scnt++;

			// if backwards stream, skip bits not required from the added sample
			if (pg->strtype == isitGap1)
				skip=pg->esloopbc-smod;
		}

		// enable loop
		SetLoop(pg, scnt);
	}

	// encode gap data
	int res=ProcessStream(pg, di.encbitpos, gapsize, skip, !di.encwritebc);
	if (res != imgeOk)
		return res;

	// error if the requested data size was not filled
	if (pg->prcwritebc != gapsize)
		return imgeGeneric;

	// update bit position and bits written
	di.encbitpos=pg->prcbitpos;
	di.encwritebc+=pg->prcwritebc;

	if (pg->strtype == isitGap0)
		di.pdt->sip[pg->actblock].gapsizews0=gapsize;
	else
		di.pdt->sip[pg->actblock].gapsizews1=gapsize;

	return imgeOk;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x131bd
movl $0x0, -0x4(%rbp)
jmp 0x13367
movl $0x0, -0x20(%rbp)
movq -0x18(%rbp), %rax
movl 0x90(%rax), %eax
cmpl -0x1c(%rbp), %eax
jl 0x13200
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rsi
xorl %edx, %edx
callq 0x136f0
movq -0x18(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x131fb
movq -0x18(%rbp), %rax
movl 0x90(%rax), %eax
subl -0x1c(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0x13285
movq -0x18(%rbp), %rax
cmpl $0x0, 0x94(%rax)
jne 0x13219
movl $0x2, -0x4(%rbp)
jmp 0x13367
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
subl 0x90(%rcx), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x18(%rbp), %rcx
cltd
idivl 0x94(%rcx)
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
movq -0x18(%rbp), %rcx
cltd
idivl 0x94(%rcx)
movl %edx, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x13275
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x13273
movq -0x18(%rbp), %rax
movl 0x94(%rax), %eax
subl -0x2c(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0x13275
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x28(%rbp), %edx
callq 0x136f0
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl 0x1d8(%rdi), %edx
movl -0x1c(%rbp), %ecx
movl -0x20(%rbp), %r8d
cmpl $0x0, 0x1dc(%rdi)
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %r9d
callq 0x12240
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x132c5
movl -0x30(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x13367
movq -0x18(%rbp), %rax
movl 0x80(%rax), %eax
cmpl -0x1c(%rbp), %eax
je 0x132e0
movl $0x2, -0x4(%rbp)
jmp 0x13367
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x70(%rcx), %ecx
movl %ecx, 0x1d8(%rax)
movq -0x18(%rbp), %rcx
movl 0x80(%rcx), %ecx
addl 0x1dc(%rax), %ecx
movl %ecx, 0x1dc(%rax)
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x13339
movq -0x38(%rbp), %rax
movl -0x1c(%rbp), %ecx
movq 0x210(%rax), %rax
movq 0xf8(%rax), %rax
movq -0x18(%rbp), %rdx
movslq 0x4(%rdx), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x18(%rax)
jmp 0x13360
movq -0x38(%rbp), %rax
movl -0x1c(%rbp), %ecx
movq 0x210(%rax), %rax
movq 0xf8(%rax), %rax
movq -0x18(%rbp), %rdx
movslq 0x4(%rdx), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x1c(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq