// File: /workspace/github_cpp_makefile_star2_O1/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 0x5c6f
cmpl $0x2, %eax
jl 0x5cb9
movq 0x193c3(%rip), %rbx     # 0x1eff0
leaq 0xf4b9(%rip), %rsi      # 0x150ed
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 0x5fb8
movq 0x1933a(%rip), %rbx     # 0x1efb0
leaq 0xf456(%rip), %rsi      # 0x150d3
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 0x5fb8
movq %rdi, %rbx
movq (%rdi), %rdi
callq 0x894c
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x7e58
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0x5d31
movl $0x1, %ebp
xorl %r14d, %r14d
movq (%rax,%r14,8), %rdi
movl $0x20000, %esi          # imm = 0x20000
callq 0x1135a
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x5fc9
movq (%rax,%r14,8), %rdi
callq 0x11674
movl %ebp, %r14d
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
incl %ebp
cmpq %r14, %rcx
ja 0x5ce5
movq (%rbx), %rdi
callq 0x89b2
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x12f3a
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq 0x19259(%rip), %r14     # 0x1efb0
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 0x5d9c
callq 0x5390
cmpl $0x3, 0x8(%rbx)
sete %bpl
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
jne 0x5f03
cmpq %rax, %rcx
je 0x5fec
movq (%rax), %rdi
callq 0x11352
movl $0x20000, %edi          # imm = 0x20000
callq 0x5030
movq %rax, %r15
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x8(%rsp), %rax
cmpq %rax, 0x10(%rsp)
je 0x5e1c
movl $0x1, %r12d
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdi
callq 0x11538
testb %al, %al
je 0x5f38
movl %r12d, %ecx
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
incl %r12d
cmpq %rcx, %rdx
ja 0x5def
movq 0x8(%rsp), %rax
cmpq %rax, 0x10(%rsp)
je 0x5fda
movq (%rax), %rdi
callq 0x11352
movq %rax, %r13
testq %rax, %rax
je 0x5e4d
xorl %eax, %eax
movb $0x0, (%r15,%rax)
incq %rax
movl %eax, %ecx
cmpq %rcx, %r13
ja 0x5e3e
movq 0x8(%rsp), %rax
cmpq %rax, 0x10(%rsp)
je 0x5e96
xorl %r12d, %r12d
movq (%rax,%r12,8), %rdi
callq 0x11258
testq %r13, %r13
je 0x5e7d
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
xorb %dl, (%r15,%rcx)
incq %rcx
movl %ecx, %edx
cmpq %rdx, %r13
ja 0x5e6c
incq %r12
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %r12
jb 0x5e5c
movq (%rbx), %rdi
callq 0x89b2
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x12924
xorps %xmm0, %xmm0
cvtsi2ss %r13, %xmm0
movss 0xf148(%rip), %xmm1     # 0x15004
mulss %xmm1, %xmm0
mulss %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0xf22c(%rip), %rdi      # 0x1510e
movb $0x1, %al
callq 0x5050
movq 0x19078(%rip), %rax     # 0x1ef68
movq (%rax), %rdi
callq 0x5470
movsd 0x20(%rsp), %xmm0
jmp 0x5dd5
cmpq %rax, %rcx
je 0x5fa9
movl $0x1, %ebx
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdi
callq 0x11674
movl %ebx, %ecx
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
incl %ebx
cmpq %rcx, %rdx
ja 0x5f13
jmp 0x5fa9
movq (%rbx), %rdi
callq 0x89b2
movq %rax, %rdi
callq 0x12da6
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x53b0
movsbl %al, %esi
movq 0x1904a(%rip), %rdi     # 0x1efb0
callq 0x5040
movq %rax, %rdi
callq 0x5260
movq 0x8(%rsp), %rax
cmpq %rax, 0x10(%rsp)
je 0x5fa9
movl $0x1, %ebx
xorl %ecx, %ecx
movq (%rax,%rcx,8), %rdi
callq 0x11674
movl %ebx, %ecx
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
incl %ebx
cmpq %rcx, %rdx
ja 0x5f86
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x5fb8
callq 0x5390
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf8f9(%rip), %rdi      # 0x158c9
movq %r14, %rsi
xorl %eax, %eax
callq 0x5570
leaq 0xf8e8(%rip), %rdi      # 0x158c9
xorl %esi, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x5570
leaq 0xf8d6(%rip), %rdi      # 0x158c9
xorl %esi, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x5570
jmp 0x602a
jmp 0x602a
jmp 0x602a
jmp 0x602a
jmp 0x602a
movq %rax, %rbx
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x602d
callq 0x5390
jmp 0x602d
jmp 0x602a
jmp 0x602a
jmp 0x602a
jmp 0x602a
jmp 0x602a
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x603c
callq 0x5390
movq %rbx, %rdi
callq 0x5650