// File: /workspace/github_c_makefile_star2_O2/baskerville[P]bspwm/src/restore.c
// Function: restore_state
bool restore_state(const char *file_path)
{
	size_t jslen;
	char *json = read_string(file_path, &jslen);

	if (json == NULL) {
		return false;
	}

	int nbtok = 256;
	jsmn_parser parser;
	jsmntok_t *tokens = malloc(nbtok * sizeof(jsmntok_t));

	if (tokens == NULL) {
		perror("Restore tree: malloc");
		free(json);
		return false;
	}

	jsmn_init(&parser);
	int ret;

	while ((ret = jsmn_parse(&parser, json, jslen, tokens, nbtok)) == JSMN_ERROR_NOMEM) {
		nbtok *= 2;
		jsmntok_t *rtokens = realloc(tokens, nbtok * sizeof(jsmntok_t));
		if (rtokens == NULL) {
			perror("Restore tree: realloc");
			free(tokens);
			free(json);
			return false;
		} else {
			tokens = rtokens;
		}
	}

	if (ret < 0) {
		warn("Restore tree: jsmn_parse: ");
		switch (ret) {
			case JSMN_ERROR_NOMEM:
				warn("not enough memory.\n");
				break;
			case JSMN_ERROR_INVAL:
				warn("found invalid character inside JSON string.\n");
				break;
			case JSMN_ERROR_PART:
				warn("not a full JSON packet.\n");
				break;
			default:
				warn("unknown error.\n");
				break;
		}

		free(tokens);
		free(json);

		return false;
	}

	int num = tokens[0].size;

	if (num < 1) {
		free(tokens);
		free(json);

		return false;
	}

	mon = NULL;
	while (mon_head != NULL) {
		remove_monitor(mon_head);
	}

	jsmntok_t *t = tokens + 1;
	uint32_t focused_monitor_id = 0, primary_monitor_id = 0;
	jsmntok_t *focus_history_token = NULL, *stacking_list_token = NULL;

	for (int i = 0; i < num; i++) {
		if (keyeq("focusedMonitorId", t, json)) {
			t++;
			sscanf(json + t->start, "%u", &focused_monitor_id);
		} else if (keyeq("primaryMonitorId", t, json)) {
			t++;
			sscanf(json + t->start, "%u", &primary_monitor_id);
		} else if (keyeq("clientsCount", t, json)) {
			t++;
			sscanf(json + t->start, "%u", &clients_count);
		} else if (keyeq("monitors", t, json)) {
			t++;
			int s = t->size;
			t++;
			for (int j = 0; j < s; j++) {
				monitor_t *m = restore_monitor(&t, json);
				if (m->desk == NULL) {
					add_desktop(m, make_desktop(NULL, XCB_NONE));
				}
				add_monitor(m);
			}
			continue;
		} else if (keyeq("focusHistory", t, json)) {
			t++;
			if (mon == NULL) {
				focus_history_token = t;
			}
			restore_history(&t, json);
			continue;
		} else if (keyeq("stackingList", t, json)) {
			t++;
			if (mon == NULL) {
				stacking_list_token = t;
			}
			restore_stack(&t, json);
			continue;
		} else if (keyeq("eventSubscribers", t, json)) {
			t++;
			restore_subscribers(&t, json);
			continue;
		}
		t++;
	}

	if (focused_monitor_id != 0) {
		coordinates_t loc;
		if (monitor_from_id(focused_monitor_id, &loc)) {
			mon = loc.monitor;
		}
	}

	if (primary_monitor_id != 0) {
		coordinates_t loc;
		if (monitor_from_id(primary_monitor_id, &loc)) {
			pri_mon = loc.monitor;
		}
	}

	if (focus_history_token != NULL) {
		restore_history(&focus_history_token, json);
	}

	if (stacking_list_token != NULL) {
		restore_stack(&stacking_list_token, json);
	}

	for (monitor_t *m = mon_head; m != NULL; m = m->next) {
		m->id = xcb_generate_id(dpy);
		for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
			d->id = xcb_generate_id(dpy);
			regenerate_ids_in(d->root);
			refresh_presel_feedbacks(m, d, d->root);
			restack_presel_feedbacks(d);

			for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
				if (n->client == NULL) {
					continue;
				}
				initialize_client(n);
				uint32_t values[] = {CLIENT_EVENT_MASK | (focus_follows_pointer ? XCB_EVENT_MASK_ENTER_WINDOW : 0)};
				xcb_change_window_attributes(dpy, n->id, XCB_CW_EVENT_MASK, values);
				window_grab_buttons(n->id);
			}
		}
	}

	ewmh_update_number_of_desktops();
	ewmh_update_desktop_names();
	ewmh_update_desktop_viewport();
	ewmh_update_current_desktop();
	ewmh_update_client_list(false);
	ewmh_update_client_list(true);
	ewmh_update_active_window();

	free(tokens);
	free(json);

	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
