// File: /workspace/github_cpp_makefile_star2_O3/lrq3000[P]rfrb/./src/raidSystem.cpp
// Function: RaidSystem::easyCheck()
bool RaidSystem::easyCheck()
{
	//Initial buffer allocations
	char buf1[CHECKSIZE];
	char buf5[CHECKSIZE];
	char *checkAgainstMe;
	char *in;
	unsigned int raid1=0, raid5=0, misses=0;

	//Starting fileReader for each image
	std::vector<FileReader *> inFiles = handle->getInFiles();

	//Check if enough images are specified
	if(inFiles.size() < 2)
	{
		std::cout << "\tThere are too few devices for a Raid system." << std::endl;
		return false;
	}
	//Check if images are too broken
	if (handle->findGoodBlock() == false)
	{
		std::cerr << "\tNo valid readable block found! Disc too broken!" << std::endl;
		return false;
	}

	for (int j = 0; j < CHECKSIZE; ++j)
	{
		buf1[j] = 0;
	}

	std::cout << "\tTesting 1.5 million blocks to check if a valid Raid-version can be estimated." << std::endl;

	//Iterate over many blocks and trying to estimate the RAID-level
	for (size_t i = 0; i < inFiles.size(); ++i)
	{
		inFiles.at(i)->setBlockSize(CHECKSIZE);
	}
	std::cout << "| ";
	for (size_t count=0; count < 1500000; ++count)
	{
		if ((count%5000) == 0)
			std::cout << "* ";
		//Get a block from the first image
		checkAgainstMe = inFiles.at(0)->getBlock();
		int raid1_miss = 0;
		int raid1_hit = 0;
		for (int j = 0; j < CHECKSIZE; ++j)
		{
			buf5[j] = checkAgainstMe[j];
		}
		//Get blocks from the other images
		for (unsigned int j = 1; j < inFiles.size(); ++j)
		{
			in = inFiles.at(j)->getBlock();
			//Calculate XOR
			for (int x = 0; x < CHECKSIZE; ++x)
			{
				buf1[x] = checkAgainstMe[x]^in[x];
				buf5[x] = buf5[x]^in[x];
			}
			//If XOR is in buf1 is not zero, then we have no RAID1
			if (checkForNull(buf1, CHECKSIZE)==false)
			{
				++raid1_miss;
				for (int x = 0; x < CHECKSIZE; ++x)
				{
					buf1[x] = 0;
				}
			}
			else
			{
				++raid1_hit;
			}
		}
		if (raid1_miss == 0)
		{
			//Blocks are equal, we have a RAID1 hit
			++raid1;
		}
		//else if (checkForEqual(buf5,checkAgainstMe+startAdress,CHECKSIZE) == true)
		else if (checkForNull(buf5, CHECKSIZE)==true)
		{
			//block0 = block1 XOR block2 XOR ...
			//There is Parity information, that's why we assume RAID5
			++raid5;
		}
		else
		{
			++misses;
		}
		if (handle->findGoodBlock() == false)
		{
			std::cout << "\tCouldnt find enough testable blocks. Trying to estimate with heuristics." << std::endl;
			break;
		}
	}
	std::cout << std::endl;

	//Final outputs
	std::cout << "\tMirrored blocks:\t\t: " << std::setw(6) << raid1 << "\t(Raid1)"<< std::endl;
	std::cout << "\tBlocks with parity info\t\t: " << std::setw(6) << raid5 << "\t(Raid5)"<< std::endl;
	std::cout << "\tBlocks without relevant info\t: "       << std::setw(6) << misses << std::endl;
	handle->reset();

	//Trying to estimate the RAID-level from the results above
	if (raid1 > (misses+raid5)*1.5)
	{
		//Enough RAID1 hits to evaluate this to RAID1
		raidSystem = Raid1;
		std::cout << "Found a Raid 1 system." << std::endl;
		return true;
	}
	else if (raid5 > (misses+raid1)*1.5 && inFiles.size() >= 3)
	{
		//Enough RAID5 hits and enough images to evaluate this to RAID5
		raidSystem = Raid5_complete;
		std::cout << "Found a complete Raid 5 system." << std::endl;
		return true;
	}

	//See if you can find two identical partitiontables at the beginning of two images out of all.
	std::vector<char *> tables;
	for (unsigned int i = 0; i < inFiles.size(); ++i)
	{
		char *buf = inFiles.at(i)->getBlock();
		if (checkForNull(buf, 512)==false)
			tables.push_back(buf);
	}
	//If this can be found, it's RAID5 with one missing image (incomplete).
	if (tables.size() == 2)
	{
		if (checkForEqual(tables.at(0),tables.at(1),512)==true)
		{
			lostImages = 1;
			raidSystem = Raid5_incomplete;
			std::cout << "Found a mirrored partition table. Has to be an incomplete Raid 5 system." << std::endl;
			handle->reset();
			return true;
		}
	}
	//Otherwise it can still be RAID5-incomplete or RAID0
	if (raid1 < (100*misses/(inFiles.size()+1)) && (raid1+raid5) > 0.007*(float)misses)
	{
		//At this point it could only be RAID0 or RAID5.
		//There are enough hits for identical blocks and parity blocks => RAID5
		lostImages = 1;
		raidSystem = Raid5_incomplete;
		std::cout << "Some mirrored blocks and some parity blocks found. It's probably an incomplete Raid 5 system." << std::endl;
		handle->reset();
		return true;
	}
	else if (raid1 >= (100*misses/(inFiles.size()+1)))
	{
		//Still too much identical blocks found. This has to be RAID1
		raidSystem = Raid1;
		std::cout << "Above average mirrored blocks found. Has to be a Raid 1 system." << std::endl;
		return true;
	}
	else
	{
		//Now it can only be RAID0
		raidSystem = Raid0;
		std::cout << "Too few mirrored blocks or parity infos found. Has to be a Raid 0 system." << std::endl;
		return true;
	}
	return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x468, %rsp            # imm = 0x468
movq %rdi, %rbx
movq (%rdi), %rdi
callq 0x8916
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x7e20
movq 0x8(%r14), %rax
subq (%r14), %rax
cmpq $0x8, %rax
ja 0x6837
movq 0x167a6(%rip), %rbx     # 0x1cfb0
leaq 0xdd75(%rip), %rsi      # 0x14586
movl $0x2d, %edx
movq %rbx, %rdi
callq 0x5450
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x53b0
jmp 0x6aea
movq (%rbx), %rdi
callq 0x8aaa
testb %al, %al
je 0x6abb
leaq 0x260(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x51e0
movq 0x1674e(%rip), %r14     # 0x1cfb0
leaq 0xdd7c(%rip), %rsi      # 0x145e5
movl $0x4e, %edx
movq %r14, %rdi
callq 0x5450
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x53b0
movsbl %al, %esi
movq 0x1671c(%rip), %rdi     # 0x1cfb0
callq 0x5040
movq %rax, %rdi
callq 0x5260
movq %rbx, 0x50(%rsp)
movq 0x18(%rsp), %rax
cmpq %rax, 0x20(%rsp)
je 0x68db
xorl %ebx, %ebx
movq (%rax,%rbx,8), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x1095e
incq %rbx
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %rbx
jb 0x68b4
movq 0x166ce(%rip), %rdi     # 0x1cfb0
leaq 0xdd4b(%rip), %rsi      # 0x14634
movl $0x2, %edx
callq 0x5450
movq 0x50(%rsp), %rbx
movq $0x0, 0x10(%rsp)
leaq 0x60(%rsp), %r14
leaq 0x260(%rsp), %r13
movq $0x0, (%rsp)
xorl %r15d, %r15d
movq $0x0, 0x8(%rsp)
imull $0x3afb7e91, %r15d, %eax # imm = 0x3AFB7E91
rorl $0x3, %eax
cmpl $0xd1b71, %eax          # imm = 0xD1B71
ja 0x694b
movl $0x2, %edx
movq 0x16671(%rip), %rdi     # 0x1cfb0
leaq 0xdcf1(%rip), %rsi      # 0x14637
callq 0x5450
movq 0x18(%rsp), %rax
cmpq %rax, 0x20(%rsp)
je 0x7024
movq (%rax), %rdi
callq 0x1085c
movq %rax, %rbp
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
movq %rax, %rsi
callq 0x52d0
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
subq %rax, %rcx
cmpq $0x9, %rcx
jb 0x6a58
xorl %ebx, %ebx
movl $0x1, %r12d
movl $0x1, %ecx
movq (%rax,%rcx,8), %rdi
callq 0x1085c
xorl %ecx, %ecx
movupd (%rbp,%rcx), %xmm0
movupd (%rax,%rcx), %xmm1
xorpd %xmm1, %xmm0
xorpd 0x60(%rsp,%rcx), %xmm1
movapd %xmm0, 0x260(%rsp,%rcx)
movapd %xmm1, 0x60(%rsp,%rcx)
addq $0x10, %rcx
cmpq $0x200, %rcx            # imm = 0x200
jne 0x69a5
cmpb $0x0, 0x260(%rsp)
jle 0x6a0f
movl $0x200, %edx            # imm = 0x200
movq %r13, %rdi
xorl %esi, %esi
callq 0x51e0
incl %ebx
incl %r12d
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movq %r12, %rcx
cmpq %r12, %rdx
ja 0x699a
jmp 0x6a3a
movq $-0x1, %rax
cmpq $0x1fe, %rax            # imm = 0x1FE
je 0x69f1
leaq 0x1(%rax), %rcx
cmpb $0x0, 0x262(%rsp,%rax)
movq %rcx, %rax
jle 0x6a16
cmpq $0x1fe, %rcx            # imm = 0x1FE
jbe 0x69e0
jmp 0x69f1
testl %ebx, %ebx
movq 0x50(%rsp), %rbx
je 0x6a58
cmpb $0x0, 0x60(%rsp)
jle 0x6a89
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
jmp 0x6a64
movq 0x10(%rsp), %rax
incl %eax
movq %rax, 0x10(%rsp)
movq (%rbx), %rdi
callq 0x8aaa
testb %al, %al
je 0x6b22
incq %r15
cmpq $0x16e360, %r15         # imm = 0x16E360
jne 0x6922
jmp 0x6b68
movq $-0x1, %rax
cmpq $0x1fe, %rax            # imm = 0x1FE
je 0x6aaf
leaq 0x1(%rax), %rcx
cmpb $0x0, 0x62(%rsp,%rax)
movq %rcx, %rax
jle 0x6a90
cmpq $0x1ff, %rcx            # imm = 0x1FF
jb 0x6a4a
movq (%rsp), %rax
incl %eax
movq %rax, (%rsp)
jmp 0x6a64
movq 0x1652e(%rip), %rbx     # 0x1cff0
leaq 0xdaeb(%rip), %rsi      # 0x145b4
movl $0x30, %edx
movq %rbx, %rdi
callq 0x5450
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x53b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5260
xorl %ebx, %ebx
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x6b0e
callq 0x5390
movl %ebx, %eax
addq $0x468, %rsp            # imm = 0x468
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x16487(%rip), %r15     # 0x1cfb0
leaq 0xdb0a(%rip), %rsi      # 0x1463a
movl $0x49, %edx
movq %r15, %rdi
callq 0x5450
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x53b0
movsbl %al, %esi
movq 0x16455(%rip), %rdi     # 0x1cfb0
callq 0x5040
movq %rax, %rdi
callq 0x5260
movq 0x16441(%rip), %rdi     # 0x1cfb0
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x53b0
movsbl %al, %esi
movq 0x16426(%rip), %rdi     # 0x1cfb0
callq 0x5040
movq %rax, %rdi
callq 0x5260
movq 0x16412(%rip), %r15     # 0x1cfb0
leaq 0xdadf(%rip), %rsi      # 0x14684
movl $0x15, %edx
movq %r15, %rdi
callq 0x5450
movq (%r15), %rax
movq -0x18(%rax), %rax
movq $0x6, 0x10(%r15,%rax)
movl 0x10(%rsp), %r14d
movq %r15, %rdi
movq %r14, %rsi
callq 0x5250
movq %rax, %r15
leaq 0xdabe(%rip), %rsi      # 0x1469a
movl $0x8, %edx
movq %rax, %rdi
callq 0x5450
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x53b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5260
movq 0x16399(%rip), %r15     # 0x1cfb0
leaq 0xda85(%rip), %rsi      # 0x146a3
movl $0x1c, %edx
movq %r15, %rdi
callq 0x5450
movq (%r15), %rax
movq -0x18(%rax), %rax
movq $0x6, 0x10(%r15,%rax)
movl (%rsp), %r12d
movq %r15, %rdi
movq %r12, %rsi
callq 0x5250
movq %rax, %r15
leaq 0xda6c(%rip), %rsi      # 0x146c0
movl $0x8, %edx
movq %rax, %rdi
callq 0x5450
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x53b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5260
movq 0x16321(%rip), %r13     # 0x1cfb0
leaq 0xda33(%rip), %rsi      # 0x146c9
movl $0x20, %edx
movq %r13, %rdi
callq 0x5450
movq (%r13), %rax
movq -0x18(%rax), %rax
movq $0x6, 0x10(%r13,%rax)
movl 0x8(%rsp), %r15d
movq %r13, %rdi
movq %r15, %rsi
callq 0x5250
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x53b0
movsbl %al, %esi
movq %r13, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5260
movq (%rbx), %rdi
callq 0x8baa
cvtsi2sd %r14, %xmm0
movq (%rsp), %rax
movq 0x8(%rsp), %rdi
addl %edi, %eax
cvtsi2sd %rax, %xmm1
mulsd 0xd355(%rip), %xmm1     # 0x14068
ucomisd %xmm1, %xmm0
jbe 0x6d2c
movl $0x16, %edx
leaq 0xd9c5(%rip), %rsi      # 0x146ea
movl $0x1, %eax
jmp 0x6d78
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
movq 0x10(%rsp), %rax
addl %edi, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0xd31d(%rip), %xmm1     # 0x14068
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rdx
movq %rdx, %rcx
subq %rax, %rcx
ucomisd %xmm1, %xmm0
jbe 0x6dbc
cmpq $0x11, %rcx
jb 0x6dbc
movl $0x1f, %edx
leaq 0xd98e(%rip), %rsi      # 0x14701
movl $0x4, %eax
movl %eax, 0x8(%rbx)
movq 0x1622e(%rip), %rbx     # 0x1cfb0
movq %rbx, %rdi
callq 0x5450
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x53b0
movsbl %al, %esi
movq 0x16208(%rip), %rdi     # 0x1cfb0
callq 0x5040
movb $0x1, %bl
movq %rax, %rdi
callq 0x5260
jmp 0x6aff
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
xorl %ebp, %ebp
cmpq %rax, %rdx
je 0x6e6b
leaq 0x30(%rsp), %r12
leaq 0x58(%rsp), %r13
movq (%rax,%rbp,8), %rdi
callq 0x1085c
movq %rax, 0x58(%rsp)
cmpb $0x0, (%rax)
jle 0x6e0e
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x6e37
movq %rax, (%rsi)
addq $0x8, 0x38(%rsp)
jmp 0x6e42
movq $-0x1, %rcx
cmpq $0x1fe, %rcx            # imm = 0x1FE
je 0x6e42
leaq 0x1(%rcx), %rdx
cmpb $0x0, 0x2(%rax,%rcx)
movq %rdx, %rcx
jle 0x6e15
cmpq $0x1fe, %rdx            # imm = 0x1FE
jbe 0x6df7
jmp 0x6e42
movq %r12, %rdi
movq %r13, %rdx
callq 0x7fc0
incl %ebp
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rbp, %rcx
ja 0x6de4
movq 0x30(%rsp), %rbp
movq 0x38(%rsp), %rax
movq 0x8(%rsp), %rdi
jmp 0x6e71
sarq $0x3, %rcx
xorl %eax, %eax
subq %rbp, %rax
cmpq $0x10, %rax
jne 0x6ee1
movq (%rbp), %rax
movq 0x8(%rbp), %rdx
xorpd %xmm0, %xmm0
xorl %esi, %esi
pcmpeqd %xmm1, %xmm1
movdqa 0xd1ec(%rip), %xmm2     # 0x14080
movd (%rdx,%rsi), %xmm3
movd (%rax,%rsi), %xmm4
pcmpeqb %xmm3, %xmm4
pxor %xmm1, %xmm4
punpcklbw %xmm4, %xmm4    # xmm4 = xmm4[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
punpcklwd %xmm4, %xmm4    # xmm4 = xmm4[0,0,1,1,2,2,3,3]
pand %xmm2, %xmm4
paddd %xmm4, %xmm0
addq $0x4, %rsi
cmpq $0x200, %rsi            # imm = 0x200
jne 0x6e94
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
paddd %xmm0, %xmm1
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
paddd %xmm1, %xmm0
movd %xmm0, %eax
testl %eax, %eax
je 0x6fb2
imull $0x64, %edi, %eax
incq %rcx
xorl %edi, %edi
xorl %edx, %edx
divq %rcx
cmpl %r14d, %eax
jbe 0x6f54
movq 0x10(%rsp), %rax
addl (%rsp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %r15, %xmm1
cvtss2sd %xmm1, %xmm1
mulsd 0xd159(%rip), %xmm1     # 0x14070
ucomisd %xmm1, %xmm0
jbe 0x6f67
movl $0x1, 0x28(%rbx)
movl $0x3, 0x8(%rbx)
movq 0x1607e(%rip), %rdi     # 0x1cfb0
leaq 0xd831(%rip), %rsi      # 0x1476a
movl $0x5d, %edx
callq 0x5450
movq 0x16066(%rip), %rdi     # 0x1cfb0
callq 0x5190
jmp 0x7006
movl $0x3f, %edx
leaq 0xd868(%rip), %rsi      # 0x147c8
movl $0x1, %edi
jmp 0x6f73
movl $0x49, %edx
leaq 0xd895(%rip), %rsi      # 0x14808
movl %edi, 0x8(%rbx)
movq 0x16033(%rip), %rbx     # 0x1cfb0
movq %rbx, %rdi
callq 0x5450
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x53b0
movsbl %al, %esi
movq 0x1600d(%rip), %rdi     # 0x1cfb0
callq 0x5040
movq %rax, %rdi
callq 0x5260
jmp 0x700e
movl $0x1, 0x28(%rbx)
movl $0x3, 0x8(%rbx)
movq 0x15fe9(%rip), %r14     # 0x1cfb0
leaq 0xd753(%rip), %rsi      # 0x14721
movl $0x48, %edx
movq %r14, %rdi
callq 0x5450
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x53b0
movsbl %al, %esi
movq 0x15fb7(%rip), %rdi     # 0x1cfb0
callq 0x5040
movq %rax, %rdi
callq 0x5260
movq (%rbx), %rdi
callq 0x8baa
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x701d
callq 0x5390
movb $0x1, %bl
jmp 0x6aff
leaq 0xd89e(%rip), %rdi      # 0x148c9
xorl %esi, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x5570
jmp 0x703a
jmp 0x7058
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x705b
callq 0x5390
jmp 0x705b
jmp 0x7058
jmp 0x7058
jmp 0x7058
jmp 0x7058
jmp 0x7058
movq %rax, %rbx
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x706a
callq 0x5390
movq %rbx, %rdi
callq 0x5650