// File: /workspace/github_cpp_makefile_star2_O1/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
leaq (%r10,%r10,4), %rbx
movl %r8d, 0x10(%rsi)
movw $0x822, 0x30(%rsi)      # imm = 0x822
movb 0x18(%rax,%rbx,8), %r8b
movb %r8b, 0x32(%rsi)
movb $0x0, 0x33(%rsi)
movl 0x14(%rax,%rbx,8), %r8d
testb $0x3, %r8b
je 0xb736
xorl %r9d, %r9d
cmpl $0x1, %ecx
setne %r9b
incl %r9d
testl %r9d, %r8d
je 0xb751
leaq (%rax,%rbx,8), %rax
movl 0x8(%rax), %r9d
movl %r9d, 0x14(%rsi)
movq 0x210(%rdi), %r11
movl $0x7, %eax
cmpl 0x24(%r11), %r9d
jae 0xb7bb
movslq 0x204(%rdi), %r11
movq 0x1f8(%rdi), %r14
leal 0x1(%r10), %ebp
cmpl %ebp, %r11d
cmovgl %r11d, %ebp
incq %r10
leaq (%r14,%rbx,8), %rbx
addq $0x3c, %rbx
cmpq %r11, %r10
jge 0xb764
incl %edx
incq %r10
testb $0x3, (%rbx)
leaq 0x28(%rbx), %rbx
je 0xb721
jmp 0xb766
leaq 0x30(%rsi), %rax
movq $0x0, 0x14(%rsi)
movq %rax, 0x28(%rsi)
movl $0x4, 0x20(%rsi)
xorl %eax, %eax
jmp 0xb7bb
xorl %eax, %eax
movq %rax, 0x14(%rsi)
movq %rax, 0x28(%rsi)
movl $0x0, 0x20(%rsi)
jmp 0xb7bb
movl %ebp, %edx
movq 0x1f8(%rdi), %r10
leaq 0x1c(%r10), %rbx
cmpl %r11d, %edx
movslq %edx, %rdx
leaq (%rdx,%rdx,4), %rdx
leaq 0x8(%r10,%rdx,8), %rdx
cmoveq %rbx, %rdx
movl (%rdx), %r10d
movl %r10d, 0x18(%rsi)
cmpl %r9d, %r10d
jbe 0xb7bb
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 0xb814
popq %rbx
popq %r14
popq %rbp
retq