// File: /workspace/github_cpp_makefile_star2_O1/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), %edi
leal (%rdi,%r8), %eax
movl %eax, %edx
subl %ecx, %edx
jge 0xc8ed
movl 0x94(%rsi), %r9d
testl %r9d, %r9d
jne 0xc85b
cmpl $0x0, 0x94(%rbx)
je 0xc99b
movl %ecx, %r11d
subl %eax, %r11d
movl %r11d, %r10d
sarl %r10d
movl 0x204(%r14), %eax
decl %eax
cmpl %eax, 0x4(%rsi)
jne 0xc89f
movl 0x1c4(%r14), %ebp
movl 0x1d8(%r14), %eax
xorl %edx, %edx
divl %ebp
addl %r8d, %edx
movl %ebp, %eax
subl %edx, %eax
leal (%rdx,%r11), %r15d
cmpl %ebp, %r15d
cmovbl %r10d, %eax
cmpl %edx, %ebp
cmovael %eax, %r10d
subl %r10d, %r11d
movl %r10d, %eax
orl %r11d, %eax
je 0xc951
movl 0x94(%rbx), %eax
movl %r8d, %edx
movl %edi, %r15d
xorl %edi, %edi
testl %r9d, %r9d
movl %r10d, %r8d
cmovel %r9d, %r8d
cmovnel %edi, %r10d
addl %r8d, %edx
addl %r11d, %r10d
testl %eax, %eax
movl %r10d, %r8d
cmovel %eax, %r8d
cmovnel %edi, %r10d
addl %r8d, %r15d
movl $0x0, %r11d
testl %r10d, %r10d
jne 0xc8bc
jmp 0xc957
movl %edx, %eax
sarl %eax
movl %edx, %r9d
subl %eax, %r9d
cmpl $0x2, %edx
setae %dl
testl %r9d, %r9d
setne %r10b
orb %dl, %r10b
cmpb $0x1, %r10b
jne 0xc951
xorl %r10d, %r10d
movl %r8d, %r11d
subl %eax, %r11d
movl %r11d, %edx
cmovll %r10d, %edx
negl %r11d
cmpl %eax, %r8d
cmovgel %r10d, %r11d
addl %r9d, %r11d
movl %edi, %eax
subl %r11d, %eax
movl %eax, %r15d
cmovll %r10d, %r15d
negl %eax
cmpl %r11d, %edi
cmovgel %r10d, %eax
movl %edx, %r8d
movl %r15d, %edi
movl $0x0, %r9d
testl %eax, %eax
jne 0xc910
jmp 0xc957
movl %edi, %r15d
movl %r8d, %edx
leal (%rdx,%r15), %edi
movl $0x2, %eax
cmpl %ecx, %edi
jne 0xc9a0
movq %r14, %rdi
callq 0xc6b4
testl %eax, %eax
jne 0xc9a0
movl $0x1, 0x1e0(%r14)
movl 0x1d8(%r14), %eax
movl %eax, 0x1e4(%r14)
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0xc6b4
xorl %eax, %eax
jmp 0xc9a0
movl $0x2, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nop