// File: /workspace/github_c_makefile_star2_O1/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 $0x178, %rsp            # imm = 0x178
movl %ecx, %r13d
movl %esi, %r14d
movl %edi, %ebp
movq %rdx, 0x18(%rsp)
cmpb $0x0, 0xa767(%rip)      # 0x104c8
jne 0x5e76
xorl %edi, %edi
callq 0x3570
movq %rax, 0xa71b(%rip)      # 0x10490
testq %rax, %rax
je 0x6488
movq 0xe8(%rax), %rcx
movslq 0xe0(%rax), %rsi
movq %rsi, %rdx
shlq $0x7, %rdx
movq 0x10(%rcx,%rdx), %rcx
movq %rcx, 0xa6f9(%rip)      # 0x10498
leaq 0x48(%rsp), %rcx
movl $0x0, (%rcx)
leaq 0xa66f(%rip), %rdx      # 0x10420
movq %rax, %rdi
callq 0x3560
testq %rax, %rax
je 0x6491
movq (%rax), %rsi
movq %rsi, 0xa6ec(%rip)      # 0x104b8
movq 0xa6bd(%rip), %rdi      # 0x10490
callq 0x35a0
movq %rax, 0xa6c1(%rip)      # 0x104a0
testq %rax, %rax
je 0x649a
movq 0xa6a1(%rip), %rdi      # 0x10490
movq %rax, %rsi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x3440
movq %rax, %rbx
leaq 0x6e12(%rip), %rdi      # 0xcc1a
callq 0x3360
movq %rax, 0xa674(%rip)      # 0x10488
movq 0xa675(%rip), %rdi      # 0x10490
movq %rbx, %rsi
callq 0x3250
cmpq $0x0, 0xa65d(%rip)      # 0x10488
je 0x64a3
movq 0xa658(%rip), %rdi      # 0x10490
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x36d0
movq %rax, 0xa65e(%rip)      # 0x104a8
testq %rax, %rax
je 0x64ac
movq 0xa636(%rip), %rdi      # 0x10490
leaq 0x6df2(%rip), %rsi      # 0xcc53
xorl %edx, %edx
callq 0x32a0
movq %rax, 0xa641(%rip)      # 0x104b0
movb $0x1, 0xa652(%rip)      # 0x104c8
movl $0x1, %eax
testb $0x1, %r13b
jne 0x5ec5
movq 0xa608(%rip), %rax      # 0x10490
movq 0xe8(%rax), %rdx
movslq 0xe0(%rax), %rax
shlq $0x7, %rax
movl 0x18(%rdx,%rax), %ecx
movl 0x1c(%rdx,%rax), %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %ebp, %esi
movl %r14d, %edi
incl %eax
cmpl %ecx, %esi
jg 0x5ebf
leal (%rdi,%r14), %r8d
addl %ebp, %esi
cmpl %edx, %edi
movl %r8d, %edi
jle 0x5eac
cmpl $0x1, %eax
adcl $0x0, %eax
cmpl $0x5, %eax
movl $0x4, %ecx
cmovgel %eax, %ecx
testb $0x8, %r13b
cmovel %eax, %ecx
cmpl $0x4, %ecx
movl $0x3, %eax
cmovgel %ecx, %eax
testb $0x4, %r13b
cmovel %ecx, %eax
cmpl $0x3, %eax
movl $0x2, %r12d
cmovgel %eax, %r12d
testb $0x2, %r13b
cmovel %eax, %r12d
movq 0xa58b(%rip), %rdi      # 0x10490
movq 0xa58c(%rip), %rsi      # 0x10498
movq 0xa58d(%rip), %rax      # 0x104a0
movq (%rax), %rdx
xorl %ecx, %ecx
callq 0x3240
leaq 0x108(%rsp), %r10
movq %rax, 0x60(%r10)
movq $0x20000, 0x48(%r10)    # imm = 0x20000
movq 0xa558(%rip), %rdi      # 0x10490
movq 0xa559(%rip), %rsi      # 0x10498
movl %r12d, %r8d
imull %ebp, %r8d
movl %r12d, %r9d
imull %r14d, %r9d
movq 0xa54c(%rip), %rax      # 0x104a0
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 0x3630
addq $0x30, %rsp
movq %rax, %r15
movq 0xa512(%rip), %rdi      # 0x10490
movq %rax, %rsi
callq 0x33b0
leaq 0x48(%rsp), %rbx
testb $0x40, %r13b
jne 0x6045
movq 0xa4f4(%rip), %rdi      # 0x10490
movq %rbx, %rsi
callq 0x35b0
cmpl $0x13, 0x48(%rsp)
jne 0x5f95
movq 0xa4de(%rip), %rdi      # 0x10490
leaq 0x48(%rsp), %rbx
movq %r15, %rsi
movq %rbx, %rdx
callq 0x3710
movl 0x8(%rbx), %ecx
movl 0xc(%rbx), %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %ebp, %esi
movl %r14d, %edi
incl %eax
cmpl %ecx, %esi
jg 0x5fe5
leal (%rdi,%r14), %r8d
addl %ebp, %esi
cmpl %edx, %edi
movl %r8d, %edi
jle 0x5fd2
cmpl $0x1, %eax
adcl $0x0, %eax
cmpl $0x5, %eax
movl $0x4, %ecx
cmovgel %eax, %ecx
testb $0x8, %r13b
cmovel %eax, %ecx
cmpl $0x4, %ecx
movl $0x3, %eax
cmovgel %ecx, %eax
testb $0x4, %r13b
cmovel %ecx, %eax
cmpl $0x3, %eax
movl $0x2, %r12d
cmovgel %eax, %r12d
testb $0x2, %r13b
cmovel %eax, %r12d
movq 0xa465(%rip), %rdi      # 0x10490
movl %r12d, %edx
imull %ebp, %edx
movl %r12d, %ecx
imull %r14d, %ecx
movq %r15, %rsi
callq 0x35d0
jmp 0x6124
movq $0x2, (%rbx)
movq $0x0, 0x10(%rbx)
movq 0xa435(%rip), %rdi      # 0x10490
leaq 0x69d8(%rip), %rsi      # 0xca3a
movl $0x1, %edx
callq 0x32a0
movq 0xa41d(%rip), %rdi      # 0x10490
movq %r15, %rsi
movq %rax, %rdx
movq %rax, %rcx
movl $0x20, %r8d
xorl %r9d, %r9d
pushq $0x5
pushq %rbx
callq 0x32f0
addq $0x10, %rsp
movq 0xa3f8(%rip), %rdi      # 0x10490
movslq 0xe0(%rdi), %rax
movq 0xe8(%rdi), %rcx
shlq $0x7, %rax
movl 0x18(%rcx,%rax), %r8d
movl 0x1c(%rcx,%rax), %r9d
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x3300
movq 0xa3c9(%rip), %rdi      # 0x10490
movq %r15, %rsi
callq 0x37b0
movq 0xa3ba(%rip), %rdi      # 0x10490
subq $0x20, %rsp
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rsp)
movq %r15, (%rsp)
movq %r15, %rsi
movl $0x1, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
movl $0x1, %r9d
callq 0x30f0
addq $0x20, %rsp
movq 0xa384(%rip), %rdi      # 0x10490
movq %r15, %rsi
xorl %edx, %edx
movl $0x1, %ecx
movl $0x1, %r8d
xorl %r9d, %r9d
callq 0x3450
movq 0xa365(%rip), %rdi      # 0x10490
leaq 0x18(%rsp), %rsi
leaq 0x48(%rsp), %r8
movl $0x1, %edx
movl $0x4, %ecx
callq 0x34b0
testl %eax, %eax
jne 0x617f
movq 0xa341(%rip), %rdi      # 0x10490
leaq 0x68f4(%rip), %rsi      # 0xca4a
xorl %edx, %edx
callq 0x32a0
movq 0xa32c(%rip), %rdi      # 0x10490
leaq 0x48(%rsp), %rbx
movq %r15, %rsi
movq %rbx, %rdx
movq %rax, %rcx
callq 0x33d0
movq (%rbx), %rdi
callq 0x34a0
movl %r13d, 0xc(%rsp)
movq 0xa31d(%rip), %rdi      # 0x104a8
leaq 0x68c5(%rip), %rsi      # 0xca57
leaq 0x68c9(%rip), %rcx      # 0xca62
movl $0x408, %edx            # imm = 0x408
movq %r15, %r8
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x3310
testq %rax, %rax
je 0x6456
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0x36a0
movq 0xa2c8(%rip), %rdi      # 0x10490
leaq 0xa2e1(%rip), %rdx      # 0x104b0
movq %r15, %rsi
movl $0x1, %ecx
callq 0x31e0
movq 0xa2ad(%rip), %rdi      # 0x10490
movq 0xa2b6(%rip), %rsi      # 0x104a0
xorl %edx, %edx
movl $0x1, %ecx
callq 0x3440
movaps 0x6b03(%rip), %xmm0     # 0xcd00
leaq 0x30(%rsp), %r8
movaps %xmm0, (%r8)
movl $0x0, 0x10(%r8)
movq 0xa27b(%rip), %rdi      # 0x10490
movq 0xa29c(%rip), %rsi      # 0x104b8
xorl %edx, %edx
movl $0x1, %ecx
callq *0xa25f(%rip)           # 0x10488
movq %rax, %r13
movq 0xa25d(%rip), %rdi      # 0x10490
movq %r15, %rsi
movq %rax, %rdx
callq 0x32c0
movq 0xa24b(%rip), %rbx      # 0x10490
leaq 0x6a18(%rip), %rsi      # 0xcc64
movq %rbx, %rdi
callq 0x80c9
testl %eax, %eax
je 0x6278
leaq 0x6a1a(%rip), %rdi      # 0xcc79
callq 0x3360
testq %rax, %rax
je 0x62bf
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq *%rax
jmp 0x62bf
leaq 0x6a0d(%rip), %rsi      # 0xcc8c
movq %rbx, %rdi
callq 0x80c9
testl %eax, %eax
je 0x6294
leaq 0x6a10(%rip), %rdi      # 0xcca2
jmp 0x62ae
leaq 0x6a1b(%rip), %rsi      # 0xccb6
movq %rbx, %rdi
callq 0x80c9
testl %eax, %eax
je 0x62bf
leaq 0x6a1d(%rip), %rdi      # 0xcccb
callq 0x3360
testq %rax, %rax
je 0x62bf
movl $0x1, %edi
callq *%rax
movl %ebp, %edi
movl %r14d, %esi
movl $0x2a0, %edx            # imm = 0x2A0
callq 0x38ec
movq %r15, 0x20(%rax)
testq %r15, %r15
je 0x6469
movq %rax, %r14
movq %r15, 0x68(%rax)
movq 0xa1a7(%rip), %rbx      # 0x10490
movq %rbx, 0x60(%rax)
movq %r13, 0x70(%rax)
movq 0x10(%rsp), %rax
movq %rax, 0x78(%r14)
xorl %r13d, %r13d
movq %r13, 0x30(%r14)
movl %r12d, 0xa8(%r14)
movl %r13d, 0xbc(%r14)
movl 0xc(%rsp), %ebp
movl %ebp, 0xa4(%r14)
movl $0x3f800000, 0xa0(%r14) # imm = 0x3F800000
movl %r13d, 0x88(%r14)
movb $0x0, 0x8c(%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x90(%r14)
movq %r13, 0x80(%r14)
movl %r13d, 0x54(%r14)
movq %r14, %rdi
addq $0xc0, %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x31d0
testb $0x20, %bpl
je 0x63e5
leaq 0x20(%rsp), %r12
movw $0x0, 0xc(%r12)
movl %r13d, 0x8(%r12)
leaq 0xa14a(%rip), %rdx      # 0x104c9
movq %rbx, %rdi
movq %r15, %rsi
movl $0x8, %ecx
movl $0x8, %r8d
callq 0x33f0
movq %rax, %rbx
movq 0x60(%r14), %rdi
subq $0x8, %rsp
movq %rax, %rsi
movq %rax, %rdx
movq %r12, %rcx
movq %r12, %r8
xorl %r9d, %r9d
pushq %r13
callq 0x3290
addq $0x10, %rsp
movq %rax, %r15
movq 0x60(%r14), %rdi
movq 0x68(%r14), %rsi
movq %rax, %rdx
callq 0x36f0
movq 0x60(%r14), %rdi
movq %r15, %rsi
callq 0x3380
movq 0x60(%r14), %rdi
movq %rbx, %rsi
callq 0x3720
movl 0xa8(%r14), %eax
movl (%r14), %ecx
movl 0x4(%r14), %edx
movl %ecx, %esi
imull %eax, %esi
subl %esi, %ecx
movl %ecx, %edi
shrl $0x1f, %edi
addl %ecx, %edi
sarl %edi
movl %edi, 0xac(%r14)
imull %edx, %eax
subl %eax, %edx
movl %edx, %ecx
shrl $0x1f, %ecx
addl %edx, %ecx
sarl %ecx
movl %ecx, 0xb0(%r14)
addl %esi, %edi
movl %edi, 0xb4(%r14)
addl %eax, %ecx
movl %ecx, 0xb8(%r14)
movq %r14, %rdi
callq 0x64ba
movq %r14, %rdi
callq 0x6665
movq %r14, %rax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6881(%rip), %rdi      # 0xccde
callq 0x3620
xorl %edi, %edi
callq 0x3420
leaq 0x6590(%rip), %rdi      # 0xca00
leaq 0x6595(%rip), %rsi      # 0xca0c
leaq 0x6599(%rip), %rcx      # 0xca17
movl $0x741, %edx            # imm = 0x741
callq 0x3260
leaq 0x672b(%rip), %rsi      # 0xcbba
jmp 0x64b3
leaq 0x673d(%rip), %rsi      # 0xcbd5
jmp 0x64b3
leaq 0x674f(%rip), %rsi      # 0xcbf0
jmp 0x64b3
leaq 0x6762(%rip), %rsi      # 0xcc0c
jmp 0x64b3
leaq 0x6782(%rip), %rsi      # 0xcc35
xorl %eax, %eax
callq 0x714b