// File: lrq3000[P]rfrb/./src/raidSystem.cpp
// Function: RaidSystem::recoverLostImage()
bool RaidSystem::recoverLostImage()
{
	//Check cases, which are not permitted
	if (lostImages == 0)
	{
		std::cout << "\tNo lost disc to recover." << std::endl;
		return true;
	}
	else if (lostImages > 1)
	{
		std::cerr << "\tToo many lost discs to recover." << std::endl;
		return false;
	}
	//Start handler for each image
	size_t maxBuf=128*1024;
	std::vector<FileReader *> inFiles = handle->getInFiles();
	for (unsigned int j = 0; j < inFiles.size(); ++j)
	{
		inFiles.at(j)->setBlockSize(maxBuf);
		inFiles.at(j)->reset();
	}
	std::cout << handle->getFileWriter()->getPath() << std::endl;
	//Check, if the right RAID-level is estimated. Should only be RAID5 with ONE missing image.
	if (raidSystem == Raid5_incomplete)
	{
		size_t blocklength = inFiles.at(0)->getCurrentBlockSize();
		double written = 0.0f;
		size_t runs=0;
		char *buf = new char[maxBuf];
		//Loop until all datablocks are written
		while (true)
		{
			for (unsigned int j = 0; j < inFiles.size(); ++j)
			{
				if (inFiles.at(j)->newBlock() == false)
				{
					handle->getFileWriter()->closeFile();
					std::cout << std::endl;
					for (unsigned int t = 0; t < inFiles.size(); ++t)
						inFiles.at(t)->reset();
					return true;
				}
			}
			blocklength = inFiles.at(0)->getCurrentBlockSize();
			//Set buffer to zero
			for (unsigned int i = 0; i < blocklength; ++i)
			{
				buf[i] = 0;
			}
			++runs;
			//Iterate over all images and read a block
			for (size_t j = 0; j < inFiles.size(); ++j)
			{
				FileReader *reader = inFiles.at(j);
				char *read = reader->getBlock();
				//Calculate XOR to get the block of the lost image
				for (unsigned int i = 0; i < blocklength; ++i)
				{
					buf[i] = buf[i]^read[i];
				}
			}
			//Write calculated block to file
			handle->getFileWriter()->writeToFile(buf,blocklength);
			//Make progress-output
			written += blocklength/1024.0f/1024.0f;
			printf("\r%f MB written", written);
			fflush(stdout);
			//Get new blocks and stop if file is fully written
		}
		//Close files
		std::cout << std::endl;
		handle->getFileWriter()->closeFile();
		for (unsigned int j = 0; j < inFiles.size(); ++j)
			inFiles.at(j)->reset();
		return true;
	}
	for (unsigned int j = 0; j < inFiles.size(); ++j)
		inFiles.at(j)->reset();
	return false;
}

