// File: /workspace/github_c_makefile_star2_O3/baskerville[P]bspwm/src/desktop.c
// Function: swap_desktops
bool swap_desktops(monitor_t *m1, desktop_t *d1, monitor_t *m2, desktop_t *d2, bool follow)
{
	if (d1 == NULL || d2 == NULL || d1 == d2) {
		return false;
	}

	put_status(SBSC_MASK_DESKTOP_SWAP, "desktop_swap 0x%08X 0x%08X 0x%08X 0x%08X\n", m1->id, d1->id, m2->id, d2->id);

	bool d1_was_active = (m1->desk == d1);
	bool d2_was_active = (m2->desk == d2);
	bool d1_was_focused = (mon->desk == d1);
	bool d2_was_focused = (mon->desk == d2);
	desktop_t *d1_stickies = NULL;
	desktop_t *d2_stickies = NULL;

	if (m1->sticky_count > 0 && d1 == m1->desk && sticky_count(d1->root) > 0) {
		d1_stickies = make_desktop(NULL, XCB_NONE);
		insert_desktop(m1, d1_stickies);
		transfer_sticky_nodes(m1, d1, m1, d1_stickies, d1->root);
	}

	if (m2->sticky_count > 0 && d2 == m2->desk && sticky_count(d2->root) > 0) {
		d2_stickies = make_desktop(NULL, XCB_NONE);
		insert_desktop(m2, d2_stickies);
		transfer_sticky_nodes(m2, d2, m2, d2_stickies, d2->root);
	}

	if (m1 != m2) {
		if (m1->desk == d1) {
			m1->desk = d2;
		}
		if (m1->desk_head == d1) {
			m1->desk_head = d2;
		}
		if (m1->desk_tail == d1) {
			m1->desk_tail = d2;
		}
		if (m2->desk == d2) {
			m2->desk = d1;
		}
		if (m2->desk_head == d2) {
			m2->desk_head = d1;
		}
		if (m2->desk_tail == d2) {
			m2->desk_tail = d1;
		}
	} else {
		if (m1->desk == d1) {
			m1->desk = d2;
		} else if (m1->desk == d2) {
			m1->desk = d1;
		}
		if (m1->desk_head == d1) {
			m1->desk_head = d2;
		} else if (m1->desk_head == d2) {
			m1->desk_head = d1;
		}
		if (m1->desk_tail == d1) {
			m1->desk_tail = d2;
		} else if (m1->desk_tail == d2) {
			m1->desk_tail = d1;
		}
	}

	desktop_t *p1 = d1->prev;
	desktop_t *n1 = d1->next;
	desktop_t *p2 = d2->prev;
	desktop_t *n2 = d2->next;

	if (p1 != NULL && p1 != d2) {
		p1->next = d2;
	}
	if (n1 != NULL && n1 != d2) {
		n1->prev = d2;
	}
	if (p2 != NULL && p2 != d1) {
		p2->next = d1;
	}
	if (n2 != NULL && n2 != d1) {
		n2->prev = d1;
	}

	d1->prev = p2 == d1 ? d2 : p2;
	d1->next = n2 == d1 ? d2 : n2;
	d2->prev = p1 == d2 ? d1 : p1;
	d2->next = n1 == d2 ? d1 : n1;

	if (m1 != m2) {
		adapt_geometry(&m1->rectangle, &m2->rectangle, d1->root);
		adapt_geometry(&m2->rectangle, &m1->rectangle, d2->root);
		history_remove(d1, NULL, false);
		history_remove(d2, NULL, false);
		arrange(m1, d2);
		arrange(m2, d1);
	}

	if (d1_stickies != NULL) {
		transfer_sticky_nodes(m1, d1_stickies, m1, d2, d1_stickies->root);
		unlink_desktop(m1, d1_stickies);
		free(d1_stickies);
	}

	if (d2_stickies != NULL) {
		transfer_sticky_nodes(m2, d2_stickies, m2, d1, d2_stickies->root);
		unlink_desktop(m2, d2_stickies);
		free(d2_stickies);
	}

	if (d1_was_active && !d2_was_active) {
		if ((!follow && m1 != m2) || !d1_was_focused) {
			hide_desktop(d1);
		}
		show_desktop(d2);
	} else if (!d1_was_active && d2_was_active) {
		show_desktop(d1);
		if ((!follow && m1 != m2) || !d2_was_focused) {
			hide_desktop(d2);
		}
	}

	if (follow || m1 == m2) {
		if (d1_was_focused) {
			focus_node(m2, d1, d1->focus);
		} else if (d1_was_active) {
			activate_node(m2, d1, d1->focus);
		}

		if (d2_was_focused) {
			focus_node(m1, d2, d2->focus);
		} else if (d2_was_active) {
			activate_node(m1, d2, d2->focus);
		}
	} else {
		if (d1_was_focused) {
			focus_node(m1, d2, d2->focus);
		} else if (d1_was_active) {
			activate_node(m1, d2, d2->focus);
		}

		if (d2_was_focused) {
			focus_node(m2, d1, d1->focus);
		} else if (d2_was_active) {
			activate_node(m2, d1, d1->focus);
		}
	}

	ewmh_update_wm_desktops();
	ewmh_update_desktop_names();
	ewmh_update_desktop_viewport();
	ewmh_update_current_desktop();

	put_status(SBSC_MASK_REPORT);

	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, %r14
movq %rdx, (%rsp)
testq %rsi, %rsi
setne %al
testq %rcx, %rcx
setne %cl
andb %al, %cl
cmpq %r14, %rsi
setne %al
andb %cl, %al
movb %al, 0xb(%rsp)
cmpb $0x1, %al
jne 0x88e0
movq %rsi, %r15
movq %rdi, %r13
movl %r8d, 0xc(%rsp)
movl 0x20(%rdi), %edx
movl 0x20(%rsi), %ecx
movq (%rsp), %rbx
movl 0x20(%rbx), %r8d
movl 0x20(%r14), %r9d
leaq 0x171a6(%rip), %rsi     # 0x1f5bb
movq $0x0, 0x28(%rsp)
movl $0x400, %edi            # imm = 0x400
xorl %eax, %eax
callq 0x1e6b8
movq 0x58(%r13), %rax
movq %rax, 0x20(%rsp)
cmpq %r15, %rax
sete %al
movq 0x58(%rbx), %rbx
leaq 0x212cc(%rip), %rcx     # 0x29710
movq (%rcx), %rcx
cmpl $0x0, 0x40(%r13)
movq 0x58(%rcx), %rcx
movq %rcx, 0x10(%rsp)
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x84bb
movq 0x30(%r15), %rdi
callq 0xba68
testl %eax, %eax
je 0x848a
xorl %edi, %edi
xorl %esi, %esi
callq 0x813c
cmpq $0x0, 0x58(%r13)
je 0x8495
movq 0x68(%r13), %rcx
movq %rax, 0x48(%rcx)
movq %rcx, 0x40(%rax)
jmp 0x849d
movq $0x0, 0x28(%rsp)
jmp 0x84bb
movq %rax, 0x58(%r13)
movq %rax, 0x60(%r13)
movq %rax, 0x68(%r13)
movq 0x30(%r15), %r8
movq %r13, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %rax, %rcx
movq %rax, 0x28(%rsp)
callq 0x9de8
movq (%rsp), %rax
cmpl $0x0, 0x40(%rax)
je 0x84d3
movq (%rsp), %rax
cmpq %r14, 0x58(%rax)
je 0x857b
xorl %ebp, %ebp
movq 0x58(%r13), %rax
cmpq (%rsp), %r13
je 0x853b
cmpq %r15, %rax
jne 0x84e8
movq %r14, 0x58(%r13)
cmpq %r15, 0x60(%r13)
jne 0x84f2
movq %r14, 0x60(%r13)
cmpq %r15, 0x68(%r13)
jne 0x84fc
movq %r14, 0x68(%r13)
movq (%rsp), %rax
cmpq %r14, 0x58(%rax)
jne 0x850e
movq (%rsp), %rax
movq %r15, 0x58(%rax)
movq (%rsp), %rax
cmpq %r14, 0x60(%rax)
jne 0x8520
movq (%rsp), %rax
movq %r15, 0x60(%rax)
movq (%rsp), %rax
cmpq %r14, 0x68(%rax)
jne 0x85c0
movq (%rsp), %rax
movq %r15, 0x68(%rax)
jmp 0x85c0
movq %r14, %rcx
cmpq %r15, %rax
je 0x854b
movq %r15, %rcx
cmpq %r14, %rax
jne 0x854f
movq %rcx, 0x58(%r13)
movq 0x60(%r13), %rcx
movq %r14, %rax
cmpq %r15, %rcx
je 0x8563
movq %r15, %rax
cmpq %r14, %rcx
jne 0x8567
movq %rax, 0x60(%r13)
movq 0x68(%r13), %rax
cmpq %r15, %rax
je 0x85bc
cmpq %r14, %rax
jne 0x85c0
movq %r15, 0x68(%r13)
jmp 0x85c0
movq 0x30(%r14), %rdi
callq 0xba68
testl %eax, %eax
je 0x84d3
xorl %edi, %edi
xorl %esi, %esi
callq 0x813c
movq %rax, %rbp
movq (%rsp), %rax
cmpq $0x0, 0x58(%rax)
je 0x88f3
movq (%rsp), %rax
movq 0x68(%rax), %rax
movq %rbp, 0x48(%rax)
movq %rax, 0x40(%rbp)
jmp 0x88ff
movq %r14, 0x68(%r13)
movq 0x40(%r15), %rax
movq 0x48(%r15), %rcx
movq 0x40(%r14), %rdx
movq 0x48(%r14), %rsi
testq %rax, %rax
sete %dil
cmpq %r14, %rax
sete %r8b
orb %dil, %r8b
jne 0x85e7
movq %r14, 0x48(%rax)
testq %rcx, %rcx
sete %dil
cmpq %r14, %rcx
sete %r8b
orb %dil, %r8b
jne 0x85fe
movq %r14, 0x40(%rcx)
testq %rdx, %rdx
sete %dil
cmpq %r15, %rdx
sete %r8b
orb %dil, %r8b
jne 0x8615
movq %r15, 0x48(%rdx)
testq %rsi, %rsi
sete %dil
cmpq %r15, %rsi
sete %r8b
orb %dil, %r8b
jne 0x862c
movq %r15, 0x40(%rsi)
cmpq %r15, %rdx
cmoveq %r14, %rdx
cmpq %r15, %rsi
cmoveq %r14, %rsi
cmpq %r14, %rax
cmoveq %r15, %rax
cmpq %r14, %rcx
cmoveq %r15, %rcx
movq %rdx, 0x40(%r15)
movq %rsi, 0x48(%r15)
movq %rax, 0x40(%r14)
movq %rcx, 0x48(%r14)
cmpq (%rsp), %r13
movq %rbx, 0x18(%rsp)
je 0x86d1
leaq 0x4c(%r13), %rbx
movq (%rsp), %r12
movq %r13, 0x30(%rsp)
movq %rbp, %r13
leaq 0x4c(%r12), %rbp
movq 0x30(%r15), %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x6d15
movq 0x30(%r14), %rdx
movq %rbp, %rdi
movq %r13, %rbp
movq 0x30(%rsp), %r13
movq %rbx, %rsi
movq 0x18(%rsp), %rbx
callq 0x6d15
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xd6b9
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xd6b9
movq %r13, %rdi
movq %r14, %rsi
callq 0x8958
movq %r12, %rdi
movq %r15, %rsi
callq 0x8958
movq 0x28(%rsp), %rsi
testq %rsi, %rsi
je 0x870f
movq 0x30(%rsi), %r8
movq %r13, %rdi
movq %r13, %rdx
movq %rbp, %r12
movq %rbx, %rbp
movq %rsi, %rbx
movq %r14, %rcx
callq 0x9de8
movq %r13, %rdi
movq %rbx, %rsi
callq 0x80cc
movq %rbx, %rdi
movq %rbp, %rbx
movq %r12, %rbp
callq 0x4460
testq %rbp, %rbp
je 0x8740
movq 0x30(%rbp), %r8
movq (%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x9de8
movq %r12, %rdi
movq %rbp, %rsi
callq 0x80cc
movq %rbp, %rdi
callq 0x4460
cmpq %r14, %rbx
setne %al
movq 0x20(%rsp), %rdx
cmpq %r15, %rdx
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x87be
cmpq (%rsp), %r13
sete %bl
cmpq %r15, 0x10(%rsp)
sete %al
orb 0xc(%rsp), %bl
testb %al, %bl
jne 0x877b
movq 0x30(%r15), %rsi
movq %r15, %rdi
callq 0xa781
movq 0x30(%r14), %rsi
movq %r14, %rdi
callq 0xa7fb
testb %bl, %bl
movq 0x20(%rsp), %rcx
je 0x87e1
cmpq %r15, 0x10(%rsp)
je 0x8845
cmpq %r15, %rcx
movq 0x18(%rsp), %rbx
jne 0x885a
movq 0x38(%r15), %rdx
movq (%rsp), %rdi
movq %r15, %rsi
callq 0x9a3d
jmp 0x885a
cmpq %r14, %rbx
setne %al
cmpq %r15, %rdx
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x880b
movq %rdx, %rcx
cmpq (%rsp), %r13
sete %al
orb 0xc(%rsp), %al
jne 0x8790
cmpq %r15, 0x10(%rsp)
je 0x8872
cmpq %r15, %rcx
movq 0x18(%rsp), %rbx
jne 0x8886
movq 0x38(%r14), %rdx
movq %r13, %rdi
movq %r14, %rsi
callq 0x9a3d
jmp 0x8886
cmpq (%rsp), %r13
sete %bl
cmpq %r14, 0x10(%rsp)
sete %bpl
movq 0x30(%r15), %rsi
movq %r15, %rdi
callq 0xa7fb
orb 0xc(%rsp), %bl
testb %bpl, %bl
jne 0x8787
movq 0x30(%r14), %rsi
movq %r14, %rdi
callq 0xa781
jmp 0x8787
movq 0x38(%r15), %rdx
movq (%rsp), %rdi
movq %r15, %rsi
callq 0xa308
movq 0x18(%rsp), %rbx
cmpq %r14, 0x10(%rsp)
je 0x88a4
cmpq %r14, %rbx
jne 0x88c0
movq 0x38(%r14), %rdx
movq %r13, %rdi
movq %r14, %rsi
jmp 0x889d
movq 0x38(%r14), %rdx
movq %r13, %rdi
movq %r14, %rsi
callq 0xa308
movq 0x18(%rsp), %rbx
cmpq %r14, 0x10(%rsp)
je 0x88b0
cmpq %r14, %rbx
jne 0x88c0
movq 0x38(%r15), %rdx
movq (%rsp), %rdi
movq %r15, %rsi
callq 0x9a3d
jmp 0x88c0
movq 0x38(%r14), %rdx
movq %r13, %rdi
movq %r14, %rsi
jmp 0x88bb
movq 0x38(%r15), %rdx
movq (%rsp), %rdi
movq %r15, %rsi
callq 0xa308
callq 0x1dc3b
callq 0x1dcd6
callq 0x1dd9c
callq 0x1db67
movl $0x1, %edi
xorl %eax, %eax
callq 0x1e6b8
movb 0xb(%rsp), %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsp), %rax
movq %rbp, 0x58(%rax)
movq %rbp, 0x60(%rax)
movq (%rsp), %rdi
movq %rbp, 0x68(%rdi)
movq 0x30(%r14), %r8
movq %r14, %rsi
movq %rdi, %rdx
movq %rbp, %rcx
callq 0x9de8
jmp 0x84d5