leaq 0x50(%rsp), %rsi
callq 0x5c80
testq %rax, %rax
je 0x19f11
movq %rax, %r14
movl $0x1000, %edi           # imm = 0x1000
callq 0x4290
testq %rax, %rax
je 0x19f24
movq %rax, %r12
leaq 0x5c(%rsp), %r15
movq %r15, %rdi
callq 0x67b9
movl $0x100, %ebp            # imm = 0x100
movq %r12, %rbx
movq 0x50(%rsp), %rdx
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rcx
movl %ebp, %r8d
callq 0x62d0
cmpl $-0x1, %eax
jne 0x19ad8
addl %ebp, %ebp
movslq %ebp, %rsi
shlq $0x4, %rsi
movq %rbx, %rdi
callq 0x49f0
movq %rax, %r12
testq %rax, %rax
jne 0x19a90
leaq 0x67cc(%rip), %rdi      # 0x2029a
callq 0x4a10
jmp 0x19f01
movl %eax, %r12d
testl %eax, %eax
js 0x19eb3
movl 0xc(%rbx), %r13d
testl %r13d, %r13d
jle 0x19f01
leaq 0xec19(%rip), %rax      # 0x28710
andq $0x0, (%rax)
leaq 0xec06(%rip), %r15      # 0x28708
movq (%r15), %rdi
testq %rdi, %rdi
je 0x19b11
callq 0x7194
jmp 0x19b02
leaq 0x10(%rbx), %rax
movq %rsp, %rcx
movq %rax, (%rcx)
leaq 0x14(%rsp), %rax
andl $0x0, (%rax)
leaq 0x10(%rsp), %rax
andl $0x0, (%rax)
leaq 0x5a9d(%rip), %rax      # 0x1f5cf
movq $0x0, 0x8(%rsp)
movq $0x0, 0x18(%rsp)
xorl %r12d, %r12d
movq %rbx, 0x20(%rsp)
movq %rax, %rbx
cmpl %r13d, %r12d
je 0x19d18
movq (%rsp), %rbp
leaq 0x67ae(%rip), %rdi      # 0x20311
movq %rbp, %rsi
movq %r14, %rdx
callq 0x19f32
testb %al, %al
je 0x19b81
leaq 0x10(%rbp), %rax
movq %rax, (%rsp)
leaq 0x14(%rsp), %rdx
jmp 0x19bcb
leaq 0x679a(%rip), %rdi      # 0x20322
movq %rbp, %rsi
movq %r14, %rdx
callq 0x19f32
testb %al, %al
je 0x19ba6
leaq 0x10(%rbp), %rax
movq %rax, (%rsp)
leaq 0x10(%rsp), %rdx
jmp 0x19bcb
leaq 0x6786(%rip), %rdi      # 0x20333
movq %rbp, %rsi
movq %r14, %rdx
callq 0x19f32
testb %al, %al
je 0x19be9
leaq 0x10(%rbp), %rax
movq %rax, (%rsp)
leaq 0xeb29(%rip), %rdx      # 0x286f4
movslq 0x14(%rbp), %rdi
addq %r14, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x41c0
addq $0x10, (%rsp)
incl %r12d
jmp 0x19b4f
leaq 0x55ad(%rip), %rdi      # 0x1f19d
movq %rbp, %rsi
movq %r14, %rdx
callq 0x19f32
testb %al, %al
je 0x19c4c
movl 0x1c(%rbp), %ebx
addq $0x20, %rbp
movq %rbp, (%rsp)
testl %ebx, %ebx
movl $0x0, %eax
cmovlel %eax, %ebx
subl $0x1, %ebx
jb 0x19c93
movq %rsp, %rdi
movq %r14, %rsi
callq 0x19f7b
movq %rax, %rbp
cmpq $0x0, 0x58(%rax)
jne 0x19c42
xorl %edi, %edi
xorl %esi, %esi
callq 0x7efd
movq %rbp, %rdi
movq %rax, %rsi
callq 0x7fe2
movq %rbp, %rdi
callq 0x7056
jmp 0x19c14
leaq 0x66ed(%rip), %rdi      # 0x20340
movq %rbp, %rsi
movq %r14, %rdx
callq 0x19f32
testb %al, %al
je 0x19c9f
addq $0x10, %rbp
movq %rbp, (%rsp)
leaq 0xea9f(%rip), %rax      # 0x28710
cmpq $0x0, (%rax)
movq 0x8(%rsp), %rax
cmoveq %rbp, %rax
movq %rax, 0x8(%rsp)
movq %rsp, %rdi
movq %r14, %rsi
callq 0x1a308
jmp 0x19be1
leaq 0x5935(%rip), %rbx      # 0x1f5cf
jmp 0x19be1
leaq 0x66a7(%rip), %rdi      # 0x2034d
movq %rbp, %rsi
movq %r14, %rdx
callq 0x19f32
testb %al, %al
je 0x19ce6
addq $0x10, %rbp
movq %rbp, (%rsp)
leaq 0xea4c(%rip), %rax      # 0x28710
cmpq $0x0, (%rax)
movq 0x18(%rsp), %rax
cmoveq %rbp, %rax
movq %rax, 0x18(%rsp)
movq %rsp, %rdi
movq %r14, %rsi
callq 0x1a38e
jmp 0x19be1
leaq 0x666d(%rip), %rdi      # 0x2035a
movq %rbp, %rsi
movq %r14, %rdx
callq 0x19f32
testb %al, %al
je 0x19bdc
addq $0x10, %rbp
movq %rbp, (%rsp)
movq %rsp, %rdi
movq %r14, %rsi
callq 0x1a41f
jmp 0x19be1
movq 0x18(%rsp), %rbx
movq %rbx, 0x40(%rsp)
movq 0x8(%rsp), %r12
movq %r12, 0x48(%rsp)
movl 0x14(%rsp), %edi
testl %edi, %edi
je 0x19d51
leaq 0x28(%rsp), %rsi
callq 0x199a1
testb %al, %al
je 0x19d51
movq 0x28(%rsp), %rax
leaq 0xe9c2(%rip), %rcx      # 0x28710
movq %rax, (%rcx)
movl 0x10(%rsp), %edi
testl %edi, %edi
je 0x19d76
leaq 0x28(%rsp), %rsi
callq 0x199a1
testb %al, %al
je 0x19d76
movq 0x28(%rsp), %rax
leaq 0xe985(%rip), %rcx      # 0x286f8
movq %rax, (%rcx)
testq %r12, %r12
je 0x19d88
leaq 0x48(%rsp), %rdi
movq %r14, %rsi
callq 0x1a308
testq %rbx, %rbx
je 0x19d9a
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x1a38e
movq %r14, 0x8(%rsp)
leaq 0xe922(%rip), %rbx      # 0x286c8
leaq 0xf04d(%rip), %r14      # 0x28dfa
leaq 0x28(%rsp), %r12
movq (%r15), %r15
testq %r15, %r15
je 0x19e72
movq (%rbx), %rdi
callq 0x4920
movl %eax, 0x20(%r15)
leaq 0x60(%r15), %r13
movq (%r13), %r13
testq %r13, %r13
je 0x19e69
movq (%rbx), %rdi
callq 0x4920
movl %eax, 0x20(%r13)
movq 0x30(%r13), %rdi
callq 0xc4fa
movq 0x30(%r13), %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0xf636
movq %r13, %rdi
callq 0xc91e
movq 0x30(%r13), %rdi
callq 0x8e72
movq %rax, %rbp
testq %rax, %rax
je 0x19e60
cmpq $0x0, 0x48(%rbp)
je 0x19e52
movq %rbp, %rdi
callq 0xa246
movzbl (%r14), %eax
shll $0x4, %eax
orl $0x600000, %eax         # imm = 0x600000
movl %eax, 0x28(%rsp)
movq (%rbx), %rdi
movl (%rbp), %esi
movl $0x800, %edx            # imm = 0x800
movq %r12, %rcx
callq 0x4700
movl (%rbp), %edi
callq 0xe1a9
movq 0x30(%r13), %rsi
movq %rbp, %rdi
callq 0x8ed0
jmp 0x19e10
addq $0x48, %r13
jmp 0x19dce
addq $0x78, %r15
jmp 0x19db2
callq 0x1c53f
callq 0x1c6ea
callq 0x1c78f
callq 0x1c5f3
xorl %edi, %edi
callq 0x1ca74
pushq $0x1
popq %rdi
callq 0x1ca74
callq 0x1c508
movq 0x20(%rsp), %rdi
callq 0x4460
movq 0x8(%rsp), %rdi
callq 0x4460
movb $0x1, %bpl
jmp 0x19f13
leaq 0x63f6(%rip), %rdi      # 0x202b0
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x5b50
cmpl $-0x2, %r12d
leaq 0x63fd(%rip), %rax      # 0x202cb
leaq 0x43e9(%rip), %rcx      # 0x1e2be
cmoveq %rax, %rcx
cmpl $-0x3, %r12d
leaq 0x6414(%rip), %rdi      # 0x202f8
cmovneq %rcx, %rdi
xorl %eax, %eax
callq 0x5b50
movq %rbx, %rdi
callq 0x4460
movq %r14, %rdi
callq 0x4460
jmp 0x19f13
movq %rbx, %rdi
callq 0x4460
movq %r14, %rdi
callq 0x4460
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x635a(%rip), %rdi      # 0x20285
callq 0x4a10
jmp 0x19f09