// File: /workspace/github_cpp_makefile_star2_O1/Laakeri[P]Triangulator/src/bayes.cpp
// Function: Bayes::SolveGraph::solveGraph(Graph const&, long, long, std::vector<long, std::allocator<long>>)
pair<int64_t, vector<pair<int, int> > > solveGraph(const Graph& G, int64_t lowerBound, int64_t upperBound, vector<int64_t> tableSizes) {
	assert(lowerBound >= 0);
	assert(lowerBound <= upperBound);
	assert((int)tableSizes.size() == G.n);
	for (int i = 0; i < G.n; i++) {
		for (int nx : G.g[i]) {
			lowerBound = max(lowerBound, tableSizes[i]*tableSizes[nx]);
		}
	}
	
	auto fill = MCS::MCS_MP(G);
	
	Graph gFill = G;
	gFill.addEdges(fill.F);
	
	int64_t fillWidth = MCS::getBayesWidth(gFill, tableSizes);
	
	assert(fillWidth >= lowerBound);
	if (fillWidth <= lowerBound) return {fillWidth, fill.F};
	
	if (fill.F.size() <= 0) {
		if (fillWidth > upperBound) return {-1, FAILSOLUTION};
		return {fillWidth, fill.F};
	}
	
	vector<Graph> atoms = MCS::getAtoms(G, gFill, fill).S;
	auto cmp = [](const Graph& a, const Graph& b) {
		return a.n > b.n;
	};
	sort(atoms.begin(), atoms.end(), cmp);
	pair<int64_t, vector<pair<int, int> > > sol = {0, {}};
	for (int i = 0; i < (int)atoms.size(); i++) {
		pair<int64_t, vector<pair<int, int> > > tSol;
		if (i+1 < (int)atoms.size()) {
			tSol = SolveAtom::solveAtom(atoms[i], 0, upperBound - sol.F, atoms[i].mapTableInto(tableSizes));
		}
		else {
			tSol = SolveAtom::solveAtom(atoms[i], max((int64_t)0, lowerBound - sol.F), upperBound - sol.F, atoms[i].mapTableInto(tableSizes));
		}
		if (tSol.S == FAILSOLUTION) {
			if (fillWidth <= upperBound) {
				return {fillWidth, fill.F};
			}
			else {
				return {-1, FAILSOLUTION};
			}
		}
		sol.F += tSol.F;
		if (sol.F > upperBound) return {-1, FAILSOLUTION};
		tSol.S = atoms[i].mapBack(tSol.S);
		sol.S.insert(sol.S.end(), tSol.S.begin(), tSol.S.end());
	}
	assert(sol.F <= upperBound);
	return sol;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %r8, 0x48(%rsp)
testq %rdx, %rdx
js 0x52b8
movq %rcx, %r12
cmpq %rcx, %rdx
jg 0x52d7
movq %rsi, %r14
movq 0x48(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rsi
subq %rax, %rsi
shrq $0x3, %rsi
movl 0x50(%r14), %ecx
cmpl %esi, %ecx
jne 0x52f6
movq %rdi, %r13
testl %ecx, %ecx
jle 0x4bc6
movq 0x38(%r14), %rsi
xorl %edi, %edi
leaq (%rdi,%rdi,2), %r9
movq (%rsi,%r9,8), %r8
movq 0x8(%rsi,%r9,8), %r9
cmpq %r9, %r8
je 0x4bb6
movq (%rax,%rdi,8), %r10
movslq (%r8), %r11
movq (%rax,%r11,8), %rbx
imulq %r10, %rbx
cmpq %rbx, %rdx
cmovgq %rdx, %rbx
addq $0x4, %r8
movq %rbx, %rdx
cmpq %r9, %r8
jne 0x4b96
jmp 0x4bb9
movq %rdx, %rbx
incq %rdi
movq %rbx, %rdx
cmpq %rcx, %rdi
jne 0x4b80
jmp 0x4bc9
movq %rdx, %rbx
leaq 0xc8(%rsp), %rdi
movq %r14, %rsi
callq 0x12a60
leaq 0x110(%rsp), %rdi
movq %r14, %rsi
callq 0x431c
leaq 0x128(%rsp), %rdi
leaq 0x18(%r14), %rsi
callq 0x7e36
movb 0x30(%r14), %al
leaq 0x148(%rsp), %rdi
movb %al, -0x8(%rdi)
leaq 0x38(%r14), %rsi
callq 0x4164
movq 0x50(%r14), %rax
leaq 0x110(%rsp), %rdi
movq %rax, 0x50(%rdi)
leaq 0xc8(%rsp), %rsi
callq 0x9880
leaq 0x110(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x13a1b
movq %rax, 0x70(%rsp)
cmpq %rbx, %rax
jl 0x5315
jle 0x4d40
movq 0xd0(%rsp), %rax
cmpq 0xc8(%rsp), %rax
je 0x4d5f
leaq 0x50(%rsp), %rdi
leaq 0x110(%rsp), %rdx
leaq 0xc8(%rsp), %rcx
movq %r14, %rsi
callq 0x12fba
leaq 0x58(%rsp), %rdi
movq (%rdi), %rax
movq %rax, 0x78(%rsp)
movups 0x8(%rdi), %xmm0
movups %xmm0, 0x80(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
callq 0x69a0
movq 0x78(%rsp), %r15
movq 0x80(%rsp), %r14
cmpq %r14, %r15
je 0x4d9d
movq %r12, 0x40(%rsp)
movq %r13, %r12
movq %r14, %r13
subq %r15, %r13
movq %r13, %rax
sarq $0x3, %rax
movabsq $0x2e8ba2e8ba2e8ba3, %rcx # imm = 0x2E8BA2E8BA2E8BA3
imulq %rcx, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x5bfd
cmpq $0x581, %r13            # imm = 0x581
jl 0x4d8a
leaq 0x580(%r15), %rbp
movq %r15, %rdi
movq %rbp, %rsi
callq 0x6471
cmpq %r14, %rbp
movq %r12, %r13
movq 0x40(%rsp), %r12
je 0x4d9d
movq %rbp, %rdi
callq 0x666a
addq $0x58, %rbp
cmpq %r14, %rbp
jne 0x4d2d
jmp 0x4d9d
movq 0x70(%rsp), %rax
movq %rax, (%r13)
leaq 0x8(%r13), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x402a
jmp 0x51f5
leaq 0x8(%r13), %rdi
movq 0x70(%rsp), %rax
cmpq %r12, %rax
jle 0x519c
movq $-0x1, (%r13)
leaq 0x31468(%rip), %rsi     # 0x361e8
callq 0x402a
jmp 0x51f5
movq %r15, %rdi
movq %r14, %rsi
callq 0x6471
movq %r12, %r13
movq 0x40(%rsp), %r12
xorl %eax, %eax
movq %rsp, %rsi
movq %rax, 0x10(%rsi)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsi)
movq %rax, 0x50(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x402a
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x4dd1
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x78(%rsp), %rbp
movq 0x80(%rsp), %rax
subq %rbp, %rax
sarq $0x3, %rax
movabsq $0x2e8ba2e8ba2e8ba3, %rcx # imm = 0x2E8BA2E8BA2E8BA3
imulq %rcx, %rax
testl %eax, %eax
jle 0x515a
leaq 0xb0(%rsp), %r14
xorl %ecx, %ecx
movq %r13, %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, (%rsp)
cltq
imulq $0x58, %rcx, %rdx
incq %rcx
movq %rdx, 0x40(%rsp)
addq %rdx, %rbp
movq %rcx, 0x90(%rsp)
cmpq %rax, %rcx
jge 0x4ef4
movq 0x50(%rsp), %r13
movq %r14, %rdi
movq %rbp, %rsi
movq 0x48(%rsp), %rdx
callq 0x68e2
movq %r12, %rcx
subq %r13, %rcx
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
xorl %edx, %edx
movq %r14, %r8
callq 0x464c
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rax
movups 0x20(%rsp), %xmm0
movups 0x30(%rsp), %xmm1
movaps %xmm0, (%rsp)
movq 0x18(%rsp), %rsi
movaps %xmm1, 0x10(%rsp)
movq %rdi, 0x28(%rsp)
movq %rax, 0x30(%rsp)
movq %rsi, 0x38(%rsp)
leaq 0x28(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
testq %rdi, %rdi
movq %r15, %r13
je 0x4eb7
subq %rdi, %rsi
callq 0x2210
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
leaq 0x98(%rsp), %r14
je 0x4ed6
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x4fc9
movq 0xc0(%rsp), %rsi
jmp 0x4fc1
movq 0x50(%rsp), %r13
leaq 0xf8(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
movq 0x48(%rsp), %rdx
callq 0x68e2
movq %rbx, %rdx
subq %r13, %rdx
testq %rdx, %rdx
movl $0x0, %eax
cmovleq %rax, %rdx
movq %r12, %rcx
subq %r13, %rcx
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %r8
callq 0x464c
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rax
movups 0x20(%rsp), %xmm0
movups 0x30(%rsp), %xmm1
movaps %xmm0, (%rsp)
movq 0x18(%rsp), %rsi
movaps %xmm1, 0x10(%rsp)
movq %rdi, 0x28(%rsp)
movq %rax, 0x30(%rsp)
movq %rsi, 0x38(%rsp)
leaq 0x28(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
testq %rdi, %rdi
movq %r15, %r13
je 0x4f8d
subq %rdi, %rsi
callq 0x2210
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
leaq 0x98(%rsp), %r14
je 0x4fac
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0xf8(%rsp), %rdi
testq %rdi, %rdi
je 0x4fc9
movq 0x108(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rax
movq %rax, %rsi
subq %rcx, %rsi
movq 0x31210(%rip), %rdi     # 0x361f0
movq 0x31201(%rip), %rdx     # 0x361e8
subq %rdx, %rdi
cmpq %rdi, %rsi
jne 0x503b
cmpq %rax, %rcx
sete %sil
je 0x5032
movl (%rcx), %esi
cmpl (%rdx), %esi
jne 0x503b
movl 0x4(%rdx), %esi
cmpl %esi, 0x4(%rcx)
jne 0x503b
addq $0x8, %rcx
addq $0xc, %rdx
cmpq %rax, %rcx
sete %sil
je 0x5032
movl (%rcx), %esi
cmpl -0x4(%rdx), %esi
jne 0x503b
leaq 0x8(%rcx), %rsi
movl (%rdx), %edi
addq $0x8, %rdx
cmpl %edi, 0x4(%rcx)
movq %rsi, %rcx
je 0x500e
jmp 0x503b
testb %sil, %sil
jne 0x527b
movq 0x50(%rsp), %rax
addq (%rsp), %rax
movq %rax, 0x50(%rsp)
cmpq %r12, %rax
jg 0x51a5
movq 0x78(%rsp), %rbp
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
callq 0x402a
addq 0x40(%rsp), %rbp
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x9f86
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rax
movq %rax, 0x8(%rsp)
movups 0x28(%rsp), %xmm0
movaps %xmm0, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
testq %rdi, %rdi
je 0x50b5
subq %rdi, %rsi
callq 0x2210
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x50cc
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x50e9
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x60(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rcx
leaq 0x58(%rsp), %rdi
callq 0x8bb4
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x5119
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x78(%rsp), %rbp
movq 0x80(%rsp), %rax
subq %rbp, %rax
sarq $0x3, %rax
movabsq $0x2e8ba2e8ba2e8ba3, %rcx # imm = 0x2E8BA2E8BA2E8BA3
imulq %rcx, %rax
movslq %eax, %rcx
movq 0x90(%rsp), %rdx
cmpq %rcx, %rdx
movq %rdx, %rcx
leaq 0xb0(%rsp), %r14
jl 0x4e05
movq 0x50(%rsp), %rax
cmpq %r12, %rax
jg 0x5334
movq %rax, (%r13)
movq 0x58(%rsp), %rax
movq %rax, 0x8(%r13)
movq 0x60(%rsp), %rax
movq %rax, 0x10(%r13)
movq 0x68(%rsp), %rax
movq %rax, 0x18(%r13)
xorps %xmm0, %xmm0
leaq 0x58(%rsp), %rax
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
jmp 0x51d4
movq %rax, (%r13)
jmp 0x4d4d
movq $-0x1, (%r13)
leaq 0x8(%r13), %rdi
leaq 0x31030(%rip), %rsi     # 0x361e8
callq 0x402a
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x51d4
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x51eb
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
leaq 0x78(%rsp), %rdi
callq 0x69a0
leaq 0x148(%rsp), %rdi
callq 0x41fe
leaq 0x128(%rsp), %rdi
callq 0x43b8
movq 0x110(%rsp), %rdi
testq %rdi, %rdi
je 0x522c
movq 0x120(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0xe0(%rsp), %rdi
testq %rdi, %rdi
je 0x5249
movq 0xf0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0xc8(%rsp), %rdi
testq %rdi, %rdi
je 0x5266
movq 0xd8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq %r13, %rax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8(%r13), %rdi
movq 0x70(%rsp), %rax
cmpq %r12, %rax
jle 0x52a2
movq $-0x1, (%r13)
leaq 0x30f50(%rip), %rsi     # 0x361e8
callq 0x402a
jmp 0x51bd
movq %rax, (%r13)
leaq 0xc8(%rsp), %rsi
callq 0x402a
jmp 0x51bd
leaq 0x2507f(%rip), %rdi     # 0x2a33e
leaq 0x24fd2(%rip), %rsi     # 0x2a298
leaq 0x25081(%rip), %rcx     # 0x2a34e
movl $0x33, %edx
callq 0x2140
leaq 0x250e6(%rip), %rdi     # 0x2a3c4
leaq 0x24fb3(%rip), %rsi     # 0x2a298
leaq 0x25062(%rip), %rcx     # 0x2a34e
movl $0x34, %edx
callq 0x2140
leaq 0x250e0(%rip), %rdi     # 0x2a3dd
leaq 0x24f94(%rip), %rsi     # 0x2a298
leaq 0x25043(%rip), %rcx     # 0x2a34e
movl $0x35, %edx
callq 0x2140
leaq 0x24f64(%rip), %rdi     # 0x2a280
leaq 0x24f75(%rip), %rsi     # 0x2a298
leaq 0x25024(%rip), %rcx     # 0x2a34e
movl $0x43, %edx
callq 0x2140
leaq 0x250c0(%rip), %rdi     # 0x2a3fb
leaq 0x24f56(%rip), %rsi     # 0x2a298
leaq 0x25005(%rip), %rcx     # 0x2a34e
movl $0x66, %edx
callq 0x2140
jmp 0x5429
jmp 0x5429
jmp 0x5429
jmp 0x5400
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x545a
movq 0x10(%rsp), %rsi
jmp 0x5452
jmp 0x5400
jmp 0x5400
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x43b8
jmp 0x539a
movq %rax, %rbx
movq 0x110(%rsp), %rdi
testq %rdi, %rdi
je 0x5471
movq 0x120(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
jmp 0x5471
movq %rax, %rbx
jmp 0x5471
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x542c
movq 0xc0(%rsp), %rsi
jmp 0x541f
jmp 0x5429
movq %rax, %rbx
movq 0xf8(%rsp), %rdi
testq %rdi, %rdi
je 0x542c
movq 0x108(%rsp), %rsi
jmp 0x541f
jmp 0x5429
movq %rax, %rbx
jmp 0x5464
jmp 0x5429
movq %rax, %rbx
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x542c
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
jmp 0x542c
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x5443
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x545a
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
leaq 0x78(%rsp), %rdi
callq 0x69a0
leaq 0x110(%rsp), %rdi
callq 0x412c
leaq 0xc8(%rsp), %rdi
callq 0x40a0
movq %rbx, %rdi
callq 0x2310