// File: /workspace/github_cpp_makefile_star2_O3/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 0x33c4
movaps 0x140(%rsp), %xmm0
leaq 0x20(%rsp), %rdi
movaps %xmm0, (%rdi)
callq 0x346e
movq 0x20(%rsp), %rbx
cmpl $0x0, 0x4(%rbx)
js 0x24b9
cmpl $0x1, 0x10(%rbx)
jne 0x24db
movl $0x1, %ebx
movq 0x8b23(%rip), %rdi      # 0xafe8
leaq 0x4bb2(%rip), %rsi      # 0x707e
movl $0x59, %edx
callq 0x21a0
jmp 0x28a6
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)
movl 0x4(%rbx), %edx
testl %edx, %edx
js 0x2531
xorl %ecx, %ecx
xorl %esi, %esi
movl %ecx, %edi
leal 0x1(%rdi), %ecx
testl %edi, %edi
je 0x252d
cmpl 0x10(%rbx), %ecx
je 0x252d
movslq %esi, %rsi
movl %ecx, (%rax,%rsi,4)
incl %esi
movl %esi, 0x14(%rsp)
movl 0x4(%rbx), %edx
cmpl %edx, %ecx
jle 0x2510
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 0x4058
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 0x4dc6
leaq 0x134(%rsp), %rdi
movl $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 0x30f6
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 0x2854
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)
movq 0xa0(%rsp), %rax
movq 0xa8(%rsp), %rdi
movl $0x5, %r12d
movq %rax, %rdx
cmpq %rdi, %rax
jne 0x26c9
movq 0xb8(%rsp), %rcx
movq -0x8(%rcx), %rdx
addq %r14, %rdx
addq $-0x4, %rdx
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x26e8
movl (%rdx), %ecx
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x38(%rsp)
jmp 0x2700
movq %rbp, %rdi
callq 0x4f12
movq 0xa0(%rsp), %rax
movq 0xa8(%rsp), %rdi
cmpq %rdi, %rax
je 0x270b
addq $-0x4, %rax
jmp 0x2746
callq 0x2160
movq 0xb8(%rsp), %rax
leaq -0x8(%rax), %rcx
movq %rcx, 0xb8(%rsp)
movq -0x8(%rax), %rdi
movq %rdi, 0xa8(%rsp)
leaq 0x200(%rdi), %rax
movq %rax, 0xb0(%rsp)
leaq 0x1fc(%rdi), %rax
movq %rax, 0xa0(%rsp)
decl %r12d
jne 0x26b2
leaq 0x20(%rsp), %rax
movq %rax, 0xd0(%rsp)
movl $0x1, 0xd8(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x35b4
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 0x1f2(%rip), %rax       # 0x29d8
movq %rax, 0x68(%rsp)
leaq 0x28e(%rip), %rax       # 0x2a80
movq %rax, 0x60(%rsp)
leaq 0x140(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x353e
movq 0x60(%rsp), %rax
testq %rax, %rax
je 0x2822
leaq 0x50(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0xe0(%rsp), %rdi
testq %rdi, %rdi
je 0x2834
callq 0x2160
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x2843
callq 0x2160
incl %r15d
cmpl 0xc8(%rsp), %r15d
jne 0x268b
leaq 0x140(%rsp), %rdi
callq 0x3634
movq 0x8760(%rip), %rdi      # 0xafc8
leaq 0x4869(%rip), %rsi      # 0x70d8
movl $0x4, %edx
callq 0x21a0
leaq 0x70(%rsp), %rdi
callq 0x494c
leaq 0x148(%rsp), %rdi
callq 0x4996
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
movq 0x8(%rsp), %r14
je 0x28a4
callq 0x21c0
xorl %ebx, %ebx
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x28b5
callq 0x3fe8
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 0x28e5
movq %rax, %rbx
jmp 0x299a
movq %rax, %rbx
jmp 0x29a7
jmp 0x2938
movq %rax, %rbx
jmp 0x2990
movq %r14, 0x8(%rsp)
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x29b6
movq %rbx, %rdi
callq 0x2080
movq 0x86db(%rip), %rdi      # 0xafe8
movq %rax, %rsi
callq 0x2170
callq 0x2240
movl $0x1, %ebx
movq 0x8(%rsp), %r14
jmp 0x28a6
movq %rax, %rbx
callq 0x2240
jmp 0x29b6
movq %r14, 0x8(%rsp)
movq %rax, %rbx
jmp 0x29b6
jmp 0x2976
jmp 0x297e
movq %rax, %rbx
jmp 0x2962
movq %rax, %rbx
movq 0x60(%rsp), %rax
testq %rax, %rax
je 0x2962
leaq 0x50(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0xe0(%rsp), %rdi
testq %rdi, %rdi
je 0x2981
callq 0x2160
jmp 0x2981
movq %rax, %rdi
callq 0x3a3e
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x2990
callq 0x2160
leaq 0x70(%rsp), %rdi
callq 0x494c
leaq 0x148(%rsp), %rdi
callq 0x4996
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x29b6
callq 0x21c0
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x29c5
callq 0x3fe8
movq 0x8(%rsp), %rdi
callq 0x2160
movq %rbx, %rdi
callq 0x2290
nop