// File: /workspace/github_c_makefile_star2_O3/tkln[P]mcwm/mcwm.c
// Function: setupscreen
int setupscreen(void)
{
    xcb_query_tree_reply_t *reply;
    xcb_query_pointer_reply_t *pointer;
    int i;
    int len;
    xcb_window_t *children;
    xcb_get_window_attributes_reply_t *attr;
    struct client *client;
    uint32_t ws;
    
    /* Get all children. */
    reply = xcb_query_tree_reply(conn,
                                 xcb_query_tree(conn, screen->root), 0);
    if (NULL == reply)
    {
        return -1;
    }

    len = xcb_query_tree_children_length(reply);    
    children = xcb_query_tree_children(reply);
    
    /* Set up all windows on this root. */
    for (i = 0; i < len; i ++)
    {
        attr = xcb_get_window_attributes_reply(
            conn, xcb_get_window_attributes(conn, children[i]), NULL);

        if (!attr)
        {
            fprintf(stderr, "Couldn't get attributes for window %d.",
                    children[i]);
            continue;
        }

        /*
         * Don't set up or even bother windows in override redirect
         * mode.
         *
         * This mode means they wouldn't have been reported to us
         * with a MapRequest if we had been running, so in the
         * normal case we wouldn't have seen them.
         *
         * Only handle visible windows. 
         */    
        if (!attr->override_redirect
            && attr->map_state == XCB_MAP_STATE_VIEWABLE)
        {
            client = setupwin(children[i]);
            if (NULL != client)
            {
                /*
                 * Find the physical output this window will be on if
                 * RANDR is active.
                 */
                if (-1 != randrbase)
                {
                    PDEBUG("Looking for monitor on %d x %d.\n", client->x,
                        client->y);
                    client->monitor = findmonbycoord(client->x, client->y);
#if DEBUG
                    if (NULL != client->monitor)
                    {
                        PDEBUG("Found client on monitor %s.\n",
                               client->monitor->name);
                    }
                    else
                    {
                        PDEBUG("Couldn't find client on any monitor.\n");
                    }
#endif
                }
                
                /* Fit window on physical screen. */
                fitonscreen(client);
                
                /*
                 * Check if this window has a workspace set already as
                 * a WM hint.
                 *
                 */
                ws = getwmdesktop(children[i]);

                if (ws == NET_WM_FIXED)
                {
                    /* Add to current workspace. */
                    addtoworkspace(client, curws);                    
                    /* Add to all other workspaces. */
                    fixwindow(client, false);
                }
                else if (MCWM_NOWS != ws && ws < WORKSPACES)
                {
                    addtoworkspace(client, ws);
                    /* If it's not our current workspace, hide it. */
                    if (ws != curws)
                    {
                        xcb_unmap_window(conn, client->id);
                    }
                }
                else
                {
                    /*
                     * No workspace hint at all. Just add it to our
                     * current workspace.
                     */
                    addtoworkspace(client, curws);
                }
            }
        }
        
        free(attr);
    } /* for */

    changeworkspace(0);
        
    /*
     * Get pointer position so we can set focus on any window which
     * might be under it.
     */
    pointer = xcb_query_pointer_reply(
        conn, xcb_query_pointer(conn, screen->root), 0);

    if (NULL == pointer)
    {
        focuswin = NULL;
    }
    else
    {
        setfocus(findclient(pointer->child));
        free(pointer);
    }
    
    xcb_flush(conn);
    
    free(reply);
    
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq 0x5bc2(%rip), %rbx      # 0xa978
movq 0x5b8b(%rip), %rax      # 0xa948
movl (%rax), %esi
movq %rbx, %rdi
callq 0x3190
movq %rbx, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x3240
testq %rax, %rax
je 0x4fd2
movq %rax, %rbx
movq %rax, %rdi
callq 0x3300
movl %eax, %ebp
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x31d0
testl %ebp, %ebp
jle 0x4f76
movq %rax, %r14
movl %ebp, %ebx
movq 0x5b6e(%rip), %r15      # 0xa978
movl (%r14), %esi
movq %r15, %rdi
callq 0x3310
movq %r15, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x31a0
testq %rax, %rax
je 0x4f09
movq %rax, %r12
cmpb $0x0, 0x1b(%rax)
jne 0x4f43
cmpb $0x2, 0x1a(%r12)
jne 0x4f43
movl (%r14), %edi
callq 0x6a87
testq %rax, %rax
je 0x4f43
movq %rax, %r13
cmpl $-0x1, 0x5b36(%rip)     # 0xa994
je 0x4e73
movswl 0x6(%r13), %edi
movswl 0x8(%r13), %esi
callq 0x6c88
movq %rax, 0x38(%r13)
movq %r13, %rdi
callq 0x42d3
movl (%r14), %ebp
movq 0x5af3(%rip), %rdi      # 0xa978
movl 0x5af5(%rip), %ecx      # 0xa980
movl $0x4, (%rsp)
xorl %esi, %esi
movl %ebp, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x3090
movq 0x5ad0(%rip), %rdi      # 0xa978
movl %eax, %esi
xorl %edx, %edx
callq 0x3420
testq %rax, %rax
je 0x4f5a
movq %rax, %r15
movq %rax, %rdi
callq 0x33d0
testl %eax, %eax
je 0x4f13
movq %r15, %rdi
callq 0x3100
movl (%rax), %ebp
movq %r15, %rdi
callq 0x3040
cmpl $-0x1, %ebp
je 0x4f2b
cmpl $0x9, %ebp
ja 0x4f1b
movq %r13, %rdi
movl %ebp, %esi
callq 0x622c
cmpl 0x59cb(%rip), %ebp      # 0xa8c0
je 0x4f43
movq 0x5a7a(%rip), %rdi      # 0xa978
movl (%r13), %esi
callq 0x30d0
jmp 0x4f43
movq %r14, %rdi
callq 0x35e0
jmp 0x4f4b
movq %r15, %rdi
callq 0x3040
movl 0x599f(%rip), %esi      # 0xa8c0
movq %r13, %rdi
callq 0x622c
jmp 0x4f43
movl 0x598f(%rip), %esi      # 0xa8c0
movq %r13, %rdi
callq 0x622c
movq %r13, %rdi
xorl %esi, %esi
callq 0x6083
movq %r12, %rdi
callq 0x3040
addq $0x4, %r14
decq %rbx
jne 0x4e03
jmp 0x4f76
movq 0x5097(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x23bc(%rip), %rsi      # 0x7327
movl %ebp, %edx
xorl %eax, %eax
callq 0x32d0
jmp 0x4f1b
xorl %edi, %edi
callq 0x5f8a
movq 0x59f4(%rip), %r14      # 0xa978
movq 0x59bd(%rip), %rax      # 0xa948
movl (%rax), %esi
movq %r14, %rdi
callq 0x3290
movq %r14, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x32b0
testq %rax, %rax
je 0x4fd9
movq %rax, %r14
movq 0x5918(%rip), %rax      # 0xa8c8
testq %rax, %rax
je 0x4feb
movl 0xc(%r14), %ecx
movq 0x10(%rsp), %rbx
movq (%rax), %rdi
cmpl %ecx, (%rdi)
je 0x4ff2
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0x4fbe
xorl %edi, %edi
jmp 0x4ff2
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x5015
movq $0x0, 0x595c(%rip) # 0xa940
movq 0x10(%rsp), %rbx
jmp 0x4fff
xorl %edi, %edi
movq 0x10(%rsp), %rbx
callq 0x6358
movq %r14, %rdi
callq 0x3040
movq 0x5972(%rip), %rdi      # 0xa978
callq 0x3150
movq %rbx, %rdi
callq 0x3040
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq