// File: /workspace/github_c_makefile_star2_O3/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 %r9, %rbp
xorl %r12d, %r12d
cmpq %r9, %rdx
je 0xbb74
movq %rdx, %r13
testq %rdx, %rdx
je 0xbb74
testq %rbp, %rbp
je 0xbb74
movq %r8, %r15
movq %rcx, %r9
movq %rsi, %rbx
movq %rdi, %r14
movq %r13, %rax
movq 0x40(%rax), %rax
cmpq %rbp, %rax
je 0xbb1c
testq %rax, %rax
jne 0xbb0e
cmpq %rbp, %rax
je 0xbb71
movq %rbp, %rax
movq 0x40(%rax), %rax
cmpq %r13, %rax
je 0xbb32
testq %rax, %rax
jne 0xbb24
cmpq %r13, %rax
je 0xbb71
cmpq %r15, %rbx
movq %r9, 0x8(%rsp)
je 0xbb86
cmpl $0x0, 0x40(%r14)
je 0xbb59
movq %r13, %rdi
callq 0xba68
movq 0x8(%rsp), %r9
testl %eax, %eax
jne 0xbb71
cmpl $0x0, 0x40(%r9)
je 0xbb86
movq %rbp, %rdi
callq 0xba68
movq 0x8(%rsp), %r9
testl %eax, %eax
je 0xbb86
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x20(%r14), %edx
movl 0x20(%rbx), %ecx
movl (%r13), %r8d
movl 0x20(%r9), %r9d
movl 0x20(%r15), %r10d
movl (%rbp), %r11d
leaq 0x13beb(%rip), %rsi     # 0x1f78f
movl $0x20000, %edi          # imm = 0x20000
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x1e6b8
addq $0x10, %rsp
movq 0x40(%r13), %rax
movq 0x40(%rbp), %rcx
movl $0x38, %edx
movl $0x38, %esi
testq %rax, %rax
je 0xbbe1
xorl %esi, %esi
cmpq %r13, 0x30(%rax)
setne %sil
leaq 0x30(,%rsi,8), %rsi
testq %rcx, %rcx
je 0xbbf7
xorl %edx, %edx
cmpq %rbp, 0x30(%rcx)
setne %dl
leaq 0x30(,%rdx,8), %rdx
movq 0x38(%rbx), %r9
testq %r9, %r9
movq 0x8(%rsp), %rdi
movq %r9, 0x28(%rsp)
je 0xbc2c
movq %r13, %r8
cmpq %r13, %r9
je 0xbc23
movq %r9, %r8
movq 0x40(%r8), %r8
cmpq %r13, %r8
je 0xbc23
testq %r8, %r8
jne 0xbc15
cmpq %r13, %r8
sete %r12b
jmp 0xbc2f
xorl %r12d, %r12d
movq 0x38(%r15), %r9
testq %r9, %r9
je 0xbc5f
movq %rbp, %r8
cmpq %rbp, %r9
je 0xbc51
movq %r9, %r8
movq 0x40(%r8), %r8
cmpq %rbp, %r8
je 0xbc51
testq %r8, %r8
jne 0xbc43
cmpq %rbp, %r8
sete %r8b
movl %r8d, 0x4(%rsp)
jmp 0xbc67
movl $0x0, 0x4(%rsp)
testq %rax, %rax
je 0xbc70
movq %rbp, (%rax,%rsi)
movq %r9, 0x30(%rsp)
testq %rcx, %rcx
je 0xbc7e
movq %r13, (%rcx,%rdx)
movq %rcx, 0x40(%r13)
movq %rax, 0x40(%rbp)
movq %r15, %rsi
movq %r13, %rdx
callq 0x98ae
movq %r14, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x98ae
cmpq %r15, %rbx
je 0xbdd2
cmpq %r13, 0x30(%rbx)
jne 0xbcb2
movq %rbp, 0x30(%rbx)
cmpq %rbp, 0x30(%r15)
movq 0x8(%rsp), %rcx
movq 0x30(%rsp), %rdx
jne 0xbcc6
movq %r13, 0x30(%r15)
testb %r12b, %r12b
je 0xbcdb
cmpb $0x0, 0x4(%rsp)
movq %rbp, %rax
cmovneq %rdx, %rax
movq %rax, 0x38(%rbx)
cmpb $0x0, 0x4(%rsp)
je 0xbcf2
testb %r12b, %r12b
movq %r13, %rax
cmovneq 0x28(%rsp), %rax
movq %rax, 0x38(%r15)
movl %r12d, 0x14(%rsp)
movq %r15, 0x18(%rsp)
cmpq %rcx, %r14
movq %r14, %r15
movq %rcx, %r14
je 0xbd30
leaq 0x4c(%r14), %r12
leaq 0x4c(%r15), %r14
movq %r12, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x6d15
movq %r14, %rdi
movq 0x8(%rsp), %r14
movq %r12, %rsi
movq %r13, %rdx
callq 0x6d15
movq %r13, %rdi
movq 0x18(%rsp), %r12
movq %r12, %rsi
callq 0x1dbc2
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x1dbc2
movq %rbx, %rdi
movq %r13, %rsi
movl $0x1, %edx
callq 0xd6b9
movq %r12, %rdi
movq %rbp, %rsi
movl $0x1, %edx
callq 0xd6b9
leaq 0x1d99e(%rip), %rax     # 0x29710
movq (%rax), %rax
movq 0x58(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x58(%r14), %rax
movq %r15, %r14
cmpq %rbx, 0x58(%r15)
je 0xbe11
cmpq %r12, %rax
movq %r12, %r15
movl 0x14(%rsp), %r12d
jne 0xbe49
movq %r15, %rdi
movq %r13, %rsi
callq 0xa7fb
cmpb $0x0, 0x70(%rsp)
je 0xbdc5
cmpq %r15, 0x20(%rsp)
jne 0xbdc5
movl 0x4(%rsp), %eax
xorb $0x1, %al
je 0xbe49
movq %r15, %rdi
movq %rbp, %rsi
callq 0xa781
jmp 0xbe49
testb %r12b, %r12b
jne 0xbf5e
movq 0x38(%r15), %rax
testq %rax, %rax
movq 0x8(%rsp), %rsi
je 0xbf42
movq %r13, %rcx
cmpq %r13, %rax
je 0xbe06
movq %r13, %rcx
movq 0x40(%rcx), %rcx
cmpq %rax, %rcx
je 0xbe06
testq %rcx, %rcx
jne 0xbdf8
cmpq %rax, %rcx
sete %al
jmp 0xbf44
cmpq %r12, %rax
movq %r12, %r15
movl 0x14(%rsp), %r12d
je 0xbe49
cmpb $0x0, 0x70(%rsp)
je 0xbe33
cmpq %rbx, 0x20(%rsp)
jne 0xbe33
movl %r12d, %eax
xorb $0x1, %al
je 0xbe3e
movq %rbx, %rdi
movq %r13, %rsi
callq 0xa781
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xa7fb
leaq 0x1dfa8(%rip), %rax     # 0x29df8
cmpb $0x1, (%rax)
jne 0xbebd
movq 0x30(%rbx), %rdi
movl $0x1, 0x18(%rsp)
movl $0x1, %esi
callq 0x97a3
movl $0x1, %r12d
cmpl $0x2, %eax
jl 0xbe7a
movl 0x28(%rbx), %r12d
movq 0x30(%r15), %rdi
movl $0x1, %esi
callq 0x97a3
cmpl $0x2, %eax
jl 0xbe95
movl 0x28(%r15), %eax
movl %eax, 0x18(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movl %r12d, %edx
xorl %ecx, %ecx
callq 0x7d6e
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movl 0x18(%rsp), %edx
xorl %ecx, %ecx
callq 0x7d6e
movl 0x14(%rsp), %r12d
testb %r12b, %r12b
je 0xbedd
cmpq %rbx, 0x20(%rsp)
je 0xbf07
movq 0x38(%rbx), %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x9a3d
jmp 0xbfbe
movq 0x38(%rbx), %rax
testq %rax, %rax
je 0xbf24
movq %rbp, %rcx
cmpq %rbp, %rax
je 0xbeff
movq %rbp, %rcx
movq 0x40(%rcx), %rcx
cmpq %rax, %rcx
je 0xbeff
testq %rcx, %rcx
jne 0xbef1
cmpq %rax, %rcx
sete %al
jmp 0xbf26
cmpb $0x0, 0x70(%rsp)
je 0xbfaf
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq 0x28(%rsp), %rdx
jmp 0xbfb9
xorl %eax, %eax
xorl %edx, %edx
leaq 0x1d7e1(%rip), %rcx     # 0x29710
cmpq %r14, (%rcx)
sete %dl
movzbl %al, %esi
movq %rbp, %rdi
callq 0xff61
jmp 0xbfbe
xorl %eax, %eax
leaq 0x1d7c5(%rip), %rcx     # 0x29710
xorl %edx, %edx
cmpq %rsi, (%rcx)
sete %dl
movzbl %al, %esi
movq %r13, %rdi
callq 0xff61
cmpb $0x0, 0x4(%rsp)
jne 0xc040
movq 0x38(%rbx), %rax
testq %rax, %rax
je 0xbf93
movq %rbp, %rcx
cmpq %rbp, %rax
je 0xbf8b
movq %rbp, %rcx
movq 0x40(%rcx), %rcx
cmpq %rax, %rcx
je 0xbf8b
testq %rcx, %rcx
jne 0xbf7d
cmpq %rax, %rcx
sete %al
jmp 0xbf95
xorl %eax, %eax
leaq 0x1d774(%rip), %rcx     # 0x29710
xorl %edx, %edx
cmpq %r14, (%rcx)
sete %dl
movzbl %al, %esi
movq %rbp, %rdi
jmp 0xc03b
movq 0x38(%rbx), %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0xa308
cmpb $0x0, 0x4(%rsp)
movq 0x8(%rsp), %rdi
movq 0x30(%rsp), %rdx
je 0xbfe4
cmpq %r15, 0x20(%rsp)
je 0xc00e
movq 0x38(%r15), %rdx
movq %r15, %rsi
callq 0x9a3d
jmp 0xc040
movq 0x38(%r15), %rax
testq %rax, %rax
je 0xc024
movq %r13, %rcx
cmpq %r13, %rax
je 0xc006
movq %r13, %rcx
movq 0x40(%rcx), %rcx
cmpq %rax, %rcx
je 0xc006
testq %rcx, %rcx
jne 0xbff8
cmpq %rax, %rcx
sete %al
jmp 0xc026
cmpb $0x0, 0x70(%rsp)
je 0xc0a4
movq %r14, %rdi
movq %rbx, %rsi
jmp 0xc0ab
xorl %eax, %eax
xorl %edx, %edx
leaq 0x1d6e1(%rip), %rcx     # 0x29710
cmpq %rdi, (%rcx)
sete %dl
movzbl %al, %esi
movq %r13, %rdi
callq 0xff61
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8958
cmpq %r15, %rbx
je 0xc065
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x8958
movb $0x1, %r12b
jmp 0xbb74
orb 0x4(%rsp), %r12b
movl %r12d, %eax
movb $0x1, %r12b
cmpb $0x1, %al
jne 0xbb74
leaq 0x1dd7c(%rip), %rax     # 0x29dfb
cmpb $0x0, (%rax)
je 0xbb74
movq 0x38(%rbx), %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x970b
movq %rax, %rdi
callq 0x11591
jmp 0xbb74
movq 0x38(%r15), %rdx
movq %r15, %rsi
callq 0xa308
jmp 0xc040