// File: /workspace/github_c_makefile_star2_O3/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, %r12d
movl %esi, %r14d
movl %edi, %ebp
movq %rdx, 0x18(%rsp)
cmpb $0x0, 0xa64a(%rip)      # 0x104b8
jne 0x5f83
xorl %edi, %edi
callq 0x3570
movq %rax, 0xa5fe(%rip)      # 0x10480
testq %rax, %rax
je 0x6584
movq 0xe8(%rax), %rcx
movslq 0xe0(%rax), %rsi
movq %rsi, %rdx
shlq $0x7, %rdx
movq 0x10(%rcx,%rdx), %rcx
movq %rcx, 0xa5dc(%rip)      # 0x10488
leaq 0x48(%rsp), %rcx
movl $0x0, (%rcx)
leaq 0xa552(%rip), %rdx      # 0x10410
movq %rax, %rdi
callq 0x3560
testq %rax, %rax
je 0x658d
movq (%rax), %rsi
movq %rsi, 0xa5cf(%rip)      # 0x104a8
movq 0xa5a0(%rip), %rdi      # 0x10480
callq 0x35a0
movq %rax, 0xa5a4(%rip)      # 0x10490
testq %rax, %rax
je 0x6596
movq 0xa584(%rip), %rdi      # 0x10480
movq %rax, %rsi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x3440
movq %rax, %rbx
leaq 0x6d4d(%rip), %rdi      # 0xcc62
callq 0x3360
movq %rax, 0xa557(%rip)      # 0x10478
movq 0xa558(%rip), %rdi      # 0x10480
movq %rbx, %rsi
callq 0x3250
cmpq $0x0, 0xa540(%rip)      # 0x10478
je 0x659f
movq 0xa53b(%rip), %rdi      # 0x10480
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x36d0
movq %rax, 0xa541(%rip)      # 0x10498
testq %rax, %rax
je 0x65a8
movq 0xa519(%rip), %rdi      # 0x10480
leaq 0x6d2d(%rip), %rsi      # 0xcc9b
xorl %edx, %edx
callq 0x32a0
movq %rax, 0xa524(%rip)      # 0x104a0
movb $0x1, 0xa535(%rip)      # 0x104b8
movl $0x1, %eax
movq 0xa4f1(%rip), %rdi      # 0x10480
testb $0x1, %r12b
jne 0x5fd3
movq 0xe8(%rdi), %rax
movslq 0xe0(%rdi), %rdx
shlq $0x7, %rdx
movl 0x18(%rax,%rdx), %ecx
movl 0x1c(%rax,%rdx), %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %ebp, %esi
movl %r14d, %r8d
incl %eax
cmpl %ecx, %esi
jg 0x5fcd
leal (%r8,%r14), %r9d
addl %ebp, %esi
cmpl %edx, %r8d
movl %r9d, %r8d
jle 0x5fb9
cmpl $0x1, %eax
adcl $0x0, %eax
cmpl $0x5, %eax
movl $0x4, %ecx
cmovgel %eax, %ecx
testb $0x8, %r12b
cmovel %eax, %ecx
cmpl $0x4, %ecx
movl $0x3, %eax
cmovgel %ecx, %eax
testb $0x4, %r12b
cmovel %ecx, %eax
cmpl $0x3, %eax
movl $0x2, %r13d
cmovgel %eax, %r13d
testb $0x2, %r12b
cmovel %eax, %r13d
movq 0xa475(%rip), %rsi      # 0x10488
movq 0xa476(%rip), %rax      # 0x10490
movq (%rax), %rdx
xorl %ecx, %ecx
callq 0x3240
leaq 0x108(%rsp), %r10
movq %rax, 0x60(%r10)
movq $0x20000, 0x48(%r10)    # imm = 0x20000
movq 0xa441(%rip), %rdi      # 0x10480
movq 0xa442(%rip), %rsi      # 0x10488
movl %r13d, %r8d
imull %ebp, %r8d
movl %r13d, %r9d
imull %r14d, %r9d
movq 0xa435(%rip), %rax      # 0x10490
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 0xa3fb(%rip), %rdi      # 0x10480
movq %rax, %rsi
callq 0x33b0
leaq 0x48(%rsp), %rbx
testb $0x40, %r12b
jne 0x614c
movq 0xa3dd(%rip), %rdi      # 0x10480
movq %rbx, %rsi
callq 0x35b0
cmpl $0x13, 0x48(%rsp)
jne 0x609c
movq 0xa3c7(%rip), %rdi      # 0x10480
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 0x60ec
leal (%rdi,%r14), %r8d
addl %ebp, %esi
cmpl %edx, %edi
movl %r8d, %edi
jle 0x60d9
cmpl $0x1, %eax
adcl $0x0, %eax
cmpl $0x5, %eax
movl $0x4, %ecx
cmovgel %eax, %ecx
testb $0x8, %r12b
cmovel %eax, %ecx
cmpl $0x4, %ecx
movl $0x3, %eax
cmovgel %ecx, %eax
testb $0x4, %r12b
cmovel %ecx, %eax
cmpl $0x3, %eax
movl $0x2, %r13d
cmovgel %eax, %r13d
testb $0x2, %r12b
cmovel %eax, %r13d
movq 0xa34e(%rip), %rdi      # 0x10480
movl %r13d, %edx
imull %ebp, %edx
movl %r13d, %ecx
imull %r14d, %ecx
movq %r15, %rsi
callq 0x35d0
jmp 0x622b
movq $0x2, (%rbx)
movq $0x0, 0x10(%rbx)
movq 0xa31e(%rip), %rdi      # 0x10480
leaq 0x6919(%rip), %rsi      # 0xca82
movl $0x1, %edx
callq 0x32a0
movq 0xa306(%rip), %rdi      # 0x10480
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 0xa2e1(%rip), %rdi      # 0x10480
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 0xa2b2(%rip), %rdi      # 0x10480
movq %r15, %rsi
callq 0x37a0
movq 0xa2a3(%rip), %rdi      # 0x10480
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 0xa26d(%rip), %rdi      # 0x10480
movq %r15, %rsi
xorl %edx, %edx
movl $0x1, %ecx
movl $0x1, %r8d
xorl %r9d, %r9d
callq 0x3450
movq 0xa24e(%rip), %rdi      # 0x10480
leaq 0x18(%rsp), %rsi
leaq 0x48(%rsp), %r8
movl $0x1, %edx
movl $0x4, %ecx
callq 0x34b0
testl %eax, %eax
jne 0x6286
movq 0xa22a(%rip), %rdi      # 0x10480
leaq 0x6835(%rip), %rsi      # 0xca92
xorl %edx, %edx
callq 0x32a0
movq 0xa215(%rip), %rdi      # 0x10480
leaq 0x48(%rsp), %rbx
movq %r15, %rsi
movq %rbx, %rdx
movq %rax, %rcx
callq 0x33d0
movq (%rbx), %rdi
callq 0x34a0
movl %r12d, 0xc(%rsp)
movq 0xa206(%rip), %rdi      # 0x10498
leaq 0x6806(%rip), %rsi      # 0xca9f
leaq 0x680a(%rip), %rcx      # 0xcaaa
movl $0x408, %edx            # imm = 0x408
movq %r15, %r8
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x3310
testq %rax, %rax
je 0x6552
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0x36a0
movq 0xa1b1(%rip), %rdi      # 0x10480
leaq 0xa1ca(%rip), %rdx      # 0x104a0
movq %r15, %rsi
movl $0x1, %ecx
callq 0x31e0
movq 0xa196(%rip), %rdi      # 0x10480
movq 0xa19f(%rip), %rsi      # 0x10490
xorl %edx, %edx
movl $0x1, %ecx
callq 0x3440
movaps 0x6a4c(%rip), %xmm0     # 0xcd50
leaq 0x30(%rsp), %r8
movaps %xmm0, (%r8)
movl $0x0, 0x10(%r8)
movq 0xa164(%rip), %rdi      # 0x10480
movq 0xa185(%rip), %rsi      # 0x104a8
xorl %edx, %edx
movl $0x1, %ecx
callq *0xa148(%rip)           # 0x10478
movq %rax, %r12
movq 0xa146(%rip), %rdi      # 0x10480
movq %r15, %rsi
movq %rax, %rdx
callq 0x32c0
movq 0xa134(%rip), %rbx      # 0x10480
leaq 0x6959(%rip), %rsi      # 0xccac
movq %rbx, %rdi
callq 0x8229
testl %eax, %eax
je 0x637f
leaq 0x695b(%rip), %rdi      # 0xccc1
callq 0x3360
testq %rax, %rax
je 0x63c6
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
callq *%rax
jmp 0x63c6
leaq 0x694e(%rip), %rsi      # 0xccd4
movq %rbx, %rdi
callq 0x8229
testl %eax, %eax
je 0x639b
leaq 0x6951(%rip), %rdi      # 0xccea
jmp 0x63b5
leaq 0x695c(%rip), %rsi      # 0xccfe
movq %rbx, %rdi
callq 0x8229
testl %eax, %eax
je 0x63c6
leaq 0x695e(%rip), %rdi      # 0xcd13
callq 0x3360
testq %rax, %rax
je 0x63c6
movl $0x1, %edi
callq *%rax
movl %ebp, %edi
movl %r14d, %esi
movl $0x2a0, %edx            # imm = 0x2A0
callq 0x38dc
movq %r15, 0x20(%rax)
testq %r15, %r15
je 0x6565
movq %rax, %r14
movq %r15, 0x68(%rax)
movq 0xa090(%rip), %rbp      # 0x10480
movq %rbp, 0x60(%rax)
movq %r12, 0x70(%rax)
movq 0x10(%rsp), %rax
movq %rax, 0x78(%r14)
xorl %ebx, %ebx
movq %rbx, 0x30(%r14)
movl %r13d, 0xa8(%r14)
movl %ebx, 0xbc(%r14)
movl 0xc(%rsp), %r12d
movl %r12d, 0xa4(%r14)
movl $0x3f800000, 0xa0(%r14) # imm = 0x3F800000
movl %ebx, 0x88(%r14)
movb $0x0, 0x8c(%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x90(%r14)
movq %rbx, 0x80(%r14)
movl %ebx, 0x54(%r14)
movq %r14, %rdi
addq $0xc0, %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x31d0
testb $0x20, %r12b
je 0x64e4
leaq 0x20(%rsp), %rax
movw $0x0, 0xc(%rax)
movl %ebx, 0x8(%rax)
leaq 0xa037(%rip), %rdx      # 0x104b9
movq %rbp, %rdi
movq %r15, %rsi
movl $0x8, %ecx
movl $0x8, %r8d
callq 0x33f0
movq %rax, %r12
subq $0x8, %rsp
movq %rbp, %rdi
movq %rax, %rsi
movq %rax, %rdx
leaq 0x28(%rsp), %rcx
movq %rcx, %r8
xorl %r9d, %r9d
pushq %rbx
callq 0x3290
addq $0x10, %rsp
movq %rax, %rbx
movq %rbp, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x36f0
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3380
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3720
movl (%r14), %eax
movl 0x4(%r14), %ecx
movl %eax, %edx
imull %r13d, %edx
subl %edx, %eax
movl %eax, %esi
shrl $0x1f, %esi
addl %eax, %esi
sarl %esi
movl %esi, 0xac(%r14)
imull %ecx, %r13d
subl %r13d, %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
movl %eax, 0xb0(%r14)
addl %edx, %esi
movl %esi, 0xb4(%r14)
addl %r13d, %eax
movl %eax, 0xb8(%r14)
movq %r14, %rdi
callq 0x65b6
movq %r14, %rdi
callq 0x676b
movq %r14, %rax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x67cd(%rip), %rdi      # 0xcd26
callq 0x3620
xorl %edi, %edi
callq 0x3420
leaq 0x64dc(%rip), %rdi      # 0xca48
leaq 0x64e1(%rip), %rsi      # 0xca54
leaq 0x64e5(%rip), %rcx      # 0xca5f
movl $0x741, %edx            # imm = 0x741
callq 0x3260
leaq 0x6677(%rip), %rsi      # 0xcc02
jmp 0x65af
leaq 0x6689(%rip), %rsi      # 0xcc1d
jmp 0x65af
leaq 0x669b(%rip), %rsi      # 0xcc38
jmp 0x65af
leaq 0x66ae(%rip), %rsi      # 0xcc54
jmp 0x65af
leaq 0x66ce(%rip), %rsi      # 0xcc7d
xorl %eax, %eax
callq 0x7276