// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
movq %rdx, %rbx
movq %rdi, %r14
movl 0x90(%rsi), %r8d
movl 0x90(%rdx), %r15d
leal (%r15,%r8), %edx
movl %edx, %eax
pushq $0x2
popq %rdi
subl %ecx, %eax
jge 0xa936
movl 0x94(%rsi), %r9d
testl %r9d, %r9d
jne 0xa8b6
cmpl $0x0, 0x94(%rbx)
je 0xa9c3
movl %ecx, %r10d
subl %edx, %r10d
movl %r10d, %r11d
sarl %r11d
movl 0x204(%r14), %eax
decl %eax
cmpl %eax, 0x4(%rsi)
jne 0xa8fa
movl 0x1c4(%r14), %ebp
movl 0x1d8(%r14), %eax
xorl %edx, %edx
divl %ebp
addl %r8d, %edx
movl %ebp, %eax
subl %edx, %eax
leal (%rdx,%r10), %r12d
cmpl %ebp, %r12d
cmovbl %r11d, %eax
cmpl %edx, %ebp
cmovael %eax, %r11d
subl %r11d, %r10d
movl %r11d, %eax
orl %r10d, %eax
je 0xa981
xorl %eax, %eax
testl %r9d, %r9d
movl %r11d, %edx
cmovel %r9d, %edx
cmovnel %eax, %r11d
addl %edx, %r8d
addl %r11d, %r10d
movl 0x94(%rbx), %edx
testl %edx, %edx
cmovnel %r10d, %edx
cmovnel %eax, %r10d
addl %edx, %r15d
movl %r10d, %r11d
xorl %r10d, %r10d
jmp 0xa8fd
movl %eax, %edx
sarl %edx
subl %edx, %eax
movl %edx, %r9d
orl %eax, %r9d
je 0xa981
xorl %r9d, %r9d
movl %r8d, %r10d
subl %edx, %r10d
movl %r10d, %r11d
cmovll %r9d, %r11d
negl %r10d
cmpl %edx, %r8d
cmovgel %r9d, %r10d
addl %eax, %r10d
movl %r15d, %edx
subl %r10d, %edx
movl %edx, %r8d
cmovll %r9d, %r8d
negl %edx
cmpl %r10d, %r15d
cmovgel %r9d, %edx
xorl %eax, %eax
movl %r8d, %r15d
movl %r11d, %r8d
jmp 0xa93c
leal (%r8,%r15), %eax
cmpl %ecx, %eax
jne 0xa9c3
movq %r14, %rdi
movl %r8d, %edx
callq 0xa70c
movl %eax, %edi
testl %eax, %eax
jne 0xa9c3
movl $0x1, 0x1e0(%r14)
movl 0x1d8(%r14), %eax
movl %eax, 0x1e4(%r14)
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0xa70c
xorl %edi, %edi
movl %edi, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq