// File: /workspace/github_c_makefile_star2_O3/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 0x5d06
testq %rax, %rax
je 0x1b391
movq %rax, %r14
movl $0x1000, %edi           # imm = 0x1000
callq 0x4290
testq %rax, %rax
je 0x1b3a4
movq %rax, %r12
leaq 0x6c(%rsp), %r15
movq %r15, %rdi
callq 0x68bd
movl $0x100, %ebp            # imm = 0x100
movq %r12, %rbx
movq 0x60(%rsp), %rdx
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rcx
movl %ebp, %r8d
callq 0x6388
cmpl $-0x1, %eax
jne 0x1af48
addl %ebp, %ebp
movslq %ebp, %rsi
shlq $0x4, %rsi
movq %rbx, %rdi
callq 0x49f0
movq %rax, %r12
testq %rax, %rax
jne 0x1af00
leaq 0x635c(%rip), %rdi      # 0x2129a
callq 0x4a10
jmp 0x1b381
movl %eax, %r12d
testl %eax, %eax
js 0x1b333
movl 0xc(%rbx), %r13d
testl %r13d, %r13d
jle 0x1b381
movq %rbx, 0x30(%rsp)
leaq 0xe7a4(%rip), %rax      # 0x29710
movq $0x0, (%rax)
leaq 0xe78e(%rip), %rbx      # 0x29708
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x1af89
callq 0x72d2
jmp 0x1af7a
movq 0x30(%rsp), %rax
addq $0x10, %rax
leaq 0x8(%rsp), %r15
movq %rax, (%r15)
xorl %eax, %eax
leaq 0x14(%rsp), %rbx
movl %eax, (%rbx)
leaq 0x24(%rsp), %rcx
movl %eax, (%rcx)
movq %rax, 0x40(%rsp)
movq %rax, 0x38(%rsp)
xorl %r12d, %r12d
movq $0x0, 0x28(%rsp)
movq $0x0, 0x18(%rsp)
movq 0x8(%rsp), %rbp
leaq 0x633c(%rip), %rdi      # 0x21311
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1b3b2
testb %al, %al
je 0x1aff2
leaq 0x10(%rbp), %rax
movq %rax, 0x8(%rsp)
movq %rbx, %rdx
jmp 0x1b03e
leaq 0x6329(%rip), %rdi      # 0x21322
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1b3b2
testb %al, %al
je 0x1b018
leaq 0x10(%rbp), %rax
movq %rax, 0x8(%rsp)
leaq 0x24(%rsp), %rdx
jmp 0x1b03e
leaq 0x6314(%rip), %rdi      # 0x21333
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1b3b2
testb %al, %al
je 0x1b06a
leaq 0x10(%rbp), %rax
movq %rax, 0x8(%rsp)
leaq 0xe6b6(%rip), %rdx      # 0x296f4
movslq 0x14(%rbp), %rdi
addq %r14, %rdi
leaq 0x5583(%rip), %rsi      # 0x205cf
xorl %eax, %eax
callq 0x41c0
addq $0x10, 0x8(%rsp)
incl %r12d
cmpl %r13d, %r12d
jne 0x1afc9
jmp 0x1b18f
leaq 0x512c(%rip), %rdi      # 0x2019d
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1b3b2
testb %al, %al
je 0x1b0cc
movl 0x1c(%rbp), %ebx
addq $0x20, %rbp
movq %rbp, 0x8(%rsp)
testl %ebx, %ebx
jle 0x1b0c5
movq %r15, %rdi
movq %r14, %rsi
callq 0x1b3fb
movq %rax, %rbp
cmpq $0x0, 0x58(%rax)
jne 0x1b0b9
xorl %edi, %edi
xorl %esi, %esi
callq 0x813c
movq %rbp, %rdi
movq %rax, %rsi
callq 0x822e
movq %rbp, %rdi
callq 0x7186
decl %ebx
jne 0x1b090
leaq 0x14(%rsp), %rbx
jmp 0x1b059
leaq 0x626d(%rip), %rdi      # 0x21340
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1b3b2
testb %al, %al
je 0x1b114
addq $0x10, %rbp
movq %rbp, 0x8(%rsp)
leaq 0xe61e(%rip), %rax      # 0x29710
cmpq $0x0, (%rax)
movq 0x18(%rsp), %rax
cmoveq %rbp, %rax
movq %rax, 0x18(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x1b796
jmp 0x1b059
leaq 0x6232(%rip), %rdi      # 0x2134d
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1b3b2
testb %al, %al
je 0x1b15c
addq $0x10, %rbp
movq %rbp, 0x8(%rsp)
leaq 0xe5d6(%rip), %rax      # 0x29710
cmpq $0x0, (%rax)
movq 0x28(%rsp), %rax
cmoveq %rbp, %rax
movq %rax, 0x28(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x1b812
jmp 0x1b059
leaq 0x61f7(%rip), %rdi      # 0x2135a
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1b3b2
testb %al, %al
je 0x1b053
addq $0x10, %rbp
movq %rbp, 0x8(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x1b89d
jmp 0x1b059
movl 0x14(%rsp), %edi
movq 0x28(%rsp), %r15
movq %r15, 0x38(%rsp)
movq 0x18(%rsp), %r12
movq %r12, 0x40(%rsp)
testl %edi, %edi
leaq 0xe558(%rip), %rbx      # 0x29708
je 0x1b1cf
leaq 0x48(%rsp), %rsi
callq 0x1ae14
testb %al, %al
je 0x1b1cf
movq 0x48(%rsp), %rax
leaq 0xe544(%rip), %rcx      # 0x29710
movq %rax, (%rcx)
movl 0x24(%rsp), %edi
testl %edi, %edi
je 0x1b1f4
leaq 0x48(%rsp), %rsi
callq 0x1ae14
testb %al, %al
je 0x1b1f4
movq 0x48(%rsp), %rax
leaq 0xe507(%rip), %rcx      # 0x296f8
movq %rax, (%rcx)
testq %r12, %r12
je 0x1b206
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x1b796
testq %r15, %r15
je 0x1b218
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0x1b812
movq %r14, 0x18(%rsp)
movq (%rbx), %r15
testq %r15, %r15
je 0x1b2f0
leaq 0xe498(%rip), %rbx      # 0x296c8
leaq 0xebc3(%rip), %r14      # 0x29dfa
leaq 0x48(%rsp), %r12
movq (%rbx), %rdi
callq 0x4920
movl %eax, 0x20(%r15)
movq 0x60(%r15), %r13
testq %r13, %r13
je 0x1b2e3
movq (%rbx), %rdi
callq 0x4920
movl %eax, 0x20(%r13)
movq 0x30(%r13), %rdi
callq 0xcf7f
movq 0x30(%r13), %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0x103fa
movq %r13, %rdi
callq 0xd455
movq 0x30(%r13), %rdi
callq 0x920c
movq %rax, %rbp
testq %rax, %rax
je 0x1b2da
cmpq $0x0, 0x48(%rbp)
je 0x1b2cc
movq %rbp, %rdi
callq 0xa8b8
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 0xedf5
movq 0x30(%r13), %rsi
movq %rbp, %rdi
callq 0x926e
jmp 0x1b28a
movq 0x48(%r13), %r13
jmp 0x1b24c
movq 0x78(%r15), %r15
testq %r15, %r15
jne 0x1b23c
callq 0x1dab9
callq 0x1dcd6
callq 0x1dd9c
callq 0x1db67
xorl %edi, %edi
callq 0x1e08b
movl $0x1, %edi
callq 0x1e08b
callq 0x1da82
movq 0x30(%rsp), %rdi
callq 0x4460
movq 0x18(%rsp), %rdi
callq 0x4460
movb $0x1, %bpl
jmp 0x1b393
leaq 0x5f76(%rip), %rdi      # 0x212b0
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x4b50
cmpl $-0x2, %r12d
leaq 0x5f7d(%rip), %rax      # 0x212cb
leaq 0x3f69(%rip), %rcx      # 0x1f2be
cmoveq %rax, %rcx
cmpl $-0x3, %r12d
leaq 0x5f94(%rip), %rdi      # 0x212f8
cmovneq %rcx, %rdi
xorl %eax, %eax
callq 0x4b50
movq %rbx, %rdi
callq 0x4460
movq %r14, %rdi
callq 0x4460
jmp 0x1b393
movq %rbx, %rdi
callq 0x4460
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
leaq 0x5eda(%rip), %rdi      # 0x21285
callq 0x4a10
jmp 0x1b389