// File: /workspace/github_c_makefile_star2_O2/comfies[P]custard/src/wm/window.c
// Function: manage_window
window_t *manage_window(xcb_window_t window_id) {
    window_t *window = (window_t*)calloc(1, sizeof(window_t));
    window->id = window_id;
    window->fullscreen = window->floating = 0;
    window->parent = xcb_generate_id(xcb_connection);

    window->rule = NULL;
    if (rules) {
        rule_t *rule;
        char *subject;

        while ((rule = vector_iterator(rules))) {
            if (rule->attribute == class)
                subject = class_of_window(window_id);
            else
                subject = name_of_window(window_id);

            if (expression_matches(rule->expression, subject)) {
                window->rule = rule;
                reset_vector_iterator(rules);
                break;
            }
        }
    };

    grid_geometry_t *geometry = NULL;
    monitor_t *monitor = monitor_with_cursor_residence();

    if (window->rule && window->rule->rules) {
        kv_value_t *value;
        value = get_value_from_key(window->rule->rules, "monitor");

        if (value)
            if (monitor_from_name(value->string))
                monitor = monitor_from_name(value->string);

        value = get_value_from_key(window->rule->rules, "geometry");
        if (value) {
            geometry = (grid_geometry_t *)calloc(1, sizeof(grid_geometry_t));
            memcpy(geometry,
                get_geometry_from_monitor(monitor, value->string),
                sizeof(grid_geometry_t));
        }

        value = get_value_from_key(window->rule->rules, "workspace");
        if (value)
            window->workspace = value->number;
    }

    if (!geometry) {
        geometry = (grid_geometry_t*)calloc(1, sizeof(grid_geometry_t));

        geometry->x = calculate_default_x(monitor);
        geometry->y = calculate_default_y(monitor);
        geometry->height = calculate_default_height(monitor);
        geometry->width = calculate_default_width(monitor);
    }

    window->monitor = monitor;
    if (!window->workspace)
        window->workspace = monitor->workspace;

    /* Parent window creation */

    unsigned int values[] = {
        0, 0, 1, screen_colormap
    };
    unsigned int masked_values = XCB_CW_BACK_PIXEL |
        XCB_CW_BORDER_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_COLORMAP;

    xcb_create_window(xcb_connection, 32,
        window->parent, xcb_screen->root,
        0, 0, 1, 1,
        0 /* border size */,
        XCB_WINDOW_CLASS_INPUT_OUTPUT, screen_visual->visual_id,
        masked_values, values);

    values[0] = XCB_EVENT_MASK_BUTTON_PRESS |
        XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
    masked_values = XCB_CW_EVENT_MASK;

    xcb_change_window_attributes(xcb_connection,
        window->parent, masked_values, values);

    /* Finalization */

    if (!windows)
        windows = construct_vector();

    xcb_reparent_window(xcb_connection,
        window_id, window->parent, 0, 0);
    set_window_geometry(window, geometry);

    if (window->monitor->workspace == window->workspace)
        map_window(window->parent);

    push_to_vector(windows, window);
    log_debug("Window(%08x) managed", window_id);

    return window;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edi, %ebx
pushq $0x1
popq %rdi
pushq $0x28
popq %rsi
callq 0x3340
movq %rax, %r14
movl %ebx, (%rax)
leaq 0x5f4b(%rip), %rax      # 0xc8e8
movq (%rax), %rdi
callq 0x3230
movl %eax, 0x4(%r14)
leaq 0x5f20(%rip), %r15      # 0xc8d0
cmpq $0x0, (%r15)
je 0x69fb
movq (%r15), %rdi
callq 0x383d
testq %rax, %rax
je 0x69fb
movq %rax, %r13
cmpl $0x1, 0x8(%rax)
jne 0x69d5
movl %ebx, %edi
callq 0x75af
jmp 0x69dc
movl %ebx, %edi
callq 0x75a5
movq (%r13), %rdi
movq %rax, %rsi
callq 0x6764
testw %ax, %ax
je 0x69b6
movq %r13, 0x10(%r14)
movq (%r15), %rdi
callq 0x3871
jmp 0x69fe
xorl %r13d, %r13d
callq 0x65f0
movq %rax, %r15
testq %r13, %r13
je 0x6a85
movq 0x10(%r13), %rdi
testq %rdi, %rdi
je 0x6a85
leaq 0x17aa(%rip), %rsi      # 0x81c5
callq 0x4d26
testq %rax, %rax
je 0x6a41
movq %rax, %r12
movq (%rax), %rdi
callq 0x65af
testq %rax, %rax
je 0x6a41
movq (%r12), %rdi
callq 0x65af
movq %rax, %r15
movq 0x10(%r13), %rdi
leaq 0x1fad(%rip), %rsi      # 0x89f9
callq 0x4d26
xorl %ebp, %ebp
testq %rax, %rax
je 0x6ad3
pushq $0x1
popq %rdi
pushq $0x10
popq %rsi
movq %rax, 0x38(%rsp)
callq 0x3340
movq %rax, %r12
movq 0x38(%rsp), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0x5d9b
movups (%rax), %xmm0
movups %xmm0, (%r12)
jmp 0x6ad6
leaq 0x5e5c(%rip), %r13      # 0xc8e8
pushq $0x1
popq %rdi
pushq $0x10
popq %rsi
callq 0x3340
movq %rax, %r12
movq %r15, %rdi
callq 0x5e2e
movl %eax, (%r12)
movq %r15, %rdi
callq 0x5e4e
movl %eax, 0x4(%r12)
movq %r15, %rdi
callq 0x5df4
movl %eax, 0x8(%r12)
movq %r15, %rdi
callq 0x5e11
movl %eax, 0xc(%r12)
movl 0x24(%r14), %ebp
jmp 0x6afd
xorl %r12d, %r12d
movq 0x10(%r13), %rdi
leaq 0x156a(%rip), %rsi      # 0x804b
callq 0x4d26
testq %rax, %rax
je 0x6af1
movl (%rax), %ebp
movl %ebp, 0x24(%r14)
testq %r12, %r12
leaq 0x5ded(%rip), %r13      # 0xc8e8
je 0x6a8c
movq %r15, 0x18(%r14)
testl %ebp, %ebp
jne 0x6b0d
movl 0x20(%r15), %eax
movl %eax, 0x24(%r14)
leaq 0x40(%rsp), %r15
andq $0x0, (%r15)
pushq $0x1
popq %rax
movl %eax, 0x8(%r15)
leaq 0x5ddc(%rip), %rcx      # 0xc900
movl (%rcx), %ecx
movl %ecx, 0xc(%r15)
movq (%r13), %rdi
movl 0x4(%r14), %ebp
leaq 0x5db7(%rip), %rcx      # 0xc8f0
movq (%rcx), %rcx
movl (%rcx), %ecx
leaq 0x5db3(%rip), %rdx      # 0xc8f8
movq (%rdx), %rdx
movl (%rdx), %edx
movq %r15, 0x30(%rsp)
movl %edx, 0x20(%rsp)
movl %eax, 0x18(%rsp)
movl %eax, 0x8(%rsp)
movl %eax, (%rsp)
andl $0x0, 0x10(%rsp)
movl $0x220a, 0x28(%rsp)     # imm = 0x220A
pushq $0x20
popq %rsi
movl %ebp, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x3150
movl $0x80004, (%r15)        # imm = 0x80004
movq (%r13), %rdi
movl %ebp, %esi
movl $0x800, %edx            # imm = 0x800
movq %r15, %rcx
callq 0x3170
cmpq $0x0, 0x5d3b(%rip)      # 0xc8d8
jne 0x6bab
callq 0x3808
movq %rax, 0x5d2d(%rip)      # 0xc8d8
movq (%r13), %rdi
movl %ebx, %esi
movl %ebp, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3310
movq %r14, %rdi
movq %r12, %rsi
callq 0x6c26
movq 0x18(%r14), %rax
movl 0x20(%rax), %eax
cmpl 0x24(%r14), %eax
jne 0x6bde
movl 0x4(%r14), %edi
callq 0x7244
movq 0x5cf3(%rip), %rdi      # 0xc8d8
movq %r14, %rsi
callq 0x3878
leaq 0x1b43(%rip), %rsi      # 0x8737
leaq 0x1b63(%rip), %rdx      # 0x875e
leaq 0x1b45(%rip), %r8       # 0x8747
pushq $0x3
popq %rdi
movl $0xc4, %ecx
movl %ebx, %r9d
xorl %eax, %eax
callq 0x50dd
movq %r14, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq