// File: /workspace/github_c_makefile_star2_O2/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, 0x5ad3(%rip)      # 0xc8e8
testq %rax, %rax
je 0x6e26
movq %rax, %rdi
callq 0x3530
testl %eax, %eax
je 0x6e52
leaq 0x198b(%rip), %rsi      # 0x87b8
leaq 0x1999(%rip), %rdx      # 0x87cd
leaq 0x19a1(%rip), %r8       # 0x87dc
xorl %ebx, %ebx
pushq $0x1
popq %rdi
pushq $0x12
popq %rcx
xorl %eax, %eax
callq 0x50dd
movl %ebx, %eax
addq $0x20, %rsp
popq %rbx
retq
movq 0x5a8f(%rip), %rdi      # 0xc8e8
callq 0x3590
movq %rax, %rdi
callq 0x3350
movq %rax, 0x5a83(%rip)      # 0xc8f0
leaq 0xc(%rsp), %rcx
movl $0x580014, (%rcx)       # imm = 0x580014
movq 0x5a69(%rip), %rdi      # 0xc8e8
movl (%rax), %esi
movl $0x800, %edx            # imm = 0x800
callq 0x3170
movq 0x5a56(%rip), %rdi      # 0xc8e8
movl %eax, %esi
callq 0x3440
testq %rax, %rax
je 0x6ebc
leaq 0x1913(%rip), %rsi      # 0x87b8
leaq 0x1921(%rip), %rdx      # 0x87cd
leaq 0x1963(%rip), %r8       # 0x8816
xorl %ebx, %ebx
pushq $0x1
popq %rdi
pushq $0x25
jmp 0x6e42
leaq 0xc(%rsp), %rcx
xorb $0x10, (%rcx)
movq 0x5a1d(%rip), %rdi      # 0xc8e8
movq 0x5a1e(%rip), %rax      # 0xc8f0
movl (%rax), %esi
movl $0x800, %edx            # imm = 0x800
callq 0x3480
movq 0x5a0b(%rip), %rdi      # 0xc8f0
callq 0x3570
movq %rax, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
testq %rax, %rax
sete %al
testl %edx, %edx
sete %cl
orb %al, %cl
jne 0x6f2d
leaq 0x10(%rsp), %rbx
movq 0x10(%rsp), %rdi
cmpb $0x20, (%rdi)
jne 0x6f1e
callq 0x3640
movq %rax, 0x59da(%rip)      # 0xc8f8
movq %rbx, %rdi
callq 0x30a0
cmpl $0x0, 0x18(%rsp)
jne 0x6f08
cmpq $0x0, 0x59c3(%rip)      # 0xc8f8
je 0x6fa3
movq 0x59aa(%rip), %rdi      # 0xc8e8
callq 0x3230
movl %eax, 0x59b7(%rip)      # 0xc900
movq 0x5998(%rip), %rdi      # 0xc8e8
movq 0x5999(%rip), %rcx      # 0xc8f0
movl (%rcx), %ecx
movq 0x5998(%rip), %rdx      # 0xc8f8
movl (%rdx), %r8d
xorl %esi, %esi
movl %eax, %edx
callq 0x3670
movq 0x5975(%rip), %rdi      # 0xc8e8
movq 0x5976(%rip), %rax      # 0xc8f0
movl (%rax), %edx
xorl %esi, %esi
movl $0x8000, %ecx           # imm = 0x8000
callq 0x3600
movq 0x5959(%rip), %rdi      # 0xc8e8
callq 0x30b0
movl %eax, 0x596a(%rip)      # 0xc904
movw $0x1, %bx
jmp 0x6e4a
leaq 0x180e(%rip), %rsi      # 0x87b8
leaq 0x181c(%rip), %rdx      # 0x87cd
leaq 0x188c(%rip), %r8       # 0x8844
xorl %ebx, %ebx
pushq $0x1
popq %rdi
pushq $0x42
jmp 0x6e42