// File: /workspace/github_c_makefile_star2_O2/nkanaev[P]tipsy/src/tigr.c
// Function: tigrWindow
Tigr* tigrWindow(int w, int h, const char* title, int flags) {
    Tigr* bmp = 0;
    Colormap cmap;
    XSetWindowAttributes swa;
    Window xwin;
    GLXContext glc;
    XIC ic;
    int scale;

    initX11Stuff();

    if (flags & TIGR_AUTO) {
        // Always use a 1:1 pixel size, unless downscaled by tigrEnforceScale below.
        scale = 1;
    } else {
        // See how big we can make it and still fit on-screen.
        Screen* screen = DefaultScreenOfDisplay(dpy);
        int maxW = WidthOfScreen(screen);
        int maxH = HeightOfScreen(screen);
        scale = tigrCalcScale(w, h, maxW, maxH);
    }

    scale = tigrEnforceScale(scale, flags);

    cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
    swa.colormap = cmap;
    swa.event_mask = StructureNotifyMask;

    // Create window of wanted size
    xwin = XCreateWindow(dpy, root, 0, 0, w * scale, h * scale, 0, vi->depth, InputOutput, vi->visual,
                         CWColormap | CWEventMask, &swa);
    XMapWindow(dpy, xwin);

    if (flags & TIGR_FULLSCREEN) {
        // https://www.tonyobryan.com//index.php?article=9
        WindowHints hints;
        Atom property;
        hints.flags = 2;
        hints.decorations = 0;
        property = XInternAtom(dpy, "_MOTIF_WM_HINTS", True);
        XChangeProperty(dpy, xwin, property, property, 32, PropModeReplace, (unsigned char*)&hints, 5);
        int screen = DefaultScreen(dpy);
        int dWidth = DisplayWidth(dpy, screen);
        int dHeight = DisplayHeight(dpy, screen);
        XMoveResizeWindow(dpy, xwin, 0, 0, dWidth, dHeight);
        XMapRaised(dpy, xwin);
        XGrabPointer(dpy, xwin, True, 0, GrabModeAsync, GrabModeAsync, xwin, 0L, CurrentTime);
        XGrabKeyboard(dpy, xwin, False, GrabModeAsync, GrabModeAsync, CurrentTime);
    } else {
        // Wait for window to get mapped
        for (;;) {
            XEvent e;
            XNextEvent(dpy, &e);
            if (e.type == MapNotify) {
                break;
            }
        }

        // Reset size if we did not get the window size we wanted above.
        XWindowAttributes wa;
        XGetWindowAttributes(dpy, xwin, &wa);
        scale = tigrCalcScale(w, h, wa.width, wa.height);
        scale = tigrEnforceScale(scale, flags);
        XResizeWindow(dpy, xwin, w * scale, h * scale);
    }

    XTextProperty prop;
    int result = Xutf8TextListToTextProperty(dpy, (char**)&title, 1, XUTF8StringStyle, &prop);
    if (result == Success) {
        Atom wmName = XInternAtom(dpy, "_NET_WM_NAME", 0);
        XSetTextProperty(dpy, xwin, &prop, wmName);
        XFree(prop.value);
    }

    ic = XCreateIC(inputMethod, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, xwin, NULL);
    if (ic == NULL) {
        printf("Failed to create input context\n");
        exit(0);
    }
    XSetICFocus(ic);

    XSetWMProtocols(dpy, xwin, &wmDeleteMessage, 1);

    glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
    int contextAttributes[] = { GLX_CONTEXT_MAJOR_VERSION_ARB, 3, GLX_CONTEXT_MINOR_VERSION_ARB, 3, None };
    glc = glXCreateContextAttribsARB(dpy, fbConfig, NULL, GL_TRUE, contextAttributes);
    glXMakeCurrent(dpy, xwin, glc);

    setupVSync(dpy, xwin);

    bmp = tigrBitmap2(w, h, sizeof(TigrInternal));
    bmp->handle = (void*)xwin;

    TigrInternal* win = tigrInternal(bmp);
    win->win = xwin;
    win->dpy = dpy;
    win->glc = glc;
    win->ic = ic;

    win->shown = 0;
    win->closed = 0;
    win->scale = scale;

    win->lastChar = 0;
    win->flags = flags;
    win->p1 = win->p2 = win->p3 = 0;
    win->p4 = 1;
    win->widgetsWanted = 0;
    win->widgetAlpha = 0;
    win->widgetsScale = 0;
    win->widgets = 0;
    win->gl.gl_legacy = 0;

    memset(win->keys, 0, 256);
    memset(win->prev, 0, 256);

    if (flags & TIGR_NOCURSOR) {
        tigrHideCursor(win);
    }

    tigrPosition(bmp, win->scale, bmp->w, bmp->h, win->pos);
    tigrGAPICreate(bmp);
    tigrGAPIBegin(bmp);

    return bmp;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movl %ecx, %ebp
movl %esi, %r15d
movl %edi, %r14d
movq %rdx, 0x8(%rsp)
cmpb $0x0, 0x95d7(%rip)      # 0xf4c8
jne 0x6001
xorl %edi, %edi
callq 0x3590
movq %rax, 0x958b(%rip)      # 0xf490
testq %rax, %rax
je 0x64f5
movq 0xe8(%rax), %rcx
movslq 0xe0(%rax), %rsi
movq %rsi, %rdx
shlq $0x7, %rdx
movq 0x10(%rcx,%rdx), %rcx
movq %rcx, 0x9569(%rip)      # 0xf498
leaq 0x38(%rsp), %rcx
andl $0x0, (%rcx)
leaq 0x94e2(%rip), %rdx      # 0xf420
movq %rax, %rdi
callq 0x3580
testq %rax, %rax
je 0x64fe
movq (%rax), %rsi
movq %rsi, 0x955f(%rip)      # 0xf4b8
movq 0x9530(%rip), %rdi      # 0xf490
callq 0x35c0
movq %rax, 0x9534(%rip)      # 0xf4a0
testq %rax, %rax
je 0x6507
movq 0x9514(%rip), %rdi      # 0xf490
pushq $0x1
popq %rcx
movq %rax, %rsi
xorl %edx, %edx
callq 0x3460
movq %rax, %rbx
leaq 0x5c67(%rip), %rdi      # 0xbbfa
callq 0x3370
movq %rax, 0x94e9(%rip)      # 0xf488
movq 0x94ea(%rip), %rdi      # 0xf490
movq %rbx, %rsi
callq 0x3250
cmpq $0x0, 0x94d2(%rip)      # 0xf488
je 0x6510
movq 0x94cd(%rip), %rdi      # 0xf490
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x36f0
movq %rax, 0x94d3(%rip)      # 0xf4a8
testq %rax, %rax
je 0x6519
movq 0x94ab(%rip), %rdi      # 0xf490
leaq 0x5c47(%rip), %rsi      # 0xbc33
xorl %edx, %edx
callq 0x32b0
movq %rax, 0x94b6(%rip)      # 0xf4b0
movb $0x1, 0x94c7(%rip)      # 0xf4c8
movq 0x9488(%rip), %rbx      # 0xf490
testb $0x1, %bpl
jne 0x6037
movq 0xe8(%rbx), %rax
movslq 0xe0(%rbx), %rcx
shlq $0x7, %rcx
movl 0x18(%rax,%rcx), %edx
movl 0x1c(%rax,%rcx), %ecx
movl %r14d, %edi
movl %r15d, %esi
callq 0x3a17
movl %eax, %edi
jmp 0x603a
pushq $0x1
popq %rdi
movl %ebp, %esi
callq 0x3a3d
movl %eax, %r13d
movq 0x944d(%rip), %rsi      # 0xf498
movq 0x944e(%rip), %rax      # 0xf4a0
movq (%rax), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x3240
leaq 0xf8(%rsp), %r10
movq %rax, 0x60(%r10)
movq $0x20000, 0x48(%r10)    # imm = 0x20000
movq 0x9416(%rip), %rdi      # 0xf490
movq 0x9417(%rip), %rsi      # 0xf498
movl %r13d, %r8d
imull %r14d, %r8d
movl %r13d, %r9d
imull %r15d, %r9d
movq 0x940a(%rip), %rax      # 0xf4a0
movl 0x14(%rax), %r11d
xorl %edx, %edx
xorl %ecx, %ecx
pushq %r10
pushq $0x2800                 # imm = 0x2800
pushq (%rax)
pushq $0x1
pushq %r11
pushq $0x0
callq 0x3650
addq $0x30, %rsp
movq %rax, %r12
movq 0x93d0(%rip), %rdi      # 0xf490
movq %rax, %rsi
callq 0x33c0
testb $0x40, %bpl
jne 0x613d
leaq 0x38(%rsp), %rbx
movq 0x93b6(%rip), %rdi      # 0xf490
movq %rbx, %rsi
callq 0x35d0
cmpl $0x13, 0x38(%rsp)
jne 0x60d3
movq 0x93a0(%rip), %rdi      # 0xf490
leaq 0x38(%rsp), %rbx
movq %r12, %rsi
movq %rbx, %rdx
callq 0x3730
movl 0x8(%rbx), %edx
movl 0xc(%rbx), %ecx
movl %r14d, %edi
movl %r15d, %esi
callq 0x3a17
movl %eax, %edi
movl %ebp, %esi
callq 0x3a3d
movq 0x936f(%rip), %rdi      # 0xf490
movl %eax, %edx
imull %r14d, %edx
movl %eax, (%rsp)
movl %eax, %ecx
imull %r15d, %ecx
movq %r12, %rsi
callq 0x35f0
jmp 0x6216
movl %r13d, (%rsp)
movl %r15d, %r13d
leaq 0x38(%rsp), %r15
movq $0x2, (%r15)
andq $0x0, 0x10(%r15)
movq 0x9334(%rip), %rdi      # 0xf490
leaq 0x58b7(%rip), %rsi      # 0xba1a
pushq $0x1
popq %rbx
movl %ebx, %edx
callq 0x32b0
movq 0x931c(%rip), %rdi      # 0xf490
pushq $0x20
popq %r8
movq %r12, %rsi
movq %rax, %rdx
movq %rax, %rcx
xorl %r9d, %r9d
pushq $0x5
pushq %r15
movl %r13d, %r15d
callq 0x3300
popq %rcx
popq %rdx
movq 0x92f7(%rip), %rdi      # 0xf490
movslq 0xe0(%rdi), %rax
movq 0xe8(%rdi), %rcx
shlq $0x7, %rax
movl 0x18(%rcx,%rax), %r8d
movl 0x1c(%rcx,%rax), %r9d
movq %r12, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x3310
movq 0x92c8(%rip), %rdi      # 0xf490
movq %r12, %rsi
callq 0x37c0
movq 0x92b9(%rip), %rdi      # 0xf490
subq $0x20, %rsp
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rsp)
movq %r12, (%rsp)
movq %r12, %rsi
movl %ebx, %edx
xorl %ecx, %ecx
movl %ebx, %r8d
movl %ebx, %r9d
callq 0x30f0
addq $0x20, %rsp
movq 0x928c(%rip), %rdi      # 0xf490
movq %r12, %rsi
xorl %edx, %edx
movl %ebx, %ecx
movl %ebx, %r8d
xorl %r9d, %r9d
callq 0x3470
movq 0x9273(%rip), %rdi      # 0xf490
leaq 0x8(%rsp), %rsi
pushq $0x1
popq %rdx
pushq $0x4
popq %rcx
leaq 0x38(%rsp), %r8
callq 0x34d0
testl %eax, %eax
jne 0x626d
movq 0x9253(%rip), %rdi      # 0xf490
leaq 0x57e6(%rip), %rsi      # 0xba2a
xorl %edx, %edx
callq 0x32b0
movq 0x923e(%rip), %rdi      # 0xf490
leaq 0x38(%rsp), %rbx
movq %r12, %rsi
movq %rbx, %rdx
movq %rax, %rcx
callq 0x33e0
movq (%rbx), %rdi
callq 0x34c0
movl %ebp, 0x4(%rsp)
movq 0x9230(%rip), %rdi      # 0xf4a8
leaq 0x57b8(%rip), %rsi      # 0xba37
leaq 0x57bc(%rip), %rcx      # 0xba42
movl $0x408, %edx            # imm = 0x408
movq %r12, %r8
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x3320
testq %rax, %rax
je 0x64e2
movq %rax, %r13
movq %rax, %rdi
callq 0x36c0
movq 0x91dd(%rip), %rdi      # 0xf490
leaq 0x91f6(%rip), %rdx      # 0xf4b0
pushq $0x1
popq %rbx
movq %r12, %rsi
movl %ebx, %ecx
callq 0x31e0
movq 0x91c2(%rip), %rdi      # 0xf490
movq 0x91cb(%rip), %rsi      # 0xf4a0
xorl %edx, %edx
movl %ebx, %ecx
callq 0x3460
leaq 0x20(%rsp), %r8
andl $0x0, 0x10(%r8)
movaps 0x59f1(%rip), %xmm0     # 0xbce0
movaps %xmm0, (%r8)
movq 0x9196(%rip), %rdi      # 0xf490
movq 0x91b7(%rip), %rsi      # 0xf4b8
xorl %edx, %edx
movl %ebx, %ecx
callq *0x917d(%rip)           # 0xf488
movq %rax, %rbp
movq 0x917b(%rip), %rdi      # 0xf490
movq %r12, %rsi
movq %rax, %rdx
callq 0x32d0
movq 0x9169(%rip), %rbx      # 0xf490
leaq 0x5916(%rip), %rsi      # 0xbc44
movq %rbx, %rdi
callq 0x7a33
testl %eax, %eax
je 0x6358
leaq 0x5918(%rip), %rdi      # 0xbc59
callq 0x3370
testq %rax, %rax
je 0x639d
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq *%rax
jmp 0x639d
leaq 0x590d(%rip), %rsi      # 0xbc6c
movq %rbx, %rdi
callq 0x7a33
testl %eax, %eax
je 0x6374
leaq 0x5910(%rip), %rdi      # 0xbc82
jmp 0x638e
leaq 0x591b(%rip), %rsi      # 0xbc96
movq %rbx, %rdi
callq 0x7a33
testl %eax, %eax
je 0x639d
leaq 0x591d(%rip), %rdi      # 0xbcab
callq 0x3370
testq %rax, %rax
je 0x639d
pushq $0x1
popq %rdi
callq *%rax
movl %r14d, %edi
movl %r15d, %esi
movl $0x2a0, %edx            # imm = 0x2A0
callq 0x38fc
movq %rax, %r14
movq %r12, 0x20(%rax)
movq %rax, %rdi
callq 0x5e9d
movq %rax, %rbx
movq %r12, 0x38(%rax)
movq 0x90c6(%rip), %r15      # 0xf490
movq %r15, 0x30(%rax)
movq %rbp, 0x40(%rax)
movq %r13, 0x48(%rax)
andq $0x0, (%rax)
movl (%rsp), %r13d
movl %r13d, 0x78(%rax)
andl $0x0, 0x8c(%rax)
movl 0x4(%rsp), %ebp
movl %ebp, 0x74(%rax)
movl $0x3f800000, 0x70(%rax) # imm = 0x3F800000
andl $0x0, 0x58(%rax)
movb $0x0, 0x5c(%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x60(%rax)
andq $0x0, 0x50(%rax)
andl $0x0, 0x24(%rax)
leaq 0x90(%rax), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x31d0
testb $0x20, %bpl
je 0x64a4
leaq 0x10(%rsp), %r13
andw $0x0, 0xc(%r13)
andl $0x0, 0x8(%r13)
leaq 0x908a(%rip), %rdx      # 0xf4c9
pushq $0x8
popq %rcx
movq %r15, %rdi
movq %r12, %rsi
movl %ecx, %r8d
callq 0x3410
movq %rax, %r15
movq 0x30(%rbx), %rdi
subq $0x10, %rsp
andl $0x0, (%rsp)
movq %rax, %rsi
movq %rax, %rdx
movq %r13, %rcx
movq %r13, %r8
xorl %r9d, %r9d
callq 0x32a0
popq %rcx
popq %rdx
movq %rax, %r12
movq 0x30(%rbx), %rdi
movq 0x38(%rbx), %rsi
movq %rax, %rdx
callq 0x3710
movq 0x30(%rbx), %rdi
movq %r12, %rsi
callq 0x3390
movq 0x30(%rbx), %rdi
movq %r15, %rsi
callq 0x3740
movl 0x78(%rbx), %r13d
movl (%r14), %edx
movl 0x4(%r14), %ecx
addq $0x7c, %rbx
movq %r14, %rdi
movl %r13d, %esi
movq %rbx, %r8
callq 0x3a6e
movq %r14, %rdi
callq 0x6527
movq %r14, %rdi
callq 0x66a7
movq %r14, %rax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x57d5(%rip), %rdi      # 0xbcbe
callq 0x3640
xorl %edi, %edi
callq 0x3440
leaq 0x569e(%rip), %rsi      # 0xbb9a
jmp 0x6520
leaq 0x56b0(%rip), %rsi      # 0xbbb5
jmp 0x6520
leaq 0x56c2(%rip), %rsi      # 0xbbd0
jmp 0x6520
leaq 0x56d5(%rip), %rsi      # 0xbbec
jmp 0x6520
leaq 0x56f5(%rip), %rsi      # 0xbc15
xorl %eax, %eax
callq 0x7002