// File: /workspace/github_cpp_makefile_star2_O2/OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImageStd.cpp
// Function: CCapsImageStd::ProcessStreamMFM(ImageStreamInfo*)
void CCapsImageStd::ProcessStreamMFM(PIMAGESTREAMINFO pstr)
{
	// get process state
	uint32_t bitpos=pstr->prcbitpos;
	int maxbc=pstr->prcrembc;
	int skipbc=pstr->prcskipbc;
	int actbc=0;

	// ignore last bit written if encoding the first bit and new encoding, use last bit otherwise
	uint32_t lv;
	if (!pstr->prcwritebc && pstr->prcencstate)
		lv=0;
	else {
		uint32_t bp=(bitpos ? bitpos : di.trackbc)-1;
		lv = trackbuf.ReadBit(bp);
	}

	// process remaining bits
	while (maxbc > 0) {
		// read the ipf stream
		int readbc=ReadSample(pstr, mfmreadlen);

		// number of bits not read/missing
		int diffbc=mfmreadlen-readbc;

		// write track data if anything was read
		if (readbc > 0) {
			// number of mfm encoded bits to be written
			int writebc=readbc << 1;

			// skip bits if needed and write bitfield
			if (writebc > skipbc) {
				writebc-=skipbc;
				skipbc=0;

				// calculate the number of missing bits for indexing the mfm code table
				// index size-number of bits to be encoded to reach the target encoded length
				int encodebc=mfmreadlen-((writebc+1) >> 1);

				// get value read
				uint32_t value=pstr->readvalue;

				// add 0 bits for any missing data bits; get correct table index
				value <<= encodebc;

				// convert to mfm
				value=CDiskEncoding::mfmcode[value & mfmindexmask];

				// fix mfm encoding if last bit written is 1
				if (lv & 1)
					value &= mfmmsbclear;

				// right align all valid bits to bit#0; size doubled since value is in mfm now
				value >>= (encodebc << 1);

				// if less bits to write than available, right align field to bit#0
				if (writebc > maxbc) {
					value >>= (writebc-maxbc);
					writebc=maxbc;
				}

				// save last value writen
				lv=value;

				// write value as bitfield
				trackbuf.WriteBitWrap(bitpos, value, writebc);

				// next write position
				actbc+=writebc;
				maxbc-=writebc;
				bitpos+=writebc;

				// handle track buffer wrap around (disk rotation)
				if (bitpos >= di.trackbc)
					bitpos-=di.trackbc;
			} else
				skipbc-=writebc;
		}

		// stop processing if encoding method changed or stream stopped 
		if (diffbc)
			break;
	}

	// set the type of the very first data
	if (!pstr->prcwritebc && pstr->prcencstate && actbc)
		di.block[pstr->actblock].fdenc=ibieMFM;

	// save process state
	pstr->prcbitpos=bitpos;
	pstr->prcrembc=maxbc;
	pstr->prcskipbc=skipbc;
	pstr->prcwritebc+=actbc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r12
movq %rdi, %r14
movl 0x70(%rsi), %ebx
cmpl $0x0, 0x80(%rsi)
je 0xa08e
movl %ebx, %ecx
testl %ebx, %ebx
jne 0xa071
movl 0x1c0(%r14), %ecx
decl %ecx
movq 0xc0(%r14), %rax
movl %ecx, %edx
shrl $0x3, %edx
movzbl (%rax,%rdx), %r15d
notb %cl
andb $0x7, %cl
shrl %cl, %r15d
jmp 0xa099
xorl %r15d, %r15d
cmpl $0x0, 0x7c(%r12)
je 0xa064
movl 0x74(%r12), %eax
movl %eax, 0x8(%rsp)
movl 0x78(%r12), %ebp
leaq 0xb8(%r14), %rax
movq %rax, 0x18(%rsp)
movl $0x0, 0xc(%rsp)
movq %r14, 0x20(%rsp)
movq %r12, 0x10(%rsp)
cmpl $0x0, 0x8(%rsp)
jle 0xa1b7
movl 0x220(%r14), %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x9aee
movl %eax, %r13d
movl 0x220(%r14), %ecx
testl %eax, %eax
jle 0xa1ae
movq %r12, %rdx
leal (,%r13,2), %eax
movl %eax, %r12d
subl %ebp, %r12d
jle 0xa1a9
leal 0x1(%r12), %eax
sarl %eax
movl %ecx, %ebp
subl %eax, %ecx
movl 0x40(%rdx), %eax
shll %cl, %eax
andl 0x224(%r14), %eax
andl $0x1, %r15d
xorl %esi, %esi
cmpl $0x1, %r15d
movl $0x0, %r15d
sbbl %r15d, %r15d
orl 0x228(%r14), %r15d
leaq 0xf435(%rip), %rdx      # 0x19578
movq (%rdx), %rdx
andl (%rdx,%rax,4), %r15d
addl %ecx, %ecx
shrl %cl, %r15d
movl %r12d, %ecx
movl 0x8(%rsp), %r14d
subl %r14d, %ecx
cmovgel %r14d, %r12d
cmovlel %esi, %ecx
shrl %cl, %r15d
movq 0x18(%rsp), %rdi
movl %ebx, %esi
movl %r15d, %edx
movl %r12d, %ecx
callq 0xaae2
movl %ebp, %ecx
xorl %ebp, %ebp
addl %r12d, 0xc(%rsp)
subl %r12d, %r14d
movl %r14d, 0x8(%rsp)
movq 0x20(%rsp), %r14
addl %ebx, %r12d
movl 0x1c0(%r14), %eax
cmpl %eax, %r12d
cmovbl %ebp, %eax
subl %eax, %r12d
movl %r12d, %ebx
movq 0x10(%rsp), %r12
jmp 0xa1ae
subl %eax, %ebp
movq %rdx, %r12
cmpl %r13d, %ecx
je 0xa0c5
movl 0x80(%r12), %eax
testl %eax, %eax
movl 0xc(%rsp), %esi
jne 0xa1eb
cmpl $0x0, 0x7c(%r12)
je 0xa1eb
testl %esi, %esi
je 0xa1eb
movq 0x1f8(%r14), %rcx
movslq 0x4(%r12), %rdx
imulq $0x28, %rdx, %rdx
movl $0x2, 0x20(%rcx,%rdx)
movl %ebx, 0x70(%r12)
movl 0x8(%rsp), %ecx
movl %ecx, 0x74(%r12)
movl %ebp, 0x78(%r12)
addl %esi, %eax
movl %eax, 0x80(%r12)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop