// File: /workspace/github_cpp_makefile_star2_O2/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 $0x1e8, %rsp            # imm = 0x1E8
xorl %edi, %edi
callq 0x20a0
pushq $0x1
popq %rdi
callq 0x2190
movq %rax, 0x28(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
leaq 0x108(%rsp), %rdi
pushq $0xc
popq %rdx
pushq $0x5
popq %rcx
movq %rax, %rsi
callq 0x2ede
leaq 0x10(%rsp), %rbx
leaq 0x108(%rsp), %rsi
movq %rbx, %rdi
callq 0x4c36
leaq 0x110(%rsp), %rdi
callq 0x3c12
movq %rbx, %rdi
callq 0x2f54
movq 0x10(%rsp), %rbx
cmpl $0x0, 0x4(%rbx)
js 0x247a
cmpl $0x1, 0x10(%rbx)
jne 0x2495
movq 0x9b67(%rip), %rdi      # 0xbfe8
leaq 0x4be2(%rip), %rsi      # 0x706a
callq 0x2180
pushq $0x1
popq %rbx
jmp 0x2702
pushq $0x2c
popq %rdi
callq 0x2030
xorps %xmm0, %xmm0
movups %xmm0, 0x1c(%rax)
movups %xmm0, 0x10(%rax)
andl $0x0, 0x4(%rsp)
movups %xmm0, (%rax)
movq %rax, 0x8(%rsp)
pushq $0x1
popq %rcx
xorl %edx, %edx
movl 0x4(%rbx), %esi
addl $0x2, %esi
cmpl %esi, %ecx
jge 0x24de
cmpl $0x1, %ecx
je 0x24da
cmpl 0x10(%rbx), %ecx
je 0x24da
movslq %edx, %rdx
movl %ecx, (%rax,%rdx,4)
incl %edx
movl %edx, 0x4(%rsp)
incl %ecx
jmp 0x24ba
leaq 0x110(%rsp), %rbx
movq %rbx, %rdi
callq 0x3c8e
movl $0x4, 0x108(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x2fec
leaq 0xfc(%rsp), %rdi
andl $0x0, (%rdi)
movl $0x5, 0x8(%rdi)
movq 0x10(%rsp), %rax
movl 0xc(%rax), %eax
incl %eax
movl %eax, 0x4(%rdi)
leaq 0x8(%rsp), %rsi
leaq 0x4(%rsp), %rdx
leaq 0xa8(%rsp), %rcx
callq 0x2d00
leaq 0xa8(%rsp), %r12
movq %r12, %rdi
callq 0x4882
xorl %esi, %esi
pushq $0x5
popq %rcx
xorl %edx, %edx
divq %rcx
xorps %xmm0, %xmm0
leaq 0x1c0(%rsp), %r13
movaps %xmm0, 0x10(%r13)
movaps %xmm0, (%r13)
andq $0x0, 0x20(%r13)
testl %eax, %eax
cmovlel %esi, %eax
movq %rax, 0x50(%rsp)
leaq 0x90(%rsp), %rbx
leaq 0x30(%rsp), %r14
movl $0x200, %ebp            # imm = 0x200
cmpl 0x50(%rsp), %esi
je 0x26bc
movq %rsi, 0x58(%rsp)
movq %r13, %r15
movaps %xmm0, 0x90(%rsp)
andq $0x0, 0xa0(%rsp)
pushq $0x5
popq %r13
subl $0x1, %r13d
jb 0x25f0
movq 0xd8(%rsp), %rsi
cmpq 0xe0(%rsp), %rsi
jne 0x25da
movq 0xf0(%rsp), %rax
movq -0x8(%rax), %rsi
addq %rbp, %rsi
addq $-0x4, %rsi
movq %rbx, %rdi
callq 0x3008
movq %r12, %rdi
callq 0x4a86
jmp 0x25b3
leaq 0x10(%rsp), %rax
movq %rax, 0x60(%rsp)
movl $0x1, 0x68(%rsp)
leaq 0x70(%rsp), %rdi
movq %rbx, %rsi
callq 0x3074
movq %r15, %r13
movq %r15, 0x88(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
pushq $0x30
popq %rdi
callq 0x2190
movl 0x68(%rsp), %ecx
movl %ecx, 0x8(%rax)
movq 0x60(%rsp), %rcx
movq %rcx, (%rax)
movups 0x70(%rsp), %xmm0
movups %xmm0, 0x10(%rax)
movq 0x80(%rsp), %rcx
movq %rcx, 0x20(%rax)
leaq 0x70(%rsp), %r15
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
andq $0x0, 0x10(%r15)
movq %r13, 0x28(%rax)
movq %rax, 0x30(%rsp)
leaq 0x17c(%rip), %rax       # 0x27f0
movq %rax, 0x48(%rsp)
leaq 0x204(%rip), %rax       # 0x2884
movq %rax, 0x40(%rsp)
leaq 0x108(%rsp), %rdi
movq %r14, %rsi
callq 0x3026
movq %r14, %rdi
callq 0x30c2
movq %r15, %rdi
callq 0x4c64
movq %rbx, %rdi
callq 0x4c64
movq 0x58(%rsp), %rsi
incl %esi
xorps %xmm0, %xmm0
jmp 0x258c
leaq 0x108(%rsp), %rdi
callq 0x30de
movq 0x98f8(%rip), %rdi      # 0xbfc8
leaq 0x49ed(%rip), %rsi      # 0x70c4
callq 0x2180
leaq 0xa8(%rsp), %rdi
callq 0x461c
leaq 0x110(%rsp), %rdi
callq 0x4668
leaq 0x8(%rsp), %rdi
callq 0x3192
xorl %ebx, %ebx
leaq 0x18(%rsp), %rdi
callq 0x3c12
leaq 0x28(%rsp), %rdi
callq 0x31ac
movl %ebx, %eax
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x273e
movq %rax, %r15
jmp 0x27c1
movq %rax, %r15
jmp 0x27c9
jmp 0x277a
movq %rax, %r15
jmp 0x27ac
movq %rax, %r15
cmpl $0x1, %edx
jne 0x27d3
movq %r15, %rdi
callq 0x2090
movq 0x988a(%rip), %rdi      # 0xbfe8
movq %rax, %rsi
callq 0x2180
callq 0x2250
jmp 0x248d
movq %rax, %r15
callq 0x2250
jmp 0x27d3
movq %rax, %r15
jmp 0x27d3
jmp 0x279c
jmp 0x2783
movq %rax, %r15
leaq 0x30(%rsp), %rdi
callq 0x30c2
leaq 0x70(%rsp), %rdi
callq 0x4c64
jmp 0x279f
movq %rax, %r15
leaq 0x90(%rsp), %rdi
callq 0x4c64
leaq 0x110(%rsp), %rbx
leaq 0xa8(%rsp), %rdi
callq 0x461c
movq %rbx, %rdi
callq 0x4668
leaq 0x8(%rsp), %rdi
callq 0x3192
leaq 0x18(%rsp), %rdi
callq 0x3c12
leaq 0x28(%rsp), %rdi
callq 0x31ac
movq %r15, %rdi
callq 0x22a0
nop