// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
leaq 0x60(%rsp), %rsi
callq 0x5d0e
testq %rax, %rax
je 0x1b665
movq %rax, %r14
movl $0x1000, %edi           # imm = 0x1000
callq 0x4290
testq %rax, %rax
je 0x1b651
movq %rax, %rbx
leaq 0x6c(%rsp), %r15
movq %r15, %rdi
callq 0x6997
movl $0x100, %ebp            # imm = 0x100
leaq 0x6153(%rip), %r12      # 0x212e6
movq 0x60(%rsp), %rdx
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rcx
movl %ebp, %r8d
callq 0x6414
cmpl $-0x1, %eax
jne 0x1b1f1
addl %ebp, %ebp
movslq %ebp, %rsi
shlq $0x4, %rsi
movq %rbx, %rdi
callq 0x49f0
movq %rax, %r13
testq %rax, %rax
je 0x1b1d4
movq %rax, %rbx
testq %r13, %r13
jne 0x1b193
jmp 0x1b665
movq %r12, %rdi
callq 0x4a10
movq %rbx, %rdi
callq 0x4460
movq %r14, %rdi
callq 0x4460
movq %rbx, %rax
jmp 0x1b1c7
movl %eax, %r13d
testl %eax, %eax
js 0x1b5fc
movq %rbx, %rdi
movl 0xc(%rbx), %ebx
testl %ebx, %ebx
jle 0x1b64a
movq %rdi, 0x40(%rsp)
leaq 0xe4fa(%rip), %rax      # 0x29710
movq $0x0, (%rax)
leaq 0xe4e4(%rip), %r15      # 0x29708
movq (%r15), %rdi
testq %rdi, %rdi
je 0x1b233
callq 0x73ad
jmp 0x1b224
movq 0x40(%rsp), %rax
addq $0x10, %rax
leaq 0x8(%rsp), %r15
movq %rax, (%r15)
xorl %eax, %eax
leaq 0x14(%rsp), %rcx
movl %eax, (%rcx)
leaq 0x24(%rsp), %rdx
movl %eax, (%rdx)
movq %rax, 0x38(%rsp)
movq %rax, 0x30(%rsp)
cmpl $0x2, %ebx
movl $0x1, %r13d
cmovgel %ebx, %r13d
movq %rcx, %rbx
xorl %r12d, %r12d
movq $0x0, 0x28(%rsp)
movq $0x0, 0x18(%rsp)
movq 0x8(%rsp), %rbp
leaq 0x60ce(%rip), %rdi      # 0x2135d
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1b678
testb %al, %al
je 0x1b2ac
leaq 0x10(%rbp), %rax
movq %rax, 0x8(%rsp)
movq %rbx, %rdx
jmp 0x1b2f8
leaq 0x60bb(%rip), %rdi      # 0x2136e
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1b678
testb %al, %al
je 0x1b2d2
leaq 0x10(%rbp), %rax
movq %rax, 0x8(%rsp)
leaq 0x24(%rsp), %rdx
jmp 0x1b2f8
leaq 0x60a6(%rip), %rdi      # 0x2137f
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1b678
testb %al, %al
je 0x1b324
leaq 0x10(%rbp), %rax
movq %rax, 0x8(%rsp)
leaq 0xe3fc(%rip), %rdx      # 0x296f4
movslq 0x14(%rbp), %rdi
addq %r14, %rdi
leaq 0x5315(%rip), %rsi      # 0x2061b
xorl %eax, %eax
callq 0x41c0
addq $0x10, 0x8(%rsp)
incl %r12d
cmpl %r13d, %r12d
jne 0x1b283
jmp 0x1b452
leaq 0x4eb8(%rip), %rdi      # 0x201e3
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1b678
testb %al, %al
je 0x1b38f
leaq 0x10(%rbp), %rax
movq %rax, 0x8(%rsp)
movl 0x1c(%rbp), %ebx
addq $0x20, %rbp
movq %rbp, 0x8(%rsp)
testl %ebx, %ebx
jle 0x1b388
movq %r15, %rdi
movq %r14, %rsi
callq 0x1b6c1
movq %rax, %rbp
cmpq $0x0, 0x58(%rax)
jne 0x1b37c
xorl %edi, %edi
xorl %esi, %esi
callq 0x8297
movq %rbp, %rdi
movq %rax, %rsi
callq 0x8389
movq %rbp, %rdi
callq 0x7261
decl %ebx
jne 0x1b353
leaq 0x14(%rsp), %rbx
jmp 0x1b313
leaq 0x5ff6(%rip), %rdi      # 0x2138c
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1b678
testb %al, %al
je 0x1b3d7
addq $0x10, %rbp
movq %rbp, 0x8(%rsp)
leaq 0xe35b(%rip), %rax      # 0x29710
cmpq $0x0, (%rax)
movq 0x18(%rsp), %rax
cmoveq %rbp, %rax
movq %rax, 0x18(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x1ba64
jmp 0x1b313
leaq 0x5fbb(%rip), %rdi      # 0x21399
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1b678
testb %al, %al
je 0x1b41f
addq $0x10, %rbp
movq %rbp, 0x8(%rsp)
leaq 0xe313(%rip), %rax      # 0x29710
cmpq $0x0, (%rax)
movq 0x28(%rsp), %rax
cmoveq %rbp, %rax
movq %rax, 0x28(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x1bae0
jmp 0x1b313
leaq 0x5f80(%rip), %rdi      # 0x213a6
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1b678
testb %al, %al
je 0x1b30d
addq $0x10, %rbp
movq %rbp, 0x8(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x1bb68
jmp 0x1b313
movq 0x28(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0x38(%rsp)
movl 0x14(%rsp), %edi
testl %edi, %edi
leaq 0xe295(%rip), %rbx      # 0x29708
je 0x1b492
leaq 0x48(%rsp), %rsi
callq 0x1b07b
testb %al, %al
je 0x1b492
movq 0x48(%rsp), %rax
leaq 0xe281(%rip), %rcx      # 0x29710
movq %rax, (%rcx)
movl 0x24(%rsp), %edi
testl %edi, %edi
je 0x1b4b7
leaq 0x48(%rsp), %rsi
callq 0x1b07b
testb %al, %al
je 0x1b4b7
movq 0x48(%rsp), %rax
leaq 0xe244(%rip), %rcx      # 0x296f8
movq %rax, (%rcx)
cmpq $0x0, 0x38(%rsp)
je 0x1b4cc
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0x1ba64
cmpq $0x0, 0x30(%rsp)
je 0x1b4e1
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x1bae0
movq %r14, 0x18(%rsp)
movq (%rbx), %r15
testq %r15, %r15
je 0x1b5b9
leaq 0xe1cf(%rip), %rbx      # 0x296c8
leaq 0xe8fa(%rip), %r14      # 0x29dfa
leaq 0x48(%rsp), %r12
movq (%rbx), %rdi
callq 0x4920
movl %eax, 0x20(%r15)
movq 0x60(%r15), %r13
testq %r13, %r13
je 0x1b5ac
movq (%rbx), %rdi
callq 0x4920
movl %eax, 0x20(%r13)
movq 0x30(%r13), %rdi
callq 0xce92
movq 0x30(%r13), %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0x1037f
movq %r13, %rdi
callq 0xd367
movq 0x30(%r13), %rdi
callq 0x92fd
movq %rax, %rbp
testq %rax, %rax
je 0x1b5a3
cmpq $0x0, 0x48(%rbp)
je 0x1b595
movq %rbp, %rdi
callq 0xa87d
movzbl (%r14), %eax
shll $0x4, %eax
orl $0x600000, %eax         # imm = 0x600000
movl %eax, 0x48(%rsp)
movq (%rbx), %rdi
movl (%rbp), %esi
movl $0x800, %edx            # imm = 0x800
movq %r12, %rcx
callq 0x4700
movl (%rbp), %edi
callq 0xed89
movq 0x30(%r13), %rsi
movq %rbp, %rdi
callq 0x9363
jmp 0x1b553
movq 0x48(%r13), %r13
jmp 0x1b515
movq 0x78(%r15), %r15
testq %r15, %r15
jne 0x1b505
callq 0x1de29
callq 0x1e0b4
callq 0x1e17a
callq 0x1df11
xorl %edi, %edi
callq 0x1e462
movl $0x1, %edi
callq 0x1e462
callq 0x1ddf2
movq 0x40(%rsp), %rdi
callq 0x4460
movq 0x18(%rsp), %rdi
callq 0x4460
movb $0x1, %bpl
jmp 0x1b667
leaq 0x5cf9(%rip), %rdi      # 0x212fc
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x4b50
cmpl $-0x2, %r13d
leaq 0x5d00(%rip), %rax      # 0x21317
leaq 0x3ca0(%rip), %rcx      # 0x1f2be
cmoveq %rax, %rcx
cmpl $-0x3, %r13d
leaq 0x5d17(%rip), %rdi      # 0x21344
cmovneq %rcx, %rdi
xorl %eax, %eax
callq 0x4b50
movq %rbx, %rdi
callq 0x4460
movq %r14, %rdi
callq 0x4460
jmp 0x1b667
callq 0x4460
jmp 0x1b65d
leaq 0x5c79(%rip), %rdi      # 0x212d1
callq 0x4a10
movq %r14, %rdi
callq 0x4460
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq