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

	int res;
	ImageStreamInfo gsi0, gsi1;

	// initialize the forward gap stream
	res=InitStream(&gsi0, isitGap0, blk);
	if (res != imgeOk)
		return res;

	res=CalculateStreamSize(&gsi0);
	if (res != imgeOk)
		return res;

	// initialize the backward gap stream
	res=InitStream(&gsi1, isitGap1, blk);
	if (res != imgeOk)
		return res;

	res=CalculateStreamSize(&gsi1);
	if (res != imgeOk)
		return res;

	// seN true if stream is enabled
	int se0=gsi0.esfixbc || gsi0.esloopbc;
	int se1=gsi1.esfixbc || gsi1.esloopbc;

	// number of enabled streams
	int secnt=0;
	if (se0)
		secnt++;
	if (se1)
		secnt++;

	// tlN true if stream has a real loop point defined (not generated)
	int tl0=gsi0.esloopbc && (gsi0.looptype == isiltStream);
	int tl1=gsi1.esloopbc && (gsi1.looptype == isiltStream);

	// number of streams with a real loop point
	int tlcnt=0;
	if (tl0)
		tlcnt++;
	if (tl1)
		tlcnt++;

	PDISKSECTORINFO si=di.pdt->sip+blk;

	// set size mode of the forward stream
	if (tl0)
		si->gapws0mode=csiegmResize;
	else
		si->gapws0mode=(gsi0.esloopbc) ? csiegmAuto : csiegmFixed;

	// set size mode of the backward stream
	if (tl1)
		si->gapws1mode=csiegmResize;
	else
		si->gapws1mode=(gsi1.esloopbc) ? csiegmAuto : csiegmFixed;

	// encode gap
	switch (secnt) {
		// error if gap area is present, but there is no gap stream at all
		case 0:
			return imgeGeneric;

		// 1 stream enabled
		case 1:
			res=ProcessBlockGap((se0 ? &gsi0 : &gsi1), gapsize);
			break;

		// 2 streams enabled, modify 1 stream if 1 true loop point is defined, otherwise modify both
		case 2:
			if (tlcnt == 1)
				res=ProcessBlockGap(&gsi0, &gsi1, gapsize, (tl0 ? 0 : 1));
			else
				res=ProcessBlockGap(&gsi0, &gsi1, gapsize);
			break;
	}

	return res;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x198(%rbp)
cmpl $0x0, -0x18(%rbp)
jne 0x12e22
movl $0x0, -0x4(%rbp)
jmp 0x13175
movq -0x198(%rbp), %rdi
movl -0x14(%rbp), %ecx
leaq -0xc8(%rbp), %rsi
movl $0x1, %edx
callq 0x11060
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x12e51
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x13175
movq -0x198(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x12940
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x12e78
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x13175
movq -0x198(%rbp), %rdi
movl -0x14(%rbp), %ecx
leaq -0x170(%rbp), %rsi
movl $0x2, %edx
callq 0x11060
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x12ea7
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x13175
movq -0x198(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x12940
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x12ece
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x13175
movb $0x1, %al
cmpl $0x0, -0x38(%rbp)
movb %al, -0x199(%rbp)
jne 0x12ee9
cmpl $0x0, -0x34(%rbp)
setne %al
movb %al, -0x199(%rbp)
movb -0x199(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x174(%rbp)
movb $0x1, %al
cmpl $0x0, -0xe0(%rbp)
movb %al, -0x19a(%rbp)
jne 0x12f1b
cmpl $0x0, -0xdc(%rbp)
setne %al
movb %al, -0x19a(%rbp)
movb -0x19a(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x178(%rbp)
movl $0x0, -0x17c(%rbp)
cmpl $0x0, -0x174(%rbp)
je 0x12f4e
movl -0x17c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x17c(%rbp)
cmpl $0x0, -0x178(%rbp)
je 0x12f66
movl -0x17c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x17c(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x34(%rbp)
movb %al, -0x19b(%rbp)
je 0x12f81
cmpl $0x2, -0x3c(%rbp)
sete %al
movb %al, -0x19b(%rbp)
movb -0x19b(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x180(%rbp)
xorl %eax, %eax
cmpl $0x0, -0xdc(%rbp)
movb %al, -0x19c(%rbp)
je 0x12fb3
cmpl $0x2, -0xe4(%rbp)
sete %al
movb %al, -0x19c(%rbp)
movb -0x19c(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x184(%rbp)
movl $0x0, -0x188(%rbp)
cmpl $0x0, -0x180(%rbp)
je 0x12fe6
movl -0x188(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x188(%rbp)
cmpl $0x0, -0x184(%rbp)
je 0x12ffe
movl -0x188(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x188(%rbp)
movq -0x198(%rbp), %rax
movq 0x210(%rax), %rax
movq 0xf8(%rax), %rax
movslq -0x14(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x190(%rbp)
cmpl $0x0, -0x180(%rbp)
je 0x1303e
movq -0x190(%rbp), %rax
movl $0x2, 0x20(%rax)
jmp 0x13058
movl -0x34(%rbp), %edx
xorl %ecx, %ecx
movl $0x1, %eax
cmpl $0x0, %edx
cmovnel %eax, %ecx
movq -0x190(%rbp), %rax
movl %ecx, 0x20(%rax)
cmpl $0x0, -0x184(%rbp)
je 0x13071
movq -0x190(%rbp), %rax
movl $0x2, 0x24(%rax)
jmp 0x1308e
movl -0xdc(%rbp), %edx
xorl %ecx, %ecx
movl $0x1, %eax
cmpl $0x0, %edx
cmovnel %eax, %ecx
movq -0x190(%rbp), %rax
movl %ecx, 0x24(%rax)
movl -0x17c(%rbp), %eax
movl %eax, -0x1a0(%rbp)
testl %eax, %eax
je 0x130bd
jmp 0x130a0
movl -0x1a0(%rbp), %eax
subl $0x1, %eax
je 0x130c9
jmp 0x130ad
movl -0x1a0(%rbp), %eax
subl $0x2, %eax
je 0x1310d
jmp 0x1316f
movl $0x2, -0x4(%rbp)
jmp 0x13175
cmpl $0x0, -0x174(%rbp)
je 0x130e2
leaq -0xc8(%rbp), %rax
movq %rax, -0x1a8(%rbp)
jmp 0x130f2
leaq -0x170(%rbp), %rax
movq %rax, -0x1a8(%rbp)
jmp 0x130f2
movq -0x198(%rbp), %rdi
movq -0x1a8(%rbp), %rsi
movl -0x18(%rbp), %edx
callq 0x13190
movl %eax, -0x1c(%rbp)
jmp 0x1316f
cmpl $0x1, -0x188(%rbp)
jne 0x1314d
movq -0x198(%rbp), %rdi
movl -0x18(%rbp), %ecx
movl -0x180(%rbp), %edx
movl $0x1, %r8d
xorl %eax, %eax
cmpl $0x0, %edx
cmovnel %eax, %r8d
leaq -0xc8(%rbp), %rsi
leaq -0x170(%rbp), %rdx
callq 0x13370
movl %eax, -0x1c(%rbp)
jmp 0x1316d
movq -0x198(%rbp), %rdi
movl -0x18(%rbp), %ecx
leaq -0xc8(%rbp), %rsi
leaq -0x170(%rbp), %rdx
callq 0x13450
movl %eax, -0x1c(%rbp)
jmp 0x1316f
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
nopw %cs:(%rax,%rax)