// File: /workspace/github_cpp_makefile_star2_O3/lrq3000[P]rfrb/./src/fileHandler.cpp
// Function: FileHandler::FileHandler(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
FileHandler::FileHandler(std::string inputFolder, std::string outputFolder)
{
	inFolder = inputFolder;
	outFolder = outputFolder;
	std::vector<std::string> imgs;

	//Get all files in the input folder for windows and unix
#ifdef WINDOWS
	HANDLE dir;
	WIN32_FIND_DATA file_data;

	if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
	{
		std::cerr << "Couldn't find any files in directory " << inputFolder << std::endl;
		exit(EXIT_FAILURE);
	}
	do {
		const string file_name = file_data.cFileName;
		const string full_file_name = directory + "/" + file_name;
		const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;

		if (file_name[0] == '.')
			continue;
		if (is_directory)
			continue;
		imgs.push_back(full_file_name);
	} while (FindNextFile(dir, &file_data));

	FindClose(dir);
#else
	DIR *dpdf;
	struct dirent *epdf;
	dpdf = opendir(inputFolder.c_str());
	if (dpdf != NULL)
	{
		while ((epdf=readdir(dpdf))!=NULL)
		{
			if (epdf->d_name[0] == '.')
				continue;
			if (epdf->d_type == DT_REG)
			{
				std::string tmp = inputFolder+epdf->d_name;
				std::cout << tmp << std::endl;
				imgs.push_back(tmp);
			}
		}
	}
#endif

	//Error
	if (imgs.empty())
	{
		std::cerr << "No valid directory or no valid datas in directory." << std::endl;
		exit(EXIT_FAILURE);
	}

	//Initial values are set
	maxBufferSize = (BUFLENGTH/(imgs.size()+0))/2*2;
	size_t realLength=0;
	size_t counter=0;
	while (maxBufferSize != 0)
	{
		maxBufferSize = maxBufferSize >> 1;
		++counter;
	}
	counter-=1;
	realLength = std::pow(2,counter);
	maxBufferSize = std::min(realLength, 16lu*1024lu*1024lu);
	std::cout << "Image-Buffers:\t" << maxBufferSize/1024/1024 << "MB" << std::endl;

	//Calculate MD5Sum, if it is not windows, and initialize the fileReaders and the fileWriter
	for (unsigned int i = 0; i < imgs.size(); ++i)
	{
#ifndef WINDOWS
		std::string sys = "md5sum ";
		sys += imgs.at(i);
		std::cout << std::endl;
		std::cout << "Hash5 for " << imgs.at(i) << ":" << std::endl;
		if (system(sys.c_str()))
		{}
		std::cout << std::endl;
#endif
		FileReader *a = new FileReader(imgs.at(i),maxBufferSize);
		inFiles.push_back(a);
	}
	writer = new FileWriter(outFolder+"recoveredImage.dd",maxBufferSize);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdx, %r15
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x10(%rdi), %rax
movq %rax, 0x58(%rsp)
movq %rax, (%rdi)
xorl %eax, %eax
movq %rax, 0x8(%rdi)
movb %al, 0x10(%rdi)
leaq 0x20(%rdi), %r12
leaq 0x30(%rdi), %rcx
movq %rcx, 0x48(%rsp)
movq %rcx, 0x20(%rdi)
movq %rax, 0x28(%rdi)
movb %al, 0x30(%rdi)
leaq 0x40(%rdi), %rcx
movq %rcx, 0x40(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x40(%rdi)
movq %rax, 0x50(%rdi)
movq %r12, 0x50(%rsp)
callq 0x51d0
movq %r12, %rdi
movq %r15, %rsi
callq 0x51d0
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq (%r14), %rdi
callq 0x5560
testq %rax, %rax
je 0x87e8
movq %rax, %r15
leaq 0x20(%rsp), %r12
movq %rsp, %rbp
movq %r15, %rdi
callq 0x5580
movq %rax, %r13
testq %rax, %rax
je 0x841f
cmpb $0x2e, 0x13(%r13)
je 0x836a
cmpb $0x8, 0x12(%r13)
jne 0x836a
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x7f0e
addq $0x13, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x5680
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x14be8(%rip), %rdi     # 0x1cfb0
callq 0x5450
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
movq %rbp, %rdi
movq %r12, %rsi
callq 0xd4d6
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x836a
callq 0x5390
jmp 0x836a
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
cmpq %rcx, %rax
je 0x87e8
subq %rax, %rcx
sarq $0x5, %rcx
movl $0x40000000, %eax       # imm = 0x40000000
xorl %edx, %edx
divq %rcx
movl %eax, %ecx
andl $0x7ffffffe, %ecx       # imm = 0x7FFFFFFE
movl %ecx, 0x14f88(%rip)     # 0x1d3d8
je 0x8480
shrl %eax
movl $0x20, %edx
movl $0x20, %esi
je 0x8466
bsrl %eax, %esi
xorl $0x1f, %esi
subl %esi, %edx
addl $-0x21, %esi
shrl %ecx
incl %esi
jne 0x846b
movl %ecx, 0x14f61(%rip)     # 0x1d3d8
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
jmp 0x8488
movsd 0xc578(%rip), %xmm0     # 0x14a00
callq 0x5330
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd 0xc56b(%rip), %xmm0     # 0x14a08
cvttsd2si %xmm0, %rdx
sarq $0x3f, %rcx
andq %rcx, %rdx
orq %rax, %rdx
movl $0x1000000, %eax        # imm = 0x1000000
cmpq %rax, %rdx
cmovaeq %rax, %rdx
movl %edx, 0x14f1a(%rip)     # 0x1d3d8
movq 0x14aeb(%rip), %rdi     # 0x1cfb0
leaq 0xc60a(%rip), %rsi      # 0x14ad6
movl $0xf, %edx
callq 0x5450
movl 0x14efc(%rip), %eax     # 0x1d3d8
leal 0xfffff(%rax), %esi
testl %eax, %eax
cmovnsl %eax, %esi
sarl $0x14, %esi
movq 0x14abf(%rip), %r12     # 0x1cfb0
movq %r12, %rdi
callq 0x5630
movq %rax, %r14
leaq 0xc5e3(%rip), %rsi      # 0x14ae6
movl $0x2, %edx
movq %rax, %rdi
callq 0x5450
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x53b0
movsbl %al, %esi
movq %r14, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5260
movq 0x8(%rsp), %rax
cmpq (%rsp), %rax
je 0x8748
movl $0x1, %r14d
xorl %ebp, %ebp
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0xc585(%rip), %rsi      # 0x14ae9
leaq 0xc585(%rip), %rdx      # 0x14af0
callq 0xe190
movq (%rsp), %rax
movq 0x8(%rsp), %rdx
subq %rax, %rdx
sarq $0x5, %rdx
cmpq %rbp, %rdx
jbe 0x87c6
movq %rbp, %r15
shlq $0x5, %r15
movq (%rax,%r15), %rsi
movq 0x8(%rax,%r15), %rdx
leaq 0x20(%rsp), %rdi
callq 0x5100
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x53b0
movsbl %al, %esi
movq %r12, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5260
movl $0xa, %edx
movq %r12, %rdi
leaq 0xc517(%rip), %rsi      # 0x14af1
callq 0x5450
movq (%rsp), %rax
movq 0x8(%rsp), %rdx
subq %rax, %rdx
sarq $0x5, %rdx
cmpq %rbp, %rdx
jbe 0x87c6
movq (%rax,%r15), %rsi
movq 0x8(%rax,%r15), %rdx
movq %r12, %rdi
callq 0x5450
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0xbf5e(%rip), %rsi      # 0x14579
callq 0x5450
movq (%r13), %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
movq 0x20(%rsp), %rdi
callq 0x52f0
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x53b0
movsbl %al, %esi
movq %r12, %rdi
callq 0x5040
movq %rax, %rdi
callq 0x5260
movl $0x2b8, %edi            # imm = 0x2B8
callq 0x53d0
movq %rax, %r13
movq (%rsp), %rax
movq 0x8(%rsp), %rdx
subq %rax, %rdx
sarq $0x5, %rdx
cmpq %rbp, %rdx
jbe 0x87d7
leaq 0x98(%rsp), %rbp
movq %rbp, 0x88(%rsp)
movq (%rax,%r15), %rsi
movq 0x8(%rax,%r15), %rdx
addq %rsi, %rdx
leaq 0x88(%rsp), %r15
movq %r15, %rdi
callq 0x7f0e
movslq 0x14d05(%rip), %rdx     # 0x1d3d8
movq %r13, %rdi
movq %r15, %rsi
callq 0x107b2
movq 0x88(%rsp), %rdi
cmpq %rbp, %rdi
je 0x86f0
callq 0x5390
movq %r13, 0x60(%rsp)
movq 0x48(%rbx), %rsi
cmpq 0x50(%rbx), %rsi
je 0x8709
movq %r13, (%rsi)
addq $0x8, 0x48(%rbx)
jmp 0x8718
movq 0x40(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0xe226
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x872c
callq 0x5390
movq 0x8(%rsp), %rax
subq (%rsp), %rax
movl %r14d, %ebp
sarq $0x5, %rax
incl %r14d
cmpq %rbp, %rax
ja 0x854e
movl $0x2a0, %edi            # imm = 0x2A0
callq 0x53d0
movq %rax, %r15
leaq 0x78(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
addq %rsi, %rdx
leaq 0x68(%rsp), %rdi
callq 0x7f0e
leaq 0xc316(%rip), %rsi      # 0x14a91
leaq 0x68(%rsp), %rdi
callq 0x5680
movslq 0x14c4c(%rip), %rdx     # 0x1d3d8
leaq 0x68(%rsp), %rsi
movq %r15, %rdi
callq 0x11ad0
movq %r15, 0x58(%rbx)
movq 0x68(%rsp), %rdi
cmpq %r12, %rdi
je 0x87ac
callq 0x5390
movq %rsp, %rdi
callq 0xd514
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc0fc(%rip), %rdi      # 0x148c9
movq %rbp, %rsi
xorl %eax, %eax
callq 0x5570
leaq 0xc0eb(%rip), %rdi      # 0x148c9
movq %rbp, %rsi
xorl %eax, %eax
callq 0x5570
movq 0x14801(%rip), %rdi     # 0x1cff0
leaq 0xc2ad(%rip), %rsi      # 0x14aa3
movl $0x32, %edx
callq 0x5450
movq 0x147e9(%rip), %rdi     # 0x1cff0
callq 0x5190
movl $0x1, %edi
callq 0x5480
jmp 0x8818
movq %rax, %r14
movq 0x68(%rsp), %rdi
cmpq %r12, %rdi
je 0x882f
callq 0x5390
jmp 0x882f
movq %rax, %r14
movq %r15, %rdi
jmp 0x8886
jmp 0x886b
movq %rax, %r14
jmp 0x8893
jmp 0x886b
jmp 0x8874
jmp 0x886b
jmp 0x8843
movq %rax, %r14
jmp 0x885d
movq %rax, %r14
movq 0x88(%rsp), %rdi
cmpq %rbp, %rdi
je 0x885d
callq 0x5390
movq %r13, %rdi
callq 0x5390
jmp 0x8877
jmp 0x886b
jmp 0x8874
movq %rax, %r14
jmp 0x888b
jmp 0x8874
jmp 0x8874
movq %rax, %r14
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x888b
callq 0x5390
movq %rsp, %rdi
callq 0xd514
movq 0x40(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x88a5
callq 0x5390
movq 0x50(%rsp), %rax
movq (%rax), %rdi
cmpq 0x48(%rsp), %rdi
je 0x88b9
callq 0x5390
movq (%rbx), %rdi
cmpq 0x58(%rsp), %rdi
je 0x88c8
callq 0x5390
movq %r14, %rdi
callq 0x5650