// File: /workspace/github_cpp_makefile_star2_O3/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 $0x178, %rsp            # imm = 0x178
testq %rdx, %rdx
js 0x5297
movq %rcx, %rbx
cmpq %rcx, %rdx
jg 0x52b6
movq %r8, %r13
movq %rsi, %r14
movq (%r8), %rax
movq 0x8(%r8), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
cmpl %ecx, 0x50(%rsi)
jne 0x52d5
movq %rdi, %rbp
testl %ecx, %ecx
jle 0x4b8a
movq 0x38(%r14), %rsi
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %edi, %edi
leaq (%rdi,%rdi,2), %r9
movq (%rsi,%r9,8), %r8
movq 0x8(%rsi,%r9,8), %r9
cmpq %r9, %r8
je 0x4b7a
movq (%rax,%rdi,8), %r10
movslq (%r8), %r11
movq (%rax,%r11,8), %r12
imulq %r10, %r12
cmpq %r12, %rdx
cmovgq %rdx, %r12
addq $0x4, %r8
movq %r12, %rdx
cmpq %r9, %r8
jne 0x4b5a
jmp 0x4b7d
movq %rdx, %r12
incq %rdi
movq %r12, %rdx
cmpq %rcx, %rdi
jne 0x4b44
jmp 0x4b8d
movq %rdx, %r12
leaq 0xc0(%rsp), %rdi
movq %r14, %rsi
callq 0x12cbc
leaq 0xf0(%rsp), %rdi
movq %r14, %rsi
callq 0x42ce
movq %rbx, 0x28(%rsp)
leaq 0x108(%rsp), %r15
leaq 0x18(%r14), %rsi
movq %r15, %rdi
callq 0x815a
movb 0x30(%r14), %al
leaq 0x128(%rsp), %rbx
movb %al, -0x8(%rbx)
leaq 0x38(%r14), %rsi
movq %rbx, %rdi
callq 0x4110
movq 0x50(%r14), %rax
leaq 0xf0(%rsp), %rdi
movq %rax, 0x50(%rdi)
leaq 0xc0(%rsp), %rsi
callq 0x9bd8
leaq 0xf0(%rsp), %rdi
movq %r13, %rsi
callq 0x13cbb
movq %rax, 0x58(%rsp)
cmpq %r12, %rax
jl 0x52f4
jle 0x4d02
movq 0xc8(%rsp), %rax
cmpq 0xc0(%rsp), %rax
je 0x4d21
leaq 0x60(%rsp), %rdi
leaq 0xf0(%rsp), %rdx
leaq 0xc0(%rsp), %rcx
movq %r14, %rsi
callq 0x13217
leaq 0x68(%rsp), %rdi
movupd (%rdi), %xmm0
movapd %xmm0, 0x80(%rsp)
movq 0x10(%rdi), %rax
movq %rax, 0x90(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
callq 0x6b12
movq 0x80(%rsp), %r15
movq 0x88(%rsp), %r14
cmpq %r14, %r15
je 0x4d59
movq %r14, %rbx
subq %r15, %rbx
movq %rbx, %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 0x5bbf
cmpq $0x581, %rbx            # imm = 0x581
jl 0x4d4e
leaq 0x580(%r15), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x65cd
cmpq %r14, %rbx
je 0x4d59
movq %rbx, %rdi
callq 0x6794
addq $0x58, %rbx
jmp 0x4cef
movq 0x58(%rsp), %rax
movq %rax, (%rbp)
leaq 0x8(%rbp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x3f7c
jmp 0x51e9
leaq 0x8(%rbp), %rdi
movq 0x58(%rsp), %rax
cmpq 0x28(%rsp), %rax
jle 0x5184
movq $-0x1, (%rbp)
leaq 0x304a4(%rip), %rsi     # 0x351e8
callq 0x3f7c
jmp 0x51e9
movq %r15, %rdi
movq %r14, %rsi
callq 0x65cd
xorl %eax, %eax
movq %rsp, %rsi
movq %rax, 0x10(%rsi)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsi)
leaq 0x68(%rsp), %rdi
movq %rax, -0x8(%rdi)
callq 0x3f7c
movq %rbp, 0x50(%rsp)
movq (%rsp), %rdi
testq %rdi, %rdi
leaq 0x128(%rsp), %rbx
je 0x4d9b
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x80(%rsp), %r14
movq 0x88(%rsp), %rax
subq %r14, %rax
sarq $0x3, %rax
movabsq $0x2e8ba2e8ba2e8ba3, %rcx # imm = 0x2E8BA2E8BA2E8BA3
imulq %rcx, %rax
testl %eax, %eax
jle 0x5113
leaq 0x30(%rsp), %rbp
xorl %ecx, %ecx
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movapd %xmm0, (%rsp)
cltq
imulq $0x58, %rcx, %rdx
incq %rcx
movq %rdx, 0xa0(%rsp)
addq %rdx, %r14
movq %rcx, 0x98(%rsp)
cmpq %rax, %rcx
jge 0x4ec1
movq %rbp, %r15
movq 0x60(%rsp), %rbx
movq %r13, %rdx
leaq 0x160(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x6a54
movq 0x28(%rsp), %rcx
subq %rbx, %rcx
movq %r15, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rbp, %r8
callq 0x464c
movq 0x30(%rsp), %rax
movq %rax, (%rsp)
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movupd 0x38(%rsp), %xmm0
movupd %xmm0, 0x8(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0x38(%rsp), %rax
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
testq %rdi, %rdi
je 0x4e9b
subq %rdi, %rsi
callq 0x2210
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x4e9b
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
movq 0x28(%rsp), %rbx
movq %r15, %rbp
je 0x4f8e
movq 0x170(%rsp), %rsi
jmp 0x4f86
movq %r13, %rdx
movq %rbp, %r15
movq 0x60(%rsp), %rbx
leaq 0x148(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x6a54
movq %r12, %rdx
subq %rbx, %rdx
testq %rdx, %rdx
movl $0x0, %eax
cmovleq %rax, %rdx
movq 0x28(%rsp), %rcx
subq %rbx, %rcx
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %r8
callq 0x464c
movq 0x30(%rsp), %rax
movq %rax, (%rsp)
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movupd 0x38(%rsp), %xmm0
movupd %xmm0, 0x8(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0x38(%rsp), %rax
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movq $0x0, 0x10(%rax)
testq %rdi, %rdi
je 0x4f69
subq %rdi, %rsi
callq 0x2210
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x4f69
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x148(%rsp), %rdi
testq %rdi, %rdi
movq 0x28(%rsp), %rbx
movq %r15, %rbp
je 0x4f8e
movq 0x158(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
leaq 0xa8(%rsp), %r15
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, %rsi
subq %rax, %rsi
movq 0x30243(%rip), %rdi     # 0x351f0
movq 0x30234(%rip), %rdx     # 0x351e8
subq %rdx, %rdi
cmpq %rdi, %rsi
jne 0x4fe9
cmpq %rcx, %rax
je 0x514b
movq (%rax), %xmm0
movq (%rdx), %xmm1
pcmpeqd %xmm0, %xmm1
pshufd $0x50, %xmm1, %xmm0     # xmm0 = xmm1[0,0,1,1]
movmskpd %xmm0, %esi
cmpl $0x3, %esi
jne 0x4fe9
addq $0x8, %rax
addq $0x8, %rdx
jmp 0x4fbc
movq 0x60(%rsp), %rax
addq (%rsp), %rax
movq %rax, 0x60(%rsp)
cmpq %rbx, %rax
jg 0x5265
movq 0x80(%rsp), %rbx
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
callq 0x3f7c
addq 0xa0(%rsp), %rbx
movq %rbp, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0xa2d6
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movapd 0x30(%rsp), %xmm0
movupd %xmm0, 0x8(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x18(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
testq %rdi, %rdi
leaq 0x128(%rsp), %rbx
je 0x508a
subq %rdi, %rsi
callq 0x2210
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x508a
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x50a7
movq 0xb8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x70(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rcx
leaq 0x68(%rsp), %rdi
callq 0x8ecc
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x50d7
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x80(%rsp), %r14
movq 0x88(%rsp), %rax
subq %r14, %rax
sarq $0x3, %rax
movabsq $0x2e8ba2e8ba2e8ba3, %rcx # imm = 0x2E8BA2E8BA2E8BA3
imulq %rcx, %rax
movslq %eax, %rcx
movq 0x98(%rsp), %rdx
cmpq %rcx, %rdx
movq %rdx, %rcx
jl 0x4dcf
movq 0x60(%rsp), %rax
cmpq 0x28(%rsp), %rax
jg 0x5313
movq 0x50(%rsp), %rbp
movq %rax, (%rbp)
movups 0x68(%rsp), %xmm0
movups %xmm0, 0x8(%rbp)
movq 0x78(%rsp), %rax
movq %rax, 0x18(%rbp)
leaq 0x108(%rsp), %r15
jmp 0x51dc
movq 0x50(%rsp), %rbp
leaq 0x8(%rbp), %rdi
movq 0x58(%rsp), %rax
cmpq %rbx, %rax
jle 0x518d
movq $-0x1, (%rbp)
leaq 0x3007b(%rip), %rsi     # 0x351e8
callq 0x3f7c
leaq 0x108(%rsp), %r15
leaq 0x128(%rsp), %rbx
jmp 0x51ae
movq %rax, (%rbp)
jmp 0x4d0f
movq %rax, (%rbp)
leaq 0xc0(%rsp), %rsi
callq 0x3f7c
leaq 0x108(%rsp), %r15
leaq 0x128(%rsp), %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x51c5
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x51dc
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
leaq 0x80(%rsp), %rdi
callq 0x6b12
movq %rbx, %rdi
callq 0x41aa
movq %r15, %rdi
callq 0x436a
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x5216
movq 0x100(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0x5233
movq 0xe8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x5250
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq %rbp, %rax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x50(%rsp), %rbp
movq $-0x1, (%rbp)
leaq 0x8(%rbp), %rdi
leaq 0x2ff6b(%rip), %rsi     # 0x351e8
callq 0x3f7c
leaq 0x108(%rsp), %r15
leaq 0x128(%rsp), %rbx
jmp 0x51ae
leaq 0x240b0(%rip), %rdi     # 0x2934e
leaq 0x24003(%rip), %rsi     # 0x292a8
leaq 0x240b2(%rip), %rcx     # 0x2935e
movl $0x33, %edx
callq 0x2140
leaq 0x24117(%rip), %rdi     # 0x293d4
leaq 0x23fe4(%rip), %rsi     # 0x292a8
leaq 0x24093(%rip), %rcx     # 0x2935e
movl $0x34, %edx
callq 0x2140
leaq 0x24111(%rip), %rdi     # 0x293ed
leaq 0x23fc5(%rip), %rsi     # 0x292a8
leaq 0x24074(%rip), %rcx     # 0x2935e
movl $0x35, %edx
callq 0x2140
leaq 0x23f95(%rip), %rdi     # 0x29290
leaq 0x23fa6(%rip), %rsi     # 0x292a8
leaq 0x24055(%rip), %rcx     # 0x2935e
movl $0x43, %edx
callq 0x2140
leaq 0x240f1(%rip), %rdi     # 0x2940b
leaq 0x23f87(%rip), %rsi     # 0x292a8
leaq 0x24036(%rip), %rcx     # 0x2935e
movl $0x66, %edx
callq 0x2140
jmp 0x53c1
jmp 0x53c1
jmp 0x53c1
jmp 0x5437
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x53f2
movq 0x10(%rsp), %rsi
jmp 0x53ea
jmp 0x5437
movq %rax, %rbx
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0x53c4
movq 0x170(%rsp), %rsi
jmp 0x53b7
jmp 0x53c1
movq %rax, %rbx
movq 0x148(%rsp), %rdi
testq %rdi, %rdi
je 0x53c4
movq 0x158(%rsp), %rsi
jmp 0x53b7
jmp 0x53c1
jmp 0x53c1
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x53c4
movq 0xb8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
jmp 0x53c4
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x53db
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x53f2
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
leaq 0x80(%rsp), %rdi
callq 0x6b12
jmp 0x543a
jmp 0x5437
movq %rax, %rbx
movq %r15, %rdi
callq 0x436a
jmp 0x5413
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x5447
movq 0x100(%rsp), %rsi
subq %rdi, %rsi
callq 0x2210
jmp 0x5447
movq %rax, %rbx
jmp 0x5447
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x40d8
leaq 0xc0(%rsp), %rdi
callq 0x3ff2
movq %rbx, %rdi
callq 0x2310