// File: OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImageStd.cpp
// Function: CCapsImageStd::UpdateWeakBit(int)
int CCapsImageStd::UpdateWeakBit(int group)
{
	// don't change track data if updates are disabled
	if (di.flag & DI_LOCK_NOUPDATE)
		return imgeOk;

	// get random seed
	uint32_t seed=di.pdt->wseed;

	// update all areas marked
	for (int pos=0; pos < di.pdt->fdpsize; pos++) {
		PDISKDATAMARK pd=di.pdt->fdp+pos;

		// skip markers that belong to a different group
		if (pd->group != group)
			continue;

		uint32_t bitpos=pd->position;
		int bitcnt=pd->size;

		// write generated data
		while (bitcnt > 0) {
			// simple random generator
			seed<<=1;
			if ((seed>>22 ^ seed) & DF_1)
				seed++;

			// write largest possible bitfield
			int writebc=(bitcnt >= maxwritelen) ? maxwritelen : bitcnt;

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

			// next write position
			bitcnt-=writebc;
			bitpos+=writebc;

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

	// save random seed, so any new update on the same track would give new results
	di.pdt->wseed=seed;

	return imgeOk;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
movl 0x208(%rax), %eax
andq $0x2000, %rax           # imm = 0x2000
cmpq $0x0, %rax
je 0xf045
movl $0x0, -0x4(%rbp)
jmp 0xf197
movq -0x40(%rbp), %rax
movq 0x210(%rax), %rax
movl 0xc4(%rax), %eax
movl %eax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x40(%rbp), %rcx
movl -0x1c(%rbp), %eax
movq 0x210(%rcx), %rcx
cmpl 0xd0(%rcx), %eax
jge 0xf17c
movq -0x40(%rbp), %rax
movq 0x210(%rax), %rax
movq 0xd8(%rax), %rax
movslq -0x1c(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
je 0xf0ab
jmp 0xf16e
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jle 0xf16c
movl -0x18(%rbp), %eax
shll %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
shrl $0x16, %eax
xorl -0x18(%rbp), %eax
movl %eax, %eax
andq $0x2, %rax
cmpq $0x0, %rax
je 0xf0ef
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
movq -0x40(%rbp), %rcx
movl -0x30(%rbp), %eax
cmpl 0x218(%rcx), %eax
jl 0xf10d
movq -0x40(%rbp), %rax
movl 0x218(%rax), %eax
movl %eax, -0x44(%rbp)
jmp 0xf113
movl -0x30(%rbp), %eax
movl %eax, -0x44(%rbp)
movq -0x40(%rbp), %rdi
movl -0x44(%rbp), %eax
movl %eax, -0x34(%rbp)
addq $0xb8, %rdi
movl -0x2c(%rbp), %esi
movl -0x18(%rbp), %edx
movl -0x34(%rbp), %ecx
callq 0x13a60
movq -0x40(%rbp), %rcx
movl -0x34(%rbp), %edx
movl -0x30(%rbp), %eax
subl %edx, %eax
movl %eax, -0x30(%rbp)
movl -0x34(%rbp), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl 0x1c0(%rcx), %eax
jb 0xf167
movq -0x40(%rbp), %rax
movl 0x1c0(%rax), %ecx
movl -0x2c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x2c(%rbp)
jmp 0xf0bf
jmp 0xf16e
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xf060
movq -0x40(%rbp), %rax
movl -0x18(%rbp), %ecx
movq 0x210(%rax), %rax
movl %ecx, 0xc4(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq