// File: /workspace/github_c_makefile_star2_O2/tkln[P]mcwm/mcwm.c
// Function: main
int main(int argc, char **argv)
{
    uint32_t mask = 0;
    uint32_t values[2];
    int ch;                    /* Option character */
    xcb_void_cookie_t cookie;
    xcb_generic_error_t *error;
    xcb_drawable_t root;
    char *focuscol;
    char *unfocuscol;
    char *fixedcol;    
    int scrno;
    xcb_screen_iterator_t iter;
    
    /* Install signal handlers. */

    /* We ignore child exists. Don't create zombies. */
    if (SIG_ERR == signal(SIGCHLD, SIG_IGN))
    {
        perror("mcwm: signal");
        exit(1);
    }

    if (SIG_ERR == signal(SIGINT, sigcatch))
    {
        perror("mcwm: signal");
        exit(1);
    }

    if (SIG_ERR == signal(SIGTERM, sigcatch))
    {
        perror("mcwm: signal");
        exit(1);
    }
    
    /* Set up defaults. */
    
    conf.borderwidth = BORDERWIDTH;
    conf.snapmargin = SNAPMARGIN;
    conf.terminal = TERMINAL;
    conf.runcmd = RUNCMD;
    conf.allowicons = ALLOWICONS;
    focuscol = FOCUSCOL;
    unfocuscol = UNFOCUSCOL;
    fixedcol = FIXEDCOL;
    
    while (1)
    {
        ch = getopt(argc, argv, "b:s:it:f:u:x:");
        if (-1 == ch)
        {
                
            /* No more options, break out of while loop. */
            break;
        }
        
        switch (ch)
        {
        case 'b':
            /* Border width */
            conf.borderwidth = atoi(optarg);            
            break;

        case 's':
            /* Snap margin */
            conf.snapmargin = atoi(optarg);
            break;

        case 'i':
            conf.allowicons = true;
            break;
            
        case 't':
            conf.terminal = optarg;
            break;

        case 'f':
            focuscol = optarg;
            break;

        case 'u':
            unfocuscol = optarg;
            break;

        case 'x':
            fixedcol = optarg;
            break;
            
        default:
            printhelp();
            exit(0);
        } /* switch */
    }

    /*
     * Use $DISPLAY. After connecting scrno will contain the value of
     * the display's screen.
     */
    conn = xcb_connect(NULL, &scrno);
    if (xcb_connection_has_error(conn))
    {
        perror("xcb_connect");
        exit(1);
    }

    /* Find our screen. */
    iter = xcb_setup_roots_iterator(xcb_get_setup(conn));
    for (int i = 0; i < scrno; ++ i)
    {
        xcb_screen_next(&iter);
    }

    screen = iter.data;
    if (!screen)
    {
        fprintf (stderr, "mcwm: Can't get the current screen. Exiting.\n");
        xcb_disconnect(conn);
        exit(1);
    }

    root = screen->root;
    
    PDEBUG("Screen size: %dx%d\nRoot window: %d\n", screen->width_in_pixels,
           screen->height_in_pixels, screen->root);
    
    /* Get some colours. */
    conf.focuscol = getcolor(focuscol);
    conf.unfocuscol = getcolor(unfocuscol);
    conf.fixedcol = getcolor(fixedcol);
    
    /* Get some atoms. */
    atom_desktop = getatom("_NET_WM_DESKTOP");
    wm_delete_window = getatom("WM_DELETE_WINDOW");
    wm_change_state = getatom("WM_CHANGE_STATE");
    wm_state = getatom("WM_STATE");
    wm_protocols = getatom("WM_PROTOCOLS");
    
    /* Check for RANDR extension and configure. */
    randrbase = setuprandr();

    /* Loop over all clients and set up stuff. */
    if (0 != setupscreen())
    {
        fprintf(stderr, "mcwm: Failed to initialize windows. Exiting.\n");
        xcb_disconnect(conn);
        exit(1);
    }

    /* Set up key bindings. */
    if (0 != setupkeys())
    {
        fprintf(stderr, "mcwm: Couldn't set up keycodes. Exiting.");
        xcb_disconnect(conn);
        exit(1);
    }

    /* Grab mouse buttons. */

    xcb_grab_button(conn, 0, root, XCB_EVENT_MASK_BUTTON_PRESS
                    | XCB_EVENT_MASK_BUTTON_RELEASE,
                    XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, root, XCB_NONE,
                    1 /* left mouse button */,
                    MOUSEMODKEY);
    
    xcb_grab_button(conn, 0, root, XCB_EVENT_MASK_BUTTON_PRESS
                    | XCB_EVENT_MASK_BUTTON_RELEASE,
                    XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, root, XCB_NONE,
                    2 /* middle mouse button */,
                    MOUSEMODKEY);

    xcb_grab_button(conn, 0, root, XCB_EVENT_MASK_BUTTON_PRESS
                    | XCB_EVENT_MASK_BUTTON_RELEASE,
                    XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, root, XCB_NONE,
                    3 /* right mouse button */,
                    MOUSEMODKEY);

    /* Subscribe to events. */
    mask = XCB_CW_EVENT_MASK;

    values[0] = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
        | XCB_EVENT_MASK_STRUCTURE_NOTIFY
        | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;

    cookie =
        xcb_change_window_attributes_checked(conn, root, mask, values);
    error = xcb_request_check(conn, cookie);

    xcb_flush(conn);
    
    if (NULL != error)
    {
        fprintf(stderr, "mcwm: Can't get SUBSTRUCTURE REDIRECT. "
                "Error code: %d\n"
                "Another window manager running? Exiting.\n",
                error->error_code);

        xcb_disconnect(conn);
        
        exit(1);
    }

    /* Loop over events. */
    events();

    /* Die gracefully. */
    cleanup(sigcode);

    exit(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r12
movl %edi, %ebp
pushq $0x11
popq %rdi
pushq $0x1
popq %rsi
callq 0x32a0
cmpq $-0x1, %rax
je 0x4d0b
leaq 0x64b(%rip), %rsi       # 0x4d17
pushq $0x2
popq %rdi
callq 0x32a0
cmpq $-0x1, %rax
je 0x4d0b
leaq 0x632(%rip), %rsi       # 0x4d17
pushq $0xf
popq %rdi
callq 0x32a0
cmpq $-0x1, %rax
je 0x4d0b
movq $0x1, 0x624e(%rip) # 0xa950
leaq 0x29f7(%rip), %rax      # 0x7100
movq %rax, 0x6248(%rip)      # 0xa958
leaq 0x29f0(%rip), %rax      # 0x7107
movq %rax, 0x6242(%rip)      # 0xa960
movb $0x0, 0x624f(%rip)      # 0xa974
leaq 0x29f2(%rip), %rbx      # 0x711e
leaq 0x29f6(%rip), %rax      # 0x7129
movq %rax, 0x8(%rsp)
leaq 0x29f1(%rip), %rax      # 0x7130
movq %rax, (%rsp)
leaq 0x29ed(%rip), %r13      # 0x7137
movq 0x587f(%rip), %r14      # 0x9fd0
leaq 0x28d4(%rip), %r15      # 0x702c
movl %ebp, %edi
movq %r12, %rsi
movq %r13, %rdx
callq 0x3480
leal -0x73(%rax), %ecx
cmpl $0x5, %ecx
ja 0x4786
movslq (%r15,%rcx,4), %rax
addq %r15, %rax
jmpq *%rax
movq (%r14), %rdi
callq 0x34c0
movl %eax, 0x61d0(%rip)      # 0xa954
jmp 0x4758
cmpl $0x62, %eax
je 0x47a5
cmpl $0x69, %eax
jne 0x47b5
movb $0x1, 0x61dd(%rip)      # 0xa974
jmp 0x4758
movq (%r14), %rax
movq %rax, 0x61b5(%rip)      # 0xa958
jmp 0x4758
movq (%r14), %rdi
callq 0x34c0
movl %eax, 0x619d(%rip)      # 0xa950
jmp 0x4758
cmpl $0x66, %eax
jne 0x47d2
movq (%r14), %rbx
jmp 0x4758
movq (%r14), %rax
movq %rax, 0x8(%rsp)
jmp 0x4758
movq (%r14), %rax
movq %rax, (%rsp)
jmp 0x4758
cmpl $-0x1, %eax
jne 0x482f
xorl %r14d, %r14d
leaq 0x14(%rsp), %rsi
xorl %edi, %edi
callq 0x3120
movq %rax, 0x618b(%rip)      # 0xa978
movq %rax, %rdi
callq 0x3440
testl %eax, %eax
jne 0x483b
movq 0x6178(%rip), %rdi      # 0xa978
callq 0x3490
movq %rax, %rdi
callq 0x3270
leaq 0x20(%rsp), %r12
movq %rax, (%r12)
movq %rdx, 0x8(%r12)
cmpl 0x14(%rsp), %r14d
jge 0x484c
movq %r12, %rdi
callq 0x31f0
incl %r14d
jmp 0x481b
callq 0x4d1e
xorl %edi, %edi
callq 0x3500
leaq 0x2903(%rip), %rdi      # 0x7145
callq 0x3470
jmp 0x49a5
movq 0x20(%rsp), %rax
movq %rax, 0x60f0(%rip)      # 0xa948
testq %rax, %rax
jne 0x4873
movq 0x5794(%rip), %rax      # 0x9ff8
movq (%rax), %rcx
leaq 0x28e3(%rip), %rdi      # 0x7151
jmp 0x498e
movl (%rax), %r15d
movq %rbx, %rdi
callq 0x4d74
movl %eax, 0x60e4(%rip)      # 0xa968
movq 0x8(%rsp), %rdi
callq 0x4d74
movl %eax, 0x60d8(%rip)      # 0xa96c
movq (%rsp), %rdi
callq 0x4d74
movl %eax, 0x60cd(%rip)      # 0xa970
leaq 0x28d5(%rip), %rdi      # 0x717f
callq 0x4dfc
movl %eax, 0x60cb(%rip)      # 0xa980
leaq 0x28d3(%rip), %rdi      # 0x718f
callq 0x4dfc
movl %eax, 0x60bd(%rip)      # 0xa984
leaq 0x28d2(%rip), %rdi      # 0x71a0
callq 0x4dfc
movl %eax, 0x60af(%rip)      # 0xa988
leaq 0x28d0(%rip), %rdi      # 0x71b0
callq 0x4dfc
movl %eax, 0x60a1(%rip)      # 0xa98c
leaq 0x28c7(%rip), %rdi      # 0x71b9
callq 0x4dfc
movl %eax, 0x6093(%rip)      # 0xa990
movq 0x6074(%rip), %rdi      # 0xa978
movq 0x56d5(%rip), %rsi      # 0x9fe0
callq 0x3060
cmpb $0x0, 0x8(%rax)
jne 0x491b
pushq $-0x1
popq %rbx
jmp 0x494b
movq %rax, %rbx
callq 0x64b9
movzbl 0xa(%rbx), %ebx
movq 0x604a(%rip), %rdi      # 0xa978
movq 0x6013(%rip), %rax      # 0xa948
movl (%rax), %esi
pushq $0xf
popq %rdx
callq 0x3030
movq 0x6032(%rip), %rdi      # 0xa978
callq 0x3150
movl %ebx, 0x6043(%rip)      # 0xa994
movq 0x6020(%rip), %rbx      # 0xa978
movq 0x5fe9(%rip), %rax      # 0xa948
movl (%rax), %esi
movq %rbx, %rdi
callq 0x3190
xorl %r14d, %r14d
movq %rbx, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x3240
testq %rax, %rax
jne 0x49ad
movq 0x5674(%rip), %rax      # 0x9ff8
movq (%rax), %rcx
leaq 0x2838(%rip), %rdi      # 0x71c6
pushq $0x2d
popq %rsi
pushq $0x1
popq %rdx
callq 0x3510
movq 0x5fd8(%rip), %rdi      # 0xa978
callq 0x3430
pushq $0x1
popq %rdi
callq 0x3500
movq %rax, %rbx
movq %r15, 0x8(%rsp)
movq %rax, %rdi
callq 0x3300
movl %eax, %r13d
movq %rbx, 0x18(%rsp)
movq %rbx, %rdi
callq 0x31d0
movq %rax, %r12
testl %r13d, %r13d
cmovlel %r14d, %r13d
xorl %r15d, %r15d
movq %r13, (%rsp)
cmpq %r15, %r13
je 0x4b76
movq 0x5f8a(%rip), %rbx      # 0xa978
movl (%r12,%r15,4), %esi
movq %rbx, %rdi
callq 0x3310
movq %rbx, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x31a0
testq %rax, %rax
je 0x4af4
movq %rax, %rbx
cmpb $0x0, 0x1b(%rax)
jne 0x4b4a
cmpb $0x2, 0x1a(%rbx)
jne 0x4b4a
movl (%r12,%r15,4), %edi
callq 0x683d
testq %rax, %rax
je 0x4b4a
movq %rax, %r14
cmpl $-0x1, 0x5f52(%rip)     # 0xa994
je 0x4a57
movswl 0x6(%r14), %edi
movswl 0x8(%r14), %esi
callq 0x6a26
movq %rax, 0x38(%r14)
movq %r14, %rdi
callq 0x4135
movl (%r12,%r15,4), %ebp
movq 0x5f0e(%rip), %rdi      # 0xa978
movl 0x5f10(%rip), %ecx      # 0xa980
subq $0x8, %rsp
xorl %esi, %esi
movl %ebp, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq $0x4
callq 0x3090
addq $0x10, %rsp
movq 0x5ee8(%rip), %rdi      # 0xa978
movl %eax, %esi
xorl %edx, %edx
callq 0x3420
testq %rax, %rax
je 0x4b5a
movq %rax, %r13
movq %rax, %rdi
callq 0x33d0
testl %eax, %eax
je 0x4b12
movq %r13, %rdi
callq 0x3100
movl (%rax), %ebp
movq %r13, %rdi
callq 0x3040
cmpl $-0x1, %ebp
je 0x4b2e
cmpl $0x9, %ebp
movq (%rsp), %r13
ja 0x4b1e
movq %r14, %rdi
movl %ebp, %esi
callq 0x5fda
cmpl 0x5ddf(%rip), %ebp      # 0xa8c0
je 0x4b4a
movq 0x5e8e(%rip), %rdi      # 0xa978
movl (%r14), %esi
callq 0x30d0
jmp 0x4b4a
movq 0x54fd(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
movl (%r12,%r15,4), %edx
leaq 0x27c4(%rip), %rsi      # 0x72cd
xorl %eax, %eax
callq 0x32d0
jmp 0x4b52
movq %r13, %rdi
callq 0x3040
movq (%rsp), %r13
movl 0x5d9c(%rip), %esi      # 0xa8c0
movq %r14, %rdi
callq 0x5fda
jmp 0x4b4a
movl 0x5d8c(%rip), %esi      # 0xa8c0
movq %r14, %rdi
callq 0x5fda
movq %r14, %rdi
xorl %esi, %esi
callq 0x5e6b
movq (%rsp), %r13
movq %rbx, %rdi
callq 0x3040
incq %r15
jmp 0x49de
movq 0x5497(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
leaq 0x27bc(%rip), %rsi      # 0x7327
movl %ebp, %edx
xorl %eax, %eax
callq 0x32d0
jmp 0x4b1a
xorl %edi, %edi
callq 0x5d71
movq 0x5df4(%rip), %rbx      # 0xa978
movq 0x5dbd(%rip), %rax      # 0xa948
movl (%rax), %esi
movq %rbx, %rdi
callq 0x3290
movq %rbx, %rdi
movl %eax, %esi
xorl %edx, %edx
callq 0x32b0
testq %rax, %rax
jne 0x4bb0
andq $0x0, 0x5d92(%rip)      # 0xa940
jmp 0x4bcb
movq %rax, %rbx
movl 0xc(%rax), %edi
callq 0x6a6d
movq %rax, %rdi
callq 0x6100
movq %rbx, %rdi
callq 0x3040
movq 0x8(%rsp), %r15
movq 0x18(%rsp), %rbx
movq 0x5d9c(%rip), %rdi      # 0xa978
callq 0x3150
movq %rbx, %rdi
callq 0x3040
callq 0x4e4a
testl %eax, %eax
jne 0x4cd2
movq 0x5d7b(%rip), %rdi      # 0xa978
subq $0x20, %rsp
pushq $0x40
popq %r14
movl %r14d, 0x18(%rsp)
movl %r15d, (%rsp)
movl $0x1, 0x10(%rsp)
andl $0x0, 0x8(%rsp)
pushq $0xc
popq %rbp
pushq $0x1
popq %rbx
xorl %esi, %esi
movl %r15d, %edx
movl %ebp, %ecx
movl %ebx, %r8d
movl %ebx, %r9d
callq 0x3280
addq $0x20, %rsp
movq 0x5d3a(%rip), %rdi      # 0xa978
xorl %esi, %esi
movl %r15d, %edx
movl %ebp, %ecx
movl %ebx, %r8d
movl %ebx, %r9d
pushq %r14
pushq $0x2
pushq $0x0
pushq %r15
callq 0x3280
addq $0x20, %rsp
movq 0x5d15(%rip), %rdi      # 0xa978
xorl %esi, %esi
movl %r15d, %edx
movl %ebp, %ecx
movl %ebx, %r8d
movl %ebx, %r9d
pushq %r14
pushq $0x3
pushq $0x0
pushq %r15
callq 0x3280
addq $0x20, %rsp
leaq 0x30(%rsp), %rcx
movl $0x1a0000, (%rcx)       # imm = 0x1A0000
movq 0x5ce5(%rip), %rdi      # 0xa978
movl %r15d, %esi
movl $0x800, %edx            # imm = 0x800
callq 0x33c0
movq 0x5cd1(%rip), %rdi      # 0xa978
movl %eax, %esi
callq 0x3380
movq %rax, %rbx
movq 0x5cc0(%rip), %rdi      # 0xa978
callq 0x3150
testq %rbx, %rbx
jne 0x4cea
callq 0x5095
movl 0x5ccb(%rip), %edi      # 0xa998
callq 0x5d3c
movq 0x531f(%rip), %rax      # 0x9ff8
movq (%rax), %rcx
leaq 0x2511(%rip), %rdi      # 0x71f4
pushq $0x28
jmp 0x4990
movq 0x5307(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
movzbl 0x1(%rbx), %edx
leaq 0x251e(%rip), %rsi      # 0x721d
xorl %eax, %eax
callq 0x32d0
jmp 0x4999
leaq 0x23ff(%rip), %rdi      # 0x7111
jmp 0x4842