// File: /workspace/github_cpp_makefile_star2_O2/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 0x598c
cmpl $0x2, %eax
jl 0x59af
movq 0x1867f(%rip), %rdi     # 0x1dff0
leaq 0xd735(%rip), %rsi      # 0x130ad
callq 0x5380
movq %rax, %rdi
callq 0x5180
xorl %ebp, %ebp
jmp 0x5c1c
movq 0x1861d(%rip), %rdi     # 0x1dfb0
leaq 0xd6f9(%rip), %rsi      # 0x13093
callq 0x5380
movq %rax, %rdi
callq 0x5180
movb $0x1, %bpl
jmp 0x5c1c
movq %rdi, %rbx
movq (%rdi), %rdi
callq 0x7986
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x6ed4
xorl %r15d, %r15d
movl %r15d, %r15d
movq 0x10(%rsp), %rax
subq 0x8(%rsp), %rax
sarq $0x3, %rax
cmpq %r15, %rax
jbe 0x5a13
movq %r14, %rdi
movq %r15, %rsi
callq 0x6f22
movq (%rax), %rdi
movl $0x20000, %esi          # imm = 0x20000
callq 0xf644
movq %r14, %rdi
movq %r15, %rsi
callq 0x6f22
movq (%rax), %rdi
callq 0xf8b2
incl %r15d
jmp 0x59cd
movq (%rbx), %rdi
callq 0x79cc
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x10f2e
movq 0x18581(%rip), %rdi     # 0x1dfb0
leaq 0x28(%rsp), %rsi
callq 0x52f0
movq %rax, %rdi
callq 0x5180
leaq 0x28(%rsp), %rdi
callq 0x5160
cmpl $0x3, 0x8(%rbx)
sete %bpl
jne 0x5b90
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x6f22
movq (%rax), %rdi
callq 0xf63c
movl $0x20000, %edi          # imm = 0x20000
callq 0x5030
movq %rax, %r14
xorps %xmm0, %xmm0
leaq 0x8(%rsp), %r15
movsd %xmm0, 0x20(%rsp)
xorl %r12d, %r12d
movl %r12d, %r12d
movq 0x10(%rsp), %rax
subq 0x8(%rsp), %rax
sarq $0x3, %rax
cmpq %r12, %rax
jbe 0x5ac0
movq %r15, %rdi
movq %r12, %rsi
callq 0x6f22
movq (%rax), %rdi
callq 0xf7bc
incl %r12d
testb %al, %al
jne 0x5a8b
jmp 0x5bc3
movq %r15, %rdi
xorl %esi, %esi
callq 0x6f22
movq (%rax), %rdi
callq 0xf63c
movq %rax, %r13
xorl %eax, %eax
movl %eax, %eax
cmpq %rax, %r13
jbe 0x5ae7
movb $0x0, (%r14,%rax)
incl %eax
jmp 0x5ad7
xorl %r12d, %r12d
movq 0x10(%rsp), %rax
subq 0x8(%rsp), %rax
sarq $0x3, %rax
cmpq %rax, %r12
jae 0x5b29
movq %r15, %rdi
movq %r12, %rsi
callq 0x6f22
movq (%rax), %rdi
callq 0xf544
xorl %ecx, %ecx
movl %ecx, %ecx
cmpq %rcx, %r13
jbe 0x5b24
movb (%rax,%rcx), %dl
xorb %dl, (%r14,%rcx)
incl %ecx
jmp 0x5b12
incq %r12
jmp 0x5aea
movq (%rbx), %rdi
callq 0x79cc
movq %rax, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x1099a
cvtsi2ss %r13, %xmm0
movss 0xd4b8(%rip), %xmm1     # 0x13004
mulss %xmm1, %xmm0
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0xd55f(%rip), %rdi      # 0x130ce
movb $0x1, %al
callq 0x5040
movq 0x183f3(%rip), %rax     # 0x1df70
movq (%rax), %rdi
callq 0x5410
movsd 0x20(%rsp), %xmm0
jmp 0x5a82
xorl %ebx, %ebx
leaq 0x8(%rsp), %r14
movl %ebx, %ebx
movq 0x10(%rsp), %rax
subq 0x8(%rsp), %rax
sarq $0x3, %rax
cmpq %rbx, %rax
jbe 0x5c12
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6f22
movq (%rax), %rdi
callq 0xf8b2
incl %ebx
jmp 0x5b97
movq (%rbx), %rdi
callq 0x79cc
movq %rax, %rdi
callq 0x10e40
movq 0x183d6(%rip), %rdi     # 0x1dfb0
callq 0x5180
xorl %ebx, %ebx
leaq 0x8(%rsp), %r14
movl %ebx, %ebx
movq 0x10(%rsp), %rax
subq 0x8(%rsp), %rax
sarq $0x3, %rax
cmpq %rbx, %rax
jbe 0x5c12
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6f22
movq (%rax), %rdi
callq 0xf8b2
incl %ebx
jmp 0x5be6
leaq 0x8(%rsp), %rdi
callq 0x714e
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5c50
jmp 0x5c50
jmp 0x5c50
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x5160
jmp 0x5c53
jmp 0x5c50
jmp 0x5c50
jmp 0x5c50
jmp 0x5c50
jmp 0x5c50
jmp 0x5c50
jmp 0x5c50
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x714e
movq %rbx, %rdi
callq 0x5610
nop