// File: /workspace/github_cpp_makefile_star2_O2/lrq3000[P]rfrb/./src/raidSystem.cpp
// Function: RaidSystem::buildDataImage(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
bool RaidSystem::buildDataImage(std::string path)
{
	//Start readers for every image and one writer for the lost image which has to be recovered
	std::vector<FileReader *> inFiles = handle->getInFiles();
	char *in;
	FileWriter *writeMe = handle->getFileWriter();
	writeMe->setPath(path);
	handle->reset();

	for (unsigned int j = 0; j < inFiles.size(); ++j)
	{
		inFiles.at(j)->setBlockSize(stripeSize);
		inFiles.at(j)->reset();
	}

	//Parity buffer for check parity plausibility
	char *parity = new char[stripeSize]{};
	size_t block = 0;
	size_t written = 0;
	size_t finished = 0;

	//Will break when dataImage is built
	while (true)
	{
		//Iterate stripeMap-rows
		for (size_t i = 0; i < stripeMap.size(); ++i)
		{
			for (auto &readers : inFiles)
			{
				if (readers->newBlock()==false)
				{
					++finished;
				}
			}
			//Break-case when everything is done
			if (finished == inFiles.size())
			{
				printf("\r%12zu MB written\t%12fMB/s", written/1024/1024, writeMe->getTransferRate());
				fflush(stdout);
				writeMe->closeFile();
				return true;
			}
			int runner=0;
			//Re-iterate over stripemap-colums, maximum of #colums
			for (size_t worstCase = 0; worstCase < stripeMap.at(i).size(); ++worstCase)
			{
				//Iterate over stripeMap-columns
				for (size_t j = 0; j < stripeMap.at(i).size(); ++j)
				{
					//Right position at stripeMap -> write it to outfile
					//WorstCase loop forces that we don't miss anything
					if (stripeMap.at(i).at(j).dataBlock == runner)
					{
						in = inFiles.at(j)->getBlock();
						//Calculation parity
						for (size_t k = 0; k < inFiles.at(j)->getCurrentBlockSize(); ++k)
						{
							parity[k] = parity[k] ^ in[k];
						}

						//If not parity, write to outfile
						if (stripeMap.at(i).at(j).isParity == false)
						{
							writeMe->writeToFile(in, inFiles.at(j)->getCurrentBlockSize());
							written += inFiles.at(j)->getCurrentBlockSize();
						}
						else
						{
							if (checkForNull(parity, inFiles.at(0)->getCurrentBlockSize()) == false)
							{
								std::cout << "Parity Error at block: " << block << std::endl;
							}
							for (int k = 0; k < stripeSize; ++k)
							{
								parity[k] = 0;
							}
						}
						++runner;
					}
				}
			}
			//Updated output so we don't have to stare against a blinking cursor
			printf("\r%12f MB written\t%12fMB/s", (double)written/1024.f/1024.f, writeMe->getTransferRate());
			fflush(stdout);

			++block;
		}
	}

	//Shouldn't reach this, if valid Raidsystem & Stripesize & Stripemap
	return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %rbx
movq %rdi, %r12
movq (%rdi), %rdi
callq 0x7986
leaq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x6ed4
movq (%r12), %rdi
callq 0x79cc
movq %rax, (%rsp)
leaq 0x58(%rsp), %rdi
movq %rbx, %rsi
callq 0x54b0
leaq 0x58(%rsp), %rsi
movq (%rsp), %rdi
callq 0x10f44
leaq 0x58(%rsp), %rdi
callq 0x5160
movq (%r12), %rdi
callq 0x7b86
xorl %ebx, %ebx
leaq 0x10(%rsp), %r14
movl %ebx, %ebx
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x3, %rax
cmpq %rbx, %rax
jbe 0x67cd
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6f22
movq (%rax), %rdi
movslq 0xc(%r12), %rsi
callq 0xf644
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6f22
movq (%rax), %rdi
callq 0xf8b2
incl %ebx
jmp 0x6789
movl 0xc(%r12), %eax
testl %eax, %eax
pushq $-0x1
popq %rcx
cmovnsl %eax, %ecx
movslq %ecx, %rbx
movq %rbx, %rdi
callq 0x5030
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x51d0
movq %r12, 0x40(%rsp)
addq $0x10, %r12
leaq 0x10(%rsp), %rbp
movq $0x0, 0x30(%rsp)
xorl %ebx, %ebx
movq $0x0, 0x50(%rsp)
xorl %r13d, %r13d
movq %r12, 0x48(%rsp)
movq %rbx, 0x38(%rsp)
shrq $0x14, %rbx
movq %rbx, 0x8(%rsp)
movq 0x40(%rsp), %rcx
movq 0x18(%rcx), %rax
subq 0x10(%rcx), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
movq %rax, %rbx
cmpq %rax, %r13
jae 0x68bb
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %r14
cmpq %r14, %r12
je 0x6872
movq (%r12), %rdi
callq 0xf7bc
xorb $0x1, %al
movzbl %al, %eax
addq %rax, 0x50(%rsp)
addq $0x8, %r12
jmp 0x6854
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x3, %rax
cmpq %rax, 0x50(%rsp)
jne 0x68cf
movq (%rsp), %rdi
callq 0x10942
leaq 0xcf7b(%rip), %rdi      # 0x13812
movq 0x8(%rsp), %rsi
movb $0x1, %al
callq 0x5040
movq 0x176c6(%rip), %rax     # 0x1df70
movq (%rax), %rdi
callq 0x5410
movq (%rsp), %rdi
callq 0x10e40
cmpq %rbx, %r13
movl $0x0, %r13d
jae 0x682d
jmp 0x6adb
movq $0x0, 0x8(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x48(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x7028
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x3, %rcx
cmpq %rcx, 0x28(%rsp)
jae 0x6a6f
xorl %r14d, %r14d
movq %r12, %rdi
movq %r13, %rsi
callq 0x7028
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x3, %rcx
cmpq %rcx, %r14
jae 0x6a65
movq %r12, %rdi
movq %r13, %rsi
callq 0x7028
movq %rax, %rdi
movq %r14, %rsi
callq 0x7046
movq 0x8(%rsp), %rcx
cmpl %ecx, 0x4(%rax)
jne 0x6a5d
movq %rbp, %rdi
movq %r14, %rsi
callq 0x6f22
movq (%rax), %rdi
callq 0xf544
movq %rax, %rbx
xorl %r12d, %r12d
movq %rbp, %rdi
movq %r14, %rsi
callq 0x6f22
movq (%rax), %rdi
callq 0xf63c
cmpq %rax, %r12
jae 0x698b
movb (%rbx,%r12), %al
xorb %al, (%r15,%r12)
incq %r12
jmp 0x6966
movq 0x48(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x7028
movq %rax, %rdi
movq %r14, %rsi
callq 0x7046
cmpb $0x0, (%rax)
je 0x6a17
movq %rbp, %rdi
xorl %esi, %esi
callq 0x6f22
movq (%rax), %rdi
callq 0xf63c
movq %r15, %rsi
movq %rax, %rdx
callq 0x6e60
testb %al, %al
jne 0x69f4
movq 0x175dd(%rip), %rdi     # 0x1dfb0
leaq 0xce53(%rip), %rsi      # 0x1382d
callq 0x5380
movq %rax, %rdi
movq 0x30(%rsp), %rsi
callq 0x5240
movq %rax, %rdi
callq 0x5180
movq 0x40(%rsp), %rax
movl 0xc(%rax), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x6a51
movb $0x0, (%r15,%rcx)
incq %rcx
jmp 0x6a08
movq %rbp, %rdi
movq %r14, %rsi
callq 0x6f22
movq (%rax), %rdi
callq 0xf63c
movq (%rsp), %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x1099a
movq %rbp, %rdi
movq %r14, %rsi
callq 0x6f22
movq (%rax), %rdi
callq 0xf63c
addq %rax, 0x38(%rsp)
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
incq %r14
jmp 0x690a
incq 0x28(%rsp)
jmp 0x68e6
movq (%rsp), %rdi
callq 0x10942
movaps %xmm0, %xmm1
movq 0x38(%rsp), %rbx
movq %rbx, %xmm2
punpckldq 0xc5c3(%rip), %xmm2 # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0xc5cb(%rip), %xmm2     # 0x13060
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
addsd %xmm2, %xmm0
movsd 0xc59f(%rip), %xmm2     # 0x13048
mulsd %xmm2, %xmm0
mulsd %xmm2, %xmm0
leaq 0xcd8d(%rip), %rdi      # 0x13845
movb $0x2, %al
callq 0x5040
movq 0x174aa(%rip), %rax     # 0x1df70
movq (%rax), %rdi
callq 0x5410
incq 0x30(%rsp)
incq %r13
jmp 0x681f
leaq 0x10(%rsp), %rdi
callq 0x714e
movb $0x1, %al
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6b15
jmp 0x6b15
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x5160
jmp 0x6b18
jmp 0x6b15
jmp 0x6b15
jmp 0x6b15
jmp 0x6b15
jmp 0x6b15
jmp 0x6b15
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x714e
movq %rbx, %rdi
callq 0x5610