// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
movq (%rdi), %rdi
callq 0x8916
leaq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x7e20
movq %r14, 0x8(%rsp)
movq (%r14), %rdi
callq 0x897c
movq %rax, %r13
leaq 0x68(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %rdi
callq 0x7f0e
leaq 0x58(%rsp), %rsi
movq %r13, %rdi
callq 0x12228
movq 0x58(%rsp), %rdi
cmpq %r14, %rdi
je 0x7100
callq 0x5390
movq 0x8(%rsp), %rax
movq (%rax), %rdi
callq 0x8baa
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x716f
movl $0x1, %ebp
xorl %ebx, %ebx
movq (%rax,%rbx,8), %rdi
movq 0x8(%rsp), %rax
movslq 0xc(%rax), %rsi
callq 0x1095e
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
jbe 0x7634
movq (%rax,%rbx,8), %rdi
callq 0x10c3a
movl %ebp, %ebx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
incl %ebp
cmpq %rbx, %rcx
ja 0x7120
movq 0x8(%rsp), %rax
movl 0xc(%rax), %eax
testl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmovnsl %eax, %ecx
movslq %ecx, %rbx
movq %rbx, %rdi
callq 0x5030
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x51e0
movq $0x0, 0x40(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x38(%rsp)
movq %r13, 0x48(%rsp)
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rax
cmpq 0x10(%rcx), %rax
je 0x71bc
xorl %r14d, %r14d
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r15
movq %rbx, %rax
cmpq %r15, %rbx
je 0x7205
movq (%rbx), %rdi
callq 0x10b20
xorb $0x1, %al
movzbl %al, %eax
addq %rax, 0x38(%rsp)
addq $0x8, %rbx
cmpq %r15, %rbx
jne 0x71e0
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %rax
subq %rbx, %rax
sarq $0x3, %rax
cmpq %rax, 0x38(%rsp)
je 0x75a1
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rax
movq 0x18(%rcx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rsi # imm = 0xAAAAAAAAAAAAAAAB
imulq %rsi, %rdx
cmpq %r14, %rdx
jbe 0x7623
leaq (%r14,%r14,2), %rdx
movq (%rax,%rdx,8), %rsi
movq %rdx, 0x30(%rsp)
movq 0x8(%rax,%rdx,8), %rdi
xorl %r9d, %r9d
xorl %r13d, %r13d
subq %rsi, %rdi
sarq $0x3, %rdi
cmpq %rdi, %r9
jae 0x750e
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rsi # imm = 0xAAAAAAAAAAAAAAAB
imulq %rsi, %rdx
cmpq %r14, %rdx
jbe 0x760b
movq %r9, 0x50(%rsp)
movq 0x30(%rsp), %rdx
movq (%rax,%rdx,8), %rsi
movq 0x8(%rax,%rdx,8), %rdi
cmpq %rsi, %rdi
je 0x74e0
xorl %r15d, %r15d
cmpl %r13d, 0x4(%rsi,%r15,8)
jne 0x749b
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x7608
movq (%rax,%r15,8), %rdi
callq 0x1085c
movq %rax, %rbx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x75f7
movq %r14, %rbp
xorl %r14d, %r14d
movq (%rax,%r15,8), %rdi
callq 0x10956
cmpq %rax, %r14
jae 0x7332
movb (%rbx,%r14), %al
xorb %al, (%r12,%r14)
incq %r14
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
ja 0x72fe
jmp 0x75f7
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rax
movq 0x18(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
movq %rbp, %r14
cmpq %rbp, %rdx
jbe 0x760b
movq 0x30(%rsp), %rdx
movq (%rax,%rdx,8), %rcx
movq 0x8(%rax,%rdx,8), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x7608
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
cmpb $0x0, (%rcx,%r15,8)
je 0x743a
cmpq %rax, %rdx
je 0x761c
movq (%rax), %rdi
callq 0x10956
testq %rax, %rax
je 0x7420
cmpb $0x0, (%r12)
jg 0x73cd
movl $0x1, %edx
movq %rdx, %rcx
cmpq %rdx, %rax
je 0x73c8
leaq 0x1(%rcx), %rdx
cmpb $0x0, (%r12,%rcx)
jle 0x73b5
cmpq %rax, %rcx
jae 0x7420
movl $0x17, %edx
movq 0x15bd7(%rip), %rdi     # 0x1cfb0
leaq 0xd48d(%rip), %rsi      # 0x1486d
callq 0x5450
movq 0x15bc4(%rip), %rdi     # 0x1cfb0
movq 0x40(%rsp), %rsi
callq 0x5250
movq %rax, %rbx
movq (%rax), %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
movq 0x8(%rsp), %rax
movslq 0xc(%rax), %rdx
testq %rdx, %rdx
jle 0x748b
movq %r12, %rdi
xorl %esi, %esi
callq 0x51e0
jmp 0x748b
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x7608
movq (%rax,%r15,8), %rdi
callq 0x10956
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x11ca8
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x7608
movq (%rax,%r15,8), %rdi
callq 0x10956
addq %rax, 0x28(%rsp)
incl %r13d
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %rax
movq 0x18(%rcx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rsi # imm = 0xAAAAAAAAAAAAAAAB
imulq %rsi, %rdx
cmpq %r14, %rdx
jbe 0x760b
incq %r15
movq 0x30(%rsp), %rdx
movq (%rax,%rdx,8), %rsi
movq 0x8(%rax,%rdx,8), %rdi
movq %rdi, %rdx
subq %rsi, %rdx
sarq $0x3, %rdx
cmpq %rdx, %r15
jb 0x72ad
movq 0x50(%rsp), %r9
incq %r9
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %r8 # imm = 0xAAAAAAAAAAAAAAAB
imulq %r8, %rdx
cmpq %r14, %rdx
ja 0x725d
jmp 0x7623
movq 0x48(%rsp), %r13
movq %r13, %rdi
callq 0x11c50
movaps %xmm0, %xmm1
movsd 0x28(%rsp), %xmm2
unpcklps 0xcb65(%rip), %xmm2     # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0xcb6d(%rip), %xmm2     # 0x140a0
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
addsd %xmm2, %xmm0
movsd 0xcb31(%rip), %xmm2     # 0x14078
mulsd %xmm2, %xmm0
mulsd %xmm2, %xmm0
leaq 0xd32f(%rip), %rdi      # 0x14885
movb $0x2, %al
callq 0x5050
movq 0x15a04(%rip), %rax     # 0x1cf68
movq (%rax), %rdi
callq 0x5470
incq 0x40(%rsp)
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rax
subq 0x10(%rcx), %rax
incq %r14
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rax
cmpq %rax, %r14
jb 0x71ce
jmp 0x71bc
movq %r13, %rdi
callq 0x11c50
movq 0x28(%rsp), %rsi
shrq $0x14, %rsi
leaq 0xd299(%rip), %rdi      # 0x14852
movb $0x1, %al
callq 0x5050
movq 0x159a1(%rip), %rax     # 0x1cf68
movq (%rax), %rdi
callq 0x5470
movq %r13, %rdi
callq 0x1206a
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x75e6
callq 0x5390
movb $0x1, %al
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd2cb(%rip), %rdi      # 0x148c9
movq %r15, %rsi
xorl %eax, %eax
callq 0x5570
movq %r15, %r14
leaq 0xd2b7(%rip), %rdi      # 0x148c9
movq %r14, %rsi
xorl %eax, %eax
callq 0x5570
xorl %r14d, %r14d
xorl %edx, %edx
jmp 0x760b
leaq 0xd29f(%rip), %rdi      # 0x148c9
movq %r14, %rsi
xorl %eax, %eax
callq 0x5570
leaq 0xd28e(%rip), %rdi      # 0x148c9
movq %rbx, %rsi
xorl %eax, %eax
callq 0x5570
jmp 0x766f
movq %rax, %rbx
movq 0x58(%rsp), %rdi
cmpq %r14, %rdi
je 0x7672
callq 0x5390
jmp 0x7672
jmp 0x766f
jmp 0x766f
jmp 0x766f
jmp 0x766f
jmp 0x766f
jmp 0x766f
jmp 0x766f
jmp 0x766f
jmp 0x766f
jmp 0x766f
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x7681
callq 0x5390
movq %rbx, %rdi
callq 0x5650
nop