// File: /workspace/github_c_makefile_star2_O1/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 $0xb8, %rsp
movq %rdx, %r12
movq %rdi, %r14
movq $0x0, 0x10(%rdx)
movq 0x10(%rsi), %rax
movq %rax, 0x20(%rsp)
movups (%rsi), %xmm0
movaps %xmm0, 0x10(%rsp)
callq 0x4470
movq %r14, %rdi
movq %rax, %rsi
callq 0x5e6e
movq %rax, %r14
movq %rax, %rdi
movl $0x23, %esi
callq 0x4560
movq %rax, %rbp
movq %r14, %rdi
movl $0x40, %esi
callq 0x4560
movq %rax, %r15
movq %r14, %rdi
movl $0x3a, %esi
callq 0x4560
movq %rax, %r13
testq %rbp, %rbp
setne %al
testq %r13, %r13
setne %cl
andb %al, %cl
testq %r15, %r15
setne %al
cmpq %rbp, %r15
setb %dl
andb %al, %dl
andb %cl, %dl
cmpq %r13, %rbp
setb %al
andb %dl, %al
cmpb $0x1, %al
jne 0x1a4e9
cmpq %r14, %r15
jbe 0x1a4e7
cmpb $0x23, -0x1(%r15)
jne 0x1a4e7
decq %r15
movq %r15, %rbp
jmp 0x1a4e9
xorl %ebp, %ebp
testq %rbp, %rbp
je 0x1a535
movb $0x0, (%rbp)
leaq 0xf217(%rip), %rax      # 0x29710
movq (%rax), %rax
leaq 0x50(%rsp), %rsi
movq %rax, (%rsi)
movq 0x58(%rax), %rax
movq %rax, 0x8(%rsi)
movq 0x38(%rax), %rax
movq %rax, 0x10(%rsi)
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
callq 0x1a436
movl %eax, %r15d
testl %eax, %eax
je 0x1a53a
movq %r14, %rdi
callq 0x4460
movq %r14, %rbx
jmp 0x1a53e
movq %r14, %rbx
jmp 0x1a547
leaq 0x1(%rbp), %rbx
testl %r15d, %r15d
jne 0x1a7d7
movq %r12, 0x8(%rsp)
xorl %r12d, %r12d
cmpq %rbp, %r13
cmovaeq %r13, %r12
xorps %xmm0, %xmm0
leaq 0x50(%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 %r12, %r12
movq %r12, %rdi
cmoveq %rbx, %rdi
callq 0x17bf5
testb %al, %al
je 0x1a5b9
leaq 0x34(%rsp), %rsi
movq %rbx, %rdi
callq 0x16be3
testb %al, %al
je 0x1a5cc
movl 0x34(%rsp), %edx
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rcx
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0xafec
jmp 0x1a7c3
movq %r14, %rdi
callq 0x4460
movl $0x2, %r15d
jmp 0x1a7d7
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0x16c60
testb %al, %al
je 0x1a5fd
movl 0x30(%rsp), %edx
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rcx
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0xc063
jmp 0x1a7c3
leaq 0x2c(%rsp), %rsi
movq %rbx, %rdi
callq 0x16cf2
testb %al, %al
je 0x1a62e
movl 0x2c(%rsp), %edi
leaq 0x10(%rsp), %rsi
leaq 0x50(%rsp), %rcx
movq 0x8(%rsp), %rbx
movq %rbx, %rdx
callq 0xd8cd
jmp 0x1a7c3
leaq 0x6480(%rip), %rdi      # 0x20ab5
movq %rbx, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a73d
leaq 0x6c2b(%rip), %rdi      # 0x21277
movq %rbx, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a756
leaq 0x6c23(%rip), %rdi      # 0x21286
movq %rbx, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a76f
leaq 0x6c11(%rip), %rdi      # 0x2128b
movq %rbx, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a780
leaq 0x6c01(%rip), %rdi      # 0x21292
movq %rbx, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a799
leaq 0x6bf2(%rip), %rdi      # 0x2129a
movq %rbx, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a7a7
leaq 0x6be4(%rip), %rdi      # 0x212a3
movq %rbx, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a7ec
leaq 0x65ab(%rip), %rdi      # 0x20c81
movq %rbx, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a834
cmpb $0x40, (%rbx)
jne 0x1a888
incq %rbx
movq 0x20(%rsp), %rax
movq 0x8(%rsp), %rdx
movq %rax, 0x10(%rdx)
movaps 0x10(%rsp), %xmm0
movups %xmm0, (%rdx)
testq %r12, %r12
je 0x1a909
movb $0x0, (%r12)
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x19fc7
movl %eax, %ebp
testl %eax, %eax
je 0x1a8ea
movq %r14, %rdi
callq 0x4460
movl %ebp, %r15d
jmp 0x1a901
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0xae75
jmp 0x1a7c3
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0xaf76
jmp 0x1a7c3
leaq 0x10(%rsp), %rsi
leaq 0x50(%rsp), %rcx
xorl %edi, %edi
jmp 0x1a61c
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0xd857
jmp 0x1a7c3
leaq 0x10(%rsp), %rsi
leaq 0x50(%rsp), %rcx
xorl %edi, %edi
jmp 0x1a7b6
leaq 0x10(%rsp), %rsi
leaq 0x50(%rsp), %rcx
movl $0x1, %edi
movq 0x8(%rsp), %rbx
movq %rbx, %rdx
callq 0xb168
movq %r14, %rdi
callq 0x4460
xorl %r15d, %r15d
cmpq $0x0, 0x10(%rbx)
sete %r15b
movl %r15d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x38(%rsp), %r14
movl $0x0, (%r14)
xorl %r15d, %r15d
movq %r14, %rdi
xorl %esi, %esi
callq 0x10928
movl (%r14), %edi
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x1ad6b
testb %al, %al
je 0x1a8df
leaq 0x10(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movq %rbx, %rdi
callq 0x192cf
jmp 0x1a8d7
leaq 0xeed5(%rip), %rax      # 0x29710
movq (%rax), %rax
leaq 0x38(%rsp), %rdi
movq %rax, (%rdi)
movq 0x58(%rax), %rax
movq %rax, 0x8(%rdi)
movq 0x38(%rax), %rax
movq %rax, 0x10(%rdi)
leaq 0x10(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x192cf
testb %al, %al
movq 0x8(%rsp), %rbx
je 0x1a7c3
movq 0x48(%rsp), %rax
movq %rax, 0x10(%rbx)
movups 0x38(%rsp), %xmm0
movups %xmm0, (%rbx)
jmp 0x1a7c3
leaq 0x38(%rsp), %rsi
movq %rbx, %rdi
callq 0x17346
movl %eax, %ebp
movq %r14, %rdi
callq 0x4460
movl $0x3, %r15d
testb %bpl, %bpl
je 0x1a7d7
movl 0x38(%rsp), %edi
movq 0x8(%rsp), %rsi
callq 0xadc2
testb %al, %al
je 0x1a8df
leaq 0x10(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movq 0x8(%rsp), %rdi
callq 0x192cf
xorl %r15d, %r15d
testb %al, %al
jne 0x1a7d7
movl $0x1, %r15d
jmp 0x1a7d7
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x38(%rax), %rax
movq %rax, 0x10(%rcx)
incq %r12
movq %r12, %rbx
testl %ebp, %ebp
jne 0x1a7d7
cmpb $0x2f, (%rbx)
jne 0x1a91f
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x30(%rax), %rax
movq %rax, 0x10(%rcx)
leaq 0x6985(%rip), %rsi      # 0x212ab
movq %rbx, %rdi
callq 0x4850
testq %rax, %rax
je 0x1aa53
movq %rax, %rbp
leaq 0x696c(%rip), %rbx      # 0x212ad
leaq 0x6963(%rip), %r13      # 0x212ab
movq 0x8(%rsp), %rax
movq 0x10(%rax), %r12
testq %r12, %r12
je 0x1aa53
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a9fa
leaq 0x612b(%rip), %rdi      # 0x20a9f
movq %rbp, %rsi
callq 0x4820
testl %eax, %eax
je 0x1a9fa
leaq 0x692c(%rip), %rdi      # 0x212b3
movq %rbp, %rsi
callq 0x4820
testl %eax, %eax
je 0x1aa01
leaq 0x610a(%rip), %rdi      # 0x20aa4
movq %rbp, %rsi
callq 0x4820
testl %eax, %eax
je 0x1aa01
leaq 0x690d(%rip), %rdi      # 0x212ba
movq %rbp, %rsi
callq 0x4820
testl %eax, %eax
je 0x1aa27
leaq 0x6901(%rip), %rdi      # 0x212c1
movq %rbp, %rsi
callq 0x4820
testl %eax, %eax
je 0x1aa2e
movq %rbx, %r12
movq %rbp, %rdi
leaq 0x38(%rsp), %rsi
callq 0x16be3
movl %eax, %ebp
testb %al, %al
je 0x1aa38
movq 0x8(%rsp), %rbx
movq 0x10(%rbx), %rdi
movl 0x38(%rsp), %esi
callq 0xacd5
movq %rax, 0x10(%rbx)
jmp 0x1aa46
movq 0x30(%r12), %rax
jmp 0x1aa06
movq 0x38(%r12), %rax
movq 0x8(%rsp), %rcx
movq %rax, 0x10(%rcx)
xorl %edi, %edi
movq %r13, %rsi
callq 0x4850
movq %rax, %rbp
testq %rax, %rax
jne 0x1a948
jmp 0x1aa53
movq 0x40(%r12), %rax
jmp 0x1aa06
movq %r12, %rdi
callq 0xaaf8
jmp 0x1aa06
movq %r14, %rdi
callq 0x4460
movl $0x3, %r15d
testb %bpl, %bpl
movq %r12, %rbx
jne 0x1aa0f
jmp 0x1a7d7
movq %r14, %rdi
callq 0x4460
movq 0x8(%rsp), %rdi
cmpq $0x0, 0x10(%rdi)
je 0x1aa81
leaq 0x10(%rsp), %rsi
leaq 0x50(%rsp), %rdx
callq 0x192cf
xorb $0x1, %al
movzbl %al, %r15d
jmp 0x1a7d7
movq 0x8(%rdi), %rax
xorl %r15d, %r15d
cmpq $0x0, 0x30(%rax)
setne %r15b
jmp 0x1a7d7