// File: /workspace/github_c_makefile_star2_O3/baskerville[P]bspwm/src/query.c
// Function: node_from_desc
int node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
{
	dst->node = NULL;

	coordinates_t ref_copy = *ref;
	ref = &ref_copy;
	char *desc_copy = copy_string(desc, strlen(desc));
	desc = desc_copy;

	char *hash = strrchr(desc, '#');
	char *path = strrchr(desc, '@');
	char *colon = strrchr(desc, ':');

	/* Adjust or discard hashes inside a DESKTOP_SEL, e.g. `newest#@prev#older:/1/2` */
	if (hash != NULL && colon != NULL && path != NULL &&
	    path < hash && hash < colon) {
		if (path > desc && *(path - 1) == '#') {
			hash = path - 1;
		} else {
			hash = NULL;
		}
	}

	if (hash != NULL) {
		*hash = '\0';
		int ret;
		coordinates_t tmp = {mon, mon->desk, mon->desk->focus};
		if ((ret = node_from_desc(desc, &tmp, ref)) == SELECTOR_OK) {
			desc = hash + 1;
		} else {
			free(desc_copy);
			return ret;
		}
	}

	/* Discard colons within references, e.g. `@next.occupied:/#any.descendant_of.window` */
	if (colon != NULL && hash != NULL && colon < hash) {
		colon = NULL;
	}

	node_select_t sel = make_node_select();

	if (!parse_node_modifiers(colon != NULL ? colon : desc, &sel)) {
		free(desc_copy);
		return SELECTOR_BAD_MODIFIERS;
	}

	direction_t dir;
	cycle_dir_t cyc;
	history_dir_t hdi;
	if (parse_direction(desc, &dir)) {
		find_nearest_neighbor(ref, dst, dir, &sel);
	} else if (parse_cycle_direction(desc, &cyc)) {
		find_closest_node(ref, dst, cyc, &sel);
	} else if (parse_history_direction(desc, &hdi)) {
		history_find_node(hdi, ref, dst, &sel);
	} else if (streq("any", desc)) {
		find_any_node(ref, dst, &sel);
	} else if (streq("first_ancestor", desc)) {
		find_first_ancestor(ref, dst, &sel);
	} else if (streq("last", desc)) {
		history_find_node(HISTORY_OLDER, ref, dst, &sel);
	} else if (streq("newest", desc)) {
		history_find_newest_node(ref, dst, &sel);
	} else if (streq("biggest", desc)) {
		find_by_area(AREA_BIGGEST, ref, dst, &sel);
	} else if (streq("smallest", desc)) {
		find_by_area(AREA_SMALLEST, ref, dst, &sel);
	} else if (streq("pointed", desc)) {
		xcb_window_t win = XCB_NONE;
		query_pointer(&win, NULL);
		if (locate_leaf(win, dst) && node_matches(dst, ref, &sel)) {
			return SELECTOR_OK;
		} else {
			return SELECTOR_INVALID;
		}
	} else if (streq("focused", desc)) {
		coordinates_t loc = {mon, mon->desk, mon->desk->focus};
		if (node_matches(&loc, ref, &sel)) {
			*dst = loc;
		}
	} else if (*desc == '@') {
		desc++;
		*dst = *ref;
		if (colon != NULL) {
			*colon = '\0';
			int ret;
			if ((ret = desktop_from_desc(desc, ref, dst)) == SELECTOR_OK) {
				dst->node = dst->desktop->focus;
				desc = colon + 1;
			} else {
				free(desc_copy);
				return ret;
			}
		}
		if (*desc == '/') {
			dst->node = dst->desktop->root;
		}
		char *move = strtok(desc, PTH_TOK);
		while (move != NULL && dst->node != NULL) {
			if (streq("first", move) || streq("1", move)) {
				dst->node = dst->node->first_child;
			} else if (streq("second", move) || streq("2", move)) {
				dst->node = dst->node->second_child;
			} else if (streq("parent", move)) {
				dst->node = dst->node->parent;
			} else if (streq("brother", move)) {
				dst->node = brother_tree(dst->node);
			} else {
				direction_t dir;
				if (parse_direction(move, &dir)) {
					dst->node = find_fence(dst->node, dir);
				} else {
					free(desc_copy);
					return SELECTOR_BAD_DESCRIPTOR;
				}
			}
			move = strtok(NULL, PTH_TOK);
		}
		free(desc_copy);
		if (dst->node != NULL) {
			if (node_matches(dst, ref, &sel)) {
				return SELECTOR_OK;
			} else {
				return SELECTOR_INVALID;
			}
		} else if (dst->desktop->root != NULL) {
			return SELECTOR_INVALID;
		}
		return SELECTOR_OK;
	} else {
		uint32_t id;
		if (parse_id(desc, &id)) {
			free(desc_copy);
			if (find_by_id(id, dst) && node_matches(dst, ref, &sel)) {
				return SELECTOR_OK;
			} else {
				return SELECTOR_INVALID;
			}
		} else {
			free(desc_copy);
			return SELECTOR_BAD_DESCRIPTOR;
		}
	}

	free(desc_copy);

	if (dst->node == NULL) {
		return SELECTOR_INVALID;
	}

	return SELECTOR_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdx, %rbx
movq %rdi, %r14
movq $0x0, 0x10(%rdx)
movq 0x10(%rsi), %rax
movq %rax, 0x10(%rsp)
movups (%rsi), %xmm0
movaps %xmm0, (%rsp)
callq 0x4470
movq %r14, %rdi
movq %rax, %rsi
callq 0x5e2f
movq %rax, %r14
movq %rax, %rdi
movl $0x23, %esi
callq 0x4560
movq %rax, %r13
movq %r14, %rdi
movl $0x40, %esi
callq 0x4560
movq %rax, %r15
movq %r14, %rdi
movl $0x3a, %esi
callq 0x4560
movq %rax, %r12
testq %r13, %r13
setne %al
testq %r12, %r12
setne %cl
andb %al, %cl
testq %r15, %r15
setne %al
cmpq %r13, %r15
setb %dl
andb %al, %dl
andb %cl, %dl
cmpq %r12, %r13
setb %al
andb %dl, %al
cmpb $0x1, %al
jne 0x1a39d
cmpq %r14, %r15
jbe 0x1a3e8
cmpb $0x23, -0x1(%r15)
jne 0x1a3e8
decq %r15
movq %r15, %r13
jmp 0x1a3a2
testq %r13, %r13
je 0x1a3e8
movb $0x0, (%r13)
leaq 0xf362(%rip), %rax      # 0x29710
movq (%rax), %rax
leaq 0x40(%rsp), %rsi
movq %rax, (%rsi)
movq 0x58(%rax), %rax
movq %rax, 0x8(%rsi)
movq 0x38(%rax), %rax
movq %rax, 0x10(%rsi)
movq %rsp, %rdx
movq %r14, %rdi
callq 0x1a2ed
testl %eax, %eax
je 0x1a3f2
movl %eax, %r15d
movq %r14, %rdi
callq 0x4460
jmp 0x1a762
xorl %eax, %eax
xorl %r13d, %r13d
movq %r14, %r15
jmp 0x1a3f8
leaq 0x1(%r13), %r15
movb $0x1, %al
xorl %ebp, %ebp
cmpq %r13, %r12
cmovaeq %r12, %rbp
testb %al, %al
cmoveq %r12, %rbp
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rsi
movaps %xmm0, 0x50(%rsi)
movaps %xmm0, 0x40(%rsi)
movaps %xmm0, 0x30(%rsi)
movaps %xmm0, 0x20(%rsi)
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
testq %rbp, %rbp
movq %rbp, %rdi
cmoveq %r15, %rdi
callq 0x17ad4
testb %al, %al
je 0x1a463
leaq 0x24(%rsp), %rsi
movq %r15, %rdi
callq 0x16ab6
testb %al, %al
je 0x1a476
movl 0x24(%rsp), %edx
movq %rsp, %rdi
leaq 0x40(%rsp), %rcx
movq %rbx, %rsi
callq 0xaf9a
jmp 0x1a74e
movq %r14, %rdi
callq 0x4460
movl $0x2, %r15d
jmp 0x1a762
leaq 0x20(%rsp), %rsi
movq %r15, %rdi
callq 0x16b33
testb %al, %al
je 0x1a4a0
movl 0x20(%rsp), %edx
movq %rsp, %rdi
leaq 0x40(%rsp), %rcx
movq %rbx, %rsi
callq 0xc0b2
jmp 0x1a74e
leaq 0x1c(%rsp), %rsi
movq %r15, %rdi
callq 0x16bc5
testb %al, %al
je 0x1a4ca
movl 0x1c(%rsp), %edi
movq %rsp, %rsi
leaq 0x40(%rsp), %rcx
movq %rbx, %rdx
callq 0xd96f
jmp 0x1a74e
leaq 0x6598(%rip), %rdi      # 0x20a69
movq %r15, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a6e8
leaq 0x6d43(%rip), %rdi      # 0x2122b
movq %r15, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a6fa
leaq 0x6d3b(%rip), %rdi      # 0x2123a
movq %r15, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a70c
leaq 0x6d29(%rip), %rdi      # 0x2123f
movq %r15, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a71b
leaq 0x6d19(%rip), %rdi      # 0x21246
movq %r15, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a72d
leaq 0x6d0a(%rip), %rdi      # 0x2124e
movq %r15, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a739
leaq 0x6cfc(%rip), %rdi      # 0x21257
movq %r15, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a777
leaq 0x66c3(%rip), %rdi      # 0x20c35
movq %r15, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a7b8
cmpb $0x40, (%r15)
jne 0x1a805
incq %r15
movq 0x10(%rsp), %rax
movq %rax, 0x10(%rbx)
movaps (%rsp), %xmm0
movups %xmm0, (%rbx)
testq %rbp, %rbp
je 0x1a5d0
movb $0x0, (%rbp)
movq %rsp, %rsi
movq %r15, %rdi
movq %rbx, %rdx
callq 0x19eaf
testl %eax, %eax
jne 0x1a3d8
movq 0x8(%rbx), %rax
movq 0x38(%rax), %rax
movq %rax, 0x10(%rbx)
incq %rbp
movq %rbp, %r15
cmpb $0x2f, (%r15)
jne 0x1a5e2
movq 0x8(%rbx), %rax
movq 0x30(%rax), %rax
movq %rax, 0x10(%rbx)
leaq 0x6c76(%rip), %rsi      # 0x2125f
movq %r15, %rdi
callq 0x4850
testq %rax, %rax
je 0x1a861
movq %rax, %rbp
leaq 0x6c5b(%rip), %r12      # 0x2125f
movq 0x10(%rbx), %r13
testq %r13, %r13
je 0x1a861
leaq 0x6c49(%rip), %rdi      # 0x21261
movq %rbp, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a6af
movzbl (%rbp), %r15d
movl %r15d, %eax
subl $0x31, %eax
jne 0x1a639
movzbl 0x1(%rbp), %eax
negl %eax
testl %eax, %eax
je 0x1a6af
leaq 0x6c21(%rip), %rdi      # 0x21267
movq %rbp, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a6b5
subl $0x32, %r15d
jne 0x1a65d
movzbl 0x1(%rbp), %r15d
negl %r15d
testl %r15d, %r15d
je 0x1a6b5
leaq 0x6c02(%rip), %rdi      # 0x2126e
movq %rbp, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a6d8
leaq 0x6bf6(%rip), %rdi      # 0x21275
movq %rbp, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a6de
movq %rbp, %rdi
leaq 0x28(%rsp), %rsi
callq 0x16ab6
testb %al, %al
je 0x1a8a0
movq 0x10(%rbx), %rdi
movl 0x28(%rsp), %esi
callq 0xaccc
jmp 0x1a6b9
movq 0x30(%r13), %rax
jmp 0x1a6b9
movq 0x38(%r13), %rax
movq %rax, 0x10(%rbx)
xorl %edi, %edi
movq %r12, %rsi
callq 0x4850
movq %rax, %rbp
testq %rax, %rax
jne 0x1a604
jmp 0x1a861
movq 0x40(%r13), %rax
jmp 0x1a6b9
movq %r13, %rdi
callq 0xab3a
jmp 0x1a6b9
movq %rsp, %rdi
leaq 0x40(%rsp), %rdx
movq %rbx, %rsi
callq 0xae2b
jmp 0x1a74e
movq %rsp, %rdi
leaq 0x40(%rsp), %rdx
movq %rbx, %rsi
callq 0xaf2c
jmp 0x1a74e
movq %rsp, %rsi
leaq 0x40(%rsp), %rcx
xorl %edi, %edi
jmp 0x1a4bd
movq %rsp, %rdi
leaq 0x40(%rsp), %rdx
movq %rbx, %rsi
callq 0xd904
jmp 0x1a74e
movq %rsp, %rsi
leaq 0x40(%rsp), %rcx
xorl %edi, %edi
jmp 0x1a746
movq %rsp, %rsi
leaq 0x40(%rsp), %rcx
movl $0x1, %edi
movq %rbx, %rdx
callq 0xb14b
movq %r14, %rdi
callq 0x4460
xorl %r15d, %r15d
cmpq $0x0, 0x10(%rbx)
sete %r15b
movl %r15d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x28(%rsp), %r14
movl $0x0, (%r14)
xorl %r15d, %r15d
movq %r14, %rdi
xorl %esi, %esi
callq 0x1095f
movl (%r14), %edi
movq %rbx, %rsi
callq 0x1ab94
testb %al, %al
je 0x1a856
movq %rsp, %rsi
leaq 0x40(%rsp), %rdx
movq %rbx, %rdi
callq 0x191e4
jmp 0x1a84e
leaq 0xef51(%rip), %rax      # 0x29710
movq (%rax), %rax
leaq 0x28(%rsp), %rdi
movq %rax, (%rdi)
movq 0x58(%rax), %rax
movq %rax, 0x8(%rdi)
movq 0x38(%rax), %rax
movq %rax, 0x10(%rdi)
movq %rsp, %rsi
leaq 0x40(%rsp), %rdx
callq 0x191e4
testb %al, %al
je 0x1a74e
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rbx)
movups 0x28(%rsp), %xmm0
movups %xmm0, (%rbx)
jmp 0x1a74e
leaq 0x28(%rsp), %rsi
movq %r15, %rdi
callq 0x17217
movl %eax, %ebp
movq %r14, %rdi
callq 0x4460
movl $0x3, %r15d
testb %bpl, %bpl
je 0x1a762
movl 0x28(%rsp), %edi
movq %rbx, %rsi
callq 0xad91
testb %al, %al
je 0x1a856
movq %rsp, %rsi
leaq 0x40(%rsp), %rdx
movq %rbx, %rdi
callq 0x191e4
xorl %r15d, %r15d
testb %al, %al
jne 0x1a762
movl $0x1, %r15d
jmp 0x1a762
movq %r14, %rdi
callq 0x4460
cmpq $0x0, 0x10(%rbx)
je 0x1a88b
movq %rsp, %rsi
leaq 0x40(%rsp), %rdx
movq %rbx, %rdi
callq 0x191e4
xorb $0x1, %al
movzbl %al, %r15d
jmp 0x1a762
movq 0x8(%rbx), %rax
xorl %r15d, %r15d
cmpq $0x0, 0x30(%rax)
setne %r15b
jmp 0x1a762
movq %r14, %rdi
callq 0x4460
movl $0x3, %r15d
jmp 0x1a762