// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x6560
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x26c40
movb $0x0, -0x1(%rbp)
jmp 0x2731a
movl $0x100, -0x24(%rbp)     # imm = 0x100
movslq -0x24(%rbp), %rdi
shlq $0x4, %rdi
callq 0x42a0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jne 0x26c7d
leaq 0x87d9(%rip), %rdi      # 0x2f43f
callq 0x49e0
movq -0x20(%rbp), %rdi
callq 0x4450
movb $0x0, -0x1(%rbp)
jmp 0x2731a
leaq -0x30(%rbp), %rdi
callq 0x7b80
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x38(%rbp), %rcx
movl -0x24(%rbp), %r8d
leaq -0x30(%rbp), %rdi
callq 0x7210
movl %eax, -0x3c(%rbp)
cmpl $-0x1, %eax
jne 0x26cfc
movl -0x24(%rbp), %eax
shll %eax
movl %eax, -0x24(%rbp)
movq -0x38(%rbp), %rdi
movslq -0x24(%rbp), %rsi
shlq $0x4, %rsi
callq 0x49c0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x26cf2
leaq 0x8782(%rip), %rdi      # 0x2f454
callq 0x49e0
movq -0x38(%rbp), %rdi
callq 0x4450
movq -0x20(%rbp), %rdi
callq 0x4450
movb $0x0, -0x1(%rbp)
jmp 0x2731a
movq -0x48(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x26c86
cmpl $0x0, -0x3c(%rbp)
jge 0x26d97
leaq 0x875d(%rip), %rdi      # 0x2f46a
xorl %eax, %eax
callq 0x6410
movl -0x3c(%rbp), %eax
movl %eax, -0xdc(%rbp)
subl $-0x3, %eax
je 0x26d5e
jmp 0x26d24
movl -0xdc(%rbp), %eax
subl $-0x2, %eax
je 0x26d4e
jmp 0x26d31
movl -0xdc(%rbp), %eax
subl $-0x1, %eax
jne 0x26d6e
jmp 0x26d3e
leaq 0x65b2(%rip), %rdi      # 0x2d2f7
movb $0x0, %al
callq 0x6410
jmp 0x26d7c
leaq 0x8730(%rip), %rdi      # 0x2f485
movb $0x0, %al
callq 0x6410
jmp 0x26d7c
leaq 0x874d(%rip), %rdi      # 0x2f4b2
movb $0x0, %al
callq 0x6410
jmp 0x26d7c
leaq 0x65f2(%rip), %rdi      # 0x2d367
movb $0x0, %al
callq 0x6410
movq -0x38(%rbp), %rdi
callq 0x4450
movq -0x20(%rbp), %rdi
callq 0x4450
movb $0x0, -0x1(%rbp)
jmp 0x2731a
movq -0x38(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x4c(%rbp)
cmpl $0x1, -0x4c(%rbp)
jge 0x26dc2
movq -0x38(%rbp), %rdi
callq 0x4450
movq -0x20(%rbp), %rdi
callq 0x4450
movb $0x0, -0x1(%rbp)
jmp 0x2731a
leaq 0xe937(%rip), %rax      # 0x35700
movq $0x0, (%rax)
leaq 0xe921(%rip), %rax      # 0x356f8
cmpq $0x0, (%rax)
je 0x26dee
leaq 0xe914(%rip), %rax      # 0x356f8
movq (%rax), %rdi
callq 0x8e00
jmp 0x26dd0
movq -0x38(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x270b8
movq -0x58(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq 0x8691(%rip), %rdi      # 0x2f4cb
callq 0x27330
testb $0x1, %al
jne 0x26e45
jmp 0x26e77
movq -0x58(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rdi
movq -0x58(%rbp), %rax
movslq 0x4(%rax), %rax
addq %rax, %rdi
leaq 0x7944(%rip), %rsi      # 0x2e7ab
leaq -0x5c(%rbp), %rdx
movb $0x0, %al
callq 0x41c0
jmp 0x2709e
movq -0x58(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq 0x8656(%rip), %rdi      # 0x2f4dc
callq 0x27330
testb $0x1, %al
jne 0x26e91
jmp 0x26ec3
movq -0x58(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rdi
movq -0x58(%rbp), %rax
movslq 0x4(%rax), %rax
addq %rax, %rdi
leaq 0x78f8(%rip), %rsi      # 0x2e7ab
leaq -0x60(%rbp), %rdx
movb $0x0, %al
callq 0x41c0
jmp 0x2709c
movq -0x58(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq 0x861b(%rip), %rdi      # 0x2f4ed
callq 0x27330
testb $0x1, %al
jne 0x26edd
jmp 0x26f12
movq -0x58(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rdi
movq -0x58(%rbp), %rax
movslq 0x4(%rax), %rax
addq %rax, %rdi
leaq 0x78ac(%rip), %rsi      # 0x2e7ab
leaq 0xe7de(%rip), %rdx      # 0x356e4
movb $0x0, %al
callq 0x41c0
jmp 0x2709a
movq -0x58(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq 0x7452(%rip), %rdi      # 0x2e373
callq 0x27330
testb $0x1, %al
jne 0x26f2f
jmp 0x26fc6
movq -0x58(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x78(%rbp)
movq -0x58(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x78(%rbp), %eax
jge 0x26fc1
movq -0x20(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x273a0
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpq $0x0, 0x58(%rax)
jne 0x26faa
movq -0x88(%rbp), %rax
movq %rax, -0xe8(%rbp)
xorl %eax, %eax
movl %eax, %edi
xorl %esi, %esi
callq 0xa460
movq -0xe8(%rbp), %rdi
movq %rax, %rsi
callq 0xa610
movq -0x88(%rbp), %rdi
callq 0x8bc0
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x26f58
jmp 0x270aa
movq -0x58(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq 0x8525(%rip), %rdi      # 0x2f4fa
callq 0x27330
testb $0x1, %al
jne 0x26fe0
jmp 0x27013
movq -0x58(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x58(%rbp)
leaq 0xe70d(%rip), %rax      # 0x35700
cmpq $0x0, (%rax)
jne 0x27001
movq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x278d0
jmp 0x270aa
movq -0x58(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq 0x84e5(%rip), %rdi      # 0x2f507
callq 0x27330
testb $0x1, %al
jne 0x2702d
jmp 0x2705d
movq -0x58(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x58(%rbp)
leaq 0xe6c0(%rip), %rax      # 0x35700
cmpq $0x0, (%rax)
jne 0x2704e
movq -0x58(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x27970
jmp 0x270aa
movq -0x58(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq 0x84a8(%rip), %rdi      # 0x2f514
callq 0x27330
testb $0x1, %al
jne 0x27077
jmp 0x27092
movq -0x58(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x27a20
jmp 0x270aa
jmp 0x27094
jmp 0x27096
jmp 0x27098
jmp 0x2709a
jmp 0x2709c
jmp 0x2709e
movq -0x58(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x58(%rbp)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x26e1f
cmpl $0x0, -0x5c(%rbp)
je 0x270e6
movl -0x5c(%rbp), %edi
leaq -0xa0(%rbp), %rsi
callq 0x26ab0
testb $0x1, %al
jne 0x270d3
jmp 0x270e4
movq -0xa0(%rbp), %rcx
leaq 0xe61f(%rip), %rax      # 0x35700
movq %rcx, (%rax)
jmp 0x270e6
cmpl $0x0, -0x60(%rbp)
je 0x27114
movl -0x60(%rbp), %edi
leaq -0xb8(%rbp), %rsi
callq 0x26ab0
testb $0x1, %al
jne 0x27101
jmp 0x27112
movq -0xb8(%rbp), %rcx
leaq 0xe5d9(%rip), %rax      # 0x356e8
movq %rcx, (%rax)
jmp 0x27114
cmpq $0x0, -0x68(%rbp)
je 0x27128
movq -0x20(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x278d0
cmpq $0x0, -0x70(%rbp)
je 0x2713c
movq -0x20(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x27970
leaq 0xe5b5(%rip), %rax      # 0x356f8
movq (%rax), %rax
movq %rax, -0xc0(%rbp)
cmpq $0x0, -0xc0(%rbp)
je 0x272da
leaq 0xe556(%rip), %rax      # 0x356b8
movq (%rax), %rdi
callq 0x4900
movl %eax, %ecx
movq -0xc0(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0xc0(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, -0xc8(%rbp)
je 0x272c1
leaq 0xe51b(%rip), %rax      # 0x356b8
movq (%rax), %rdi
callq 0x4900
movl %eax, %ecx
movq -0xc8(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0xc8(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x11ae0
movq -0xc0(%rbp), %rdi
movq -0xc8(%rbp), %rsi
movq -0xc8(%rbp), %rax
movq 0x30(%rax), %rdx
callq 0x16e00
movq -0xc8(%rbp), %rdi
callq 0x12280
movq -0xc8(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0xbe70
movq %rax, -0xd0(%rbp)
cmpq $0x0, -0xd0(%rbp)
je 0x272a8
movq -0xd0(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jne 0x27220
jmp 0x27285
movq -0xd0(%rbp), %rdi
callq 0xe010
leaq 0xebb7(%rip), %rax      # 0x35dea
movb (%rax), %dl
xorl %eax, %eax
movl $0x10, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
orl $0x600000, %eax         # imm = 0x600000
movl %eax, -0xd4(%rbp)
leaq 0xe464(%rip), %rax      # 0x356b8
movq (%rax), %rdi
movq -0xd0(%rbp), %rax
movl (%rax), %esi
leaq -0xd4(%rbp), %rcx
movl $0x800, %edx            # imm = 0x800
callq 0x46e0
movl %eax, -0xd8(%rbp)
movq -0xd0(%rbp), %rax
movl (%rax), %edi
callq 0x14ce0
movq -0xd0(%rbp), %rdi
movq -0xc8(%rbp), %rax
movq 0x30(%rax), %rsi
callq 0xbf80
movq %rax, -0xd0(%rbp)
jmp 0x27202
jmp 0x272aa
movq -0xc8(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0xc8(%rbp)
jmp 0x27188
jmp 0x272c3
movq -0xc0(%rbp), %rax
movq 0x78(%rax), %rax
movq %rax, -0xc0(%rbp)
jmp 0x2714d
callq 0x2acc0
callq 0x2afe0
callq 0x2b1a0
callq 0x2ae60
xorl %edi, %edi
callq 0x2b630
movl $0x1, %edi
callq 0x2b630
callq 0x2ac40
movq -0x38(%rbp), %rdi
callq 0x4450
movq -0x20(%rbp), %rdi
callq 0x4450
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xf0, %rsp
popq %rbp
retq
nopl (%rax,%rax)