// File: /workspace/github_cpp_makefile_star2_O1/It4innovations[P]Handicap/src/main.cpp
// Function: main
int main() {

	std::cout.sync_with_stdio(false);
	std::cout.tie();

	if (REGULAR_NUMBER > 5 || REGULAR_NUMBER < 2) {
		std::cerr << "Regular number must be in <2;4>\n. It is equal to " << REGULAR_NUMBER << '\n';
		return 1;
	}


	if (THREAD_COUNT < 1) {
		std::cerr << "CONST THREAD_COUT must be greater than 0. Please change them.\n";
		return 1;
	}

	std::unique_ptr<ExtendedHandicapGraphFactory> handicapGraphFactory(new ExtendedHandicapGraphFactory());
	std::shared_ptr<ExtendedHandicapGraph> graph;

	int vertexCount = VERTICES_COUNT;
	//std::cin >> vertexCount;

	try {
		// Create extended handicap graphs
		graph = handicapGraphFactory->create(vertexCount, REGULAR_NUMBER);
	} catch (const char * msg) {
		std::cerr << msg;
		return 1;
	}
	GraphInfoPrinter printer;
	printer.print(graph);

	if (STARTING_LABEL < 1 || STARTING_LABEL > graph->getVerticesCount() + 1 || STARTING_LABEL == graph->getMissingLabel()) {
		std::cerr << "CONST STARTING_LABEL is not any label in labeling.\nPlease select another STARTING LABEL.\n";
		return 1;
	}

	std::unique_ptr<int[]> arr(new int[vertexCount - 1]());
	int index = 0;
	for (int i = 1; i < graph->getVerticesCount() + 2; i++) {
		if (i == STARTING_LABEL || i == graph->getMissingLabel()) continue;
		arr[index] = i;
		++index;
	}

	//number of threads running parallel
	ThreadPool pool(THREAD_COUNT);
	std::stack<int> stack;
	kSumProblemSolver<int> solver;
	solver.setK(REGULAR_NUMBER);
	solver.setSearchNumber(STARTING_LABEL + graph->getMagicConstant());
	solver.solve(arr, index, stack);
	const auto countOfResult = (int) (stack.size() / REGULAR_NUMBER);
	std::mutex mut;

	for (int i = 0; i < countOfResult; ++i) {
		std::vector<int> vec;
		for (int j = 0; j < REGULAR_NUMBER; ++j) {
			vec.push_back(stack.top());
			stack.pop();
		}
		int startingLabel = STARTING_LABEL;
		pool.addJob([&graph, startingLabel, vec, &mut]() {
			std::unique_ptr<GraphLabeling> graphLabeling(new GraphLabeling(graph));
			graphLabeling->prepareSolve(startingLabel, vec, mut);
		});
	}
	pool.execute();

	std::cout << "END\n";
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
xorl %edi, %edi
callq 0x2090
movl $0x1, %edi
callq 0x2180
movq %rax, %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x140(%rsp), %rdi
movq %rax, %rsi
movl $0xc, %edx
movl $0x5, %ecx
callq 0x3404
movaps 0x140(%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, 0x140(%rsp)
movq 0x28(%rsp), %rdi
movaps %xmm0, 0x20(%rsp)
testq %rdi, %rdi
je 0x24ba
callq 0x4040
movq 0x148(%rsp), %rdi
testq %rdi, %rdi
je 0x24cc
callq 0x4040
leaq 0x20(%rsp), %rdi
callq 0x34b8
movq 0x20(%rsp), %rbx
cmpl $0x0, 0x4(%rbx)
js 0x24e7
cmpl $0x1, 0x10(%rbx)
jne 0x2509
movl $0x1, %ebx
movq 0x8af5(%rip), %rdi      # 0xafe8
leaq 0x4b70(%rip), %rsi      # 0x706a
movl $0x59, %edx
callq 0x21a0
jmp 0x28cb
movl $0x2c, %edi
movq %r14, 0x8(%rsp)
callq 0x2030
xorps %xmm0, %xmm0
movups %xmm0, 0x1c(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq %rax, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
cmpl $0x0, 0x4(%rbx)
js 0x255c
xorl %edx, %edx
xorl %ecx, %ecx
movl %edx, %esi
leal 0x1(%rsi), %edx
testl %esi, %esi
je 0x2557
cmpl 0x10(%rbx), %edx
je 0x2557
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
incl %ecx
movl %ecx, 0x14(%rsp)
cmpl 0x4(%rbx), %edx
jle 0x253d
leaq 0x178(%rsp), %rdi
movb $0x1, -0x30(%rdi)
movups %xmm0, -0x28(%rdi)
movups %xmm0, -0x18(%rdi)
movups %xmm0, -0x8(%rdi)
movups %xmm0, 0x8(%rdi)
movups %xmm0, 0x18(%rdi)
movups %xmm0, 0x28(%rdi)
movups %xmm0, 0x38(%rdi)
movq $0x0, 0x48(%rdi)
xorl %esi, %esi
callq 0x40b0
leaq 0x148(%rsp), %rbx
leaq 0x1c8(%rsp), %rdi
callq 0x21e0
movl $0x4, -0x8(%rbx)
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %rdi
movaps %xmm0, 0x40(%rdi)
movaps %xmm0, 0x30(%rdi)
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
xorl %esi, %esi
callq 0x4e1e
leaq 0x134(%rsp), %rdi
movq $0x0, (%rdi)
movl $0x5, 0x8(%rdi)
movq 0x20(%rsp), %rax
movl 0xc(%rax), %eax
incl %eax
movl %eax, 0x4(%rdi)
leaq 0x18(%rsp), %rsi
leaq 0x14(%rsp), %rdx
leaq 0x70(%rsp), %rcx
callq 0x3142
movq 0xb8(%rsp), %rax
movq %rax, %rcx
subq 0x98(%rsp), %rcx
shrq $0x3, %rcx
cmpq $0x1, %rax
adcq $-0x1, %rcx
shlq $0x7, %rcx
movq 0x90(%rsp), %rax
movq 0xa0(%rsp), %rdx
subq 0xa8(%rsp), %rdx
subq 0x80(%rsp), %rax
sarq $0x2, %rdx
sarq $0x2, %rax
addq %rdx, %rax
addq %rcx, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
mulq %rcx
shrq $0x2, %rdx
xorps %xmm0, %xmm0
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
movq $0x0, 0x120(%rsp)
movq %rdx, 0xc8(%rsp)
testl %edx, %edx
jle 0x2879
xorl %r15d, %r15d
leaq 0xe0(%rsp), %r13
movl $0x200, %r14d           # imm = 0x200
leaq 0x30(%rsp), %rbp
leaq 0x100(%rsp), %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movl $0x5, %r12d
movq 0xa0(%rsp), %rdx
cmpq 0xa8(%rsp), %rdx
jne 0x26ef
movq 0xb8(%rsp), %rax
movq -0x8(%rax), %rdx
addq %r14, %rdx
addq $-0x4, %rdx
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x270e
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rsp)
jmp 0x2716
movq %rbp, %rdi
callq 0x4f6a
movq 0xa0(%rsp), %rax
movq 0xa8(%rsp), %rdi
cmpq %rdi, %rax
je 0x2731
addq $-0x4, %rax
jmp 0x276b
callq 0x2160
movq 0xb8(%rsp), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0xb8(%rsp)
movq -0x8(%rax), %rax
movq %rax, 0xa8(%rsp)
leaq 0x200(%rax), %rcx
movq %rcx, 0xb0(%rsp)
addq $0x1fc, %rax            # imm = 0x1FC
movq %rax, 0xa0(%rsp)
decl %r12d
jne 0x26ce
leaq 0x20(%rsp), %rax
movq %rax, 0xd0(%rsp)
movl $0x1, 0xd8(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x35fe
movq %rbx, 0xf8(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x60(%rsp)
movl $0x30, %edi
callq 0x2180
movl 0xd8(%rsp), %ecx
movl %ecx, 0x8(%rax)
movq 0xd0(%rsp), %rcx
movq %rcx, (%rax)
movups 0xe0(%rsp), %xmm0
movups %xmm0, 0x10(%rax)
movq 0xf0(%rsp), %rcx
movq %rcx, 0x20(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%r13)
movq $0x0, 0x10(%r13)
movq %rbx, 0x28(%rax)
movq %rax, 0x50(%rsp)
leaq 0x1fd(%rip), %rax       # 0x2a08
movq %rax, 0x68(%rsp)
leaq 0x2a5(%rip), %rax       # 0x2abc
movq %rax, 0x60(%rsp)
leaq 0x140(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x3588
movq 0x60(%rsp), %rax
testq %rax, %rax
je 0x2847
leaq 0x50(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0xe0(%rsp), %rdi
testq %rdi, %rdi
je 0x2859
callq 0x2160
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x2868
callq 0x2160
incl %r15d
cmpl 0xc8(%rsp), %r15d
jne 0x26b7
leaq 0x140(%rsp), %rdi
callq 0x367e
movq 0x873b(%rip), %rdi      # 0xafc8
leaq 0x4830(%rip), %rsi      # 0x70c4
movl $0x4, %edx
callq 0x21a0
leaq 0x70(%rsp), %rdi
callq 0x49b6
leaq 0x148(%rsp), %rdi
callq 0x49fa
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
movq 0x8(%rsp), %r14
je 0x28c9
callq 0x21c0
xorl %ebx, %ebx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x28da
callq 0x4040
movq %r14, %rdi
callq 0x2160
movl %ebx, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x290a
movq %rax, %rbx
jmp 0x29c2
movq %rax, %rbx
jmp 0x29cf
jmp 0x295d
movq %rax, %rbx
jmp 0x29b8
movq %r14, 0x8(%rsp)
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x29e7
movq %rbx, %rdi
callq 0x2080
movq 0x86b6(%rip), %rdi      # 0xafe8
movq %rax, %rsi
callq 0x2170
callq 0x2240
movl $0x1, %ebx
movq 0x8(%rsp), %r14
jmp 0x28cb
movq %rax, %rbx
callq 0x2240
jmp 0x29e7
movq %r14, 0x8(%rsp)
movq %rax, %rbx
jmp 0x29e7
jmp 0x299e
jmp 0x29a6
movq %rax, %rbx
jmp 0x298a
movq %rax, %rbx
movq 0x60(%rsp), %rax
testq %rax, %rax
je 0x298a
leaq 0x50(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0xe0(%rsp), %rdi
testq %rdi, %rdi
je 0x29a9
callq 0x2160
jmp 0x29a9
movq %rax, %rdi
callq 0x3a8a
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x29b8
callq 0x2160
leaq 0x70(%rsp), %rdi
callq 0x49b6
leaq 0x148(%rsp), %rdi
callq 0x49fa
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x29de
callq 0x21c0
movq $0x0, 0x18(%rsp)
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x29f6
callq 0x4040
movq 0x8(%rsp), %rdi
callq 0x2160
movq %rbx, %rdi
callq 0x2290