// File: /workspace/github_c_makefile_star2_O3/kaiserschmarrn0[P]mywm/window.c
// Function: create_window
static window *create_window(window *win, uint32_t x, uint32_t y, uint32_t w, uint32_t h) {
	if (!win->normal) {
		return win;
	}

	xcb_change_save_set(conn, XCB_SET_MODE_INSERT, win->windows[WIN_CHILD]);

	win->windows[WIN_PARENT] = xcb_generate_id(conn);
	
	uint32_t mask = XCB_CONFIG_WINDOW_BORDER_WIDTH;
	uint32_t vals[6];
	vals[0] = 0;
	xcb_configure_window(conn, win->windows[WIN_CHILD], mask, vals);
	
	mask = XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK |
			XCB_CW_COLORMAP;
	vals[0] = 0xffffffff;
	vals[1] = 0xffffffff;
	vals[2] = 0;
	vals[3] = XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY | XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_FOCUS_CHANGE;
	vals[4] = cm;
	xcb_create_window(conn, depth, win->windows[WIN_PARENT], scr->root, x, y, w, h, 0,
			XCB_WINDOW_CLASS_INPUT_OUTPUT, vis, mask, vals);

	win->gc = xcb_generate_id(conn);
	vals[0] = 0xffffff00;
	vals[1] = 0;
	xcb_create_gc(conn, win->gc, win->windows[WIN_PARENT], XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES, vals);

	for (int i = WIN_COUNT, j = 0; i < REGION_COUNT; i++, j++) {
		win->windows[i] = xcb_generate_id(conn);

		mask = XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_WIN_GRAVITY | 
				XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;
		vals[0] = controls[i - WIN_COUNT].colors[PM_BG(PM_FOCUS)];
		vals[1] = 0xff000000;
		vals[2] = controls[j].gravity;
		vals[3] = 0;
		vals[4] = XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_EXPOSURE |
				XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW;
		vals[5] = cm;
		xcb_create_window(conn, depth, win->windows[i], win->windows[WIN_PARENT],
				controls[j].geom.x, controls[j].geom.y, controls[j].geom.width, 
				controls[j].geom.height, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
				vis, mask, vals);

		xcb_map_window(conn, win->windows[i]);

		xcb_flush(conn);

		draw_region(win, i, PM_FOCUS);
	}

	xcb_reparent_window(conn, win->windows[WIN_CHILD], win->windows[WIN_PARENT], PAD, PAD_N);

	resize_region resize_regions[8] = {
		{ XCB_GRAVITY_NORTH_WEST, 144, { 0, 0, RESIZE_REGION_CORNER, RESIZE_REGION_CORNER } },
		{ XCB_GRAVITY_NORTH_WEST, 140, { RESIZE_REGION_CORNER, 0, w - 2 * RESIZE_REGION_CORNER, RESIZE_REGION_WIDTH } },
		{ XCB_GRAVITY_NORTH_EAST, 148, { w - RESIZE_REGION_CORNER, 0, RESIZE_REGION_CORNER, RESIZE_REGION_CORNER } },
		{ XCB_GRAVITY_NORTH_EAST, 98, { w - RESIZE_REGION_WIDTH, RESIZE_REGION_CORNER, RESIZE_REGION_WIDTH, h - 2 * RESIZE_REGION_CORNER } },
		{ XCB_GRAVITY_SOUTH_EAST, 78, { w - RESIZE_REGION_CORNER, h - RESIZE_REGION_CORNER, RESIZE_REGION_CORNER, RESIZE_REGION_CORNER } },
		{ XCB_GRAVITY_SOUTH_WEST, 18, { RESIZE_REGION_CORNER, h - RESIZE_REGION_WIDTH, w - 2 * RESIZE_REGION_CORNER, RESIZE_REGION_WIDTH } },
		{ XCB_GRAVITY_SOUTH_WEST, 76, { 0, h - RESIZE_REGION_CORNER, RESIZE_REGION_CORNER, RESIZE_REGION_CORNER } },
		{ XCB_GRAVITY_NORTH_WEST, 72, { 0, RESIZE_REGION_CORNER, RESIZE_REGION_WIDTH, h - RESIZE_REGION_CORNER } },
	};

	for (int i = 0; i < 8; i++) {
		win->resize_regions[i] = xcb_generate_id(conn);
		mask = XCB_CW_WIN_GRAVITY | XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
		vals[0] = resize_regions[i].gravity;
		vals[1] = XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_ENTER_WINDOW;
		vals[2] = resize_cursors[i];
		xcb_create_window(conn, XCB_COPY_FROM_PARENT, win->resize_regions[i],
			win->windows[WIN_PARENT], resize_regions[i].geom.x, resize_regions[i].geom.y,
			resize_regions[i].geom.width, resize_regions[i].geom.height, 0,
			XCB_WINDOW_CLASS_INPUT_ONLY, XCB_COPY_FROM_PARENT, mask, vals);
	}
	
	xcb_rectangle_t resize_region_rects[4][2] = {
		{ { 0, 0, RESIZE_REGION_CORNER, RESIZE_REGION_WIDTH }, { 0, RESIZE_REGION_WIDTH, RESIZE_REGION_WIDTH, RESIZE_REGION_CORNER - RESIZE_REGION_WIDTH } },
		{ { 0, 0, RESIZE_REGION_CORNER, RESIZE_REGION_WIDTH }, { RESIZE_REGION_CORNER - RESIZE_REGION_WIDTH, RESIZE_REGION_WIDTH, RESIZE_REGION_WIDTH, RESIZE_REGION_CORNER - RESIZE_REGION_WIDTH } },
		{ { RESIZE_REGION_CORNER - RESIZE_REGION_WIDTH, 0, RESIZE_REGION_WIDTH, RESIZE_REGION_CORNER }, { 0, RESIZE_REGION_CORNER - RESIZE_REGION_WIDTH, RESIZE_REGION_CORNER, RESIZE_REGION_CORNER - RESIZE_REGION_WIDTH } },
		{ { 0, 0, RESIZE_REGION_WIDTH, RESIZE_REGION_CORNER }, { RESIZE_REGION_WIDTH, RESIZE_REGION_CORNER - RESIZE_REGION_WIDTH, RESIZE_REGION_CORNER - RESIZE_REGION_WIDTH, RESIZE_REGION_WIDTH } }, 
	};

	for (int i = 0; i < 4; i++) {
		xcb_shape_rectangles(conn, XCB_SHAPE_SO_SET, XCB_SHAPE_SK_BOUNDING,
			XCB_CLIP_ORDERING_UNSORTED, win->resize_regions[2 * i], 0, 0, 2,
			resize_region_rects[i]);
	}

	for (int i = 0; i < 8; i++) {
		xcb_map_window(conn, win->resize_regions[i]);
	}

	vals[0] = x;
	vals[1] = y;
	vals[2] = w;
	vals[3] = h;
	update_geometry(win, MOVE_RESIZE_MASK, vals);

	mask = XCB_CW_EVENT_MASK;
	vals[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
	xcb_change_window_attributes(conn, win->windows[WIN_CHILD], mask, vals);

	normal_events(win);
	
	show(win);

	if (state != MOVE && state != RESIZE) {
		focus(win);
	} else {
		unfocus(win);
	}

	return win;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdi, %rbx
cmpl $0x0, 0xc0(%rdi)
je 0x62d8
movl %r8d, %r15d
movl %ecx, %r12d
leaq 0x7649(%rip), %r13      # 0xd448
movq (%r13), %rdi
movl %edx, -0x30(%rbp)
movl 0xcc(%rbx), %edx
movl %esi, -0x2c(%rbp)
xorl %esi, %esi
callq 0x3510
movq (%r13), %rdi
callq 0x3560
movl %eax, 0xd0(%rbx)
leaq -0x50(%rbp), %r14
xorl %eax, %eax
movl %eax, (%r14)
movq (%r13), %rdi
movl 0xcc(%rbx), %esi
movl $0x10, %edx
movq %r14, %rcx
callq 0x31e0
movaps 0x32f4(%rip), %xmm0     # 0x9140
movaps %xmm0, (%r14)
leaq 0x7665(%rip), %rax      # 0xd4bc
movl (%rax), %eax
movl %eax, 0x10(%r14)
movq (%r13), %rdi
leaq 0x7658(%rip), %rax      # 0xd4c0
movzbl (%rax), %esi
movl 0xd0(%rbx), %edx
leaq 0x75d8(%rip), %rax      # 0xd450
movq (%rax), %rax
movl (%rax), %ecx
leaq 0x7634(%rip), %rax      # 0xd4b8
movl (%rax), %eax
subq $0x8, %rsp
movl $0xffff, %r10d          # imm = 0xFFFF
movq %r15, -0x60(%rbp)
movl %r15d, %r11d
andl %r10d, %r11d
movq %r12, -0x58(%rbp)
andl %r12d, %r10d
movswl -0x2c(%rbp), %r8d
movswl -0x30(%rbp), %r9d
pushq %r14
pushq $0x2a0a                 # imm = 0x2A0A
pushq %rax
pushq $0x1
xorl %eax, %eax
pushq %rax
pushq %r11
pushq %r10
callq 0x3030
addq $0x40, %rsp
movq (%r13), %rdi
callq 0x3560
movl %eax, 0x104(%rbx)
movl $0xffffff00, %ecx       # imm = 0xFFFFFF00
movq %rcx, (%r14)
movq (%r13), %rdi
movl 0xd0(%rbx), %edx
movl %eax, %esi
movl $0x10004, %ecx          # imm = 0x10004
movq %r14, %r8
callq 0x3490
leaq 0x6d84(%rip), %r15      # 0xcc80
movl $0x35, %r14d
movl $0x20, %r12d
movq (%r13), %rdi
callq 0x3560
movl %eax, (%rbx,%r14,4)
movaps 0x3234(%rip), %xmm0     # 0x9150
movaps %xmm0, -0x50(%rbp)
movl $0x28030, -0x40(%rbp)   # imm = 0x28030
leaq 0x758e(%rip), %rcx      # 0xd4bc
movl (%rcx), %ecx
movl %ecx, -0x3c(%rbp)
movq (%r13), %rdi
leaq 0x7582(%rip), %rcx      # 0xd4c0
movzbl (%rcx), %esi
movl 0xd0(%rbx), %ecx
movswl (%r15), %r8d
leaq 0x7566(%rip), %rdx      # 0xd4b8
movl (%rdx), %r10d
subq $0x8, %rsp
movl %eax, %edx
xorl %r9d, %r9d
leaq -0x50(%rbp), %rax
pushq %rax
pushq $0x2a2a                 # imm = 0x2A2A
pushq %r10
pushq $0x1
pushq $0x0
pushq %r12
pushq %r12
callq 0x3030
addq $0x40, %rsp
movq (%r13), %rdi
movl (%rbx,%r14,4), %esi
callq 0x3240
movq (%r13), %rdi
callq 0x3360
leal -0x33(%r14), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x58ef
addq $0x50, %r15
incq %r14
cmpq $0x37, %r14
jne 0x5f08
movq (%r13), %rdi
movl 0xcc(%rbx), %esi
movl 0xd0(%rbx), %edx
movl $0x1, %ecx
movl $0x20, %r8d
callq 0x3310
movabsq $0x9000000001, %rax     # imm = 0x9000000001
movq %rax, -0xe0(%rbp)
movabsq $0x20002000000000, %rax # imm = 0x20002000000000
movq %rax, -0xd8(%rbp)
movabsq $0x8c00000001, %rax     # imm = 0x8C00000001
movq %rax, -0xd0(%rbp)
movl $0x20, -0xc8(%rbp)
movq -0x58(%rbp), %r8
leal -0x40(%r8), %ecx
movw %cx, -0xc4(%rbp)
movw $0x8, %ax
movw %ax, -0xc2(%rbp)
movabsq $0x9400000003, %rdx     # imm = 0x9400000003
movq %rdx, -0xc0(%rbp)
leal -0x20(%r8), %edx
movw %dx, -0xb8(%rbp)
movl $0x200000, -0xb6(%rbp)  # imm = 0x200000
movw $0x20, %si
movw %si, -0xb2(%rbp)
movabsq $0x6200000003, %rdi     # imm = 0x6200000003
movq %rdi, -0xb0(%rbp)
leal -0x8(%r8), %edi
movw %di, -0xa8(%rbp)
movl $0x80020, -0xa6(%rbp)   # imm = 0x80020
movq -0x60(%rbp), %r8
leal -0x40(%r8), %edi
movw %di, -0xa2(%rbp)
movabsq $0x4e00000009, %rdi     # imm = 0x4E00000009
movq %rdi, -0xa0(%rbp)
movw %dx, -0x98(%rbp)
leal -0x20(%r8), %edx
movw %dx, -0x96(%rbp)
movabsq $0x700200020, %rdi      # imm = 0x700200020
movq %rdi, -0x94(%rbp)
movl $0x12, -0x8c(%rbp)
movw %si, -0x88(%rbp)
leal -0x8(%r8), %esi
movw %si, -0x86(%rbp)
movw %cx, -0x84(%rbp)
movw %ax, -0x82(%rbp)
movabsq $0x4c00000007, %rcx     # imm = 0x4C00000007
movq %rcx, -0x80(%rbp)
movw $0x0, -0x78(%rbp)
movw %dx, -0x76(%rbp)
movaps 0x3056(%rip), %xmm0     # 0x9160
movups %xmm0, -0x74(%rbp)
movw %ax, -0x64(%rbp)
movw %dx, -0x62(%rbp)
xorl %r14d, %r14d
leaq 0x77f0(%rip), %r15      # 0xd910
xorl %r12d, %r12d
movq (%r13), %rdi
callq 0x3560
movl %eax, 0xe4(%rbx,%r12)
movl -0xe0(%rbp,%r12,4), %ecx
movl %ecx, -0x50(%rbp)
movl $0x14, -0x4c(%rbp)
movl (%r12,%r15), %ecx
movl %ecx, -0x48(%rbp)
movq (%r13), %rdi
movl 0xd0(%rbx), %ecx
movswl -0xd8(%rbp,%r12,4), %r8d
movswl -0xd6(%rbp,%r12,4), %r9d
movzwl -0xd4(%rbp,%r12,4), %r10d
movzwl -0xd2(%rbp,%r12,4), %r11d
subq $0x8, %rsp
xorl %esi, %esi
movl %eax, %edx
leaq -0x50(%rbp), %rax
pushq %rax
pushq $0x4820                 # imm = 0x4820
pushq %r14
pushq $0x2
pushq %r14
pushq %r11
pushq %r10
callq 0x3030
addq $0x40, %rsp
addq $0x4, %r12
cmpq $0x20, %r12
jne 0x6123
movaps 0x2ffb(%rip), %xmm0     # 0x91b0
leaq -0x130(%rbp), %r14
movaps %xmm0, 0x30(%r14)
movaps 0x2fd8(%rip), %xmm0     # 0x91a0
movaps %xmm0, 0x20(%r14)
movaps 0x2fbc(%rip), %xmm0     # 0x9190
movaps %xmm0, 0x10(%r14)
movaps 0x2fa0(%rip), %xmm0     # 0x9180
movaps %xmm0, (%r14)
movl $0xe4, %r15d
movq (%r13), %rdi
movl (%rbx,%r15), %r8d
subq $0x8, %rsp
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r9d, %r9d
pushq %r14
pushq $0x2
pushq $0x0
callq 0x3260
addq $0x20, %rsp
addq $0x8, %r15
addq $0x10, %r14
cmpq $0x104, %r15            # imm = 0x104
jne 0x61ea
movl $0x39, %r14d
movq (%r13), %rdi
movl (%rbx,%r14,4), %esi
callq 0x3240
incq %r14
cmpq $0x41, %r14
jne 0x6225
leaq -0x50(%rbp), %r12
movl -0x2c(%rbp), %eax
movl %eax, (%r12)
movl -0x30(%rbp), %eax
movl %eax, 0x4(%r12)
movq -0x58(%rbp), %rax
movl %eax, 0x8(%r12)
movq -0x60(%rbp), %rax
movl %eax, 0xc(%r12)
movq %rbx, %rdi
movl $0xf, %esi
movq %r12, %rdx
callq 0x5498
movl $0x400000, (%r12)       # imm = 0x400000
movq (%r13), %rdi
movl 0xcc(%rbx), %esi
movl $0x800, %edx            # imm = 0x800
movq %r12, %rcx
callq 0x33b0
leaq -0xe4(%rbp), %rcx
movl $0x18001c, (%rcx)       # imm = 0x18001C
movq (%r13), %rdi
movl 0xd0(%rbx), %esi
movl $0x800, %edx            # imm = 0x800
callq 0x33b0
movq %rbx, %rdi
callq 0x52c1
leaq 0x7185(%rip), %rax      # 0xd444
movl (%rax), %eax
addl $-0x3, %eax
movq %rbx, %rdi
cmpl $-0x2, %eax
jae 0x62d3
callq 0x5181
jmp 0x62d8
callq 0x5011
movq %rbx, %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq