// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movl %edi, -0x4(%rbp)
movl $0x1, %edi
movl $0x28, %esi
callq 0x3350
movq %rax, -0x10(%rbp)
movl -0x4(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movw $0x0, 0x22(%rax)
movq -0x10(%rbp), %rax
movw $0x0, 0x20(%rax)
leaq 0x5783(%rip), %rax      # 0xd8f8
movq (%rax), %rdi
callq 0x3230
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x10(%rax)
leaq 0x5747(%rip), %rax      # 0xd8e0
cmpq $0x0, (%rax)
je 0x8215
jmp 0x81a1
leaq 0x5738(%rip), %rax      # 0xd8e0
movq (%rax), %rdi
callq 0x38c0
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
je 0x8213
movq -0x18(%rbp), %rax
cmpl $0x1, 0x8(%rax)
jne 0x81d2
movl -0x4(%rbp), %edi
callq 0x9410
movq %rax, -0x20(%rbp)
jmp 0x81de
movl -0x4(%rbp), %edi
callq 0x93e0
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x7d50
cmpw $0x0, %ax
je 0x8211
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
leaq 0x56d9(%rip), %rax      # 0xd8e0
movq (%rax), %rdi
callq 0x3970
jmp 0x8213
jmp 0x81a1
jmp 0x8215
movq $0x0, -0x28(%rbp)
callq 0x7b10
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x8324
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
cmpq $0x0, 0x10(%rax)
je 0x8324
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rdi
leaq 0x1f6e(%rip), %rsi      # 0xa1c9
callq 0x56f0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x828f
movq -0x38(%rbp), %rax
movq (%rax), %rdi
callq 0x7aa0
cmpq $0x0, %rax
je 0x828d
movq -0x38(%rbp), %rax
movq (%rax), %rdi
callq 0x7aa0
movq %rax, -0x30(%rbp)
jmp 0x828f
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rdi
leaq 0x26e5(%rip), %rsi      # 0xa987
callq 0x56f0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x82f2
movl $0x1, %edi
movl $0x10, %esi
callq 0x3350
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rsi
callq 0x6d40
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rdi
leaq 0x1d48(%rip), %rsi      # 0xa04d
callq 0x56f0
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x8322
movq -0x38(%rbp), %rax
movl (%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x24(%rax)
jmp 0x8324
cmpq $0x0, -0x28(%rbp)
jne 0x8385
movl $0x1, %edi
movl $0x10, %esi
callq 0x3350
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rdi
callq 0x6e70
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x30(%rbp), %rdi
callq 0x6ec0
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x30(%rbp), %rdi
callq 0x6dd0
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x30(%rbp), %rdi
callq 0x6e20
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x24(%rax)
jne 0x83a9
movq -0x30(%rbp), %rax
movl 0x20(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x24(%rax)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x1, -0x48(%rbp)
leaq 0x554b(%rip), %rax      # 0xd910
movl (%rax), %eax
movl %eax, -0x44(%rbp)
movl $0x220a, -0x54(%rbp)    # imm = 0x220A
leaq 0x5520(%rip), %rax      # 0xd8f8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
leaq 0x5517(%rip), %rax      # 0xd900
movq (%rax), %rax
movl (%rax), %ecx
leaq 0x5513(%rip), %rax      # 0xd908
movq (%rax), %rax
movl (%rax), %r11d
movl -0x54(%rbp), %r10d
leaq -0x50(%rbp), %rax
movl $0x20, %esi
xorl %r9d, %r9d
movl %r9d, %r8d
movl $0x1, (%rsp)
movl $0x1, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
movl $0x1, 0x18(%rsp)
movl %r11d, 0x20(%rsp)
movl %r10d, 0x28(%rsp)
movq %rax, 0x30(%rsp)
callq 0x3150
movl %eax, -0x58(%rbp)
movl $0x80004, -0x50(%rbp)   # imm = 0x80004
movl $0x800, -0x54(%rbp)     # imm = 0x800
leaq 0x549f(%rip), %rax      # 0xd8f8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %esi
movl -0x54(%rbp), %edx
leaq -0x50(%rbp), %rcx
callq 0x3170
movl %eax, -0x5c(%rbp)
cmpq $0x0, 0x546e(%rip)      # 0xd8e8
jne 0x8488
callq 0x3860
movq %rax, 0x5460(%rip)      # 0xd8e8
leaq 0x5469(%rip), %rax      # 0xd8f8
movq (%rax), %rdi
movl -0x4(%rbp), %esi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
xorl %r8d, %r8d
movl %r8d, %ecx
callq 0x3320
movl %eax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x8520
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x20(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x24(%rcx), %eax
jne 0x84d7
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x8ec0
movq 0x540a(%rip), %rdi      # 0xd8e8
movq -0x10(%rbp), %rsi
callq 0x3990
movl -0x4(%rbp), %r9d
movl $0x3, %edi
leaq 0x21c4(%rip), %rsi      # 0xa6bb
leaq 0x21e4(%rip), %rdx      # 0xa6e2
movl $0xc4, %ecx
leaq 0x21c1(%rip), %r8       # 0xa6cb
movb $0x0, %al
callq 0x5bf0
movq -0x10(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nop