// File: /workspace/github_cpp_makefile_star2_O3/EstherJin[P]TetrisRemake/window.cc
// Function: Xwindow::Xwindow(int, int, int)
Xwindow::Xwindow(int width, int height, int player) {
    d = XOpenDisplay(NULL);
    if (d == NULL) {
        cerr << "Cannot open display" << endl;
        exit(1);
    }

    s = DefaultScreen(d);
    if (player == 1) {
        w = XCreateSimpleWindow(
                d, RootWindow(d, s), 10, 10, width, height,
                1, BlackPixel(d, s), WhitePixel(d, s)
        );
    } else {
        w = XCreateSimpleWindow(
                d, RootWindow(d, s), 800, 10, width, height,
                1, BlackPixel(d, s), WhitePixel(d, s)
        );
    }

    XSelectInput(d, w, ExposureMask | KeyPressMask);

    Pixmap pix = XCreatePixmap(
            d, w, width, height,
            DefaultDepth(d, DefaultScreen(d))
    );

    gc = XCreateGC(d, pix, 0, 0);

    // Set up colours.
    XColor xcolour;
    Colormap cmap;

    const size_t numColours = 12;
    char color_vals[numColours][20] = {
            "PapayaWhip", "gray20", "MediumPurple3",
            "RoyalBlue4", "tomato4", "goldenrod1",
            "SeaGreen", "sienna1", "turquoise", "SaddleBrown", "White", "Brown"
    };

    cmap = DefaultColormap(d, DefaultScreen(d));

    for(unsigned int i = 0; i < numColours; ++i) {
        XParseColor(d, cmap, color_vals[i], &xcolour);
        XAllocColor(d, cmap, &xcolour);
        colours[i] = xcolour.pixel;
    }

    XSetForeground(d, gc, colours[gray20]);

    // Make window non-resizeable.
    XSizeHints hints;
    hints.flags = (USPosition | PSize | PMinSize | PMaxSize );
    hints.height = hints.base_height = hints.min_height = hints.max_height = height;
    hints.width = hints.base_width = hints.min_width = hints.max_width = width;
    XSetNormalHints(d, w, &hints);

    // map window and flush
    XMapRaised(d, w);
    XFlush(d);

    // wait 1 second for setup
    sleep(1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movl %ecx, %r15d
movl %edx, %r14d
movl %esi, %ebp
movq %rdi, %rbx
xorl %edi, %edi
callq 0x5370
movq %rax, (%rbx)
testq %rax, %rax
je 0x12af7
movslq 0xe0(%rax), %r10
movl %r10d, 0x10(%rbx)
shlq $0x7, %r10
cmpl $0x1, %r15d
movq 0xe8(%rax), %r11
movq 0x10(%r11,%r10), %rsi
movl $0xa, %ecx
movl $0x320, %edx            # imm = 0x320
cmovel %ecx, %edx
subq $0x8, %rsp
movq %rax, %rdi
movl $0xa, %ecx
movl %ebp, %r8d
movl %r14d, %r9d
pushq 0x58(%r11,%r10)
pushq 0x60(%r11,%r10)
pushq $0x1
callq 0x50c0
addq $0x20, %rsp
movq %rax, 0x8(%rbx)
movq (%rbx), %rdi
movl $0x8001, %edx           # imm = 0x8001
movq %rax, %rsi
callq 0x51b0
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
movq 0xe8(%rdi), %rax
movslq 0xe0(%rdi), %rcx
shlq $0x7, %rcx
movl 0x38(%rax,%rcx), %r8d
movl %ebp, 0xc(%rsp)
movl %ebp, %edx
movl %r14d, %ecx
callq 0x5080
movq (%rbx), %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x5170
movq %rax, 0x18(%rbx)
leaq 0xf5c(%rip), %rsi       # 0x13970
leaq 0x70(%rsp), %r15
movl $0xf0, %edx
movq %r15, %rdi
callq 0x5230
movq (%rbx), %rax
movq 0xe8(%rax), %rcx
movslq 0xe0(%rax), %rax
shlq $0x7, %rax
movq 0x50(%rcx,%rax), %r12
movl $0x4, %ebp
leaq 0x10(%rsp), %r13
movq (%rbx), %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x5210
movq (%rbx), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x5240
movq 0x10(%rsp), %rax
movq %rax, (%rbx,%rbp,8)
incq %rbp
addq $0x14, %r15
cmpq $0x10, %rbp
jne 0x12a4a
movq (%rbx), %rdi
movq 0x18(%rbx), %rsi
movq 0x28(%rbx), %rdx
callq 0x50f0
leaq 0x20(%rsp), %rdx
movq $0x39, (%rdx)
movl %r14d, 0x24(%rdx)
movl %r14d, 0x1c(%rdx)
movl %r14d, 0x44(%rdx)
movl %r14d, 0x14(%rdx)
movl 0xc(%rsp), %eax
movl %eax, 0x20(%rdx)
movl %eax, 0x18(%rdx)
movl %eax, 0x40(%rdx)
movl %eax, 0x10(%rdx)
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
callq 0x51f0
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
callq 0x54d0
movq (%rbx), %rdi
callq 0x53f0
movl $0x1, %edi
callq 0x5200
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x84f2(%rip), %rdi      # 0x1aff0
leaq 0xe49(%rip), %rsi       # 0x1394e
callq 0x5290
movq %rax, %rdi
callq 0x5130
movl $0x1, %edi
callq 0x5330