// File: /workspace/github_cpp_makefile_star2_O3/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:
testl %edx, %edx
je 0xc4e3
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x150, %rsp            # imm = 0x150
movl %edx, %ebx
movl %esi, %ebp
movq %rdi, %r14
leaq 0xa8(%rsp), %rsi
movl $0x1, %edx
movl %ebp, %ecx
callq 0xb536
testl %eax, %eax
jne 0xc4d3
leaq 0xa8(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xc1c2
testl %eax, %eax
jne 0xc4d3
movq %rsp, %rsi
movq %r14, %rdi
movl $0x2, %edx
movl %ebp, %ecx
callq 0xb536
testl %eax, %eax
jne 0xc4d3
movq %rsp, %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0xc1c2
testl %eax, %eax
je 0xc4e6
addq $0x150, %rsp            # imm = 0x150
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
retq
movl 0x13c(%rsp), %edi
xorl %edx, %edx
movl 0x138(%rsp), %ecx
orl %edi, %ecx
setne %dl
xorl %esi, %esi
cmpl $0x0, 0x94(%rsp)
setne %sil
leal 0x1(%rdx), %eax
testb $0x1, %sil
cmovnel %eax, %edx
cmpl $0x0, 0x90(%rsp)
cmovnel %eax, %edx
movl $0x2, %eax
cmpl %eax, 0x134(%rsp)
setne %r8b
xorl %r9d, %r9d
testl %edi, %edi
sete %dil
setne %r9b
orb %r8b, %dil
cmpl %eax, 0x8c(%rsp)
sete %r8b
andb %sil, %r8b
movq 0x210(%r14), %r10
movq 0xf8(%r10), %r10
movslq %ebp, %r11
leaq (%r11,%r11,2), %r11
shlq $0x4, %r11
testb %dil, %dil
cmovel %eax, %r9d
movl %r9d, 0x20(%r10,%r11)
testb %r8b, %r8b
cmovnel %eax, %esi
movl %esi, 0x24(%r10,%r11)
testl %edx, %edx
je 0xc4d3
cmpl $0x2, %edx
jne 0xc5a8
cmpb %r8b, %dil
je 0xc5c0
leaq 0xa8(%rsp), %rsi
movq %rsp, %rdx
movq %r14, %rdi
movl %ebx, %ecx
callq 0xc746
jmp 0xc4d3
testl %ecx, %ecx
cmovneq %r15, %r12
movq %r14, %rdi
movq %r12, %rsi
movl %ebx, %edx
callq 0xc5de
jmp 0xc4d3
movzbl %dil, %r8d
leaq 0xa8(%rsp), %rsi
movq %rsp, %rdx
movq %r14, %rdi
movl %ebx, %ecx
callq 0xc6d0
jmp 0xc4d3