// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %rdi, 0x20(%rsp)
movq 0x210(%rdi), %rbx
movl 0x2c(%rbx), %eax
movl %eax, 0x18(%rsp)
testl %eax, %eax
jle 0x9410
movl 0x28(%rbx), %eax
movl %eax, 0x14(%rsp)
leaq 0x30(%rbx), %rax
movq %rax, 0x38(%rsp)
leaq 0x80(%rbx), %rax
movq %rax, 0x30(%rsp)
xorl %edx, %edx
leaq 0x11a92(%rip), %r12     # 0x1ad50
movq %rbx, 0x28(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x18(%rsp), %eax
movq 0x30(%rsp), %rax
leaq (%rax,%rdx,4), %rax
cmoveq 0x38(%rsp), %rax
movslq (%rax), %rax
movq %rax, 0x48(%rsp)
testq %rax, %rax
jle 0x9400
movq 0x58(%rbx,%rdx,8), %rax
movq %rax, 0x40(%rsp)
xorl %esi, %esi
xorl %r15d, %r15d
xorl %eax, %eax
movq 0x40(%rsp), %rcx
movzbl (%rcx,%rsi), %r14d
movq %rsi, 0x50(%rsp)
leal (,%rsi,8), %ecx
movl %ecx, 0x1c(%rsp)
movl %eax, %r13d
xorl %ebp, %ebp
xorl %ecx, %ecx
testl %r13d, %r13d
jne 0x9347
movslq %ebp, %rax
shlq $0x8, %rax
leaq 0x11220(%rip), %rcx     # 0x1a550
addq %rcx, %rax
movsbl (%r14,%rax), %ebp
cmpl $0x8, %ebp
je 0x93ab
movl 0x1c(%rsp), %r15d
orl %ebp, %r15d
movl %ebp, %ecx
movslq %ebp, %rax
shlq $0x8, %rax
addq %r12, %rax
movsbl (%r14,%rax), %ebp
movl %ebp, %eax
subl %ecx, %eax
addl %r13d, %eax
cmpl $0x8, %ebp
je 0x93ad
leal -0x5(%rax), %ecx
xorl %r13d, %r13d
cmpl $0xb, %ecx
ja 0x931b
movl %edx, 0x8(%rsp)
movl %r15d, 0xc(%rsp)
movl %eax, 0x10(%rsp)
movq 0x20(%rsp), %rax
movq 0x210(%rax), %rdi
leaq 0x8(%rsp), %rsi
movq %r12, %rbx
movq %rdx, %r12
movl $0x1, %edx
movl $0x140, %ecx            # imm = 0x140
callq 0xf1de
movq %r12, %rdx
movq %rbx, %r12
jmp 0x931b
xorl %eax, %eax
movq 0x50(%rsp), %rsi
incq %rsi
cmpq 0x48(%rsp), %rsi
jne 0x92fc
leal -0x5(%rax), %ecx
cmpl $0xb, %ecx
movq 0x28(%rsp), %rbx
ja 0x9400
movl %edx, 0x8(%rsp)
movl %r15d, 0xc(%rsp)
movl %eax, 0x10(%rsp)
movq 0x20(%rsp), %rax
movq 0x210(%rax), %rdi
leaq 0x8(%rsp), %rsi
movq %rdx, %r14
movl $0x1, %edx
movl $0x140, %ecx            # imm = 0x140
callq 0xf1de
movq %r14, %rdx
incq %rdx
movslq 0x2c(%rbx), %rax
cmpq %rax, %rdx
jl 0x92c3
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop