// File: /workspace/github_cpp_makefile_star2_O3/OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImageStd.cpp
// Function: CCapsImageStd::ProcessBlockGap(ImageStreamInfo*, ImageStreamInfo*, int)
int CCapsImageStd::ProcessBlockGap(PIMAGESTREAMINFO pg0, PIMAGESTREAMINFO pg1, int gapsize)
{
	int gs0=pg0->esfixbc;
	int gs1=pg1->esfixbc;
	int fs=gs0+gs1;

	if (fs >= gapsize) {
		// fixed size would fill the gap; number of extra bits not needed
		int rem=fs-gapsize;
		int rem0=rem >> 1;
		int rem1=rem-rem0;

		// remove bits evenly as long as it is possible, then just remove from any stream that has enough bits
		while (rem0 || rem1) {
			if (gs0 >= rem0) {
				gs0-=rem0;
				rem0=0;
			} else {
				rem1+=rem0-gs0;
				rem0=0;
				gs0=0;
			}

			if (gs1 >= rem1) {
				gs1-=rem1;
				rem1=0;
			} else {
				rem0+=rem1-gs1;
				rem1=0;
				gs1=0;
			}
		}
	} else {
		// loop required to fill the gap; error if loop point is not found in either stream
		if (!pg0->esloopbc && !pg1->esloopbc)
			return imgeGeneric;

		// number of missing bits to fill the gap
		int miss=gapsize-fs;
		int miss0=miss >> 1;

		// if last block and the loop would pass the index (ie track gap at index) re-sync to index
		if (pg0->actblock == di.blockcount-1) {
			uint32_t sbp=di.encbitpos % di.singletrackbc;
			if (sbp+gs0 <= di.singletrackbc)
				if (sbp+gs0+miss >= di.singletrackbc)
					miss0=di.singletrackbc-(sbp+gs0);
		}

		int miss1=miss-miss0;

		// add bits evenly if both streams have loop points, otherwise just use the one with loop enabled
		while (miss0 || miss1) {
			if (pg0->esloopbc) {
				gs0+=miss0;
				miss0=0;
			} else {
				miss1+=miss0;
				miss0=0;
			}

			if (pg1->esloopbc) {
				gs1+=miss1;
				miss1=0;
			} else {
				miss0+=miss1;
				miss1=0;
			}
		}
	}

	// check if selected stream sizes would fill the entire gap
	if (gs0+gs1 != gapsize)
		return imgeGeneric;

	// encode forward stream
	int res=ProcessBlockGap(pg0, gs0);
	if (res != imgeOk)
		return res;

	// set gap split position
	di.encgsvalid=1;
	di.encgapsplit=di.encbitpos;

	// encode backwards stream
	ProcessBlockGap(pg1, gs1);
	if (res != imgeOk)
		return res;

	return imgeOk;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rdx, %rbx
movq %rdi, %r14
movl 0x90(%rsi), %r8d
movl 0x90(%rdx), %r15d
leal (%r15,%r8), %eax
movl %eax, %edi
subl %ecx, %edi
jge 0xc818
movl 0x94(%rsi), %edi
testl %edi, %edi
jne 0xc786
cmpl $0x0, 0x94(%rbx)
je 0xc8b1
movl %ecx, %r9d
subl %eax, %r9d
movl %r9d, %r10d
sarl %r10d
movl 0x204(%r14), %eax
decl %eax
cmpl %eax, 0x4(%rsi)
jne 0xc7cd
movl 0x1c4(%r14), %r11d
movl 0x1d8(%r14), %eax
xorl %edx, %edx
divl %r11d
addl %r8d, %edx
movl %r11d, %eax
subl %edx, %eax
leal (%rdx,%r9), %ebp
cmpl %r11d, %ebp
cmovbl %r10d, %eax
cmpl %edx, %r11d
cmovael %eax, %r10d
subl %r10d, %r9d
movl %r10d, %eax
orl %r9d, %eax
je 0xc86a
movl 0x94(%rbx), %eax
movl %r15d, %edx
movl %r8d, %r11d
movl %r9d, %ebp
xorl %r9d, %r9d
testl %edi, %edi
movl %r10d, %r8d
cmovel %edi, %r8d
cmovnel %r9d, %r10d
addl %r11d, %r8d
addl %ebp, %r10d
testl %eax, %eax
movl %r10d, %r15d
cmovel %eax, %r15d
addl %edx, %r15d
testl %eax, %eax
jne 0xc86a
testl %r10d, %r10d
jne 0xc7e2
jmp 0xc86a
movl %edi, %eax
sarl %eax
movl %edi, %edx
subl %eax, %edx
cmpl $0x2, %edi
setae %dil
testl %edx, %edx
setne %r9b
orb %dil, %r9b
cmpb $0x1, %r9b
jne 0xc86a
movl %edx, %edi
movl %r8d, %r9d
xorl %edx, %edx
movl %r8d, %r10d
subl %eax, %r10d
movl %r10d, %r8d
cmovll %edx, %r8d
negl %r10d
cmpl %eax, %r9d
cmovgel %edx, %r10d
addl %edi, %r10d
movl %r15d, %eax
subl %r10d, %eax
subl %r10d, %r15d
cmovll %edx, %r15d
jge 0xc86a
negl %eax
jne 0xc836
leal (%r8,%r15), %edx
movl $0x2, %eax
cmpl %ecx, %edx
jne 0xc8b6
movq %r14, %rdi
movl %r8d, %edx
callq 0xc5de
testl %eax, %eax
jne 0xc8b6
movl $0x1, 0x1e0(%r14)
movl 0x1d8(%r14), %eax
movl %eax, 0x1e4(%r14)
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0xc5de
xorl %eax, %eax
jmp 0xc8b6
movl $0x2, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nop