// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
testl %edx, %edx
je 0xa604
movl %edx, %ebx
movl %esi, %r12d
movq %rdi, %r14
leaq 0xb0(%rsp), %rsi
pushq $0x1
popq %rdx
movl %r12d, %ecx
callq 0x96cc
testl %eax, %eax
jne 0xa600
leaq 0xb0(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xa316
testl %eax, %eax
jne 0xa600
leaq 0x8(%rsp), %rsi
pushq $0x2
popq %rbp
movq %r14, %rdi
movl %ebp, %edx
movl %r12d, %ecx
callq 0x96cc
testl %eax, %eax
jne 0xa600
leaq 0x8(%rsp), %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0xa316
testl %eax, %eax
je 0xa61a
movl %eax, %ebp
jmp 0xa606
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x144(%rsp), %esi
xorl %ecx, %ecx
movl 0x140(%rsp), %eax
orl %esi, %eax
setne %cl
xorl %edx, %edx
cmpl $0x0, 0x9c(%rsp)
setne %dl
leal 0x1(%rcx), %edi
testb $0x1, %dl
cmovnel %edi, %ecx
cmpl $0x0, 0x98(%rsp)
cmovnel %edi, %ecx
cmpl %ebp, 0x13c(%rsp)
setne %dil
xorl %r8d, %r8d
testl %esi, %esi
sete %sil
setne %r8b
orb %dil, %sil
cmpl %ebp, 0x94(%rsp)
sete %dil
andb %dl, %dil
movq 0x210(%r14), %r9
movq 0xf8(%r9), %r9
movslq %r12d, %r10
imulq $0x30, %r10, %r10
testb %sil, %sil
cmovel %ebp, %r8d
movl %r8d, 0x20(%r9,%r10)
testb %dil, %dil
cmovnel %ebp, %edx
movl %edx, 0x24(%r9,%r10)
testl %ecx, %ecx
je 0xa606
cmpl $0x2, %ecx
jne 0xa6d3
cmpb %dil, %sil
je 0xa6eb
leaq 0xb0(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %r14, %rdi
movl %ebx, %ecx
callq 0xa870
jmp 0xa600
testl %eax, %eax
cmovneq %r15, %r13
movq %r14, %rdi
movq %r13, %rsi
movl %ebx, %edx
callq 0xa70c
jmp 0xa600
movzbl %sil, %r8d
leaq 0xb0(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %r14, %rdi
movl %ebx, %ecx
callq 0xa7fa
jmp 0xa600
nop