// File: /workspace/github_cpp_makefile_star2_O1/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 $0x458, %rsp            # imm = 0x458
movq %rdi, %rbp
movq (%rdi), %rdi
callq 0x894c
movq %rsp, %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x7e58
movq 0x8(%r14), %rax
subq (%r14), %rax
cmpq $0x8, %rax
ja 0x688b
movq 0x18752(%rip), %rbx     # 0x1efb0
leaq 0xed21(%rip), %rsi      # 0x15586
movl $0x2d, %edx
movq %rbx, %rdi
callq 0x5450
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x53b0
jmp 0x6b87
movq (%rbp), %rdi
callq 0x8ae0
testb %al, %al
je 0x6b58
leaq 0x250(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x51e0
movq 0x186f9(%rip), %r14     # 0x1efb0
leaq 0xed27(%rip), %rsi      # 0x155e5
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 0x186c7(%rip), %rdi     # 0x1efb0
callq 0x5040
movq %rax, %rdi
callq 0x5260
movq (%rsp), %rax
cmpq %rax, 0x8(%rsp)
je 0x6929
xorl %ebx, %ebx
movq (%rax,%rbx,8), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x1135a
incq %rbx
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %rbx
jb 0x6903
movq 0x18680(%rip), %r12     # 0x1efb0
leaq 0xecfd(%rip), %rsi      # 0x15634
movl $0x2, %edx
movq %r12, %rdi
callq 0x5450
xorl %r14d, %r14d
leaq 0x250(%rsp), %r13
movq $0x0, 0x28(%rsp)
xorl %ebx, %ebx
movq $0x0, 0x18(%rsp)
movq %rbx, %rax
movabsq $-0x2d77318fc504816f, %rcx # imm = 0xD288CE703AFB7E91
imulq %rcx, %rax
rorq $0x3, %rax
movabsq $0xd1b71758e2196, %rcx  # imm = 0xD1B71758E2196
cmpq %rcx, %rax
ja 0x699b
movl $0x2, %edx
movq %r12, %rdi
leaq 0xeca1(%rip), %rsi      # 0x15637
callq 0x5450
movq (%rsp), %rax
cmpq %rax, 0x8(%rsp)
je 0x707d
movq (%rax), %rdi
callq 0x11258
movq %rax, %r15
movl $0x200, %edx            # imm = 0x200
leaq 0x50(%rsp), %rdi
movq %rax, %rsi
callq 0x52d0
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
cmpq $0x9, %rcx
jb 0x6ad6
movq %r14, 0x20(%rsp)
movq %rbp, %r14
xorl %r12d, %r12d
movl $0x1, %ebp
movl $0x1, %ecx
movq (%rax,%rcx,8), %rdi
callq 0x11258
xorl %ecx, %ecx
movupd (%r15,%rcx), %xmm0
movupd (%rax,%rcx), %xmm1
xorpd %xmm1, %xmm0
xorpd 0x50(%rsp,%rcx), %xmm1
movapd %xmm0, 0x250(%rsp,%rcx)
movapd %xmm1, 0x50(%rsp,%rcx)
addq $0x10, %rcx
cmpq $0x200, %rcx            # imm = 0x200
jne 0x69fd
cmpb $0x0, 0x250(%rsp)
jg 0x6a5c
xorl %ecx, %ecx
movq %rcx, %rax
cmpq $0x1ff, %rcx            # imm = 0x1FF
je 0x6a54
leaq 0x1(%rax), %rcx
cmpb $0x0, 0x251(%rsp,%rax)
jle 0x6a3a
cmpq $0x1fe, %rax            # imm = 0x1FE
ja 0x6a6e
movl $0x200, %edx            # imm = 0x200
movq %r13, %rdi
xorl %esi, %esi
callq 0x51e0
incl %r12d
incl %ebp
movq (%rsp), %rax
movq 0x8(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movq %rbp, %rcx
cmpq %rbp, %rdx
ja 0x69f2
testl %r12d, %r12d
movq %r14, %rbp
movq 0x20(%rsp), %r14
movq 0x18512(%rip), %r12     # 0x1efb0
je 0x6ad6
cmpb $0x0, 0x50(%rsp)
jg 0x6adb
xorl %ecx, %ecx
movq %rcx, %rax
cmpq $0x1ff, %rcx            # imm = 0x1FF
je 0x6ac0
leaq 0x1(%rax), %rcx
cmpb $0x0, 0x51(%rsp,%rax)
jle 0x6aa9
cmpq $0x1ff, %rax            # imm = 0x1FF
jb 0x6adb
movq 0x28(%rsp), %rax
incl %eax
movq %rax, 0x28(%rsp)
jmp 0x6ae7
incl %r14d
jmp 0x6ae7
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
movq (%rbp), %rdi
callq 0x8ae0
movl %eax, %r15d
testb %al, %al
je 0x6b05
cmpq $0x16e35e, %rbx         # imm = 0x16E35E
jbe 0x6b50
jmp 0x6bbe
movl $0x49, %edx
movq %r12, %rdi
leaq 0xeb26(%rip), %rsi      # 0x1563a
callq 0x5450
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x53b0
movsbl %al, %esi
movq %r12, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5260
cmpq $0x16e35f, %rbx         # imm = 0x16E35F
setb %al
testb %al, %r15b
je 0x6bbe
incq %rbx
jmp 0x6963
movq 0x18491(%rip), %rbx     # 0x1eff0
leaq 0xea4e(%rip), %rsi      # 0x155b4
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 (%rsp), %rdi
testq %rdi, %rdi
je 0x6baa
callq 0x5390
movl %ebx, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, 0x20(%rsp)
movq (%r12), %rax
addq -0x18(%rax), %r12
movq %r12, %rdi
movl $0xa, %esi
callq 0x53b0
movq 0x28(%rsp), %rbx
movsbl %al, %esi
movq 0x183c9(%rip), %rdi     # 0x1efb0
callq 0x5040
movq %rax, %rdi
callq 0x5260
movq 0x183b5(%rip), %r15     # 0x1efb0
leaq 0xea82(%rip), %rsi      # 0x15684
movl $0x15, %edx
movq %r15, %rdi
callq 0x5450
movq (%r15), %rax
movq -0x18(%rax), %rax
movq $0x6, 0x10(%r15,%rax)
movl 0x20(%rsp), %r14d
movq %r15, %rdi
movq %r14, %rsi
callq 0x5250
movq %rax, %r15
leaq 0xea61(%rip), %rsi      # 0x1569a
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 0x1833c(%rip), %r15     # 0x1efb0
leaq 0xea28(%rip), %rsi      # 0x156a3
movl $0x1c, %edx
movq %r15, %rdi
callq 0x5450
movq (%r15), %rax
movq -0x18(%rax), %rax
movq $0x6, 0x10(%r15,%rax)
movl %ebx, %r12d
movq %r15, %rdi
movq %r12, %rsi
callq 0x5250
movq %rax, %r15
leaq 0xea10(%rip), %rsi      # 0x156c0
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 0x182c5(%rip), %r13     # 0x1efb0
leaq 0xe9d7(%rip), %rsi      # 0x156c9
movl $0x20, %edx
movq %r13, %rdi
callq 0x5450
movq (%r13), %rax
movq -0x18(%rax), %rax
movq $0x6, 0x10(%r13,%rax)
movl 0x18(%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 (%rbp), %rdi
callq 0x8bf4
cvtsi2sd %r14, %xmm0
movq 0x18(%rsp), %rcx
leal (%rbx,%rcx), %eax
cvtsi2sd %rax, %xmm1
mulsd 0xe2fb(%rip), %xmm1     # 0x15068
ucomisd %xmm1, %xmm0
jbe 0x6dc8
movl $0x16, %edx
leaq 0xe96b(%rip), %rsi      # 0x156ea
movl $0x1, %eax
movl %eax, 0x8(%rbp)
movq 0x18222(%rip), %rbx     # 0x1efb0
movq %rbx, %rdi
callq 0x5450
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x53b0
movsbl %al, %esi
movq 0x181fc(%rip), %rdi     # 0x1efb0
callq 0x5040
movb $0x1, %bl
movq %rax, %rdi
callq 0x5260
jmp 0x6b9c
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
movq 0x20(%rsp), %rax
addl %ecx, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0xe281(%rip), %xmm1     # 0x15068
ucomisd %xmm1, %xmm0
jbe 0x6e12
movq 0x8(%rsp), %rax
subq (%rsp), %rax
cmpq $0x11, %rax
jb 0x6e12
movl $0x1f, %edx
leaq 0xe8f9(%rip), %rsi      # 0x15701
movl $0x4, %eax
jmp 0x6d84
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
subq %rax, %rcx
je 0x6eaf
xorl %ebx, %ebx
leaq 0x30(%rsp), %r12
leaq 0x48(%rsp), %r13
movq (%rax,%rbx,8), %rdi
callq 0x11258
movq %rax, 0x48(%rsp)
cmpb $0x0, (%rax)
jg 0x6e74
xorl %edx, %edx
movq %rdx, %rcx
cmpq $0x1ff, %rdx            # imm = 0x1FF
je 0x6e6b
leaq 0x1(%rcx), %rdx
cmpb $0x0, 0x1(%rax,%rcx)
jle 0x6e54
cmpq $0x1fe, %rcx            # imm = 0x1FE
ja 0x6e96
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x6e8b
movq %rax, (%rsi)
addq $0x8, 0x38(%rsp)
jmp 0x6e96
movq %r12, %rdi
movq %r13, %rdx
callq 0x7ff2
incl %ebx
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rbx, %rcx
ja 0x6e3f
jmp 0x6eb3
sarq $0x3, %rcx
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rax
subq %rdx, %rax
cmpq $0x10, %rax
movq 0x28(%rsp), %r8
movq 0x18(%rsp), %rdi
jne 0x6f36
movq (%rdx), %rax
movq 0x8(%rdx), %rdx
xorpd %xmm0, %xmm0
xorl %esi, %esi
pcmpeqd %xmm1, %xmm1
movdqa 0xe197(%rip), %xmm2     # 0x15080
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 0x6ee9
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 0x700a
imull $0x64, %edi, %eax
incq %rcx
xorl %edx, %edx
divq %rcx
movl $0x3f, %edx
leaq 0xe87b(%rip), %rsi      # 0x157c8
movl $0x1, %ecx
cmpq %r14, %rax
jbe 0x6fcb
movq 0x20(%rsp), %rdi
addl %r8d, %edi
xorps %xmm0, %xmm0
cvtsi2sd %rdi, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %r15, %xmm1
cvtss2sd %xmm1, %xmm1
mulsd 0xe0f5(%rip), %xmm1     # 0x15070
ucomisd %xmm1, %xmm0
jbe 0x6fb8
movl $0x1, 0x28(%rbp)
movl $0x3, 0x8(%rbp)
movq 0x1801a(%rip), %rdi     # 0x1efb0
leaq 0xe7cd(%rip), %rsi      # 0x1576a
movl $0x5d, %edx
callq 0x5450
movq 0x18002(%rip), %rdi     # 0x1efb0
callq 0x5190
jmp 0x705e
cmpq %r14, %rax
jbe 0x6fcb
movl $0x49, %edx
leaq 0xe83f(%rip), %rsi      # 0x15808
xorl %ecx, %ecx
movl %ecx, 0x8(%rbp)
movq 0x17fdb(%rip), %rbx     # 0x1efb0
movq %rbx, %rdi
callq 0x5450
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x53b0
movsbl %al, %esi
movq 0x17fb5(%rip), %rdi     # 0x1efb0
callq 0x5040
movq %rax, %rdi
callq 0x5260
jmp 0x7067
movl $0x1, 0x28(%rbp)
movl $0x3, 0x8(%rbp)
movq 0x17f91(%rip), %r14     # 0x1efb0
leaq 0xe6fb(%rip), %rsi      # 0x15721
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 0x17f5f(%rip), %rdi     # 0x1efb0
callq 0x5040
movq %rax, %rdi
callq 0x5260
movq (%rbp), %rdi
callq 0x8bf4
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x7076
callq 0x5390
movb $0x1, %bl
jmp 0x6b9c
leaq 0xe845(%rip), %rdi      # 0x158c9
xorl %esi, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x5570
jmp 0x7091
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x70b2
callq 0x5390
jmp 0x70b2
jmp 0x70af
jmp 0x70af
jmp 0x70af
jmp 0x70af
jmp 0x70af
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x70c0
callq 0x5390
movq %rbx, %rdi
callq 0x5650