// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl $0x11, %edi
movl $0x1, %esi
callq 0x32a0
movq %rax, %rcx
movq $-0x1, %rax
cmpq %rcx, %rax
jne 0x5424
leaq 0x4d08(%rip), %rdi      # 0xa11d
callq 0x3470
movl $0x1, %edi
callq 0x3500
movl $0x2, %edi
leaq 0x4f0(%rip), %rsi       # 0x5920
callq 0x32a0
movq %rax, %rcx
movq $-0x1, %rax
cmpq %rcx, %rax
jne 0x545a
leaq 0x4cd2(%rip), %rdi      # 0xa11d
callq 0x3470
movl $0x1, %edi
callq 0x3500
movl $0xf, %edi
leaq 0x4ba(%rip), %rsi       # 0x5920
callq 0x32a0
movq %rax, %rcx
movq $-0x1, %rax
cmpq %rcx, %rax
jne 0x5490
leaq 0x4c9c(%rip), %rdi      # 0xa11d
callq 0x3470
movl $0x1, %edi
callq 0x3500
movl $0x1, 0x84b6(%rip)      # 0xd950
movl $0x0, 0x84b0(%rip)      # 0xd954
leaq 0x4c61(%rip), %rax      # 0xa10c
movq %rax, 0x84a6(%rip)      # 0xd958
leaq 0x4c5a(%rip), %rax      # 0xa113
movq %rax, 0x84a0(%rip)      # 0xd960
movb $0x0, 0x84ad(%rip)      # 0xd974
leaq 0x4c5c(%rip), %rax      # 0xa12a
movq %rax, -0x40(%rbp)
leaq 0x4c5c(%rip), %rax      # 0xa135
movq %rax, -0x48(%rbp)
leaq 0x4c58(%rip), %rax      # 0xa13c
movq %rax, -0x50(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x4c4d(%rip), %rdx      # 0xa143
callq 0x3480
movl %eax, -0x20(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl -0x20(%rbp), %eax
jne 0x550d
jmp 0x55c7
movl -0x20(%rbp), %eax
addl $-0x62, %eax
movl %eax, %ecx
movq %rcx, -0x98(%rbp)
subl $0x16, %eax
ja 0x55b6
movq -0x98(%rbp), %rax
leaq 0x4af9(%rip), %rcx      # 0xa02c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x7a8d(%rip), %rax      # 0xcfd0
movq (%rax), %rdi
callq 0x34c0
movl %eax, 0x83ff(%rip)      # 0xd950
jmp 0x55c2
movq 0x7a76(%rip), %rax      # 0xcfd0
movq (%rax), %rdi
callq 0x34c0
movl %eax, 0x83ec(%rip)      # 0xd954
jmp 0x55c2
movb $0x1, 0x8403(%rip)      # 0xd974
jmp 0x55c2
movq 0x7a56(%rip), %rax      # 0xcfd0
movq (%rax), %rax
movq %rax, 0x83d4(%rip)      # 0xd958
jmp 0x55c2
movq 0x7a43(%rip), %rax      # 0xcfd0
movq (%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x55c2
movq 0x7a33(%rip), %rax      # 0xcfd0
movq (%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x55c2
movq 0x7a23(%rip), %rax      # 0xcfd0
movq (%rax), %rax
movq %rax, -0x50(%rbp)
jmp 0x55c2
callq 0x5940
xorl %edi, %edi
callq 0x3500
jmp 0x54e8
xorl %eax, %eax
movl %eax, %edi
leaq -0x54(%rbp), %rsi
callq 0x3110
movq %rax, 0x839d(%rip)      # 0xd978
movq 0x8396(%rip), %rdi      # 0xd978
callq 0x3440
cmpl $0x0, %eax
je 0x5602
leaq 0x4b5e(%rip), %rdi      # 0xa151
callq 0x3470
movl $0x1, %edi
callq 0x3500
movq 0x836f(%rip), %rdi      # 0xd978
callq 0x3490
movq %rax, %rdi
callq 0x3270
movq %rax, -0x78(%rbp)
movq %rdx, -0x70(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x60(%rbp)
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x5651
leaq -0x68(%rbp), %rdi
callq 0x31f0
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x5635
movq -0x68(%rbp), %rax
movq %rax, 0x82ec(%rip)      # 0xd948
cmpq $0x0, 0x82e4(%rip)      # 0xd948
jne 0x5694
movq 0x798b(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x4ae6(%rip), %rsi      # 0xa15d
movb $0x0, %al
callq 0x32d0
movq 0x82f3(%rip), %rdi      # 0xd978
callq 0x3430
movl $0x1, %edi
callq 0x3500
movq 0x82ad(%rip), %rax      # 0xd948
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x40(%rbp), %rdi
callq 0x59b0
movl %eax, 0x82b9(%rip)      # 0xd968
movq -0x48(%rbp), %rdi
callq 0x59b0
movl %eax, 0x82ae(%rip)      # 0xd96c
movq -0x50(%rbp), %rdi
callq 0x59b0
movl %eax, 0x82a3(%rip)      # 0xd970
leaq 0x4ab7(%rip), %rdi      # 0xa18b
callq 0x5a60
movl %eax, 0x82a1(%rip)      # 0xd980
leaq 0x4ab5(%rip), %rdi      # 0xa19b
callq 0x5a60
movl %eax, 0x8293(%rip)      # 0xd984
leaq 0x4ab4(%rip), %rdi      # 0xa1ac
callq 0x5a60
movl %eax, 0x8285(%rip)      # 0xd988
leaq 0x4ab2(%rip), %rdi      # 0xa1bc
callq 0x5a60
movl %eax, 0x8277(%rip)      # 0xd98c
leaq 0x4aa9(%rip), %rdi      # 0xa1c5
callq 0x5a60
movl %eax, 0x8269(%rip)      # 0xd990
callq 0x5af0
movl %eax, 0x8262(%rip)      # 0xd994
callq 0x5b70
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x576d
movq 0x78b2(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x4a82(%rip), %rsi      # 0xa1d2
movb $0x0, %al
callq 0x32d0
movq 0x821a(%rip), %rdi      # 0xd978
callq 0x3430
movl $0x1, %edi
callq 0x3500
callq 0x5e10
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x57a8
movq 0x7877(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x4a75(%rip), %rsi      # 0xa200
movb $0x0, %al
callq 0x32d0
movq 0x81df(%rip), %rdi      # 0xd978
callq 0x3430
movl $0x1, %edi
callq 0x3500
movq 0x81c9(%rip), %rdi      # 0xd978
movl -0x34(%rbp), %edx
movl -0x34(%rbp), %eax
xorl %esi, %esi
movl $0xc, %ecx
movl $0x1, %r9d
movl %r9d, %r8d
movl %eax, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
movl $0x40, 0x18(%rsp)
callq 0x3280
movl %eax, -0x80(%rbp)
movq 0x8189(%rip), %rdi      # 0xd978
movl -0x34(%rbp), %edx
movl -0x34(%rbp), %eax
xorl %esi, %esi
movl $0xc, %ecx
movl $0x1, %r9d
movl %r9d, %r8d
movl %eax, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
movl $0x40, 0x18(%rsp)
callq 0x3280
movl %eax, -0x84(%rbp)
movq 0x8146(%rip), %rdi      # 0xd978
movl -0x34(%rbp), %edx
movl -0x34(%rbp), %eax
xorl %esi, %esi
movl $0xc, %ecx
movl $0x1, %r9d
movl %r9d, %r8d
movl %eax, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x3, 0x10(%rsp)
movl $0x40, 0x18(%rsp)
callq 0x3280
movl %eax, -0x88(%rbp)
movl $0x800, -0x14(%rbp)     # imm = 0x800
movl $0x1a0000, -0x1c(%rbp)  # imm = 0x1A0000
movq 0x80f5(%rip), %rdi      # 0xd978
movl -0x34(%rbp), %esi
movl -0x14(%rbp), %edx
leaq -0x1c(%rbp), %rcx
callq 0x33c0
movl %eax, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movl %eax, -0x24(%rbp)
movq 0x80d0(%rip), %rdi      # 0xd978
movl -0x24(%rbp), %esi
callq 0x3380
movq %rax, -0x30(%rbp)
movq 0x80bd(%rip), %rdi      # 0xd978
callq 0x3140
xorl %eax, %eax
cmpq -0x30(%rbp), %rax
je 0x58fe
movq 0x7729(%rip), %rax      # 0xcff8
movq (%rax), %rdi
movq -0x30(%rbp), %rax
movzbl 0x1(%rax), %edx
leaq 0x4948(%rip), %rsi      # 0xa229
movb $0x0, %al
callq 0x32d0
movq 0x8089(%rip), %rdi      # 0xd978
callq 0x3430
movl $0x1, %edi
callq 0x3500
callq 0x6060
movl 0x808f(%rip), %edi      # 0xd998
callq 0x6b90
xorl %edi, %edi
callq 0x3500
nopw %cs:(%rax,%rax)