// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl 0x28(%rdi), %eax
testl %eax, %eax
je 0x5c5f
cmpl $0x2, %eax
jl 0x5ca9
movq 0x173d3(%rip), %rbx     # 0x1cff0
leaq 0xe4c9(%rip), %rsi      # 0x140ed
movl $0x20, %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 %ebp, %ebp
jmp 0x5fa2
movq 0x1734a(%rip), %rbx     # 0x1cfb0
leaq 0xe466(%rip), %rsi      # 0x140d3
movl $0x19, %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
movb $0x1, %bpl
jmp 0x5fa2
movq %rdi, %rbx
movq (%rdi), %rdi
callq 0x8916
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x7e20
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0x5d21
movl $0x1, %ebp
xorl %r14d, %r14d
movq (%rax,%r14,8), %rdi
movl $0x20000, %esi          # imm = 0x20000
callq 0x1095e
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x5fb3
movq (%rax,%r14,8), %rdi
callq 0x10c3a
movl %ebp, %r14d
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
incl %ebp
cmpq %r14, %rcx
ja 0x5cd5
movq (%rbx), %rdi
callq 0x897c
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x121fe
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq 0x17269(%rip), %r14     # 0x1cfb0
movq %r14, %rdi
callq 0x5450
movq %rax, %r15
movq (%rax), %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
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5d8c
callq 0x5390
cmpl $0x3, 0x8(%rbx)
sete %bpl
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rax
jne 0x5ef2
cmpq %rdi, %rax
je 0x5fd6
movq (%rdi), %rdi
callq 0x10956
movl $0x20000, %edi          # imm = 0x20000
callq 0x5030
movq %rax, %r15
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
cmpq %rax, %rcx
je 0x5e12
movl $0x1, %r12d
xorl %edx, %edx
movq (%rax,%rdx,8), %rdi
callq 0x10b20
testb %al, %al
je 0x5f27
movl %r12d, %edx
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, %rsi
subq %rax, %rsi
sarq $0x3, %rsi
incl %r12d
cmpq %rdx, %rsi
ja 0x5de2
cmpq %rax, %rcx
je 0x5fc4
movq (%rax), %rdi
callq 0x10956
movq %rax, %r13
testq %rax, %rax
je 0x5e3c
xorl %eax, %eax
movb $0x0, (%r15,%rax)
incq %rax
movl %eax, %ecx
cmpq %rcx, %r13
ja 0x5e2d
movq 0x8(%rsp), %rax
cmpq %rax, 0x10(%rsp)
je 0x5e85
xorl %r12d, %r12d
movq (%rax,%r12,8), %rdi
callq 0x1085c
testq %r13, %r13
je 0x5e6c
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
xorb %dl, (%r15,%rcx)
incq %rcx
movl %ecx, %edx
cmpq %rdx, %r13
ja 0x5e5b
incq %r12
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %r12
jb 0x5e4b
movq (%rbx), %rdi
callq 0x897c
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x11ca8
xorps %xmm0, %xmm0
cvtsi2ss %r13, %xmm0
movss 0xe159(%rip), %xmm1     # 0x14004
mulss %xmm1, %xmm0
mulss %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0xe23d(%rip), %rdi      # 0x1410e
movb $0x1, %al
callq 0x5050
movq 0x17089(%rip), %rax     # 0x1cf68
movq (%rax), %rdi
callq 0x5470
movsd 0x20(%rsp), %xmm0
jmp 0x5dc5
cmpq %rdi, %rax
je 0x5f98
movl $0x1, %ebx
xorl %eax, %eax
movq (%rdi,%rax,8), %rdi
callq 0x10c3a
movl %ebx, %eax
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rcx
subq %rdi, %rcx
sarq $0x3, %rcx
incl %ebx
cmpq %rax, %rcx
ja 0x5f02
jmp 0x5f98
movq (%rbx), %rdi
callq 0x897c
movq %rax, %rdi
callq 0x1206a
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x53b0
movsbl %al, %esi
movq 0x1705b(%rip), %rdi     # 0x1cfb0
callq 0x5040
movq %rax, %rdi
callq 0x5260
movq 0x8(%rsp), %rdi
cmpq %rdi, 0x10(%rsp)
je 0x5f98
movl $0x1, %ebx
xorl %eax, %eax
movq (%rdi,%rax,8), %rdi
callq 0x10c3a
movl %ebx, %eax
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rcx
subq %rdi, %rcx
sarq $0x3, %rcx
incl %ebx
cmpq %rax, %rcx
ja 0x5f75
testq %rdi, %rdi
je 0x5fa2
callq 0x5390
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe90f(%rip), %rdi      # 0x148c9
movq %r14, %rsi
xorl %eax, %eax
callq 0x5570
leaq 0xe8fe(%rip), %rdi      # 0x148c9
xorl %esi, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x5570
leaq 0xe8ec(%rip), %rdi      # 0x148c9
xorl %esi, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x5570
jmp 0x6014
jmp 0x6014
jmp 0x6014
jmp 0x6014
jmp 0x6014
movq %rax, %rbx
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6017
callq 0x5390
jmp 0x6017
jmp 0x6014
jmp 0x6014
jmp 0x6014
jmp 0x6014
jmp 0x6014
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x6026
callq 0x5390
movq %rbx, %rdi
callq 0x5650