// File: /workspace/github_c_makefile_star2_O1/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 0x5bf4(%rip), %rbx      # 0xa978
movq 0x5bbd(%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 0x4faa
movq %rax, %rbx
movq %rax, %rdi
callq 0x3300
movl %eax, %ebp
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x31d0
testl %ebp, %ebp
jle 0x4f4e
movq %rax, %r14
movl %ebp, %ebx
movq 0x5ba0(%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 0x4eab
movq %rax, %r12
cmpb $0x0, 0x1b(%rax)
jne 0x4f18
cmpb $0x2, 0x1a(%r12)
jne 0x4f18
movl (%r14), %edi
callq 0x69ea
testq %rax, %rax
je 0x4f18
movq %rax, %r13
cmpl $-0x1, 0x5b68(%rip)     # 0xa994
je 0x4e41
movswl 0x6(%r13), %edi
movswl 0x8(%r13), %esi
callq 0x6c02
movq %rax, 0x38(%r13)
movq %r13, %rdi
callq 0x42e8
movl (%r14), %ebp
movq 0x5b25(%rip), %rdi      # 0xa978
movl 0x5b27(%rip), %ecx      # 0xa980
movl $0x4, (%rsp)
xorl %esi, %esi
movl %ebp, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x3090
movq 0x5b02(%rip), %rdi      # 0xa978
movl %eax, %esi
xorl %edx, %edx
callq 0x3420
testq %rax, %rax
je 0x4f2f
movq %rax, %r15
movq %rax, %rdi
callq 0x33d0
testl %eax, %eax
je 0x4eb5
movq %r15, %rdi
callq 0x3100
movl (%rax), %ebp
movq %r15, %rdi
callq 0x3040
jmp 0x4ec2
movq %r14, %rdi
callq 0x35e0
jmp 0x4f20
movq %r15, %rdi
callq 0x3040
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
cmpl $-0x1, %ebp
je 0x4ef0
cmpl $0x9, %ebp
ja 0x4f0a
movq %r13, %rdi
movl %ebp, %esi
callq 0x61cd
cmpl 0x59e4(%rip), %ebp      # 0xa8c0
je 0x4f18
movq 0x5a93(%rip), %rdi      # 0xa978
movl (%r13), %esi
callq 0x30d0
jmp 0x4f18
movl 0x59ca(%rip), %esi      # 0xa8c0
movq %r13, %rdi
callq 0x61cd
movq %r13, %rdi
xorl %esi, %esi
callq 0x6034
jmp 0x4f18
movl 0x59b0(%rip), %esi      # 0xa8c0
movq %r13, %rdi
callq 0x61cd
movq %r12, %rdi
callq 0x3040
addq $0x4, %r14
decq %rbx
jne 0x4dd1
jmp 0x4f4e
movq 0x50c2(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x23af(%rip), %rsi      # 0x72ef
movl %ebp, %edx
xorl %eax, %eax
callq 0x32d0
jmp 0x4ebd
xorl %edi, %edi
callq 0x5f3f
movq 0x5a1c(%rip), %r14      # 0xa978
movq 0x59e5(%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 0x4fb1
movq %rax, %r14
movq 0x5940(%rip), %rax      # 0xa8c8
testq %rax, %rax
je 0x4fc3
movl 0xc(%r14), %ecx
movq 0x10(%rsp), %rbx
movq (%rax), %rdi
cmpl %ecx, (%rdi)
je 0x4fca
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0x4f96
xorl %edi, %edi
jmp 0x4fca
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x4fed
movq $0x0, 0x5984(%rip) # 0xa940
movq 0x10(%rsp), %rbx
jmp 0x4fd7
xorl %edi, %edi
movq 0x10(%rsp), %rbx
callq 0x62f9
movq %r14, %rdi
callq 0x3040
movq 0x599a(%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