// File: /workspace/github_cpp_makefile_star2_O1/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, 0x28(%rsp)
movq 0x210(%rdi), %rax
movq %rax, 0x10(%rsp)
movl 0x2c(%rax), %eax
movl %eax, 0x20(%rsp)
testl %eax, %eax
jle 0x9698
movq 0x10(%rsp), %rax
movl 0x28(%rax), %ecx
movl %ecx, 0x1c(%rsp)
leaq 0x30(%rax), %rcx
movq %rcx, 0x38(%rsp)
addq $0x80, %rax
movq %rax, 0x30(%rsp)
xorl %r14d, %r14d
leaq 0x1181a(%rip), %rbp     # 0x1ad50
movl 0x1c(%rsp), %eax
cmpl 0x20(%rsp), %eax
movq 0x30(%rsp), %rax
leaq (%rax,%r14,4), %rax
cmoveq 0x38(%rsp), %rax
movslq (%rax), %rcx
movl $0x0, %eax
movl $0x0, %r15d
movq %rcx, 0x48(%rsp)
testq %rcx, %rcx
jle 0x964d
movq 0x10(%rsp), %rax
movq 0x58(%rax,%r14,8), %rax
movq %rax, 0x40(%rsp)
xorl %edx, %edx
xorl %r15d, %r15d
xorl %eax, %eax
movq 0x40(%rsp), %rcx
movzbl (%rcx,%rdx), %r12d
movq %rdx, 0x50(%rsp)
leal (,%rdx,8), %ecx
movl %ecx, 0x24(%rsp)
xorl %r13d, %r13d
movl %eax, %ecx
xorl %esi, %esi
testl %eax, %eax
je 0x95a9
movl %r13d, %edx
jmp 0x95d5
movslq %r13d, %rax
shlq $0x8, %rax
leaq 0x10f99(%rip), %rdx     # 0x1a550
addq %rdx, %rax
movsbl (%r12,%rax), %edx
xorl %eax, %eax
movl $0x0, %ebx
cmpl $0x8, %edx
je 0x9632
movl 0x24(%rsp), %r15d
orl %edx, %r15d
movl %edx, %esi
movslq %edx, %rax
shlq $0x8, %rax
addq %rbp, %rax
movsbl (%r12,%rax), %r13d
movl %r13d, %eax
subl %esi, %eax
addl %ecx, %eax
cmpl $0x8, %r13d
jne 0x95f8
movl %edx, %r13d
xorl %ebx, %ebx
jmp 0x9632
leal -0x5(%rax), %ecx
movb $0x1, %bl
cmpl $0xb, %ecx
ja 0x9630
movl %r14d, 0x4(%rsp)
movl %r15d, 0x8(%rsp)
movl %eax, 0xc(%rsp)
movq 0x28(%rsp), %rax
movq 0x210(%rax), %rdi
leaq 0x4(%rsp), %rsi
movl $0x1, %edx
movl $0x140, %ecx            # imm = 0x140
callq 0xf18c
xorl %eax, %eax
testb %bl, %bl
jne 0x959c
movq 0x50(%rsp), %rdx
incq %rdx
cmpq 0x48(%rsp), %rdx
jne 0x957f
leal -0x5(%rax), %ecx
cmpl $0xb, %ecx
ja 0x9683
movl %r14d, 0x4(%rsp)
movl %r15d, 0x8(%rsp)
movl %eax, 0xc(%rsp)
movq 0x28(%rsp), %rax
movq 0x210(%rax), %rdi
leaq 0x4(%rsp), %rsi
movl $0x1, %edx
movl $0x140, %ecx            # imm = 0x140
callq 0xf18c
incq %r14
movq 0x10(%rsp), %rax
movslq 0x2c(%rax), %rax
cmpq %rax, %r14
jl 0x9536
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop