// File: /workspace/github_c_makefile_star2_O2/baskerville[P]bspwm/src/tree.c
// Function: swap_nodes
bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2, bool follow)
{
	if (n1 == NULL || n2 == NULL || n1 == n2 || is_descendant(n1, n2) || is_descendant(n2, n1) ||
	    (d1 != d2 && ((m1->sticky_count > 0 && sticky_count(n1) > 0) ||
	                  (m2->sticky_count > 0 && sticky_count(n2) > 0)))) {
		return false;
	}

	put_status(SBSC_MASK_NODE_SWAP, "node_swap 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n", m1->id, d1->id, n1->id, m2->id, d2->id, n2->id);

	node_t *pn1 = n1->parent;
	node_t *pn2 = n2->parent;
	bool n1_first_child = is_first_child(n1);
	bool n2_first_child = is_first_child(n2);
	bool n1_held_focus = is_descendant(d1->focus, n1);
	bool n2_held_focus = is_descendant(d2->focus, n2);
	node_t *last_d1_focus = d1->focus;
	node_t *last_d2_focus = d2->focus;

	if (pn1 != NULL) {
		if (n1_first_child) {
			pn1->first_child = n2;
		} else {
			pn1->second_child = n2;
		}
	}

	if (pn2 != NULL) {
		if (n2_first_child) {
			pn2->first_child = n1;
		} else {
			pn2->second_child = n1;
		}
	}

	n1->parent = pn2;
	n2->parent = pn1;

	propagate_flags_upward(m2, d2, n1);
	propagate_flags_upward(m1, d1, n2);

	if (d1 != d2) {
		if (d1->root == n1) {
			d1->root = n2;
		}

		if (d2->root == n2) {
			d2->root = n1;
		}

		if (n1_held_focus) {
			d1->focus = n2_held_focus ? last_d2_focus : n2;
		}

		if (n2_held_focus) {
			d2->focus = n1_held_focus ? last_d1_focus : n1;
		}

		if (m1 != m2) {
			adapt_geometry(&m2->rectangle, &m1->rectangle, n2);
			adapt_geometry(&m1->rectangle, &m2->rectangle, n1);
		}

		ewmh_set_wm_desktop(n1, d2);
		ewmh_set_wm_desktop(n2, d1);

		history_remove(d1, n1, true);
		history_remove(d2, n2, true);

		bool d1_was_focused = (d1 == mon->desk);
		bool d2_was_focused = (d2 == mon->desk);

		if (m1->desk != d1 && m2->desk == d2) {
			show_node(d2, n1);
			if (!follow || !d2_was_focused || !n2_held_focus) {
				hide_node(d2, n2);
			}
		} else if (m1->desk == d1 && m2->desk != d2) {
			if (!follow || !d1_was_focused || !n1_held_focus) {
				hide_node(d1, n1);
			}
			show_node(d1, n2);
		}

		if (single_monocle) {
			layout_t l1 = tiled_count(d1->root, true) <= 1 ? LAYOUT_MONOCLE : d1->user_layout;
			layout_t l2 = tiled_count(d2->root, true) <= 1 ? LAYOUT_MONOCLE : d2->user_layout;
			set_layout(m1, d1, l1, false);
			set_layout(m2, d2, l2, false);
		}

		if (n1_held_focus) {
			if (d1_was_focused) {
				if (follow) {
					focus_node(m2, d2, last_d1_focus);
				} else {
					focus_node(m1, d1, d1->focus);
				}
			} else {
				activate_node(m1, d1, d1->focus);
			}
		} else {
			draw_border(n2, is_descendant(n2, d1->focus), (m1 == mon));
		}

		if (n2_held_focus) {
			if (d2_was_focused) {
				if (follow) {
					focus_node(m1, d1, last_d2_focus);
				} else {
					focus_node(m2, d2, d2->focus);
				}
			} else {
				activate_node(m2, d2, d2->focus);
			}
		} else {
			draw_border(n1, is_descendant(n1, d2->focus), (m2 == mon));
		}
	} else {
		if (!n1_held_focus) {
			draw_border(n1, is_descendant(n1, d2->focus), (m2 == mon));
		}
		if (!n2_held_focus) {
			draw_border(n2, is_descendant(n2, d1->focus), (m1 == mon));
		}
	}

	arrange(m1, d1);

	if (d1 != d2) {
		arrange(m2, d2);
	} else {
		if (pointer_follows_focus && (n1_held_focus || n2_held_focus)) {
			center_pointer(get_rectangle(m1, d1, d1->focus));
		}
	}

	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, 0x18(%rsp)
xorl %r12d, %r12d
cmpq %r9, %rdx
je 0xb31d
movq %rdx, %r13
testq %rdx, %rdx
je 0xb31d
movq %r9, %rbp
testq %r9, %r9
je 0xb31d
movq %r8, %r15
movq %rsi, %rbx
movq %rdi, %r14
movq %r13, %rdi
movq %rbp, %rsi
callq 0x9892
testb %al, %al
jne 0xb31a
movq %rbp, %rdi
movq %r13, %rsi
callq 0x9892
testb %al, %al
je 0xb32f
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq %r15, %rbx
movq 0x18(%rsp), %r12
je 0xb360
cmpl $0x0, 0x40(%r14)
je 0xb34c
movq %r13, %rdi
callq 0xb262
testl %eax, %eax
jne 0xb31a
cmpl $0x0, 0x40(%r12)
je 0xb360
movq %rbp, %rdi
callq 0xb262
testl %eax, %eax
jne 0xb31a
movq %r14, 0x10(%rsp)
movl 0x20(%r14), %edx
movl 0x20(%rbx), %ecx
movl (%r13), %r8d
movl 0x20(%r12), %r9d
movl 0x20(%r15), %r10d
movl (%rbp), %r11d
leaq 0x1340b(%rip), %rsi     # 0x1e78f
movl $0x20000, %edi          # imm = 0x20000
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x1d07d
popq %rax
popq %rcx
movq 0x40(%r13), %r12
movq 0x40(%rbp), %r14
pushq $0x38
popq %rcx
movq %rcx, %rax
testq %r12, %r12
je 0xb3bb
xorl %eax, %eax
cmpq %r13, 0x30(%r12)
setne %al
leaq 0x30(,%rax,8), %rax
movq %rax, 0x20(%rsp)
testq %r14, %r14
je 0xb3d6
xorl %eax, %eax
cmpq %rbp, 0x30(%r14)
setne %al
leaq 0x30(,%rax,8), %rcx
movq %rcx, 0x8(%rsp)
movq 0x38(%rbx), %rdi
movq %rdi, 0x28(%rsp)
movq %r13, %rsi
callq 0x9892
movb %al, 0x7(%rsp)
movq 0x38(%r15), %rdi
movq %rdi, 0x30(%rsp)
movq %rbp, %rsi
callq 0x9892
movb %al, 0x6(%rsp)
testq %r12, %r12
je 0xb413
movq 0x20(%rsp), %rax
movq %rbp, (%r12,%rax)
testq %r14, %r14
movq 0x8(%rsp), %rax
je 0xb421
movq %r13, (%r14,%rax)
movq %r14, 0x40(%r13)
movq %r12, 0x40(%rbp)
movq 0x18(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x94a1
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x94a1
cmpq %r15, %rbx
je 0xb57c
cmpq %r13, 0x30(%rbx)
jne 0xb462
movq %rbp, 0x30(%rbx)
cmpq %rbp, 0x30(%r15)
jne 0xb46c
movq %r13, 0x30(%r15)
cmpb $0x0, 0x7(%rsp)
je 0xb485
cmpb $0x0, 0x6(%rsp)
movq %rbp, %rax
cmovneq 0x30(%rsp), %rax
movq %rax, 0x38(%rbx)
cmpb $0x0, 0x6(%rsp)
je 0xb49e
cmpb $0x0, 0x7(%rsp)
movq %r13, %rax
cmovneq 0x28(%rsp), %rax
movq %rax, 0x38(%r15)
cmpq %r12, %r14
je 0xb4cd
leaq 0x4c(%r12), %rdi
leaq 0x4c(%r14), %r12
movq %rdi, %r14
movq %r12, %rsi
movq %rbp, %rdx
callq 0x6bfe
movq %r12, %rdi
movq %r14, %rsi
movq 0x10(%rsp), %r14
movq %r13, %rdx
callq 0x6bfe
movq %r13, %rdi
movq %r15, %rsi
callq 0x1c625
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x1c625
movq %r14, %r12
pushq $0x1
popq %r14
movq %rbx, %rdi
movq %r13, %rsi
movl %r14d, %edx
callq 0xcb44
movq %r15, %rdi
movq %rbp, %rsi
movl %r14d, %edx
movq %r12, %r14
movq 0x18(%rsp), %r12
callq 0xcb44
leaq 0x1d1fb(%rip), %rax     # 0x28710
movq (%rax), %rax
movq 0x58(%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x58(%r12), %rax
cmpq %rbx, 0x58(%r14)
je 0xb5da
cmpq %r15, %rax
jne 0xb612
cmpq %r15, 0x8(%rsp)
setne %r14b
movq %r15, %rdi
movq %r13, %rsi
callq 0xa18e
movb 0x70(%rsp), %al
notb %al
movb 0x6(%rsp), %cl
notb %cl
orb %al, %cl
orb %r14b, %cl
movq 0x10(%rsp), %r14
testb $0x1, %cl
je 0xb612
movq %r15, %rdi
movq %rbp, %rsi
callq 0xa117
jmp 0xb612
cmpb $0x0, 0x7(%rsp)
jne 0xb5a9
movq 0x38(%r15), %rsi
movq %r13, %rdi
callq 0x9892
leaq 0x1d17a(%rip), %rcx     # 0x28710
xorl %edx, %edx
cmpq %r12, (%rcx)
sete %dl
movzbl %al, %esi
movq %r13, %rdi
callq 0xf25d
cmpb $0x0, 0x6(%rsp)
jne 0xb732
movq 0x38(%rbx), %rsi
movq %rbp, %rdi
callq 0x9892
leaq 0x1d149(%rip), %rcx     # 0x28710
xorl %edx, %edx
cmpq %r14, (%rcx)
sete %dl
movzbl %al, %esi
movq %rbp, %rdi
jmp 0xb72d
cmpq %r15, %rax
je 0xb612
cmpq %rbx, 0x8(%rsp)
setne %al
movb 0x70(%rsp), %cl
notb %cl
movb 0x7(%rsp), %dl
notb %dl
orb %cl, %dl
orb %al, %dl
testb $0x1, %dl
je 0xb607
movq %rbx, %rdi
movq %r13, %rsi
callq 0xa117
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xa18e
leaq 0x1d7df(%rip), %rax     # 0x28df8
cmpb $0x1, (%rax)
jne 0xb67f
movq 0x30(%rbx), %rdi
pushq $0x1
popq %r14
movl %r14d, %esi
callq 0x93c6
movl %r14d, %r12d
cmpl $0x2, %eax
jl 0xb63a
movl 0x28(%rbx), %r12d
movq 0x30(%r15), %rdi
movl %r14d, %esi
callq 0x93c6
cmpl $0x2, %eax
jl 0xb64f
movl 0x28(%r15), %r14d
movl %r14d, 0x20(%rsp)
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl %r12d, %edx
xorl %ecx, %ecx
callq 0x7b4b
movq 0x18(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movl 0x20(%rsp), %edx
xorl %ecx, %ecx
callq 0x7b4b
cmpb $0x0, 0x7(%rsp)
je 0xb69e
cmpq %rbx, 0x8(%rsp)
je 0xb6c6
movq 0x38(%rbx), %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x95e4
jmp 0xb6e9
movq 0x38(%rbx), %rsi
movq %rbp, %rdi
callq 0x9892
xorl %edx, %edx
leaq 0x1d05d(%rip), %rcx     # 0x28710
cmpq %r14, (%rcx)
sete %dl
movzbl %al, %esi
movq %rbp, %rdi
callq 0xf25d
jmp 0xb6e9
cmpb $0x0, 0x70(%rsp)
je 0xb6da
movq %r12, %rdi
movq %r15, %rsi
movq 0x28(%rsp), %rdx
jmp 0xb6e4
movq 0x38(%rbx), %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x9cf6
cmpb $0x0, 0x6(%rsp)
je 0xb70c
cmpq %r15, 0x8(%rsp)
je 0xb78e
movq 0x38(%r15), %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x95e4
jmp 0xb732
movq 0x38(%r15), %rsi
movq %r13, %rdi
callq 0x9892
xorl %edx, %edx
leaq 0x1cfef(%rip), %rcx     # 0x28710
cmpq %r12, (%rcx)
sete %dl
movzbl %al, %esi
movq %r13, %rdi
callq 0xf25d
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8670
cmpq %r15, %rbx
je 0xb755
movq %r12, %rdi
movq %r15, %rsi
callq 0x8670
movb $0x1, %r12b
jmp 0xb31d
leaq 0x1d69f(%rip), %rax     # 0x28dfb
movb 0x7(%rsp), %cl
orb 0x6(%rsp), %cl
andb (%rax), %cl
movb $0x1, %r12b
cmpb $0x1, %cl
jne 0xb31d
movq 0x38(%rbx), %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x932e
movq %rax, %rdi
callq 0x10648
jmp 0xb31d
cmpb $0x0, 0x70(%rsp)
je 0xb7a2
movq %r14, %rdi
movq %rbx, %rsi
movq 0x30(%rsp), %rdx
jmp 0xb7ac
movq 0x38(%r15), %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x9cf6
jmp 0xb732