// File: /workspace/github_cpp_makefile_star2_O2/OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImageStd.cpp
// Function: CCapsImageStd::InitGapStream(ImageStreamInfo*)
int CCapsImageStd::InitGapStream(PIMAGESTREAMINFO pstr)
{
	// sample looping on zero data count is allowed
	pstr->allowloop=1;

	PIMAGEBLOCKINFO pi=di.block+pstr->actblock;

	// always set data size mode to bits for a gap stream
	pstr->sizemodebit=1;

	// generate gap stream from gap value; 1 byte size, 8 bit pattern, gap value
	int align=(pstr->strtype == isitGap0) ? 0 : 1;
	int gflag=align ? CAPS_BF_GP1 : CAPS_BF_GP0;
	int es=1;
	int gc=0;
	pstr->gapstr[gc++]=cpgapData | es<<CAPS_SIZE_S;
	pstr->gapstr[gc++]=8;
	pstr->gapstr[gc++]=(uint8_t)pi->gapvalue;
	pstr->gapstr[gc++]=cpgapEnd;

	// use the generated gap stream if no gap streams are available at all
	if (!(pi->flag & (CAPS_BF_GP0 | CAPS_BF_GP1))) {
		pstr->strstart=0;
		pstr->strend=0;
		pstr->strbase=pstr->gapstr;
		pstr->strsize=gc;
		return imgeOk;
	}

	// disable gap stream if not available
	if (!(pi->flag & gflag)) {
		pstr->strstart=0;
		pstr->strend=0;
		pstr->strbase=NULL;
		pstr->strsize=0;
		return imgeOk;
	}

	// stream start from block descriptor
	pstr->strstart=pi->gapoffset;

	// check buffer overrun for file limit
	if (pstr->strstart >= (uint32_t)di.pdt->datasize)
		return imgeShort;

	// find the next gap stream stored
	int bls;
	for (bls=pstr->actblock+1; bls < di.blockcount; bls++) {
		if (di.block[bls].flag & (CAPS_BF_GP0 | CAPS_BF_GP1))
			break;
	}

	// stream ends at the beginning of the data streams if this is the last gap stream stored
	// if another gap streams follows, this one ends where the next one begins
	if (bls == di.blockcount)
		pstr->strend=di.block[0].dataoffset;
	else
		pstr->strend=di.block[bls].gapoffset;

	// check buffer overrun for stream limit
	if (pstr->strstart >= pstr->strend)
		return imgeShort;

	// set stream pointer and maximum size
	pstr->strbase=di.data+pstr->strstart;
	pstr->strsize=pstr->strend-pstr->strstart;

	// resize stream; find the end of stream#0; if stream#0 is not present gap stream starts with stream#1
	int semode=align && (pi->flag & CAPS_BF_GP0);
	int res=FindGapStreamEnd(pstr, semode);

	return res;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
movl $0x1, %r8d
movl %r8d, 0x44(%rsi)
movq 0x1f8(%rdi), %rax
movl (%rsi), %ecx
movl 0x4(%rsi), %edx
movslq %edx, %r10
imulq $0x28, %r10, %r14
movl %r8d, 0x10(%rsi)
movw $0x822, 0x30(%rsi)      # imm = 0x822
movb 0x18(%rax,%r14), %r8b
movb %r8b, 0x32(%rsi)
movb $0x0, 0x33(%rsi)
movl 0x14(%rax,%r14), %r8d
testb $0x3, %r8b
je 0x9867
xorl %r9d, %r9d
cmpl $0x1, %ecx
setne %r9b
incl %r9d
testl %r9d, %r8d
je 0x987d
addq %r14, %rax
movl 0x8(%rax), %r9d
movl %r9d, 0x14(%rsi)
movq 0x210(%rdi), %r11
movl $0x7, %eax
cmpl 0x24(%r11), %r9d
jae 0x988d
movslq 0x204(%rdi), %r11
movq 0x1f8(%rdi), %rbx
leal 0x1(%r10), %ebp
cmpl %ebp, %r11d
cmovgl %r11d, %ebp
incq %r10
addq %rbx, %r14
addq $0x3c, %r14
cmpq %r11, %r10
jge 0x9892
incl %edx
incq %r10
testb $0x3, (%r14)
leaq 0x28(%r14), %r14
je 0x9851
jmp 0x9894
andq $0x0, 0x14(%rsi)
leaq 0x30(%rsi), %rax
movq %rax, 0x28(%rsi)
movl $0x4, 0x20(%rsi)
jmp 0x988b
andq $0x0, 0x14(%rsi)
andq $0x0, 0x28(%rsi)
andl $0x0, 0x20(%rsi)
xorl %eax, %eax
popq %rbx
popq %r14
popq %rbp
retq
movl %ebp, %edx
leaq 0x1c(%rbx), %r10
movslq %edx, %rdx
imulq $0x28, %rdx, %r14
cmpl %r11d, %edx
leaq 0x8(%rbx,%r14), %rdx
cmoveq %r10, %rdx
movl (%rdx), %r10d
movl %r10d, 0x18(%rsi)
cmpl %r9d, %r10d
jbe 0x988d
movq %r9, %rax
addq 0x1e8(%rdi), %rax
subl %r9d, %r10d
andl $0x1, %r8d
xorl %edx, %edx
cmpl $0x1, %ecx
movq %rax, 0x28(%rsi)
cmovnel %r8d, %edx
movl %r10d, 0x20(%rsi)
popq %rbx
popq %r14
popq %rbp
jmp 0x9936