// File: /workspace/github_cpp_makefile_star2_O1/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 $0x18, %rsp
movq %rsi, %r13
movq %rdi, %r14
movl 0x70(%rsi), %ebx
cmpl $0x0, 0x80(%rsi)
je 0xc018
movl %ebx, %ecx
testl %ebx, %ebx
jne 0xbffb
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 0xc022
xorl %r15d, %r15d
cmpl $0x0, 0x7c(%r13)
je 0xbfee
movl 0x74(%r13), %eax
movl %eax, 0x4(%rsp)
movl 0x78(%r13), %ebp
movl $0x0, 0x8(%rsp)
movq %r13, 0x10(%rsp)
cmpl $0x0, 0x4(%rsp)
jle 0xc131
movl 0x220(%r14), %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0xba30
movl %eax, %r12d
movl 0x220(%r14), %ecx
testl %eax, %eax
jle 0xc128
movq %r13, %rdx
leal (%r12,%r12), %eax
movl %eax, %r13d
subl %ebp, %r13d
jle 0xc123
leal 0x1(%r13), %eax
sarl %eax
movl %ecx, 0xc(%rsp)
movl 0xc(%rsp), %ecx
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 0xf4be(%rip), %rdx      # 0x1b578
movq (%rdx), %rdx
andl (%rdx,%rax,4), %r15d
addl %ecx, %ecx
shrl %cl, %r15d
movl %r13d, %ecx
movl 0x4(%rsp), %ebp
subl %ebp, %ecx
cmovgel %ebp, %r13d
cmovlel %esi, %ecx
shrl %cl, %r15d
movq 0xc0(%r14), %rdi
movl 0xcc(%r14), %esi
movl %ebx, %edx
movl %r15d, %ecx
movl %r13d, %r8d
callq 0x4a6a
movl 0xc(%rsp), %ecx
addl %r13d, 0x8(%rsp)
subl %r13d, %ebp
movl %ebp, 0x4(%rsp)
xorl %ebp, %ebp
addl %ebx, %r13d
movl 0x1c0(%r14), %eax
cmpl %eax, %r13d
cmovbl %ebp, %eax
subl %eax, %r13d
movl %r13d, %ebx
movq 0x10(%rsp), %r13
jmp 0xc128
subl %eax, %ebp
movq %rdx, %r13
cmpl %r12d, %ecx
je 0xc03b
movl 0x80(%r13), %eax
testl %eax, %eax
movl 0x8(%rsp), %esi
jne 0xc162
cmpl $0x0, 0x7c(%r13)
je 0xc162
testl %esi, %esi
je 0xc162
movq 0x1f8(%r14), %rcx
movslq 0x4(%r13), %rdx
leaq (%rdx,%rdx,4), %rdx
movl $0x2, 0x20(%rcx,%rdx,8)
movl %ebx, 0x70(%r13)
movl 0x4(%rsp), %ecx
movl %ecx, 0x74(%r13)
movl %ebp, 0x78(%r13)
addl %esi, %eax
movl %eax, 0x80(%r13)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq