// File: /workspace/github_c_makefile_star2_O3/comfies[P]custard/src/xcb/connection.c
// Function: initialize_xcb
unsigned short initialize_xcb() {
    xcb_connection = xcb_connect(NULL, NULL);

    if (!xcb_connection || xcb_connection_has_error(xcb_connection)) {
        log_fatal("Unable to create XCB connection; is the X server running?");
        return 0;
    }

    xcb_screen = xcb_setup_roots_iterator(xcb_get_setup(xcb_connection)).data;

    unsigned int events[1] = {
        XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
        XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY   |
        XCB_EVENT_MASK_BUTTON_PRESS          |
        XCB_EVENT_MASK_ENTER_WINDOW          |
        XCB_EVENT_MASK_PROPERTY_CHANGE
    };

    xcb_void_cookie_t window_attributes_cookie;
    window_attributes_cookie = xcb_change_window_attributes(xcb_connection,
        xcb_screen->root, XCB_CW_EVENT_MASK, events);

    if (xcb_request_check(xcb_connection, window_attributes_cookie)) {
        log_fatal("It appears another window manager is running.");
        return 0;
    }

    events[0] ^= XCB_EVENT_MASK_ENTER_WINDOW;

    xcb_change_window_attributes_checked(xcb_connection, xcb_screen->root,
        XCB_CW_EVENT_MASK, events);

    xcb_depth_iterator_t depth_iterator = xcb_screen_allowed_depths_iterator(
        xcb_screen);

    if (depth_iterator.data) {
        xcb_visualtype_iterator_t visual_iterator;

        while (depth_iterator.rem) {

            if (depth_iterator.data->depth == 32) {
                visual_iterator = xcb_depth_visuals_iterator(
                    depth_iterator.data);

                screen_visual = visual_iterator.data;
            }

            xcb_depth_next(&depth_iterator);
        }
    }

    if (!screen_visual) {
        log_fatal("Unable to obtain a screen visual with depth of 32");
        return 0;
    }

    screen_colormap = xcb_generate_id(xcb_connection);
    xcb_create_colormap(xcb_connection, XCB_COLORMAP_ALLOC_NONE,
        screen_colormap, xcb_screen->root, screen_visual->visual_id);

    xcb_ungrab_key(xcb_connection, XCB_GRAB_ANY, xcb_screen->root,
        XCB_MOD_MASK_ANY);

    xcb_file_descriptor = xcb_get_file_descriptor(xcb_connection);

    return 1;
}

// Assembly:
pushq %rbx
subq $0x20, %rsp
xorl %edi, %edi
xorl %esi, %esi
callq 0x31c0
movq %rax, 0x56a7(%rip)      # 0xc8f8
testq %rax, %rax
je 0x7262
movq %rax, %rdi
callq 0x3540
testl %eax, %eax
je 0x7292
leaq 0x155f(%rip), %rsi      # 0x87c8
leaq 0x156d(%rip), %rdx      # 0x87dd
leaq 0x1575(%rip), %r8       # 0x87ec
xorl %ebx, %ebx
movl $0x1, %edi
movl $0x12, %ecx
xorl %eax, %eax
callq 0x51d6
movl %ebx, %eax
addq $0x20, %rsp
popq %rbx
retq
movq 0x565f(%rip), %rdi      # 0xc8f8
callq 0x35a0
movq %rax, %rdi
callq 0x3350
movq %rax, 0x5653(%rip)      # 0xc900
leaq 0xc(%rsp), %rcx
movl $0x580014, (%rcx)       # imm = 0x580014
movq 0x5639(%rip), %rdi      # 0xc8f8
movl (%rax), %esi
movl $0x800, %edx            # imm = 0x800
callq 0x3170
movq 0x5626(%rip), %rdi      # 0xc8f8
movl %eax, %esi
callq 0x3450
testq %rax, %rax
je 0x7301
leaq 0x14e3(%rip), %rsi      # 0x87c8
leaq 0x14f1(%rip), %rdx      # 0x87dd
leaq 0x1533(%rip), %r8       # 0x8826
xorl %ebx, %ebx
movl $0x1, %edi
movl $0x25, %ecx
jmp 0x7283
leaq 0xc(%rsp), %rcx
xorb $0x10, (%rcx)
movq 0x55e8(%rip), %rdi      # 0xc8f8
movq 0x55e9(%rip), %rax      # 0xc900
movl (%rax), %esi
movl $0x800, %edx            # imm = 0x800
callq 0x3490
movq 0x55d6(%rip), %rdi      # 0xc900
callq 0x3580
movq %rax, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
testq %rax, %rax
sete %al
testl %edx, %edx
sete %cl
orb %al, %cl
jne 0x7372
leaq 0x10(%rsp), %rbx
movq 0x10(%rsp), %rdi
cmpb $0x20, (%rdi)
jne 0x7363
callq 0x3650
movq %rax, 0x55a5(%rip)      # 0xc908
movq %rbx, %rdi
callq 0x30a0
cmpl $0x0, 0x18(%rsp)
jne 0x734d
cmpq $0x0, 0x558e(%rip)      # 0xc908
je 0x73e8
movq 0x5575(%rip), %rdi      # 0xc8f8
callq 0x3230
movl %eax, 0x5582(%rip)      # 0xc910
movq 0x5563(%rip), %rdi      # 0xc8f8
movq 0x5564(%rip), %rcx      # 0xc900
movl (%rcx), %ecx
movq 0x5563(%rip), %rdx      # 0xc908
movl (%rdx), %r8d
xorl %esi, %esi
movl %eax, %edx
callq 0x3680
movq 0x5540(%rip), %rdi      # 0xc8f8
movq 0x5541(%rip), %rax      # 0xc900
movl (%rax), %edx
xorl %esi, %esi
movl $0x8000, %ecx           # imm = 0x8000
callq 0x3610
movq 0x5524(%rip), %rdi      # 0xc8f8
callq 0x30b0
movl %eax, 0x5535(%rip)      # 0xc914
movw $0x1, %bx
jmp 0x728a
leaq 0x13d9(%rip), %rsi      # 0x87c8
leaq 0x13e7(%rip), %rdx      # 0x87dd
leaq 0x1457(%rip), %r8       # 0x8854
xorl %ebx, %ebx
movl $0x1, %edi
movl $0x42, %ecx
jmp 0x7283