// File: /workspace/github_c_makefile_star2_O3/baskerville[P]bspwm/src/bspwm.c
// Function: setup
void setup(void)
{
	init();
	ewmh_init();
	pointer_init();

	screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;

	if (screen == NULL) {
		err("Can't acquire the default screen.\n");
	}

	root = screen->root;
	register_events();

	screen_width = screen->width_in_pixels;
	screen_height = screen->height_in_pixels;

	meta_window = xcb_generate_id(dpy);
	xcb_create_window(dpy, XCB_COPY_FROM_PARENT, meta_window, root, -1, -1, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_ONLY, XCB_COPY_FROM_PARENT, XCB_NONE, NULL);
	xcb_icccm_set_wm_class(dpy, meta_window, sizeof(META_WINDOW_IC), META_WINDOW_IC);

	motion_recorder.id = xcb_generate_id(dpy);
	motion_recorder.sequence = 0;
	motion_recorder.enabled = false;
	uint32_t values[] = {XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_POINTER_MOTION};
	xcb_create_window(dpy, XCB_COPY_FROM_PARENT, motion_recorder.id, root, 0, 0, 1, 1, 0,
	                  XCB_WINDOW_CLASS_INPUT_ONLY, XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, values);
	xcb_icccm_set_wm_class(dpy, motion_recorder.id, sizeof(MOTION_RECORDER_IC), MOTION_RECORDER_IC);

	xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
	                          ewmh->_NET_SUPPORTING_WM_CHECK,
	                          ewmh->_NET_DESKTOP_NAMES,
	                          ewmh->_NET_DESKTOP_VIEWPORT,
	                          ewmh->_NET_NUMBER_OF_DESKTOPS,
	                          ewmh->_NET_CURRENT_DESKTOP,
	                          ewmh->_NET_CLIENT_LIST,
	                          ewmh->_NET_ACTIVE_WINDOW,
	                          ewmh->_NET_CLOSE_WINDOW,
	                          ewmh->_NET_WM_STRUT_PARTIAL,
	                          ewmh->_NET_WM_DESKTOP,
	                          ewmh->_NET_WM_STATE,
	                          ewmh->_NET_WM_STATE_HIDDEN,
	                          ewmh->_NET_WM_STATE_FULLSCREEN,
	                          ewmh->_NET_WM_STATE_BELOW,
	                          ewmh->_NET_WM_STATE_ABOVE,
	                          ewmh->_NET_WM_STATE_STICKY,
	                          ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
	                          ewmh->_NET_WM_WINDOW_TYPE,
	                          ewmh->_NET_WM_WINDOW_TYPE_DOCK,
	                          ewmh->_NET_WM_WINDOW_TYPE_DESKTOP,
	                          ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
	                          ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
	                          ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
	                          ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};

	xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
	ewmh_set_supporting(meta_window);

