// File: 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:
subq $0x228, %rsp            # imm = 0x228
movq %rsi, 0x130(%rsp)
movq %rdi, 0x220(%rsp)
movq %rsi, 0x218(%rsp)
movq 0x220(%rsp), %rax
movq %rax, 0x138(%rsp)
movq (%rax), %rdi
callq 0xcc90
movq %rax, %rsi
leaq 0x200(%rsp), %rdi
callq 0x9c50
movq 0x138(%rsp), %rax
movq (%rax), %rdi
callq 0xcd30
movq %rax, 0x140(%rsp)
jmp 0x8a61
movq 0x130(%rsp), %rsi
movq 0x140(%rsp), %rax
movq %rax, 0x1f0(%rsp)
movq 0x1f0(%rsp), %rax
movq %rax, 0x128(%rsp)
leaq 0x1c0(%rsp), %rdi
callq 0x6190
jmp 0x8a98
movq 0x128(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
callq 0x34cb0
jmp 0x8aaf
leaq 0x1c0(%rsp), %rdi
callq 0x6260
movq 0x138(%rsp), %rax
movq (%rax), %rdi
callq 0xd0b0
jmp 0x8ace
movl $0x0, 0x1bc(%rsp)
movl 0x1bc(%rsp), %eax
movq %rax, 0x120(%rsp)
leaq 0x200(%rsp), %rdi
callq 0x9d50
movq %rax, %rcx
movq 0x120(%rsp), %rax
cmpq %rcx, %rax
jae 0x8bd0
movl 0x1bc(%rsp), %eax
movl %eax, %esi
leaq 0x200(%rsp), %rdi
callq 0x9d70
movq %rax, 0x118(%rsp)
jmp 0x8b29
movq 0x138(%rsp), %rax
movq 0x118(%rsp), %rcx
movq (%rcx), %rdi
movslq 0xc(%rax), %rsi
callq 0x2fed0
jmp 0x8b47
movl 0x1bc(%rsp), %eax
movl %eax, %esi
leaq 0x200(%rsp), %rdi
callq 0x9d70
movq %rax, 0x110(%rsp)
jmp 0x8b67
movq 0x110(%rsp), %rax
movq (%rax), %rdi
callq 0x30450
jmp 0x8b79
jmp 0x8b7b
movl 0x1bc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1bc(%rsp)
jmp 0x8ad9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e8(%rsp)
movl %eax, 0x1e4(%rsp)
jmp 0x930d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1e8(%rsp)
movl %eax, 0x1e4(%rsp)
leaq 0x1c0(%rsp), %rdi
callq 0x6260
jmp 0x930d
movq 0x138(%rsp), %rax
movslq 0xc(%rax), %rdi
testq %rdi, %rdi
movq $-0x1, %rax
cmovsq %rax, %rdi
movq %rdi, 0x100(%rsp)
callq 0x6030
movq %rax, 0x108(%rsp)
jmp 0x8c01
movq 0x108(%rsp), %rdi
movq 0x100(%rsp), %rdx
xorl %esi, %esi
callq 0x6200
movq 0x108(%rsp), %rax
movq %rax, 0x1b0(%rsp)
movq $0x0, 0x1a8(%rsp)
movq $0x0, 0x1a0(%rsp)
movq $0x0, 0x198(%rsp)
jmp 0x8c4e
movq $0x0, 0x190(%rsp)
movq 0x138(%rsp), %rdi
movq 0x190(%rsp), %rax
movq %rax, 0xf8(%rsp)
addq $0x10, %rdi
callq 0x9e80
movq %rax, %rcx
movq 0xf8(%rsp), %rax
cmpq %rcx, %rax
jae 0x9308
leaq 0x200(%rsp), %rax
movq %rax, 0x188(%rsp)
movq 0x188(%rsp), %rdi
callq 0xa3e0
movq %rax, 0x180(%rsp)
movq 0x188(%rsp), %rdi
callq 0xa410
movq %rax, 0x178(%rsp)
leaq 0x180(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0xa440
testb $0x1, %al
jne 0x8ce4
jmp 0x8d48
leaq 0x180(%rsp), %rdi
callq 0xa480
movq %rax, 0x170(%rsp)
movq 0x170(%rsp), %rax
movq (%rax), %rdi
callq 0x30220
movb %al, 0xf7(%rsp)
jmp 0x8d12
movb 0xf7(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x8d37
movq 0x198(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x198(%rsp)
jmp 0x8d39
leaq 0x180(%rsp), %rdi
callq 0xa490
jmp 0x8cc9
movq 0x198(%rsp), %rax
movq %rax, 0xe8(%rsp)
leaq 0x200(%rsp), %rdi
callq 0x9d50
movq %rax, %rcx
movq 0xe8(%rsp), %rax
cmpq %rcx, %rax
jne 0x8dff
movq 0x1a0(%rsp), %rax
shrq $0x14, %rax
movq %rax, 0xd8(%rsp)
movq 0x1f0(%rsp), %rdi
callq 0x34770
movsd %xmm0, 0xe0(%rsp)
jmp 0x8da5
movsd 0xe0(%rsp), %xmm0
movq 0xd8(%rsp), %rsi
leaq 0x31a25(%rip), %rdi     # 0x3a7e2
movb $0x1, %al
callq 0x6060
jmp 0x8dc6
movq 0x4b193(%rip), %rax     # 0x53f60
movq (%rax), %rdi
callq 0x64f0
jmp 0x8dd7
movq 0x1f0(%rsp), %rdi
callq 0x34ae0
jmp 0x8de6
leaq 0x200(%rsp), %rdi
callq 0x9db0
movb $0x1, %al
andb $0x1, %al
addq $0x228, %rsp            # imm = 0x228
retq
movl $0x0, 0x16c(%rsp)
movq $0x0, 0x160(%rsp)
movq 0x138(%rsp), %rdi
movq 0x160(%rsp), %rax
movq %rax, 0xc8(%rsp)
addq $0x10, %rdi
movq 0x190(%rsp), %rsi
callq 0xa4b0
movq %rax, 0xd0(%rsp)
jmp 0x8e49
movq 0xd0(%rsp), %rdi
callq 0xa4f0
movq %rax, %rcx
movq 0xc8(%rsp), %rax
cmpq %rcx, %rax
jae 0x9258
movq $0x0, 0x158(%rsp)
movq 0x138(%rsp), %rdi
movq 0x158(%rsp), %rax
movq %rax, 0xb8(%rsp)
addq $0x10, %rdi
movq 0x190(%rsp), %rsi
callq 0xa4b0
movq %rax, 0xc0(%rsp)
jmp 0x8ea9
movq 0xc0(%rsp), %rdi
callq 0xa4f0
movq %rax, %rcx
movq 0xb8(%rsp), %rax
cmpq %rcx, %rax
jae 0x923d
movq 0x138(%rsp), %rdi
addq $0x10, %rdi
movq 0x190(%rsp), %rsi
callq 0xa4b0
movq %rax, 0xb0(%rsp)
jmp 0x8eed
movq 0xb0(%rsp), %rdi
movq 0x158(%rsp), %rsi
callq 0xa510
movq %rax, 0xa8(%rsp)
jmp 0x8f0c
movq 0xa8(%rsp), %rax
movl 0x4(%rax), %eax
cmpl 0x16c(%rsp), %eax
jne 0x9222
movq 0x158(%rsp), %rsi
leaq 0x200(%rsp), %rdi
callq 0x9d70
movq %rax, 0xa0(%rsp)
jmp 0x8f43
movq 0xa0(%rsp), %rax
movq (%rax), %rdi
callq 0x2fc80
movq %rax, 0x98(%rsp)
jmp 0x8f5d
movq 0x98(%rsp), %rax
movq %rax, 0x1f8(%rsp)
movq $0x0, 0x150(%rsp)
movq 0x150(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x158(%rsp), %rsi
leaq 0x200(%rsp), %rdi
callq 0x9d70
movq %rax, 0x90(%rsp)
jmp 0x8fa8
movq 0x90(%rsp), %rax
movq (%rax), %rdi
callq 0x2feb0
movq %rax, 0x80(%rsp)
jmp 0x8fc2
movq 0x88(%rsp), %rax
movq 0x80(%rsp), %rcx
cmpq %rcx, %rax
jae 0x902f
movq 0x1b0(%rsp), %rax
movq 0x150(%rsp), %rcx
movsbl (%rax,%rcx), %eax
movq 0x1f8(%rsp), %rcx
movq 0x150(%rsp), %rdx
movsbl (%rcx,%rdx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq 0x1b0(%rsp), %rax
movq 0x150(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x150(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x150(%rsp)
jmp 0x8f79
movq 0x138(%rsp), %rdi
addq $0x10, %rdi
movq 0x190(%rsp), %rsi
callq 0xa4b0
movq %rax, 0x78(%rsp)
jmp 0x904f
movq 0x78(%rsp), %rdi
movq 0x158(%rsp), %rsi
callq 0xa510
movq %rax, 0x70(%rsp)
jmp 0x9068
movq 0x70(%rsp), %rax
movb (%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x9127
movq 0x1f0(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x1f8(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x158(%rsp), %rsi
leaq 0x200(%rsp), %rdi
callq 0x9d70
movq %rax, 0x68(%rsp)
jmp 0x90b3
movq 0x68(%rsp), %rax
movq (%rax), %rdi
callq 0x2feb0
movq %rax, 0x50(%rsp)
jmp 0x90c7
movq 0x50(%rsp), %rdx
movq 0x60(%rsp), %rsi
movq 0x58(%rsp), %rdi
callq 0x34830
jmp 0x90dd
movq 0x158(%rsp), %rsi
leaq 0x200(%rsp), %rdi
callq 0x9d70
movq %rax, 0x48(%rsp)
jmp 0x90f9
movq 0x48(%rsp), %rax
movq (%rax), %rdi
callq 0x2feb0
movq %rax, 0x40(%rsp)
jmp 0x910d
movq 0x40(%rsp), %rax
addq 0x1a0(%rsp), %rax
movq %rax, 0x1a0(%rsp)
jmp 0x9211
movq 0x1b0(%rsp), %rax
movq %rax, 0x30(%rsp)
xorl %eax, %eax
movl %eax, %esi
leaq 0x200(%rsp), %rdi
callq 0x9d70
movq %rax, 0x38(%rsp)
jmp 0x914c
movq 0x38(%rsp), %rax
movq (%rax), %rdi
callq 0x2feb0
movq %rax, 0x28(%rsp)
jmp 0x9160
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rsi
movq 0x138(%rsp), %rdi
callq 0x9ad0
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x91c9
movq 0x4ae28(%rip), %rdi     # 0x53fb0
leaq 0x3166e(%rip), %rsi     # 0x3a7fd
callq 0x6420
movq %rax, 0x20(%rsp)
jmp 0x919b
movq 0x20(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
callq 0x60f0
movq %rax, 0x18(%rsp)
jmp 0x91b4
movq 0x18(%rsp), %rdi
movq 0x4adb0(%rip), %rsi     # 0x53f70
callq 0x6470
jmp 0x91c7
jmp 0x91c9
movl $0x0, 0x14c(%rsp)
movq 0x138(%rsp), %rcx
movl 0x14c(%rsp), %eax
cmpl 0xc(%rcx), %eax
jge 0x920f
movq 0x1b0(%rsp), %rax
movslq 0x14c(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movl 0x14c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14c(%rsp)
jmp 0x91d4
jmp 0x9211
movl 0x16c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x16c(%rsp)
jmp 0x9224
movq 0x158(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x158(%rsp)
jmp 0x8e76
jmp 0x923f
movq 0x160(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x160(%rsp)
jmp 0x8e16
movaps 0x30dc1(%rip), %xmm1    # 0x3a020
movq 0x1a0(%rsp), %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x30dbc(%rip), %xmm1    # 0x3a030
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x30d8d(%rip), %xmm1    # 0x3a018
mulsd %xmm1, %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x1f0(%rsp), %rdi
callq 0x34770
movsd %xmm0, 0x10(%rsp)
jmp 0x92ae
movsd 0x10(%rsp), %xmm1
movsd 0x8(%rsp), %xmm0
leaq 0x31554(%rip), %rdi     # 0x3a815
movb $0x2, %al
callq 0x6060
jmp 0x92ca
movq 0x4ac8f(%rip), %rax     # 0x53f60
movq (%rax), %rdi
callq 0x64f0
jmp 0x92db
movq 0x1a8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x1a8(%rsp)
movq 0x190(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x190(%rsp)
jmp 0x8c5a
jmp 0x8c4c
leaq 0x200(%rsp), %rdi
callq 0x9db0
movq 0x1e8(%rsp), %rdi
callq 0x6750
nopw (%rax,%rax)