// File: /workspace/github_cpp_makefile_star2_O2/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 $0x98, %rsp
movq %rdx, %r14
leaq 0x10(%rdi), %rax
movq %rax, (%rdi)
andq $0x0, 0x8(%rdi)
movq %rsi, %rbx
xorl %eax, %eax
movb %al, 0x10(%rdi)
leaq 0x30(%rdi), %rcx
movq %rcx, 0x20(%rdi)
andq $0x0, 0x28(%rdi)
leaq 0x20(%rdi), %r15
movb %al, 0x30(%rdi)
leaq 0x40(%rdi), %rax
movq %rax, 0x28(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x40(%rdi)
andq $0x0, 0x50(%rdi)
movq %rdi, 0x18(%rsp)
movq %r15, 0x20(%rsp)
callq 0x51c0
movq %r15, %rdi
movq %r14, %rsi
callq 0x51c0
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
movq (%rbx), %rdi
callq 0x5520
testq %rax, %rax
je 0x7887
movq %rax, %r14
leaq 0x38(%rsp), %r15
movq 0x169d2(%rip), %r12     # 0x1dfb0
movq %rsp, %r13
movq %r14, %rdi
callq 0x5540
testq %rax, %rax
je 0x7634
cmpb $0x2e, 0x13(%rax)
je 0x75e1
cmpb $0x8, 0x12(%rax)
jne 0x75e1
addq $0x13, %rax
movq %r15, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0xb87b
movq %r12, %rdi
movq %r15, %rsi
callq 0x52f0
movq %rax, %rdi
callq 0x5180
movq %r13, %rdi
movq %r15, %rsi
callq 0xb8b4
movq %r15, %rdi
callq 0x5160
jmp 0x75e1
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
cmpq %rcx, %rax
je 0x7887
subq %rax, %rcx
sarq $0x5, %rcx
movl $0x40000000, %eax       # imm = 0x40000000
xorl %edx, %edx
divq %rcx
andl $0x7ffffffe, %eax       # imm = 0x7FFFFFFE
movl %eax, 0x16d66(%rip)     # 0x1e3c8
pushq $-0x1
popq %rcx
testl %eax, %eax
je 0x7676
shrl %eax
movl %eax, 0x16d57(%rip)     # 0x1e3c8
incq %rcx
jmp 0x7665
movq %rcx, %xmm1
punpckldq 0xb9cd(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0xb9d5(%rip), %xmm1     # 0x13060
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
callq 0x5300
cvttsd2si %xmm0, %rax
movq %rax, %rcx
subsd 0xc314(%rip), %xmm0     # 0x139c0
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, 0x16cfb(%rip)     # 0x1e3c8
movq 0x168dc(%rip), %rdi     # 0x1dfb0
leaq 0xc373(%rip), %rsi      # 0x13a4e
callq 0x5380
movq %rax, %rdi
movl 0x16cdf(%rip), %eax     # 0x1e3c8
movl $0x100000, %ecx         # imm = 0x100000
cltd
idivl %ecx
movl %eax, %esi
callq 0x55f0
leaq 0xc35f(%rip), %rsi      # 0x13a5e
movq %rax, %rdi
callq 0x5380
movq %rax, %rdi
callq 0x5180
xorl %r12d, %r12d
leaq 0x38(%rsp), %rbp
movq %rsp, %rbx
movq 0x1688f(%rip), %r15     # 0x1dfb0
leaq 0x78(%rsp), %r13
movl %r12d, %r12d
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x5, %rax
cmpq %r12, %rax
jbe 0x7823
movq %rbp, %rdi
leaq 0xc318(%rip), %rsi      # 0x13a61
leaq 0x30(%rsp), %rdx
callq 0x5130
movq %rbx, %rdi
movq %r12, %rsi
callq 0xb8e0
movq %rbp, %rdi
movq %rax, %rsi
callq 0x5670
movq %r15, %rdi
callq 0x5180
movq %r15, %rdi
leaq 0xc2ee(%rip), %rsi      # 0x13a69
callq 0x5380
movq %rax, %r14
movq %rbx, %rdi
movq %r12, %rsi
callq 0xb8e0
movq %r14, %rdi
movq %rax, %rsi
callq 0x52f0
movq %rax, %rdi
leaq 0xbd96(%rip), %rsi      # 0x13539
callq 0x5380
movq %rax, %rdi
callq 0x5180
movq 0x38(%rsp), %rdi
callq 0x52c0
movq %r15, %rdi
callq 0x5180
movl $0x2b8, %edi            # imm = 0x2B8
callq 0x5390
movq %rax, %r14
movq %rbx, %rdi
movq %r12, %rsi
callq 0xb8e0
movq %r13, %rdi
movq %rax, %rsi
callq 0x54b0
movslq 0x16bdc(%rip), %rdx     # 0x1e3c8
movq %r14, %rdi
movq %r13, %rsi
callq 0xf4c8
movq %r13, %rdi
callq 0x5160
movq %r14, 0x30(%rsp)
movq 0x28(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0xb902
movq %rbp, %rdi
callq 0x5160
incl %r12d
jmp 0x7726
movl $0x2a0, %edi            # imm = 0x2A0
callq 0x5390
movq %rax, %r14
leaq 0xc1d2(%rip), %rdx      # 0x13a09
leaq 0x58(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0xb87b
movslq 0x16b7b(%rip), %rdx     # 0x1e3c8
leaq 0x58(%rsp), %rsi
movq %r14, %rdi
callq 0x10806
movq 0x18(%rsp), %rax
movq %r14, 0x58(%rax)
leaq 0x58(%rsp), %rdi
callq 0x5160
movq %rsp, %rdi
callq 0xb920
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x16762(%rip), %rdi     # 0x1dff0
leaq 0xc186(%rip), %rsi      # 0x13a1b
callq 0x5380
movq %rax, %rdi
callq 0x5180
pushq $0x1
popq %rdi
callq 0x5420
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x5160
jmp 0x78bc
movq %rax, %rbx
movq %r14, %rdi
callq 0x5360
jmp 0x7905
jmp 0x78e4
movq %rax, %rbx
jmp 0x790d
jmp 0x78e4
jmp 0x78e4
jmp 0x78f8
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x5160
jmp 0x78ec
jmp 0x78e4
movq %rax, %rbx
jmp 0x7905
movq %rax, %rbx
movq %r14, %rdi
callq 0x5360
jmp 0x78fb
jmp 0x78f8
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x5160
movq %rsp, %rdi
callq 0xb920
movq 0x28(%rsp), %rdi
callq 0x714e
movq 0x20(%rsp), %rdi
callq 0x5160
movq 0x18(%rsp), %rdi
callq 0x5160
movq %rbx, %rdi
callq 0x5610
nop