#define GETATOM(a) \
	get_atom(#a, &a);
	GETATOM(WM_STATE)
	GETATOM(WM_DELETE_WINDOW)
	GETATOM(WM_TAKE_FOCUS)
#undef GETATOM

	const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
	if (qep->present && update_monitors()) {
		randr = true;
		randr_base = qep->first_event;
		xcb_randr_select_input(dpy, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
	} else {
		randr = false;
		warn("Couldn't retrieve monitors via RandR.\n");
		bool xinerama_is_active = false;

		if (xcb_get_extension_data(dpy, &xcb_xinerama_id)->present) {
			xcb_xinerama_is_active_reply_t *xia = xcb_xinerama_is_active_reply(dpy, xcb_xinerama_is_active(dpy), NULL);
			if (xia != NULL) {
				xinerama_is_active = xia->state;
				free(xia);
			}
		}

		if (xinerama_is_active) {
			xcb_xinerama_query_screens_reply_t *xsq = xcb_xinerama_query_screens_reply(dpy, xcb_xinerama_query_screens(dpy), NULL);
			xcb_xinerama_screen_info_t *xsi = xcb_xinerama_query_screens_screen_info(xsq);
			int n = xcb_xinerama_query_screens_screen_info_length(xsq);
			for (int i = 0; i < n; i++) {
				xcb_xinerama_screen_info_t info = xsi[i];
				xcb_rectangle_t rect = (xcb_rectangle_t) {info.x_org, info.y_org, info.width, info.height};
				monitor_t *m = make_monitor(NULL, &rect, XCB_NONE);
				add_monitor(m);
				add_desktop(m, make_desktop(NULL, XCB_NONE));
			}
			free(xsq);
		} else {
			warn("Xinerama is inactive.\n");
			xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
			monitor_t *m = make_monitor(NULL, &rect, XCB_NONE);
			add_monitor(m);
			add_desktop(m, make_desktop(NULL, XCB_NONE));
		}
	}

	ewmh_update_number_of_desktops();
	ewmh_update_desktop_names();
	ewmh_update_desktop_viewport();
	ewmh_update_current_desktop();
	xcb_get_input_focus_reply_t *ifo = xcb_get_input_focus_reply(dpy, xcb_get_input_focus(dpy), NULL);
	if (ifo != NULL && (ifo->focus == XCB_INPUT_FOCUS_POINTER_ROOT || ifo->focus == XCB_NONE)) {
		clear_input_focus();
	}
	free(ifo);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
callq 0x5af0
callq 0x1da34
callq 0xec64
movq 0x240c3(%rip), %rdi     # 0x296c8
callq 0x4520
movq %rax, %rdi
callq 0x43f0
movq %rax, 0x24157(%rip)     # 0x29770
testq %rax, %rax
jne 0x5633
leaq 0x19b1a(%rip), %rdi     # 0x1f13f
xorl %eax, %eax
callq 0x5c6c
movq 0x2413d(%rip), %rax     # 0x29770
movl (%rax), %eax
movl %eax, 0x2413d(%rip)     # 0x29778
callq 0x5b91
movq 0x24129(%rip), %rax     # 0x29770
movzwl 0x14(%rax), %ecx
movl %ecx, 0x2412b(%rip)     # 0x2977c
movzwl 0x16(%rax), %eax
movl %eax, 0x24125(%rip)     # 0x29780
movq 0x24066(%rip), %rdi     # 0x296c8
callq 0x4920
movl %eax, 0x24077(%rip)     # 0x296e4
movq 0x24054(%rip), %rdi     # 0x296c8
movl 0x240fe(%rip), %ecx     # 0x29778
subq $0x8, %rsp
xorl %r14d, %r14d
movl $0x2, %r15d
movl $0x1, %ebx
xorl %esi, %esi
movl %eax, %edx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
pushq $0x0
pushq %r14
pushq %r14
pushq %r15
pushq %r14
pushq %rbx
pushq %rbx
callq 0x4030
addq $0x40, %rsp
movq 0x24010(%rip), %rdi     # 0x296c8
movl 0x24026(%rip), %esi     # 0x296e4
leaq 0x19c33(%rip), %rcx     # 0x1f2f8
movl $0x9, %edx
callq 0x4710
movq 0x23ff2(%rip), %rdi     # 0x296c8
callq 0x4920
movl %eax, 0x24007(%rip)     # 0x296e8
movw $0x0, 0x24002(%rip)     # 0x296ec
movb $0x0, 0x23ffd(%rip)     # 0x296ee
leaq 0xc(%rsp), %r10
movl $0x20040, (%r10)        # imm = 0x20040
movq 0x23fc4(%rip), %rdi     # 0x296c8
movl 0x2406e(%rip), %ecx     # 0x29778
subq $0x8, %rsp
xorl %esi, %esi
movl %eax, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %r10
pushq $0x800                  # imm = 0x800
pushq %r14
pushq %r15
pushq %r14
pushq %rbx
pushq %rbx
callq 0x4030
addq $0x40, %rsp
movq 0x23f91(%rip), %rdi     # 0x296c8
movl 0x23fab(%rip), %esi     # 0x296e8
leaq 0x19bbd(%rip), %rcx     # 0x1f301
movl $0x16, %edx
callq 0x4710
leaq 0x246db(%rip), %rax     # 0x29e30
movq (%rax), %rdi
movl 0x20(%rdi), %eax
leaq 0x10(%rsp), %rcx
movl %eax, (%rcx)
movl 0x48(%rdi), %eax
movl %eax, 0x4(%rcx)
movl 0x3c(%rdi), %eax
movl %eax, 0x8(%rcx)
movl 0x34(%rdi), %eax
movl %eax, 0xc(%rcx)
movl 0x2c(%rdi), %eax
movl %eax, 0x10(%rcx)
movl 0x38(%rdi), %eax
movl %eax, 0x14(%rcx)
movl 0x24(%rdi), %eax
movl %eax, 0x18(%rcx)
movl 0x40(%rdi), %eax
movl %eax, 0x1c(%rcx)
movl 0x58(%rdi), %eax
movl %eax, 0x20(%rcx)
movl 0x90(%rdi), %eax
movl %eax, 0x24(%rcx)
movl 0x7c(%rdi), %eax
movl %eax, 0x28(%rcx)
movl 0x84(%rdi), %eax
movl %eax, 0x2c(%rcx)
movdqu 0x124(%rdi), %xmm0
pshufd $0xb4, %xmm0, %xmm0     # xmm0 = xmm0[0,1,3,2]
movdqa %xmm0, 0x30(%rcx)
movl 0x10c(%rdi), %eax
movl %eax, 0x40(%rcx)
movl 0x134(%rdi), %eax
movl %eax, 0x44(%rcx)
movl 0x80(%rdi), %eax
movl %eax, 0x48(%rcx)
movq 0xd0(%rdi), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x4c(%rcx)
movl 0xf8(%rdi), %eax
movl %eax, 0x54(%rcx)
movl 0xe8(%rdi), %eax
movl %eax, 0x58(%rcx)
movl 0xe0(%rdi), %eax
movl %eax, 0x5c(%rcx)
movl 0xd8(%rdi), %eax
movl %eax, 0x60(%rcx)
movl 0x23ead(%rip), %esi     # 0x296c0
movl $0x19, %edx
callq 0x4890
movl 0x23ec1(%rip), %edi     # 0x296e4
callq 0x1e2d8
leaq 0x19933(%rip), %rdi     # 0x1f162
leaq 0x23f4e(%rip), %rsi     # 0x29784
callq 0x115f5
leaq 0x19929(%rip), %rdi     # 0x1f16b
leaq 0x23f3f(%rip), %rsi     # 0x29788
callq 0x115f5
leaq 0x19927(%rip), %rdi     # 0x1f17c
leaq 0x23f30(%rip), %rsi     # 0x2978c
callq 0x115f5
movq 0x23e60(%rip), %rdi     # 0x296c8
movq 0x23761(%rip), %rsi     # 0x28fd0
callq 0x42b0
cmpb $0x0, 0x8(%rax)
je 0x58b5
movq %rax, %rbx
callq 0x76d0
testb %al, %al
je 0x58b5
movb $0x1, 0x23f03(%rip)     # 0x29790
movb 0xa(%rbx), %al
leaq 0x24579(%rip), %rcx     # 0x29e10
movb %al, (%rcx)
movq 0x23e28(%rip), %rdi     # 0x296c8
movl 0x23ed2(%rip), %esi     # 0x29778
movl $0x1, %edx
callq 0x4220
jmp 0x59f5
movb $0x0, 0x23ed4(%rip)     # 0x29790
leaq 0x198c7(%rip), %rdi     # 0x1f18a
xorl %eax, %eax
callq 0x4b50
movq 0x23df7(%rip), %rdi     # 0x296c8
movq 0x23720(%rip), %rsi     # 0x28ff8
callq 0x42b0
cmpb $0x0, 0x8(%rax)
je 0x59a0
movq 0x23dda(%rip), %rbx     # 0x296c8
movq %rbx, %rdi
callq 0x46d0
movq %rbx, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x4510
testq %rax, %rax
je 0x59a0
movl 0x8(%rax), %ebx
movq %rax, %rdi
callq 0x4460
testl %ebx, %ebx
je 0x59a0
movq 0x23da3(%rip), %rbx     # 0x296c8
movq %rbx, %rdi
callq 0x47c0
movq %rbx, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x4240
movq %rax, %rbx
movq %rax, %rdi
callq 0x4a90
movq %rax, %r14
movq %rbx, %rdi
callq 0x43c0
testl %eax, %eax
jle 0x5996
movl %eax, %r13d
xorl %ebp, %ebp
movq %rsp, %r15
movq (%r14,%rbp,8), %rax
movq %rax, (%rsp)
xorl %edi, %edi
movq %r15, %rsi
xorl %edx, %edx
callq 0x6a8c
movq %rax, %r12
movq %rax, %rdi
callq 0x7186
xorl %edi, %edi
xorl %esi, %esi
callq 0x813c
movq %r12, %rdi
movq %rax, %rsi
callq 0x822e
incq %rbp
cmpq %rbp, %r13
jne 0x595b
movq %rbx, %rdi
callq 0x4460
jmp 0x59f5
leaq 0x1980a(%rip), %rdi     # 0x1f1b1
xorl %eax, %eax
callq 0x4b50
movq %rsp, %rsi
movl $0x0, (%rsi)
movzwl 0x23dbe(%rip), %eax     # 0x2977c
movw %ax, 0x4(%rsi)
movzwl 0x23db7(%rip), %eax     # 0x29780
movw %ax, 0x6(%rsi)
xorl %edi, %edi
xorl %edx, %edx
callq 0x6a8c
movq %rax, %rbx
movq %rax, %rdi
callq 0x7186
xorl %edi, %edi
xorl %esi, %esi
callq 0x813c
movq %rbx, %rdi
movq %rax, %rsi
callq 0x822e
callq 0x1dab9
callq 0x1dcd6
callq 0x1dd9c
callq 0x1db67
movq 0x23cb8(%rip), %rbx     # 0x296c8
movq %rbx, %rdi
callq 0x4950
movq %rbx, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x4b10
movq %rax, %rbx
testq %rax, %rax
je 0x5a37
cmpl $0x1, 0x8(%rbx)
ja 0x5a37
callq 0x11510
movq %rbx, %rdi
callq 0x4460
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq