// File: /workspace/github_cpp_makefile_star2_O1/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 0x8447
movq %rax, %r15
leaq 0x30(%rsp), %rbp
leaq 0x20(%rsp), %r12
movq %r15, %rdi
callq 0x5580
movq %rax, %r13
testq %rax, %rax
je 0x8447
cmpb $0x2e, 0x13(%r13)
je 0x839c
cmpb $0x8, 0x12(%r13)
jne 0x839c
movq %rbp, 0x20(%rsp)
movq (%r14), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0x7f46
addq $0x13, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x5680
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x16bbb(%rip), %rdi     # 0x1efb0
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 %rsp, %rdi
movq %r12, %rsi
callq 0xdd70
movq 0x20(%rsp), %rdi
cmpq %rbp, %rdi
je 0x839c
callq 0x5390
jmp 0x839c
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
cmpq %rcx, %rax
je 0x8810
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, 0x16f60(%rip)     # 0x1f3d8
je 0x84a8
shrl %eax
movl $0x20, %edx
movl $0x20, %esi
je 0x848e
bsrl %eax, %esi
xorl $0x1f, %esi
subl %esi, %edx
addl $-0x21, %esi
shrl %ecx
incl %esi
jne 0x8493
movl %ecx, 0x16f39(%rip)     # 0x1f3d8
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
jmp 0x84b0
movsd 0xd550(%rip), %xmm0     # 0x15a00
callq 0x5330
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd 0xd543(%rip), %xmm0     # 0x15a08
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, 0x16ef2(%rip)     # 0x1f3d8
movq 0x16ac3(%rip), %rdi     # 0x1efb0
leaq 0xd5e2(%rip), %rsi      # 0x15ad6
movl $0xf, %edx
callq 0x5450
movl 0x16ed4(%rip), %eax     # 0x1f3d8
leal 0xfffff(%rax), %esi
testl %eax, %eax
cmovnsl %eax, %esi
sarl $0x14, %esi
movq 0x16a97(%rip), %r12     # 0x1efb0
movq %r12, %rdi
callq 0x5630
movq %rax, %r14
leaq 0xd5bb(%rip), %rsi      # 0x15ae6
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 0x8770
movl $0x1, %r14d
xorl %ebp, %ebp
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0xd55d(%rip), %rsi      # 0x15ae9
leaq 0xd55d(%rip), %rdx      # 0x15af0
callq 0xe9fc
movq (%rsp), %rax
movq 0x8(%rsp), %rdx
subq %rax, %rdx
sarq $0x5, %rdx
cmpq %rbp, %rdx
jbe 0x87ee
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 0xd4ef(%rip), %rsi      # 0x15af1
callq 0x5450
movq (%rsp), %rax
movq 0x8(%rsp), %rdx
subq %rax, %rdx
sarq $0x5, %rdx
cmpq %rbp, %rdx
jbe 0x87ee
movq (%rax,%r15), %rsi
movq 0x8(%rax,%r15), %rdx
movq %r12, %rdi
callq 0x5450
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0xcf36(%rip), %rsi      # 0x15579
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 0x87ff
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 0x7f46
movslq 0x16cdd(%rip), %rdx     # 0x1f3d8
movq %r13, %rdi
movq %r15, %rsi
callq 0x111ae
movq 0x88(%rsp), %rdi
cmpq %rbp, %rdi
je 0x8718
callq 0x5390
movq %r13, 0x60(%rsp)
movq 0x48(%rbx), %rsi
cmpq 0x50(%rbx), %rsi
je 0x8731
movq %r13, (%rsi)
addq $0x8, 0x48(%rbx)
jmp 0x8740
movq 0x40(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0xea8c
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x8754
callq 0x5390
movq 0x8(%rsp), %rax
subq (%rsp), %rax
movl %r14d, %ebp
sarq $0x5, %rax
incl %r14d
cmpq %rbp, %rax
ja 0x8576
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 0x7f46
leaq 0xd2ee(%rip), %rsi      # 0x15a91
leaq 0x68(%rsp), %rdi
callq 0x5680
movslq 0x16c24(%rip), %rdx     # 0x1f3d8
leaq 0x68(%rsp), %rsi
movq %r15, %rdi
callq 0x1274c
movq %r15, 0x58(%rbx)
movq 0x68(%rsp), %rdi
cmpq %r12, %rdi
je 0x87d4
callq 0x5390
movq %rsp, %rdi
callq 0xddae
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd0d4(%rip), %rdi      # 0x158c9
movq %rbp, %rsi
xorl %eax, %eax
callq 0x5570
leaq 0xd0c3(%rip), %rdi      # 0x158c9
movq %rbp, %rsi
xorl %eax, %eax
callq 0x5570
movq 0x167d9(%rip), %rdi     # 0x1eff0
leaq 0xd285(%rip), %rsi      # 0x15aa3
movl $0x32, %edx
callq 0x5450
movq 0x167c1(%rip), %rdi     # 0x1eff0
callq 0x5190
movl $0x1, %edi
callq 0x5480
jmp 0x8891
jmp 0x8842
movq %rax, %r14
movq 0x68(%rsp), %rdi
cmpq %r12, %rdi
je 0x8859
callq 0x5390
jmp 0x8859
movq %rax, %r14
movq %r15, %rdi
jmp 0x88bb
movq %rax, %r14
jmp 0x88c8
jmp 0x8896
jmp 0x8891
jmp 0x8891
jmp 0x886b
movq %rax, %r14
jmp 0x8885
movq %rax, %r14
movq 0x88(%rsp), %rdi
cmpq %rbp, %rdi
je 0x8885
callq 0x5390
movq %r13, %rdi
callq 0x5390
jmp 0x88ac
jmp 0x8891
movq %rax, %r14
jmp 0x88c0
movq %rax, %r14
movq 0x20(%rsp), %rdi
cmpq %rbp, %rdi
jne 0x88bb
jmp 0x88c0
jmp 0x88a9
jmp 0x88a9
movq %rax, %r14
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x88c0
callq 0x5390
movq %rsp, %rdi
callq 0xddae
movq 0x40(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x88da
callq 0x5390
movq 0x50(%rsp), %rax
movq (%rax), %rdi
cmpq 0x48(%rsp), %rdi
je 0x88ee
callq 0x5390
movq (%rbx), %rdi
cmpq 0x58(%rsp), %rdi
je 0x88fd
callq 0x5390
movq %r14, %rdi
callq 0x5650
nop