// File: mrGoncharuk[P]foldermonitor/src/MigrationManager.cpp
// Function: MigrationManager::migrateThreaded(std::mutex&, std::__cxx11::list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>&, std::atomic<bool>&)
void	MigrationManager::migrateThreaded(std::mutex &list_mutex, std::list<std::string> &filenames, std::atomic<bool> &isRunning)
{
	std::string currFileName;
	std::thread migrateThreads[threadAmount];
	std::atomic<bool> isThreadFinished[threadAmount];
	unsigned int i = 0;

	while (i < threadAmount && isRunning)
	{
		std::lock_guard<std::mutex> lock(list_mutex);
		if (!filenames.empty())
		{
			isThreadFinished[i] = false;
			currFileName = filenames.front();
			filenames.pop_front();
			migrateThreads[i] = std::thread(
				[currFileName](IDB *idb, std::atomic<bool> &isReady)
				{
					std::string query = FileReader::extractData(currFileName);
					idb->executeQuery(query);
					isReady = true;
				},
				this->dbwriter, std::ref(isThreadFinished[i]));
			i++;
		}
	}
	while (isRunning)
	{
		for (i = 0; i < threadAmount; i++)
		{
			if (isThreadFinished[i])
			{
				std::lock_guard<std::mutex> lock(list_mutex);
				if (!filenames.empty())
				{
					migrateThreads[i].join();
					isThreadFinished[i] = false;
					currFileName = filenames.front();
					filenames.pop_front();
					migrateThreads[i] = std::thread(
						[currFileName](IDB *idb, std::atomic<bool> &isReady)
						{
							std::string query = FileReader::extractData(currFileName);
							idb->executeQuery(query);
							isReady = true;
						},
						this->dbwriter, std::ref(isThreadFinished[i]));
					i++;
				}
			}
		}
	}
	syslog(LOG_NOTICE, "MM> Waiting for end of threads task.");
	std::this_thread::sleep_for(std::chrono::seconds(2));
	for (i = 0; i < threadAmount; i++)
	{
		if (isThreadFinished[i])
			migrateThreads[i].join();
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x108(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x32c0
movq -0x108(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x100(%rbp)
movq %rsp, %rcx
movq %rcx, -0x48(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xf8(%rbp)
movq %rcx, %rsp
movq %rax, -0x50(%rbp)
cmpq $0x0, %rax
je 0x65d4
movq -0xf8(%rbp), %rax
movq -0x100(%rbp), %rdx
shlq $0x3, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0x118(%rbp)
movq %rax, -0x110(%rbp)
movq -0x110(%rbp), %rdi
movq %rdi, -0x120(%rbp)
callq 0x7770
movq -0x120(%rbp), %rax
movq -0x118(%rbp), %rcx
addq $0x8, %rax
cmpq %rcx, %rax
movq %rax, -0x110(%rbp)
jne 0x65a3
movq -0x108(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x128(%rbp)
movq %rcx, %rsp
movq %rax, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movq -0x108(%rbp), %rdx
movl -0x5c(%rbp), %ecx
xorl %eax, %eax
cmpl 0x8(%rdx), %ecx
movb %al, -0x129(%rbp)
jae 0x662a
movq -0x20(%rbp), %rdi
callq 0x5ae0
movb %al, -0x129(%rbp)
movb -0x129(%rbp), %al
testb $0x1, %al
jne 0x6639
jmp 0x6767
movq -0x10(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x55c0
jmp 0x6648
movq -0x18(%rbp), %rdi
callq 0x76f0
testb $0x1, %al
jne 0x674b
movq -0x128(%rbp), %rdi
movl -0x5c(%rbp), %eax
addq %rax, %rdi
xorl %esi, %esi
callq 0x3d00
movq -0x18(%rbp), %rdi
callq 0x7710
movq %rax, %rsi
leaq -0x40(%rbp), %rdi
callq 0x3030
jmp 0x6684
movq -0x18(%rbp), %rdi
callq 0x7740
leaq -0xa0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x3100
jmp 0x669f
movq -0x128(%rbp), %rdi
movl -0x5c(%rbp), %eax
addq %rax, %rdi
callq 0x3dc0
movq -0x108(%rbp), %rdx
movq %rax, -0xa8(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
leaq -0xa8(%rbp), %rcx
callq 0x6a70
jmp 0x66d8
movq -0xf8(%rbp), %rdi
movl -0x5c(%rbp), %eax
shlq $0x3, %rax
addq %rax, %rdi
leaq -0x80(%rbp), %rsi
callq 0x7790
leaq -0x80(%rbp), %rdi
callq 0x3ec0
leaq -0xa0(%rbp), %rdi
callq 0x6b30
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x674b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x6a04
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x6759
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x6b30
jmp 0x6759
leaq -0x68(%rbp), %rdi
callq 0x55f0
jmp 0x6604
leaq -0x68(%rbp), %rdi
callq 0x55f0
jmp 0x6a04
jmp 0x6769
movq -0x20(%rbp), %rdi
callq 0x5ae0
testb $0x1, %al
jne 0x677b
jmp 0x6906
movl $0x0, -0x5c(%rbp)
movq -0x108(%rbp), %rcx
movl -0x5c(%rbp), %eax
cmpl 0x8(%rcx), %eax
jae 0x6901
movq -0x128(%rbp), %rdi
movl -0x5c(%rbp), %eax
addq %rax, %rdi
callq 0x5ae0
testb $0x1, %al
jne 0x67b0
jmp 0x68f1
movq -0x10(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x55c0
jmp 0x67c2
movq -0x18(%rbp), %rdi
callq 0x76f0
testb $0x1, %al
jne 0x68d2
movq -0xf8(%rbp), %rax
movl -0x5c(%rbp), %ecx
leaq (%rax,%rcx,8), %rdi
callq 0x3340
jmp 0x67e8
movq -0x128(%rbp), %rdi
movl -0x5c(%rbp), %eax
addq %rax, %rdi
xorl %esi, %esi
callq 0x3d00
movq -0x18(%rbp), %rdi
callq 0x7710
movq %rax, %rsi
leaq -0x40(%rbp), %rdi
callq 0x3030
jmp 0x6813
movq -0x18(%rbp), %rdi
callq 0x7740
leaq -0xd8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x3100
jmp 0x682e
movq -0x128(%rbp), %rdi
movl -0x5c(%rbp), %eax
addq %rax, %rdi
callq 0x3dc0
movq -0x108(%rbp), %rdx
movq %rax, -0xe0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
leaq -0xe0(%rbp), %rcx
callq 0x6b50
jmp 0x686a
movq -0xf8(%rbp), %rdi
movl -0x5c(%rbp), %eax
shlq $0x3, %rax
addq %rax, %rdi
leaq -0xb8(%rbp), %rsi
callq 0x7790
leaq -0xb8(%rbp), %rdi
callq 0x3ec0
leaq -0xd8(%rbp), %rdi
callq 0x6c10
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x68d2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x68e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x6c10
jmp 0x68e0
leaq -0xb0(%rbp), %rdi
callq 0x55f0
jmp 0x68f1
leaq -0xb0(%rbp), %rdi
callq 0x55f0
jmp 0x6a04
jmp 0x68f3
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x6782
jmp 0x6769
leaq 0x1d78(%rip), %rsi      # 0x8685
xorl %eax, %eax
movl $0x5, %edi
callq 0x3060
jmp 0x691b
movl $0x2, -0xec(%rbp)
leaq -0xe8(%rbp), %rdi
leaq -0xec(%rbp), %rsi
callq 0x7880
jmp 0x693a
leaq -0xe8(%rbp), %rdi
callq 0x77d0
jmp 0x6948
movl $0x0, -0x5c(%rbp)
movq -0x108(%rbp), %rcx
movl -0x5c(%rbp), %eax
cmpl 0x8(%rcx), %eax
jae 0x699a
movq -0x128(%rbp), %rdi
movl -0x5c(%rbp), %eax
addq %rax, %rdi
callq 0x5ae0
testb $0x1, %al
jne 0x6976
jmp 0x698d
movq -0xf8(%rbp), %rax
movl -0x5c(%rbp), %ecx
leaq (%rax,%rcx,8), %rdi
callq 0x3340
jmp 0x698b
jmp 0x698d
jmp 0x698f
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x694f
movq -0xf8(%rbp), %rcx
movq -0x100(%rbp), %rdx
shlq $0x3, %rdx
movq %rcx, %rax
addq %rdx, %rax
cmpq %rax, %rcx
movq %rax, -0x138(%rbp)
je 0x69ef
movq -0x138(%rbp), %rdi
addq $-0x8, %rdi
movq %rdi, -0x140(%rbp)
callq 0x3ec0
movq -0xf8(%rbp), %rcx
movq -0x140(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x138(%rbp)
jne 0x69be
movq -0x48(%rbp), %rax
movq %rax, %rsp
leaq -0x40(%rbp), %rdi
callq 0x3170
movq %rbp, %rsp
popq %rbp
retq
movq -0xf8(%rbp), %rcx
movq -0x100(%rbp), %rdx
shlq $0x3, %rdx
movq %rcx, %rax
addq %rdx, %rax
cmpq %rax, %rcx
movq %rax, -0x148(%rbp)
je 0x6a59
movq -0x148(%rbp), %rdi
addq $-0x8, %rdi
movq %rdi, -0x150(%rbp)
callq 0x3ec0
movq -0xf8(%rbp), %rcx
movq -0x150(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x148(%rbp)
jne 0x6a28
leaq -0x40(%rbp), %rdi
callq 0x3170
movq -0x70(%rbp), %rdi
callq 0x3360
nopl (%rax,%rax)