// File: /workspace/github_c_makefile_star2_O2/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 0x8621
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %r13
movl %r8d, 0xc(%rsp)
movl 0x20(%rdi), %edx
movl 0x20(%rsi), %ecx
movl 0x20(%r12), %r8d
movl 0x20(%r14), %r9d
leaq 0x16414(%rip), %rsi     # 0x1e5bb
movq $0x0, 0x30(%rsp)
movl $0x400, %edi            # imm = 0x400
xorl %eax, %eax
callq 0x1d07d
movq 0x58(%r13), %rax
movq %rax, 0x28(%rsp)
cmpq %r15, %rax
sete %al
movq 0x58(%r12), %rbx
leaq 0x20539(%rip), %rcx     # 0x28710
movq (%rcx), %rcx
cmpl $0x0, 0x40(%r13)
movq 0x58(%rcx), %rcx
movq %rcx, 0x10(%rsp)
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x8241
movq 0x30(%r15), %rdi
callq 0xb262
testl %eax, %eax
je 0x8238
xorl %edi, %edi
xorl %esi, %esi
callq 0x7efd
movq %rbx, %rbp
movq %rax, %rbx
movq %r13, %rdi
movq %rax, %rsi
callq 0x7ed4
movq 0x30(%r15), %r8
movq %r13, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %rbx, 0x30(%rsp)
movq %rbx, %rcx
movq %rbp, %rbx
callq 0x98bc
jmp 0x8241
movq $0x0, 0x30(%rsp)
cmpl $0x0, 0x40(%r12)
je 0x8254
cmpq %r14, 0x58(%r12)
je 0x82f0
movq $0x0, 0x20(%rsp)
movq 0x58(%r13), %rax
cmpq %r12, %r13
je 0x82b0
cmpq %r15, %rax
jne 0x826f
movq %r14, 0x58(%r13)
cmpq %r15, 0x60(%r13)
jne 0x8279
movq %r14, 0x60(%r13)
cmpq %r15, 0x68(%r13)
jne 0x8283
movq %r14, 0x68(%r13)
cmpq %r14, 0x58(%r12)
jne 0x828f
movq %r15, 0x58(%r12)
cmpq %r14, 0x60(%r12)
jne 0x829b
movq %r15, 0x60(%r12)
cmpq %r14, 0x68(%r12)
jne 0x833b
movq %r15, 0x68(%r12)
jmp 0x833b
movq %r14, %rcx
cmpq %r15, %rax
je 0x82c0
movq %r15, %rcx
cmpq %r14, %rax
jne 0x82c4
movq %rcx, 0x58(%r13)
movq 0x60(%r13), %rcx
movq %r14, %rax
cmpq %r15, %rcx
je 0x82d8
movq %r15, %rax
cmpq %r14, %rcx
jne 0x82dc
movq %rax, 0x60(%r13)
movq 0x68(%r13), %rax
cmpq %r15, %rax
je 0x8337
cmpq %r14, %rax
jne 0x833b
movq %r15, 0x68(%r13)
jmp 0x833b
movq 0x30(%r14), %rdi
callq 0xb262
testl %eax, %eax
je 0x8254
xorl %edi, %edi
xorl %esi, %esi
callq 0x7efd
movq %rax, %rbp
movq %r12, %rdi
movq %rax, %rsi
callq 0x7ed4
movq 0x30(%r14), %r8
movq %r12, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %rbp, 0x20(%rsp)
movq %rbp, %rcx
callq 0x98bc
jmp 0x825d
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 0x8362
movq %r14, 0x48(%rax)
testq %rcx, %rcx
sete %dil
cmpq %r14, %rcx
sete %r8b
orb %dil, %r8b
jne 0x8379
movq %r14, 0x40(%rcx)
testq %rdx, %rdx
sete %dil
cmpq %r15, %rdx
sete %r8b
orb %dil, %r8b
jne 0x8390
movq %r15, 0x48(%rdx)
testq %rsi, %rsi
sete %dil
cmpq %r15, %rsi
sete %r8b
orb %dil, %r8b
jne 0x83a7
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 %r12, %r13
movq %rbx, 0x18(%rsp)
je 0x8437
leaq 0x4c(%r13), %rbp
leaq 0x4c(%r12), %rbx
movq 0x30(%r15), %rdx
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x6bfe
movq 0x30(%r14), %rdx
movq %rbx, %rdi
movq 0x18(%rsp), %rbx
movq %rbp, %rsi
callq 0x6bfe
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xcb44
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xcb44
movq %r13, %rdi
movq %r14, %rsi
callq 0x8670
movq %r12, %rdi
movq %r15, %rsi
callq 0x8670
movq %rbx, %rbp
movq 0x30(%rsp), %rbx
testq %rbx, %rbx
je 0x846c
movq 0x30(%rbx), %r8
movq %r13, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x98bc
movq %r13, %rdi
movq %rbx, %rsi
callq 0x7e90
movq %rbx, %rdi
callq 0x4460
movq 0x20(%rsp), %rbx
testq %rbx, %rbx
je 0x849e
movq 0x30(%rbx), %r8
movq %r12, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x98bc
movq %r12, %rdi
movq %rbx, %rsi
callq 0x7e90
movq %rbx, %rdi
callq 0x4460
cmpq %r14, %rbp
setne %al
movq 0x28(%rsp), %rdx
cmpq %r15, %rdx
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x8512
cmpq %r12, %r13
sete %bl
cmpq %r15, 0x10(%rsp)
sete %al
orb 0xc(%rsp), %bl
testb %al, %bl
jne 0x84d4
movq %r15, %rdi
callq 0x7987
movq %r14, %rdi
callq 0x7978
testb %bl, %bl
movq 0x28(%rsp), %rcx
je 0x8534
cmpq %r15, 0x10(%rsp)
je 0x858b
cmpq %r15, %rcx
movq 0x18(%rsp), %rbx
jne 0x859f
movq 0x38(%r15), %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x95e4
jmp 0x859f
cmpq %r14, %rbp
setne %al
cmpq %r15, %rdx
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x855a
movq %rdx, %rcx
cmpq %r12, %r13
sete %al
orb 0xc(%rsp), %al
jne 0x84e5
cmpq %r15, 0x10(%rsp)
je 0x85b7
cmpq %r15, %rcx
movq 0x18(%rsp), %rbx
jne 0x85cb
movq 0x38(%r14), %rdx
movq %r13, %rdi
movq %r14, %rsi
callq 0x95e4
jmp 0x85cb
cmpq %r12, %r13
sete %bl
cmpq %r14, 0x10(%rsp)
sete %bpl
movq %r15, %rdi
callq 0x7978
orb 0xc(%rsp), %bl
testb %bpl, %bl
jne 0x84dc
movq %r14, %rdi
callq 0x7987
jmp 0x84dc
movq 0x38(%r15), %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x9cf6
movq 0x18(%rsp), %rbx
cmpq %r14, 0x10(%rsp)
je 0x85e8
cmpq %r14, %rbx
jne 0x8603
movq 0x38(%r14), %rdx
movq %r13, %rdi
movq %r14, %rsi
jmp 0x85e1
movq 0x38(%r14), %rdx
movq %r13, %rdi
movq %r14, %rsi
callq 0x9cf6
movq 0x18(%rsp), %rbx
cmpq %r14, 0x10(%rsp)
je 0x85f4
cmpq %r14, %rbx
jne 0x8603
movq 0x38(%r15), %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x95e4
jmp 0x8603
movq 0x38(%r14), %rdx
movq %r13, %rdi
movq %r14, %rsi
jmp 0x85fe
movq 0x38(%r15), %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x9cf6
callq 0x1c670
callq 0x1c6ea
callq 0x1c78f
callq 0x1c5f3
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x1d07d
movb 0xb(%rsp), %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq