// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movl 0x90(%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movl 0x90(%rax), %eax
movl %eax, -0x2c(%rbp)
movl -0x28(%rbp), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x24(%rbp), %eax
jl 0x1354c
movl -0x30(%rbp), %eax
subl -0x24(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
sarl %eax
movl %eax, -0x38(%rbp)
movl -0x34(%rbp), %eax
subl -0x38(%rbp), %eax
movl %eax, -0x3c(%rbp)
movb $0x1, %al
cmpl $0x0, -0x38(%rbp)
movb %al, -0x59(%rbp)
jne 0x134cd
cmpl $0x0, -0x3c(%rbp)
setne %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
testb $0x1, %al
jne 0x134d6
jmp 0x13547
movl -0x28(%rbp), %eax
cmpl -0x38(%rbp), %eax
jl 0x134f2
movl -0x38(%rbp), %ecx
movl -0x28(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x28(%rbp)
movl $0x0, -0x38(%rbp)
jmp 0x1350c
movl -0x38(%rbp), %eax
subl -0x28(%rbp), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jl 0x13528
movl -0x3c(%rbp), %ecx
movl -0x2c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x3c(%rbp)
jmp 0x13542
movl -0x3c(%rbp), %eax
subl -0x2c(%rbp), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x2c(%rbp)
jmp 0x134b8
jmp 0x13675
movq -0x18(%rbp), %rax
cmpl $0x0, 0x94(%rax)
jne 0x13572
movq -0x20(%rbp), %rax
cmpl $0x0, 0x94(%rax)
jne 0x13572
movl $0x2, -0x4(%rbp)
jmp 0x136e5
movq -0x58(%rbp), %rcx
movl -0x24(%rbp), %eax
subl -0x30(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
sarl %eax
movl %eax, -0x44(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl 0x204(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x135ec
movq -0x58(%rbp), %rcx
movl 0x1d8(%rcx), %eax
xorl %edx, %edx
divl 0x1c4(%rcx)
movl %edx, -0x48(%rbp)
movl -0x48(%rbp), %eax
addl -0x28(%rbp), %eax
cmpl 0x1c4(%rcx), %eax
ja 0x135ea
movq -0x58(%rbp), %rcx
movl -0x48(%rbp), %eax
addl -0x28(%rbp), %eax
addl -0x40(%rbp), %eax
cmpl 0x1c4(%rcx), %eax
jb 0x135e8
movq -0x58(%rbp), %rax
movl 0x1c4(%rax), %eax
movl -0x48(%rbp), %ecx
addl -0x28(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x44(%rbp)
jmp 0x135ea
jmp 0x135ec
movl -0x40(%rbp), %eax
subl -0x44(%rbp), %eax
movl %eax, -0x4c(%rbp)
movb $0x1, %al
cmpl $0x0, -0x44(%rbp)
movb %al, -0x5a(%rbp)
jne 0x1360a
cmpl $0x0, -0x4c(%rbp)
setne %al
movb %al, -0x5a(%rbp)
movb -0x5a(%rbp), %al
testb $0x1, %al
jne 0x13613
jmp 0x13673
movq -0x18(%rbp), %rax
cmpl $0x0, 0x94(%rax)
je 0x13632
movl -0x44(%rbp), %eax
addl -0x28(%rbp), %eax
movl %eax, -0x28(%rbp)
movl $0x0, -0x44(%rbp)
jmp 0x13642
movl -0x44(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl $0x0, -0x44(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x94(%rax)
je 0x13661
movl -0x4c(%rbp), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x4c(%rbp)
jmp 0x13671
movl -0x4c(%rbp), %eax
addl -0x44(%rbp), %eax
movl %eax, -0x44(%rbp)
movl $0x0, -0x4c(%rbp)
jmp 0x135f5
jmp 0x13675
movl -0x28(%rbp), %eax
addl -0x2c(%rbp), %eax
cmpl -0x24(%rbp), %eax
je 0x13689
movl $0x2, -0x4(%rbp)
jmp 0x136e5
movq -0x58(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x28(%rbp), %edx
callq 0x13190
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
je 0x136aa
movl -0x50(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x136e5
movq -0x58(%rbp), %rdi
movl $0x1, 0x1e0(%rdi)
movl 0x1d8(%rdi), %eax
movl %eax, 0x1e4(%rdi)
movq -0x20(%rbp), %rsi
movl -0x2c(%rbp), %edx
callq 0x13190
cmpl $0x0, -0x50(%rbp)
je 0x136de
movl -0x50(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x136e5
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop