// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
movl %edi, %ebx
movl $0x1, %edi
movl $0x28, %esi
callq 0x3340
movq %rax, %r14
movl %ebx, (%rax)
movl $0x0, 0x20(%rax)
leaq 0x5afe(%rip), %rbp      # 0xc8f8
movq (%rbp), %rdi
callq 0x3230
movl %eax, 0x4(%r14)
movq $0x0, 0x10(%r14)
leaq 0x5aca(%rip), %r12      # 0xc8e0
cmpq $0x0, (%r12)
je 0x6e61
movq (%r12), %rdi
callq 0x3851
testq %rax, %rax
je 0x6e61
movq %rax, %r15
cmpl $0x1, 0x8(%rax)
jne 0x6e3d
movl %ebx, %edi
callq 0x7b27
jmp 0x6e44
movl %ebx, %edi
callq 0x7b1b
movq (%r15), %rdi
movq %rax, %rsi
callq 0x6b96
testw %ax, %ax
je 0x6e1d
movq %r15, 0x10(%r14)
movq (%r12), %rdi
callq 0x3885
callq 0x69f2
movq %rax, %r12
movq 0x10(%r14), %rax
testq %rax, %rax
je 0x6eea
movq 0x10(%rax), %rdi
testq %rdi, %rdi
je 0x6eea
leaq 0x1347(%rip), %rsi      # 0x81c9
callq 0x4e15
testq %rax, %rax
je 0x6ea7
movq %rax, %r15
movq (%rax), %rdi
callq 0x69ad
testq %rax, %rax
je 0x6ea7
movq (%r15), %rdi
callq 0x69ad
movq %rax, %r12
movq 0x10(%r14), %rax
movq 0x10(%rax), %rdi
leaq 0x1ae5(%rip), %rsi      # 0x899b
callq 0x4e15
testq %rax, %rax
je 0x6eef
movq %rax, %r13
movl $0x1, %edi
movl $0x10, %esi
callq 0x3340
movq %rax, %r15
movq (%r13), %rsi
movq %r12, %rdi
callq 0x608a
movups (%rax), %xmm0
movups %xmm0, (%r15)
jmp 0x6ef2
xorl %r15d, %r15d
jmp 0x6f11
xorl %r15d, %r15d
movq 0x10(%r14), %rax
movq 0x10(%rax), %rdi
leaq 0x114c(%rip), %rsi      # 0x804d
callq 0x4e15
testq %rax, %rax
je 0x6f11
movl (%rax), %eax
movl %eax, 0x24(%r14)
testq %r15, %r15
jne 0x6f57
movl $0x1, %edi
movl $0x10, %esi
callq 0x3340
movq %rax, %r15
movq %r12, %rdi
callq 0x6122
movl %eax, (%r15)
movq %r12, %rdi
callq 0x6142
movl %eax, 0x4(%r15)
movq %r12, %rdi
callq 0x60e4
movl %eax, 0x8(%r15)
movq %r12, %rdi
callq 0x6103
movl %eax, 0xc(%r15)
movq %r12, 0x18(%r14)
cmpl $0x0, 0x24(%r14)
jne 0x6f6b
movl 0x20(%r12), %eax
movl %eax, 0x24(%r14)
movq %rsp, %r12
movq $0x0, (%r12)
movl $0x1, %eax
movl %eax, 0x8(%r12)
leaq 0x5989(%rip), %rcx      # 0xc910
movl (%rcx), %ecx
movl %ecx, 0xc(%r12)
movq (%rbp), %rdi
movl 0x4(%r14), %edx
leaq 0x5963(%rip), %rcx      # 0xc900
movq (%rcx), %rcx
movl (%rcx), %ecx
leaq 0x595f(%rip), %rsi      # 0xc908
movq (%rsi), %rsi
movl (%rsi), %r10d
subq $0x8, %rsp
movl $0x20, %esi
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 (%rbp), %rdi
movl 0x4(%r14), %esi
movl $0x800, %edx            # imm = 0x800
movq %r12, %rcx
callq 0x3170
cmpq $0x0, 0x58ee(%rip)      # 0xc8e8
jne 0x7008
callq 0x3818
movq %rax, 0x58e0(%rip)      # 0xc8e8
movq (%rbp), %rdi
movl 0x4(%r14), %edx
movl %ebx, %esi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3310
movq %r14, %rdi
movq %r15, %rsi
callq 0x7087
movq 0x18(%r14), %rax
movl 0x20(%rax), %eax
cmpl 0x24(%r14), %eax
jne 0x703d
movl 0x4(%r14), %edi
callq 0x76d4
movq 0x58a4(%rip), %rdi      # 0xc8e8
movq %r14, %rsi
callq 0x388c
leaq 0x1686(%rip), %rsi      # 0x86d9
leaq 0x16a6(%rip), %rdx      # 0x8700
leaq 0x1688(%rip), %r8       # 0x86e9
movl $0x3, %edi
movl $0xc4, %ecx
movl %ebx, %r9d
xorl %eax, %eax
callq 0x5206
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq