// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq %rdi, %rbx
movq (%rdi), %rdi
callq 0x894c
leaq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x7e58
movq (%rbx), %rdi
callq 0x89b2
movq %rax, 0x28(%rsp)
leaq 0x68(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %rdi
callq 0x7f46
leaq 0x58(%rsp), %rsi
movq 0x28(%rsp), %rdi
callq 0x12f64
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x7155
callq 0x5390
movq (%rbx), %rdi
callq 0x8bf4
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x71bc
movl $0x1, %ebp
xorl %r14d, %r14d
movq (%rax,%r14,8), %rdi
movslq 0xc(%rbx), %rsi
callq 0x1135a
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r14, %rdx
jbe 0x7674
movq (%rax,%r14,8), %rdi
callq 0x11674
movl %ebp, %r14d
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
incl %ebp
cmpq %r14, %rcx
ja 0x7171
movl 0xc(%rbx), %eax
testl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmovnsl %eax, %ecx
movslq %ecx, %r14
movq %r14, %rdi
callq 0x5030
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x51e0
movq $0x0, 0x40(%rsp)
movq $0x0, 0x30(%rsp)
xorl %r12d, %r12d
movq %rbx, 0x38(%rsp)
movq 0x18(%rbx), %rax
cmpq 0x10(%rbx), %rax
setne %bpl
je 0x760c
movq $0x0, 0x8(%rsp)
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r14
cmpq %r14, %rbx
je 0x723e
movq (%rbx), %rdi
callq 0x11538
xorb $0x1, %al
movzbl %al, %eax
addq %rax, %r12
addq $0x8, %rbx
jmp 0x7223
movq 0x18(%rsp), %rax
subq 0x10(%rsp), %rax
sarq $0x3, %rax
cmpq %rax, %r12
je 0x75cd
movq %r12, 0x48(%rsp)
movq 0x38(%rsp), %rbx
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
movq 0x8(%rsp), %rbp
cmpq %rbp, %rdx
jbe 0x7663
xorl %esi, %esi
leaq (,%rbp,2), %r12
addq %rbp, %r12
xorl %r14d, %r14d
movq 0x8(%rax,%r12,8), %rcx
subq (%rax,%r12,8), %rcx
sarq $0x3, %rcx
cmpq %rcx, %rsi
jae 0x7537
movq %rsi, 0x50(%rsp)
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %rbp, %rdx
jbe 0x764c
xorl %ebp, %ebp
movq (%rax,%r12,8), %rcx
movq 0x8(%rax,%r12,8), %rax
subq %rcx, %rax
sarq $0x3, %rax
cmpq %rax, %rbp
jae 0x74ff
cmpl %r14d, 0x4(%rcx,%rbp,8)
jne 0x74cf
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbp, %rdx
jbe 0x764c
movq (%rax,%rbp,8), %rdi
callq 0x11258
movq %rax, %r13
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbp, %rdx
jbe 0x7636
xorl %ebx, %ebx
movq (%rax,%rbp,8), %rdi
callq 0x11352
cmpq %rax, %rbx
jae 0x7378
movb (%r13,%rbx), %al
xorb %al, (%r15,%rbx)
incq %rbx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbp, %rdx
ja 0x7343
jmp 0x7636
movq 0x38(%rsp), %rbx
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq 0x8(%rsp), %rdx
jbe 0x7647
movq (%rax,%r12,8), %rcx
movq 0x8(%rax,%r12,8), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rbp, %rdx
jbe 0x764c
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
cmpb $0x0, (%rcx,%rbp,8)
je 0x747b
cmpq %rax, %rdx
je 0x765d
movq (%rax), %rdi
callq 0x11352
testq %rax, %rax
sete %cl
je 0x740f
cmpb $0x0, (%r15)
jg 0x740f
movl $0x1, %edx
movq %rdx, %rcx
cmpq %rdx, %rax
je 0x7409
leaq 0x1(%rcx), %rdx
cmpb $0x0, (%r15,%rcx)
jle 0x73f6
cmpq %rax, %rcx
setae %cl
testb %cl, %cl
jne 0x7466
movl $0x17, %edx
movq 0x17b91(%rip), %rdi     # 0x1efb0
leaq 0xe447(%rip), %rsi      # 0x1586d
callq 0x5450
movq 0x17b7e(%rip), %rdi     # 0x1efb0
movq 0x40(%rsp), %rsi
callq 0x5250
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x53b0
movsbl %al, %esi
movq %r13, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5260
movslq 0xc(%rbx), %rdx
testq %rdx, %rdx
jle 0x74cc
movq %r15, %rdi
xorl %esi, %esi
callq 0x51e0
jmp 0x74cc
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbp, %rdx
jbe 0x764c
movq (%rax,%rbp,8), %rdi
callq 0x11352
movq 0x28(%rsp), %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x12924
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbp, %rdx
jbe 0x764c
movq (%rax,%rbp,8), %rdi
callq 0x11352
addq %rax, 0x30(%rsp)
incl %r14d
incq %rbp
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq 0x8(%rsp), %rdx
ja 0x72dd
jmp 0x7647
movq 0x50(%rsp), %rsi
incq %rsi
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
movq 0x8(%rsp), %rbp
cmpq %rbp, %rdx
ja 0x729a
jmp 0x7663
movq 0x28(%rsp), %rdi
callq 0x128cc
movaps %xmm0, %xmm1
movsd 0x30(%rsp), %xmm2
unpcklps 0xdb3f(%rip), %xmm2     # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0xdb47(%rip), %xmm2     # 0x150a0
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
addsd %xmm2, %xmm0
movsd 0xdb0b(%rip), %xmm2     # 0x15078
mulsd %xmm2, %xmm0
mulsd %xmm2, %xmm0
leaq 0xe309(%rip), %rdi      # 0x15885
movb $0x2, %al
callq 0x5050
movq 0x179de(%rip), %rax     # 0x1ef68
movq (%rax), %rdi
callq 0x5470
incq 0x40(%rsp)
incq %rbp
movq 0x18(%rbx), %rax
subq 0x10(%rbx), %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rax
movq %rbp, 0x8(%rsp)
cmpq %rax, %rbp
setb %bpl
movq 0x48(%rsp), %r12
jb 0x7219
jmp 0x760c
movq 0x28(%rsp), %rdi
callq 0x128cc
movq 0x38(%rsp), %rbx
movq 0x30(%rsp), %rsi
shrq $0x14, %rsi
leaq 0xe266(%rip), %rdi      # 0x15852
movb $0x1, %al
callq 0x5050
movq 0x1796e(%rip), %rax     # 0x1ef68
movq (%rax), %rdi
callq 0x5470
movq 0x28(%rsp), %rdi
callq 0x12da6
testb $0x1, %bpl
je 0x71fe
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x7625
callq 0x5390
movb $0x1, %al
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe28c(%rip), %rdi      # 0x158c9
movq %rbp, %rsi
xorl %eax, %eax
callq 0x5570
movq 0x8(%rsp), %rbp
leaq 0xe276(%rip), %rdi      # 0x158c9
movq %rbp, %rsi
xorl %eax, %eax
callq 0x5570
xorl %ebp, %ebp
xorl %edx, %edx
jmp 0x764c
leaq 0xe25f(%rip), %rdi      # 0x158c9
movq %rbp, %rsi
xorl %eax, %eax
callq 0x5570
leaq 0xe24e(%rip), %rdi      # 0x158c9
movq %r14, %rsi
xorl %eax, %eax
callq 0x5570
jmp 0x76af
movq %rax, %rbx
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x76b2
callq 0x5390
jmp 0x76b2
jmp 0x76af
jmp 0x76af
jmp 0x76af
jmp 0x76af
jmp 0x76af
jmp 0x76af
jmp 0x76af
jmp 0x76af
jmp 0x76af
jmp 0x76af
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x76c1
callq 0x5390
movq %rbx, %rdi
callq 0x5650
nop