// File: /workspace/github_c_makefile_star2_O2/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 $0xf8, %rsp
movq %rdi, %rbx
cmpl $0x0, 0xc0(%rdi)
je 0x5ffc
movl %r8d, %r14d
movl %ecx, %r15d
leaq 0x7906(%rip), %r13      # 0xd448
movq (%r13), %rdi
movl %edx, -0x30(%rbp)
movl 0xcc(%rbx), %edx
movl %esi, -0x2c(%rbp)
xorl %esi, %esi
callq 0x3500
movq (%r13), %rdi
callq 0x3550
movl %eax, 0xd0(%rbx)
leaq -0x50(%rbp), %r12
andl $0x0, (%r12)
movq (%r13), %rdi
movl 0xcc(%rbx), %esi
pushq $0x10
popq %rdx
movq %r12, %rcx
callq 0x31d0
movaps 0x35c3(%rip), %xmm0     # 0x9150
movaps %xmm0, (%r12)
leaq 0x7923(%rip), %rax      # 0xd4bc
movl (%rax), %eax
movl %eax, 0x10(%r12)
movq (%r13), %rdi
leaq 0x7915(%rip), %rax      # 0xd4c0
movzbl (%rax), %esi
movl 0xd0(%rbx), %edx
leaq 0x7895(%rip), %rax      # 0xd450
movq (%rax), %rax
movl (%rax), %ecx
leaq 0x78f1(%rip), %rax      # 0xd4b8
movl (%rax), %eax
subq $0x8, %rsp
movl $0xffff, %r10d          # imm = 0xFFFF
movq %r14, -0x60(%rbp)
movl %r14d, %r11d
andl %r10d, %r11d
movq %r15, -0x58(%rbp)
andl %r15d, %r10d
movswl -0x2c(%rbp), %r8d
movswl -0x30(%rbp), %r9d
pushq %r12
pushq $0x2a0a                 # imm = 0x2A0A
pushq %rax
pushq $0x1
pushq $0x0
pushq %r11
pushq %r10
callq 0x3030
addq $0x40, %rsp
movq (%r13), %rdi
callq 0x3550
movl %eax, 0x104(%rbx)
movl $0xffffff00, %ecx       # imm = 0xFFFFFF00
movq %rcx, (%r12)
movq (%r13), %rdi
movl 0xd0(%rbx), %edx
movl %eax, %esi
movl $0x10004, %ecx          # imm = 0x10004
movq %r12, %r8
callq 0x3480
pushq $0x35
popq %r15
leaq 0x703d(%rip), %r14      # 0xcc80
pushq $0x20
popq %r12
movq (%r13), %rdi
cmpq $0x37, %r15
je 0x5cf4
callq 0x3550
movl %eax, (%rbx,%r15,4)
movaps 0x34fb(%rip), %xmm0     # 0x9160
movaps %xmm0, -0x50(%rbp)
movl $0x28030, -0x40(%rbp)   # imm = 0x28030
leaq 0x7845(%rip), %rcx      # 0xd4bc
movl (%rcx), %ecx
movl %ecx, -0x3c(%rbp)
movq (%r13), %rdi
leaq 0x7839(%rip), %rcx      # 0xd4c0
movzbl (%rcx), %esi
movl 0xd0(%rbx), %ecx
movswl (%r14), %r8d
leaq 0x781d(%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,%r15,4), %esi
callq 0x3230
movq (%r13), %rdi
callq 0x3350
leal -0x33(%r15), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x561b
incq %r15
addq $0x50, %r14
jmp 0x5c47
movl 0xcc(%rbx), %esi
movl 0xd0(%rbx), %edx
pushq $0x1
popq %rcx
pushq $0x20
popq %r8
callq 0x3300
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)
andw $0x0, -0x78(%rbp)
movw %dx, -0x76(%rbp)
movaps 0x332b(%rip), %xmm0     # 0x9170
movups %xmm0, -0x74(%rbp)
movw %ax, -0x64(%rbp)
movw %dx, -0x62(%rbp)
xorl %r14d, %r14d
leaq 0x7ab5(%rip), %r15      # 0xd910
cmpq $0x20, %r14
je 0x5eeb
movq (%r13), %rdi
callq 0x3550
movl %eax, 0xe4(%rbx,%r14)
movl -0xe0(%rbp,%r14,4), %ecx
movl %ecx, -0x50(%rbp)
movl $0x14, -0x4c(%rbp)
movl (%r14,%r15), %ecx
movl %ecx, -0x48(%rbp)
movq (%r13), %rdi
movl 0xd0(%rbx), %ecx
movswl -0xd8(%rbp,%r14,4), %r8d
movswl -0xd6(%rbp,%r14,4), %r9d
movzwl -0xd4(%rbp,%r14,4), %r10d
movzwl -0xd2(%rbp,%r14,4), %r11d
subq $0x8, %rsp
xorl %esi, %esi
movl %eax, %edx
leaq -0x50(%rbp), %rax
pushq %rax
pushq $0x4820                 # imm = 0x4820
pushq $0x0
pushq $0x2
pushq $0x0
pushq %r11
pushq %r10
callq 0x3030
addq $0x40, %rsp
addq $0x4, %r14
jmp 0x5e5b
movaps 0x32ce(%rip), %xmm0     # 0x91c0
leaq -0x120(%rbp), %r14
movaps %xmm0, 0x30(%r14)
movaps 0x32ab(%rip), %xmm0     # 0x91b0
movaps %xmm0, 0x20(%r14)
movaps 0x328f(%rip), %xmm0     # 0x91a0
movaps %xmm0, 0x10(%r14)
movaps 0x3273(%rip), %xmm0     # 0x9190
movaps %xmm0, (%r14)
movl $0xe4, %r15d
cmpq $0x104, %r15            # imm = 0x104
je 0x5f5e
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 0x3250
addq $0x20, %rsp
addq $0x8, %r15
addq $0x10, %r14
jmp 0x5f27
pushq $0x39
popq %r14
movq -0x58(%rbp), %r15
cmpq $0x41, %r14
je 0x5f7e
movq (%r13), %rdi
movl (%rbx,%r14,4), %esi
callq 0x3230
incq %r14
jmp 0x5f66
leaq -0x50(%rbp), %r12
movl -0x2c(%rbp), %eax
movl %eax, (%r12)
movl -0x30(%rbp), %eax
movl %eax, 0x4(%r12)
movl %r15d, 0x8(%r12)
movq -0x60(%rbp), %rax
movl %eax, 0xc(%r12)
pushq $0xf
popq %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x5243
movl $0x400000, (%r12)       # imm = 0x400000
movq (%r13), %rdi
movl 0xcc(%rbx), %esi
movl $0x800, %edx            # imm = 0x800
movq %r12, %rcx
callq 0x33a0
movq %rbx, %rdi
callq 0x4c4a
movq %rbx, %rdi
callq 0x5082
leaq 0x7461(%rip), %rax      # 0xd444
movl (%rax), %eax
addl $-0x3, %eax
movq %rbx, %rdi
cmpl $-0x2, %eax
jae 0x5ff7
callq 0x4f64
jmp 0x5ffc
callq 0x4dde
movq %rbx, %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq