// File: /workspace/github_cpp_makefile_star2_O2/ixchow[P]http-tweak/tweak.cpp
// Function: tweak::sync()
void sync() {
	auto &internal = get_internal();
	std::unique_lock< std::mutex > lock(internal.mutex);

	if (!internal.server) internal.server = std::make_unique< http::server >(internal.port);

	//Read adjustments (and poll requests) from the server:
	internal.server->poll([&](http::request const &request, std::unique_ptr< http::response > response){
		if (request.method == "GET" && request.url == "/") {
			//serve UI:
			std::ifstream ui(internal.ui_file, std::ios::binary);
			std::ostringstream ss;
			ss << ui.rdbuf();
			response->body = ss.str();
		} else if (request.method == "GET" && request.url == "/tweaks") {
			//serve current state (...by registering a poll):
			internal.polls.emplace_back(0, std::move(response));
		} else if (request.method == "GET" && request.url.substr(0,8) == "/tweaks?") {
			//long poll:
			uint32_t serial = -1U;
			try {
				serial = std::stoul(request.url.substr(8));
			} catch (...) {
				std::cerr << "[tweak::sync()] got invalid serial in url '" + request.url + "'." << std::endl;
				serial = -1U;
			}
			if (serial != -1U) {
				internal.polls.emplace_back(std::stoul(request.url.substr(8)), std::move(response));
			}
		} else if (request.method == "POST" && request.url == "/tweaks") {
			//adjust current state
			//std::cout << "Got tweaks:\n" << request.body << std::endl; //DEBUG

			//looking at something like: {"name":"value","name2":"value2",...}

			std::string const &json = request.body;
			uint32_t i = 0;
			auto skip_wsp = [&json,&i]() {
				while (i < json.size()
					&& (json[i] == 0x09 || json[i] == 0x0A || json[i] == 0x0D || json[i] == 0x20)
					) ++i;
			};
			auto skip_char = [&json,&i](char c) -> bool {
				if (i >= json.size()) return false;
				if (json[i] != c) return false;
				++i;
				return true;
			};
			auto extract_string = [&json, &i](std::string *into) -> bool {
				if (i >= json.size() || json[i] != '"') return false;
				uint32_t begin = i;
				++i;
				while (i < json.size() && json[i] != '"') {
					if (json[i] == '\\') ++i;
					++i;
				}
				if (i >= json.size()) return false;
				assert(json[i] == '"');
				++i;
				uint32_t end = i;
				std::string data = json.substr(begin, end - begin);
				std::string err = "";
				if (!json_to_utf8(data, into, &err)) {
					std::cerr << "Error decoding json string: " << err << std::endl;
					return false;
				}
				return true;
			};
			skip_wsp();
			if (!skip_char('{')) {
				std::cerr << "Missing opening '{'" << std::endl;
				return;
			}
			skip_wsp();
			bool first = true;
			while (i < json.size()) {
				if (json[i] == '}') break; //closing character
				if (first) {
					first = false;
				} else {
					if (!skip_char(',')) {
						std::cerr << "Missing separating ','" << std::endl;
						return;
					}
					skip_wsp();
				}
				std::string name, value;
				if (!extract_string(&name)) {
					std::cerr << "Missing name string." << std::endl;
					return;
				}
				skip_wsp();
				if (!skip_char(':')) {
					std::cerr << "Missing separating ':'" << std::endl;
					return;
				}
				skip_wsp();
				if (!extract_string(&value)) {
					std::cerr << "Missing value string." << std::endl;
					return;
				}

				//std::cout << "Received: '" << name << "':'" << value << "'" << std::endl; //DEBUG
				internal.received[name] = value;

				skip_wsp();
			}
			if (!skip_char('}')) {
				std::cerr << "Missing closing '{'" << std::endl;
				return;
			}
			skip_wsp();
			if (i < json.size()) {
				std::cerr << "Trailing garbage" << std::endl;
				return;
			}
		} else {
			response->status.code = 404;
			response->status.message = "Not Found";
			response->body = "Not Found";
			response->headers.emplace_back("Content-Type", "text/plain");
		}
	});

	//Read all adjustments from the server, and encode current state and hits:
	std::map< std::string, std::string > state;
	for (auto tweak : internal.tweaks) {
		auto f = internal.received.find(tweak->name);
		if (f != internal.received.end()) {
			try {
				tweak->deserialize(f->second);
			} catch (...) {
				std::cerr << "Failed to deserialize " << f->first << " from '" << f->second << "'" << std::endl;
			}
		}
		state[tweak->name] = "{\"hint\":" + utf8_to_json(tweak->hint) + ",\"value\":" + utf8_to_json(tweak->serialize()) + "}";
	}
	internal.received.clear();

	std::string all_state = "{\n";
	for (auto const &name_value : state) {
		if (&name_value != &(*state.begin())) all_state += ",\n";
		all_state += utf8_to_json(name_value.first) + ":" + name_value.second;
	}
	all_state += "\n}";

	//if state has changed, update serial:
	if (internal.state != all_state) {
		internal.state = all_state;
		++internal.state_serial;
	}

	//respond to polls:
	for (auto poll = internal.polls.begin(); poll != internal.polls.end(); /* later */) {
		auto next = poll;
		++next;
		if (poll->serial != internal.state_serial) {
			poll->response->body = "{\"serial\":" + std::to_string(internal.state_serial) + ",\"state\":" + internal.state + "}";
			poll->response->headers.emplace_back("Content-Type", "application/json");
			internal.polls.erase(poll);
		}
		poll = next;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
callq 0x41cd
leaq 0xaea5(%rip), %rsi      # 0xf330
leaq -0x100(%rbp), %rdi
callq 0x5a32
movq 0xaefa(%rip), %rdi      # 0xf398
testq %rdi, %rdi
jne 0x44df
leaq 0xaee6(%rip), %rsi      # 0xf390
leaq -0xb0(%rbp), %rdi
callq 0x5a6b
leaq -0xb0(%rbp), %rbx
movq (%rbx), %rsi
andq $0x0, (%rbx)
leaq 0xaecd(%rip), %rdi      # 0xf398
callq 0x6a62
movq %rbx, %rdi
callq 0x5aba
movq 0xaeb9(%rip), %rdi      # 0xf398
leaq -0xb0(%rbp), %rsi
andq $0x0, 0x8(%rsi)
leaq 0xae1e(%rip), %rax      # 0xf310
movq %rax, (%rsi)
leaq 0x708(%rip), %rax       # 0x4c04
movq %rax, 0x18(%rsi)
leaq 0xd0d(%rip), %rax       # 0x5214
movq %rax, 0x10(%rsi)
xorps %xmm0, %xmm0
callq 0x5ade
leaq -0xb0(%rbp), %rdi
callq 0x3d1e
leaq -0xa8(%rbp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0xae26(%rip), %rax      # 0xf368
leaq -0xd0(%rbp), %rbx
leaq -0x120(%rbp), %r13
movq (%rax), %rax
testq %rax, %rax
je 0x4713
movq %rbx, %r12
movq %rax, -0x30(%rbp)
movq 0x8(%rax), %r14
leaq 0xae72(%rip), %rdi      # 0xf3e0
movq %r14, %rsi
callq 0x8e76
movq %rax, %rbx
leaq 0xae68(%rip), %rax      # 0xf3e8
cmpq %rax, %rbx
je 0x45b0
leaq 0x40(%rbx), %rsi
leaq -0x180(%rbp), %r15
movq %r15, %rdi
movq %rsi, -0x60(%rbp)
callq 0x3440
leaq 0x60(%r14), %rdi
movq %r15, %rsi
callq 0x6016
movq %r15, %rdi
callq 0x31c0
leaq 0x20(%r14), %rsi
leaq -0x140(%rbp), %rbx
movq %rbx, %rdi
callq 0x4abc
movq %r12, %rdi
leaq 0x5c2b(%rip), %rsi      # 0xa1f8
movq %rbx, %rdx
callq 0x60c6
leaq -0x80(%rbp), %rdi
movq %r12, %rsi
leaq 0x5c1e(%rip), %rdx      # 0xa201
callq 0x602c
movq %r12, %rbx
leaq 0x40(%r14), %rsi
leaq -0x160(%rbp), %r12
movq %r12, %rdi
callq 0x60f4
movq %r13, %rdi
movq %r12, %rsi
callq 0x4abc
leaq -0xf0(%rbp), %r15
movq %r15, %rdi
leaq -0x80(%rbp), %rsi
movq %r13, %rdx
callq 0x6055
leaq -0x58(%rbp), %rdi
movq %r15, %rsi
leaq 0x5be5(%rip), %rdx      # 0xa212
callq 0x602c
leaq -0xb0(%rbp), %rdi
movq %r14, %rsi
callq 0x6116
movq %rax, %rdi
leaq -0x58(%rbp), %r14
movq %r14, %rsi
callq 0x33d0
movq %r14, %rdi
callq 0x31c0
movq %r15, %rdi
callq 0x31c0
movq %r13, %rdi
callq 0x31c0
leaq -0x160(%rbp), %rdi
callq 0x31c0
leaq -0x80(%rbp), %rdi
callq 0x31c0
movq %rbx, %rdi
callq 0x31c0
leaq -0x140(%rbp), %rdi
callq 0x31c0
movq -0x30(%rbp), %rax
jmp 0x4550
movq %rax, -0x38(%rbp)
movq %r15, %rdi
callq 0x31c0
jmp 0x46ac
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x3190
movq 0xa92c(%rip), %rdi      # 0xefe8
leaq 0x5b16(%rip), %rsi      # 0xa1d9
callq 0x3360
addq $0x20, %rbx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3330
movq %rax, %rdi
leaq 0x5b0f(%rip), %rsi      # 0xa1f0
callq 0x3360
movq %rax, %rdi
movq -0x60(%rbp), %rsi
callq 0x3330
movq %rax, %rdi
leaq 0x5fae(%rip), %rsi      # 0xa6aa
callq 0x3360
movq %rax, %rdi
callq 0x31d0
callq 0x34f0
jmp 0x45b0
leaq 0xacc6(%rip), %rdi      # 0xf3e0
callq 0x8ebe
leaq 0x5ae5(%rip), %rsi      # 0xa20b
leaq -0x58(%rbp), %rdi
leaq -0xf0(%rbp), %rdx
callq 0x3ac4
movq -0x98(%rbp), %r15
leaq -0xd0(%rbp), %rbx
leaq -0x80(%rbp), %r12
leaq -0xf0(%rbp), %r13
leaq -0x58(%rbp), %r14
leaq -0xa8(%rbp), %rax
cmpq %rax, %r15
je 0x47d7
cmpq -0x98(%rbp), %r15
je 0x4777
movq %r14, %rdi
leaq 0x5a9c(%rip), %rsi      # 0xa20e
callq 0x3580
leaq 0x20(%r15), %rsi
movq %rbx, %rdi
callq 0x4abc
movq %r12, %rdi
movq %rbx, %rsi
leaq 0x5a97(%rip), %rdx      # 0xa227
callq 0x602c
leaq 0x40(%r15), %rdx
movq %r13, %rdi
movq %r12, %rsi
callq 0x617f
movq %r14, %rdi
movq %r13, %rsi
callq 0x35a0
movq %r13, %rdi
callq 0x31c0
movq %r12, %rdi
callq 0x31c0
movq %rbx, %rdi
callq 0x31c0
movq %r15, %rdi
callq 0x32a0
movq %rax, %r15
jmp 0x4753
leaq 0x5a33(%rip), %rsi      # 0xa211
leaq -0x58(%rbp), %rdi
callq 0x3580
leaq 0xabd2(%rip), %rdi      # 0xf3c0
leaq -0x58(%rbp), %rsi
callq 0x61a8
testb %al, %al
je 0x4811
leaq 0xabbe(%rip), %rdi      # 0xf3c0
leaq -0x58(%rbp), %rsi
callq 0x3210
incl 0xaba7(%rip)            # 0xf3b8
movq 0xab88(%rip), %r14      # 0xf3a0
leaq -0x140(%rbp), %r12
leaq 0xab7a(%rip), %rax      # 0xf3a0
cmpq %rax, %r14
je 0x4940
movq %r14, %rbx
movq (%r14), %r14
movl 0xab7d(%rip), %esi      # 0xf3b8
cmpl %esi, 0x10(%rbx)
je 0x481f
leaq -0x120(%rbp), %r15
movq %r15, %rdi
callq 0x61b5
movq %r12, %rdi
leaq 0x59bb(%rip), %rsi      # 0xa214
movq %r15, %rdx
callq 0x60c6
leaq -0xd0(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
leaq 0x59aa(%rip), %rdx      # 0xa21f
callq 0x602c
leaq -0x80(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
leaq 0xab35(%rip), %rdx      # 0xf3c0
callq 0x617f
movq %r14, -0x30(%rbp)
movq %r12, %r14
leaq -0x120(%rbp), %r12
leaq -0xf0(%rbp), %r15
movq %r15, %rdi
movq %r13, %rsi
leaq 0x5960(%rip), %rdx      # 0xa212
callq 0x602c
movq 0x18(%rbx), %rdi
addq $0x40, %rdi
movq %r15, %rsi
callq 0x33d0
movq %r15, %rdi
callq 0x31c0
movq %r13, %rdi
callq 0x31c0
leaq -0xd0(%rbp), %rdi
callq 0x31c0
movq %r14, %rdi
callq 0x31c0
movq %r12, %rdi
callq 0x31c0
movq 0x18(%rbx), %rdi
addq $0x28, %rdi
leaq 0x5927(%rip), %rsi      # 0xa229
leaq 0x592d(%rip), %rdx      # 0xa236
callq 0x61f4
leaq 0x18(%rbx), %r15
decq 0xaa97(%rip)            # 0xf3b0
movq %rbx, %rdi
callq 0x34e0
movq %r15, %rdi
callq 0x6240
movq %rbx, %rdi
pushq $0x20
popq %rsi
callq 0x3380
movq %r14, %r12
movq -0x30(%rbp), %r14
jmp 0x481f
leaq -0x58(%rbp), %rdi
callq 0x31c0
leaq -0xb0(%rbp), %rdi
callq 0x7ef2
leaq -0x100(%rbp), %rdi
callq 0x5a52
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
callq 0x34f0
jmp 0x4a9c
movq %rax, %rdi
callq 0x3c4e
movq %rax, %rbx
jmp 0x4aa8
jmp 0x4a69
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
callq 0x3d1e
jmp 0x4aa8
jmp 0x49ed
jmp 0x49ed
movq %rax, %rbx
jmp 0x49d4
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x31c0
jmp 0x49c8
movq %rax, %rbx
jmp 0x49e0
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
callq 0x31c0
leaq -0x140(%rbp), %rdi
callq 0x31c0
leaq -0x120(%rbp), %rdi
jmp 0x4a1b
jmp 0x49ed
jmp 0x49ed
movq %rax, %rbx
jmp 0x4a20
movq %rax, %rbx
jmp 0x4a14
movq %rax, %rbx
jmp 0x4a0b
movq %rax, %rbx
leaq -0xf0(%rbp), %rdi
callq 0x31c0
leaq -0x80(%rbp), %rdi
callq 0x31c0
leaq -0xd0(%rbp), %rdi
callq 0x31c0
leaq -0x58(%rbp), %rdi
jmp 0x4a97
movq %rax, %rbx
jmp 0x4a59
movq %rax, %rbx
jmp 0x4a4d
movq %rax, %rbx
jmp 0x4a41
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0x31c0
leaq -0xf0(%rbp), %rdi
callq 0x31c0
leaq -0x120(%rbp), %rdi
callq 0x31c0
leaq -0x160(%rbp), %rdi
callq 0x31c0
jmp 0x4a7b
jmp 0x4a69
movq %rax, %rbx
jmp 0x4a9c
movq %rax, %rbx
jmp 0x4a90
movq %rax, %rbx
jmp 0x4a84
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x31c0
leaq -0xd0(%rbp), %rdi
callq 0x31c0
leaq -0x140(%rbp), %rdi
callq 0x31c0
leaq -0xb0(%rbp), %rdi
callq 0x7ef2
leaq -0x100(%rbp), %rdi
callq 0x5a52
movq %rbx, %rdi
callq 0x3540