// File: /workspace/github_c_makefile_star2_O1/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
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 0x89e7
movq %rdx, %rbp
movq %rsi, %r15
movq %rdi, %r13
movl %r8d, 0xc(%rsp)
movl 0x20(%rdi), %edx
movl 0x20(%rsi), %ecx
movl 0x20(%rbp), %r8d
movl 0x20(%r14), %r9d
leaq 0x1705a(%rip), %rsi     # 0x1f5cb
movq $0x0, 0x28(%rsp)
movl $0x400, %edi            # imm = 0x400
xorl %eax, %eax
callq 0x1eb0b
movq 0x58(%r13), %rax
movq %rax, 0x30(%rsp)
cmpq %r15, %rax
sete %al
movq 0x58(%rbp), %rcx
movq %rcx, 0x18(%rsp)
leaq 0x2116b(%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 0x861c
movq 0x30(%r15), %rdi
callq 0xba1f
testl %eax, %eax
je 0x85eb
xorl %edi, %edi
xorl %esi, %esi
callq 0x8297
cmpq $0x0, 0x58(%r13)
je 0x85f6
movq 0x68(%r13), %rcx
movq %rax, 0x48(%rcx)
movq %rcx, 0x40(%rax)
jmp 0x85fe
movq $0x0, 0x28(%rsp)
jmp 0x861c
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 0x9dd4
cmpl $0x0, 0x40(%rbp)
je 0x862c
cmpq %r14, 0x58(%rbp)
je 0x86b9
xorl %r12d, %r12d
movq 0x58(%r13), %rax
cmpq %rbp, %r13
je 0x8679
cmpq %r15, %rax
jne 0x8641
movq %r14, 0x58(%r13)
cmpq %r15, 0x60(%r13)
jne 0x864b
movq %r14, 0x60(%r13)
cmpq %r15, 0x68(%r13)
jne 0x8655
movq %r14, 0x68(%r13)
cmpq %r14, 0x58(%rbp)
jne 0x865f
movq %r15, 0x58(%rbp)
cmpq %r14, 0x60(%rbp)
jne 0x8669
movq %r15, 0x60(%rbp)
cmpq %r14, 0x68(%rbp)
jne 0x86f7
movq %r15, 0x68(%rbp)
jmp 0x86f7
movq %r14, %rcx
cmpq %r15, %rax
je 0x8689
movq %r15, %rcx
cmpq %r14, %rax
jne 0x868d
movq %rcx, 0x58(%r13)
movq 0x60(%r13), %rcx
movq %r14, %rax
cmpq %r15, %rcx
je 0x86a1
movq %r15, %rax
cmpq %r14, %rcx
jne 0x86a5
movq %rax, 0x60(%r13)
movq 0x68(%r13), %rax
cmpq %r15, %rax
je 0x86f3
cmpq %r14, %rax
jne 0x86f7
movq %r15, 0x68(%r13)
jmp 0x86f7
movq 0x30(%r14), %rdi
callq 0xba1f
testl %eax, %eax
je 0x862c
xorl %edi, %edi
xorl %esi, %esi
callq 0x8297
movq %rax, %r12
cmpq $0x0, 0x58(%rbp)
je 0x89fa
movq 0x68(%rbp), %rax
movq %r12, 0x48(%rax)
movq %rax, 0x40(%r12)
jmp 0x8a02
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 0x871e
movq %r14, 0x48(%rax)
testq %rcx, %rcx
sete %dil
cmpq %r14, %rcx
sete %r8b
orb %dil, %r8b
jne 0x8735
movq %r14, 0x40(%rcx)
testq %rdx, %rdx
sete %dil
cmpq %r15, %rdx
sete %r8b
orb %dil, %r8b
jne 0x874c
movq %r15, 0x48(%rdx)
testq %rsi, %rsi
sete %dil
cmpq %r15, %rsi
sete %r8b
orb %dil, %r8b
jne 0x8763
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 %rbp, %r13
je 0x87fe
leaq 0x4c(%r13), %rbx
movq %r13, 0x20(%rsp)
movq %r12, %r13
movq %rbp, %r12
addq $0x4c, %rbp
movq 0x30(%r15), %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x6df4
movq 0x30(%r14), %rdx
movq %rbp, %rdi
movq %r12, %rbp
movq %r13, %r12
movq 0x20(%rsp), %r13
movq %rbx, %rsi
callq 0x6df4
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xd60c
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xd60c
movq %r13, %rdi
movq %r14, %rsi
callq 0x8a5c
movq %rbp, %rdi
movq %r15, %rsi
callq 0x8a5c
movq 0x28(%rsp), %rsi
testq %rsi, %rsi
je 0x8836
movq 0x30(%rsi), %r8
movq %r13, %rdi
movq %r13, %rdx
movq %r12, %rbx
movq %rsi, %r12
movq %r14, %rcx
callq 0x9dd4
movq %r13, %rdi
movq %r12, %rsi
callq 0x8227
movq %r12, %rdi
movq %rbx, %r12
callq 0x4460
testq %r12, %r12
je 0x8864
movq 0x30(%r12), %r8
movq %rbp, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x9dd4
movq %rbp, %rdi
movq %r12, %rsi
callq 0x8227
movq %r12, %rdi
callq 0x4460
cmpq %r14, 0x18(%rsp)
setne %al
movq 0x30(%rsp), %rdx
cmpq %r15, %rdx
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x88ae
cmpq %rbp, %r13
sete %al
cmpq %r15, 0x10(%rsp)
sete %cl
orb 0xc(%rsp), %al
testb %cl, %al
jne 0x88a0
movq 0x30(%r15), %rsi
movq %r15, %rdi
callq 0xa721
movq 0x30(%r14), %rsi
movq %r14, %rdi
callq 0xa798
jmp 0x8900
cmpq %r14, 0x18(%rsp)
setne %al
cmpq %r15, %rdx
sete %cl
orb %al, %cl
jne 0x8900
cmpq %rbp, %r13
sete %bl
cmpq %r14, 0x10(%rsp)
movq %r13, 0x20(%rsp)
movq %rbp, %r12
sete %bpl
movq 0x30(%r15), %rsi
movq %r15, %rdi
callq 0xa798
orb 0xc(%rsp), %bl
testb %bpl, %bl
movq %r12, %rbp
movq 0x20(%rsp), %r13
jne 0x8900
movq 0x30(%r14), %rsi
movq %r14, %rdi
callq 0xa721
cmpq %rbp, %r13
setne %al
movl 0xc(%rsp), %ecx
xorb $0x1, %cl
testb %cl, %al
jne 0x8933
cmpq %r15, 0x10(%rsp)
movq 0x30(%rsp), %rax
je 0x8955
cmpq %r15, %rax
jne 0x8964
movq 0x38(%r15), %rdx
movq %rbp, %rdi
movq %r15, %rsi
callq 0x9af5
jmp 0x8964
cmpq %r15, 0x10(%rsp)
movq 0x30(%rsp), %rax
je 0x897e
cmpq %r15, %rax
jne 0x898d
movq 0x38(%r14), %rdx
movq %r13, %rdi
movq %r14, %rsi
callq 0x9af5
jmp 0x898d
movq 0x38(%r15), %rdx
movq %rbp, %rdi
movq %r15, %rsi
callq 0xa2cd
cmpq %r14, 0x10(%rsp)
je 0x89ac
cmpq %r14, 0x18(%rsp)
jne 0x89c7
movq 0x38(%r14), %rdx
movq %r13, %rdi
movq %r14, %rsi
jmp 0x89a5
movq 0x38(%r14), %rdx
movq %r13, %rdi
movq %r14, %rsi
callq 0xa2cd
cmpq %r14, 0x10(%rsp)
je 0x89b8
cmpq %r14, 0x18(%rsp)
jne 0x89c7
movq 0x38(%r15), %rdx
movq %rbp, %rdi
movq %r15, %rsi
callq 0x9af5
jmp 0x89c7
movq 0x38(%r14), %rdx
movq %r13, %rdi
movq %r14, %rsi
jmp 0x89c2
movq 0x38(%r15), %rdx
movq %rbp, %rdi
movq %r15, %rsi
callq 0xa2cd
callq 0x1e003
callq 0x1e0b4
callq 0x1e17a
callq 0x1df11
movl $0x1, %edi
xorl %eax, %eax
callq 0x1eb0b
movb 0xb(%rsp), %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, 0x58(%rbp)
movq %r12, 0x60(%rbp)
movq %r12, 0x68(%rbp)
movq 0x30(%r14), %r8
movq %rbp, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r12, %rcx
callq 0x9dd4
jmp 0x862f