// File: /workspace/github_cpp_makefile_star2_O2/OpenFodder[P]SOS_Unpacker/capsimg/CAPSImg/CapsImage.cpp
// Function: CCapsImage::FindWeakBits()
void CCapsImage::FindWeakBits()
{
	PDISKTRACKINFO pti = di.pdt;

	// true if all the available revolutions should be returned
	int allrev = (pti->trackcnt == pti->rawtrackcnt);

	// process all revolutions
	for (int rev = 0; rev < pti->rawtrackcnt; rev++) {
		// get the track data buffer and the size of the buffer
		PUBYTE trackbuf = pti->trackdata[rev];
		int tracklen = allrev ? pti->rawlen : pti->tracksize[rev];

		// byte offset in track buffer for the current bitcell
		int bofs = 0;

		// number of consecutive 0 bitcells since the last 1 bit
		int zcnt = 0;

		// start position of the area containing 0s
		int lastbp = 0;

		// bitcell position in the current data byte bitcell; position#0 is bit#7
		int actbit = 0;

		// process the entire track buffer
		while (bofs < tracklen) {
			// get actual byte from track buffer
			uint8_t bval = trackbuf[bofs];
			
			while (1) {
				// first 0 bit position
				int f0b;

				// if area is empty search for area starting with 0 bit value
				if (!zcnt) {
					// find the first 0 bit from the current position in the current byte
					f0b = f0b_table[actbit][bval];
					if (f0b == 8) {
						// 0 bit not found, continue with the next byte
						break;
					} else {
						// 0 bit found, set the current bit position
						actbit = f0b;

						// set the area start position
						lastbp = bofs << 3 | actbit;
					}
				} else {
					// area not empty, start from position#0
					f0b = 0;
				}

				// find the first 1 bit from the current position in the current byte
				int f1b = f1b_table[actbit][bval];

				// add the number of bits from the last position (8 if no 1 bit found)
				zcnt += f1b - f0b;

				if (f1b == 8) {
					// 1 bit not found, continue with the next byte
					break;
				} else {
					// start the next search for 0 from the current position
					actbit = f1b;
				}

				// add the area
				if (zcnt >= MIN_CTRAW_WEAK_SIZE && zcnt <= MAX_CTRAW_WEAK_SIZE)
					AddWeakBitArea(rev, lastbp, zcnt);

				// area empty
				zcnt = 0;
			}

			// continue with the next byte
			actbit = 0;
			bofs++;
		}

		// add the last area if exists (loop ended before it was added)
		if (zcnt >= MIN_CTRAW_WEAK_SIZE && zcnt <= MAX_CTRAW_WEAK_SIZE)
			AddWeakBitArea(rev, lastbp, zcnt);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, 0x10(%rsp)
movq 0x210(%rdi), %rcx
movl 0x28(%rcx), %eax
movl %eax, 0xc(%rsp)
movl 0x2c(%rcx), %eax
leaq 0x30(%rcx), %rdx
movq %rdx, 0x20(%rsp)
leaq 0x80(%rcx), %rdx
movq %rdx, 0x18(%rsp)
xorl %r12d, %r12d
leaq 0x1112d(%rip), %rdx     # 0x18d50
movl %eax, 0x8(%rsp)
movq %rcx, 0x28(%rsp)
cltq
cmpq %rax, %r12
jge 0x7d42
movl 0x8(%rsp), %eax
cmpl %eax, 0xc(%rsp)
movq 0x18(%rsp), %rax
leaq (%rax,%r12,4), %rax
cmoveq 0x20(%rsp), %rax
movq 0x58(%rcx,%r12,8), %rcx
movq %rcx, 0x30(%rsp)
movl (%rax), %eax
xorl %esi, %esi
testl %eax, %eax
cmovlel %esi, %eax
movq %rax, 0x38(%rsp)
xorl %r15d, %r15d
xorl %ebp, %ebp
cmpq 0x38(%rsp), %rsi
je 0x7d10
movq 0x30(%rsp), %rax
movzbl (%rax,%rsi), %r13d
movq %rsi, 0x40(%rsp)
leal (,%rsi,8), %ebx
xorl %r14d, %r14d
testl %r15d, %r15d
je 0x7c98
xorl %eax, %eax
jmp 0x7cbd
movslq %r14d, %rax
shlq $0x8, %rax
leaq 0x108aa(%rip), %rcx     # 0x18550
addq %rcx, %rax
movsbl (%r13,%rax), %r14d
cmpl $0x8, %r14d
je 0x7cfe
movl %ebx, %ebp
orl %r14d, %ebp
movl %r14d, %eax
movslq %r14d, %rcx
shlq $0x8, %rcx
addq %rdx, %rcx
movsbl (%r13,%rcx), %r14d
movl %r14d, %ecx
subl %eax, %ecx
addl %r15d, %ecx
cmpl $0x8, %r14d
je 0x7d00
leal -0x5(%rcx), %eax
xorl %r15d, %r15d
cmpl $0xb, %eax
ja 0x7c8f
movq 0x10(%rsp), %rdi
movl %r12d, %esi
movl %ebp, %edx
callq 0x7e42
leaq 0x11054(%rip), %rdx     # 0x18d50
jmp 0x7c8f
xorl %ecx, %ecx
movq 0x40(%rsp), %rsi
incq %rsi
movl %ecx, %r15d
jmp 0x7c6b
leal -0x5(%r15), %eax
cmpl $0xb, %eax
ja 0x7d32
movq 0x10(%rsp), %rdi
movl %r12d, %esi
movl %ebp, %edx
movl %r15d, %ecx
callq 0x7e42
leaq 0x1101e(%rip), %rdx     # 0x18d50
incq %r12
movq 0x28(%rsp), %rcx
movl 0x2c(%rcx), %eax
jmp 0x7c2c
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop