// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movl %edi, %ebx
movl $0x1, %edi
movl $0x28, %esi
callq 0x3340
movq %rax, %r14
movl %ebx, (%rax)
leaq 0x5b4d(%rip), %rax      # 0xc8f8
movq (%rax), %rdi
callq 0x3230
movl %eax, 0x4(%r14)
leaq 0x5b22(%rip), %r15      # 0xc8e0
cmpq $0x0, (%r15)
je 0x6e9e
movq (%r15), %rdi
callq 0x3849
testq %rax, %rax
je 0x6e9e
movq %rax, %r13
cmpl $0x1, 0x8(%rax)
jne 0x6deb
movl %ebx, %edi
callq 0x7ada
jmp 0x6df2
movl %ebx, %edi
callq 0x7ace
movq (%r13), %rdi
movq %rax, %rsi
callq 0x6b52
testw %ax, %ax
je 0x6dc8
movq %r13, 0x10(%r14)
movq (%r15), %rdi
callq 0x387d
callq 0x69c8
movq %rax, %r12
movq 0x10(%r13), %rdi
testq %rdi, %rdi
je 0x6ea6
leaq 0x139a(%rip), %rsi      # 0x81c5
callq 0x4de5
testq %rax, %rax
je 0x6e50
movq %rax, %r15
movq (%rax), %rdi
callq 0x6987
testq %rax, %rax
je 0x6e50
movq (%r15), %rdi
callq 0x6987
movq %rax, %r12
movq 0x10(%r13), %rdi
leaq 0x1bae(%rip), %rsi      # 0x8a09
callq 0x4de5
xorl %ebp, %ebp
testq %rax, %rax
je 0x701f
movl $0x1, %edi
movl $0x10, %esi
movq %rax, 0x8(%rsp)
callq 0x3340
movq %rax, %r15
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movq %r12, %rdi
callq 0x6099
movups (%rax), %xmm0
movups %xmm0, (%r15)
jmp 0x7022
callq 0x69c8
movq %rax, %r12
leaq 0x5a4b(%rip), %r13      # 0xc8f8
movl $0x1, %edi
movl $0x10, %esi
callq 0x3340
movq %rax, %r15
movq %r12, %rdi
callq 0x6132
movl %eax, (%r15)
movq %r12, %rdi
callq 0x6152
movl %eax, 0x4(%r15)
movq %r12, %rdi
callq 0x60f4
movl %eax, 0x8(%r15)
movq %r12, %rdi
callq 0x6113
movl %eax, 0xc(%r15)
movl 0x24(%r14), %ebp
movq %r12, 0x18(%r14)
testl %ebp, %ebp
jne 0x6f03
movl 0x20(%r12), %eax
movl %eax, 0x24(%r14)
leaq 0x10(%rsp), %r12
movq $0x0, (%r12)
movl $0x1, %eax
movl %eax, 0x8(%r12)
leaq 0x59ef(%rip), %rcx      # 0xc910
movl (%rcx), %ecx
movl %ecx, 0xc(%r12)
movq (%r13), %rdi
movl 0x4(%r14), %ebp
leaq 0x59c9(%rip), %rcx      # 0xc900
movq (%rcx), %rcx
movl (%rcx), %ecx
leaq 0x59c5(%rip), %rdx      # 0xc908
movq (%rdx), %rdx
movl (%rdx), %r10d
subq $0x8, %rsp
movl $0x20, %esi
movl %ebp, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %r12
pushq $0x220a                 # imm = 0x220A
pushq %r10
pushq %rax
pushq $0x0
pushq %rax
pushq %rax
callq 0x3150
addq $0x40, %rsp
movl $0x80004, (%r12)        # imm = 0x80004
movq (%r13), %rdi
movl %ebp, %esi
movl $0x800, %edx            # imm = 0x800
movq %r12, %rcx
callq 0x3170
cmpq $0x0, 0x5954(%rip)      # 0xc8e8
jne 0x6fa2
callq 0x3818
movq %rax, 0x5946(%rip)      # 0xc8e8
movq (%r13), %rdi
movl %ebx, %esi
movl %ebp, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3310
movq %r14, %rdi
movq %r15, %rsi
callq 0x7052
movq 0x18(%r14), %rax
movl 0x20(%rax), %eax
cmpl 0x24(%r14), %eax
jne 0x6fd5
movl 0x4(%r14), %edi
callq 0x7694
movq 0x590c(%rip), %rdi      # 0xc8e8
movq %r14, %rsi
callq 0x3884
leaq 0x175c(%rip), %rsi      # 0x8747
leaq 0x177c(%rip), %rdx      # 0x876e
leaq 0x175e(%rip), %r8       # 0x8757
movl $0x3, %edi
movl $0xc4, %ecx
movl %ebx, %r9d
xorl %eax, %eax
callq 0x51d6
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
movq 0x10(%r13), %rdi
leaq 0x101e(%rip), %rsi      # 0x804b
callq 0x4de5
testq %rax, %rax
je 0x703d
movl (%rax), %ebp
movl %ebp, 0x24(%r14)
testq %r15, %r15
leaq 0x58b1(%rip), %r13      # 0xc8f8
jne 0x6ef2
jmp 0x6ead