// Assembly:
subq $0x178, %rsp            # imm = 0x178
movq %rdi, 0x168(%rsp)
movq 0x168(%rsp), %rax
movq %rax, 0xb8(%rsp)
cmpl $0x0, 0x28(%rax)
jne 0x6cb4
movq 0x4d324(%rip), %rdi     # 0x53fb0
leaq 0x333d0(%rip), %rsi     # 0x3a063
callq 0x6420
movq %rax, %rdi
movq 0x4d2ce(%rip), %rsi     # 0x53f70
callq 0x6470
movb $0x1, 0x177(%rsp)
jmp 0x737f
movq 0xb8(%rsp), %rax
cmpl $0x1, 0x28(%rax)
jle 0x6cf1
movq 0x4d327(%rip), %rdi     # 0x53ff0
leaq 0x333ad(%rip), %rsi     # 0x3a07d
callq 0x6420
movq %rax, %rdi
movq 0x4d291(%rip), %rsi     # 0x53f70
callq 0x6470
movb $0x0, 0x177(%rsp)
jmp 0x737f
jmp 0x6cf3
movq 0xb8(%rsp), %rax
movq $0x20000, 0x160(%rsp) # imm = 0x20000
movq (%rax), %rdi
callq 0xcc90
movq %rax, %rsi
leaq 0x148(%rsp), %rdi
callq 0x9c50
movl $0x0, 0x144(%rsp)
movl 0x144(%rsp), %eax
movq %rax, 0xb0(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x9d50
movq %rax, %rcx
movq 0xb0(%rsp), %rax
cmpq %rcx, %rax
jae 0x6df7
movl 0x144(%rsp), %eax
movl %eax, %esi
leaq 0x148(%rsp), %rdi
callq 0x9d70
movq %rax, 0xa8(%rsp)
jmp 0x6d7a
movq 0xa8(%rsp), %rax
movq (%rax), %rdi
movq 0x160(%rsp), %rsi
callq 0x2fed0
jmp 0x6d94
movl 0x144(%rsp), %eax
movl %eax, %esi
leaq 0x148(%rsp), %rdi
callq 0x9d70
movq %rax, 0xa0(%rsp)
jmp 0x6db4
movq 0xa0(%rsp), %rax
movq (%rax), %rdi
callq 0x30450
jmp 0x6dc6
jmp 0x6dc8
movl 0x144(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x144(%rsp)
jmp 0x6d2a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x138(%rsp)
movl %eax, 0x134(%rsp)
jmp 0x7370
movq 0xb8(%rsp), %rax
movq (%rax), %rdi
callq 0xcd30
movq %rax, 0x98(%rsp)
jmp 0x6e11
movq 0x98(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0x34c80
jmp 0x6e28
movq 0x4d181(%rip), %rdi     # 0x53fb0
leaq 0x110(%rsp), %rsi
callq 0x6390
movq %rax, 0x90(%rsp)
jmp 0x6e46
movq 0x90(%rsp), %rdi
movq 0x4d11b(%rip), %rsi     # 0x53f70
callq 0x6470
jmp 0x6e5c
leaq 0x110(%rsp), %rdi
callq 0x6260
movq 0xb8(%rsp), %rax
cmpl $0x3, 0x8(%rax)
jne 0x72de
xorl %eax, %eax
movl %eax, %esi
leaq 0x148(%rsp), %rdi
callq 0x9d70
movq %rax, 0x88(%rsp)
jmp 0x6e96
movq 0x88(%rsp), %rax
movq (%rax), %rdi
callq 0x2feb0
movq %rax, 0x80(%rsp)
jmp 0x6eb0
movq 0x80(%rsp), %rax
movq %rax, 0x108(%rsp)
movq $0x0, 0x100(%rsp)
movq $0x0, 0xf8(%rsp)
movq 0x160(%rsp), %rdi
callq 0x6030
movq %rax, 0x78(%rsp)
jmp 0x6eec
movq 0x78(%rsp), %rax
movq %rax, 0xf0(%rsp)
jmp 0x6efb
movl $0x0, 0xec(%rsp)
movl 0xec(%rsp), %eax
movq %rax, 0x70(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x9d50
movq %rax, %rcx
movq 0x70(%rsp), %rax
cmpq %rcx, %rax
jae 0x7072
movl 0xec(%rsp), %eax
movl %eax, %esi
leaq 0x148(%rsp), %rdi
callq 0x9d70
movq %rax, 0x68(%rsp)
jmp 0x6f4d
movq 0x68(%rsp), %rax
movq (%rax), %rdi
callq 0x30220
movb %al, 0x67(%rsp)
jmp 0x6f60
movb 0x67(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x705a
movq 0xb8(%rsp), %rax
movq (%rax), %rdi
callq 0xcd30
movq %rax, 0x58(%rsp)
jmp 0x6f89
movq 0x58(%rsp), %rdi
callq 0x34ae0
jmp 0x6f95
movq 0x4d014(%rip), %rdi     # 0x53fb0
movq 0x4cfcd(%rip), %rsi     # 0x53f70
callq 0x6470
jmp 0x6faa
movl $0x0, 0xe8(%rsp)
movl 0xe8(%rsp), %eax
movq %rax, 0x50(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x9d50
movq %rax, %rcx
movq 0x50(%rsp), %rax
cmpq %rcx, %rax
jae 0x7042
movl 0xe8(%rsp), %eax
movl %eax, %esi
leaq 0x148(%rsp), %rdi
callq 0x9d70
movq %rax, 0x48(%rsp)
jmp 0x6ff8
movq 0x48(%rsp), %rax
movq (%rax), %rdi
callq 0x30450
jmp 0x7007
jmp 0x7009
movl 0xe8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe8(%rsp)
jmp 0x6fb5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x138(%rsp)
movl %eax, 0x134(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x6260
jmp 0x7370
movb $0x1, 0x177(%rsp)
movl $0x1, 0xe4(%rsp)
jmp 0x7361
jmp 0x705c
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x6f06
xorl %eax, %eax
movl %eax, %esi
leaq 0x148(%rsp), %rdi
callq 0x9d70
movq %rax, 0x40(%rsp)
jmp 0x708a
movq 0x40(%rsp), %rax
movq (%rax), %rdi
callq 0x2feb0
movq %rax, 0x38(%rsp)
jmp 0x709e
movq 0x38(%rsp), %rax
movq %rax, 0x108(%rsp)
movl $0x0, 0xe0(%rsp)
movl 0xe0(%rsp), %eax
cmpq 0x108(%rsp), %rax
jae 0x70ed
movq 0xf0(%rsp), %rax
movl 0xe0(%rsp), %ecx
movb $0x0, (%rax,%rcx)
movl 0xe0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe0(%rsp)
jmp 0x70b6
movq 0xf8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xf8(%rsp)
movq $0x0, 0xd8(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x9d50
movq %rax, %rcx
movq 0x30(%rsp), %rax
cmpq %rcx, %rax
jae 0x720b
movq 0xd8(%rsp), %rsi
leaq 0x148(%rsp), %rdi
callq 0x9d70
movq %rax, 0x28(%rsp)
jmp 0x7154
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xd0(%rsp)
movq 0xd0(%rsp), %rdi
callq 0x2fc80
movq %rax, 0x20(%rsp)
jmp 0x7178
movq 0x20(%rsp), %rax
movq %rax, 0xc8(%rsp)
movl $0x0, 0xc4(%rsp)
movl 0xc4(%rsp), %eax
cmpq 0x108(%rsp), %rax
jae 0x71f0
movq 0xf0(%rsp), %rax
movl 0xc4(%rsp), %ecx
movsbl (%rax,%rcx), %eax
movq 0xc8(%rsp), %rcx
movl 0xc4(%rsp), %edx
movsbl (%rcx,%rdx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq 0xf0(%rsp), %rax
movl 0xc4(%rsp), %ecx
movb %dl, (%rax,%rcx)
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0x7190
jmp 0x71f2
movq 0xd8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xd8(%rsp)
jmp 0x710d
movq 0xb8(%rsp), %rax
movq (%rax), %rdi
callq 0xcd30
movq %rax, 0x18(%rsp)
jmp 0x7222
movq 0x18(%rsp), %rdi
movq 0xf0(%rsp), %rsi
movq 0x108(%rsp), %rdx
callq 0x34830
jmp 0x723e
movq 0x108(%rsp), %rax
movq %rax, %rdx
shrq %rdx
movl %eax, %ecx
andl $0x1, %ecx
orq %rdx, %rcx
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
cvtsi2ss %rax, %xmm1
movss %xmm1, 0x10(%rsp)
testq %rax, %rax
movss %xmm0, 0x14(%rsp)
js 0x727f
movss 0x10(%rsp), %xmm0
movss %xmm0, 0x14(%rsp)
movss 0x14(%rsp), %xmm0
movss 0x32d77(%rip), %xmm1    # 0x3a004
mulss %xmm1, %xmm0
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x100(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x100(%rsp)
movsd 0x100(%rsp), %xmm0
leaq 0x32ddf(%rip), %rdi     # 0x3a09e
movb $0x1, %al
callq 0x6060
jmp 0x72c8
movq 0x4cc91(%rip), %rax     # 0x53f60
movq (%rax), %rdi
callq 0x64f0
jmp 0x72d9
jmp 0x6ef9
movl $0x0, 0xc0(%rsp)
movl 0xc0(%rsp), %eax
movq %rax, 0x8(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x9d50
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
jae 0x734e
movl 0xc0(%rsp), %eax
movl %eax, %esi
leaq 0x148(%rsp), %rdi
callq 0x9d70
movq %rax, (%rsp)
jmp 0x732b
movq (%rsp), %rax
movq (%rax), %rdi
callq 0x30450
jmp 0x7339
jmp 0x733b
movl 0xc0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc0(%rsp)
jmp 0x72e9
movb $0x0, 0x177(%rsp)
movl $0x1, 0xe4(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x9db0
jmp 0x737f
leaq 0x148(%rsp), %rdi
callq 0x9db0
jmp 0x7390
movb 0x177(%rsp), %al
andb $0x1, %al
addq $0x178, %rsp            # imm = 0x178
retq
movq 0x138(%rsp), %rdi
callq 0x6750
nopl (%rax)