// File: RegulusImpact[P]sadboy/src/Graphics/XGPU.cpp
// Function: XGPU::init_x()
void XGPU::init_x() {
    // setup up the display variables
    display = XOpenDisplay((char*)0);

    if (display == NULL) {
        std::cerr << "X could not open display. Make sure the DISPLAY environment variable is configured." << std::endl;
        std::cerr << "E.g. DISPLAY:0" << std::endl;
        exit(1);
    }

    screen = XDefaultScreen(display);

    black = BlackPixel(display, screen);
    white = WhitePixel(display, screen);

    // create the window
    window = XCreateSimpleWindow(
        display,
        DefaultRootWindow(display),
        0,
        0,
        (windowScalar * MAX_X), // 200 wide
        (windowScalar * MAX_Y), // 300 down
        5,
        white, // foreground white
        black // background black
    );

    // set window properties
    XSetStandardProperties(
        display,
        window,
        "Sadboy", // Title maximized
        "Emulator", // Title minimized
        None,
        NULL,
        0,
        NULL
    );

    // set inputs
    XSelectInput(display, window, ExposureMask|ButtonPressMask|KeyPressMask);

    // create graphics context
    gc = XCreateGC(display, window, 0, 0);

    // set the fore/back ground colors currently in use in the window
    XSetForeground(display, gc, white);
    XSetBackground(display, gc, white);

    // clear the window and bring to foreground
    XClearWindow(display, window);
    XMapRaised(display, window);

    XDrawString(display, window, gc, 5, 5,"Sadboy world!", strlen("Sadboy world!"));
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x5660
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x5a20(%rax)
cmpq $0x0, 0x5a20(%rax)
jne 0x34953
movq 0x176e4(%rip), %rdi     # 0x4bff0
leaq 0x52eb(%rip), %rsi      # 0x39bfe
callq 0x5460
movq %rax, %rdi
movq 0x17686(%rip), %rsi     # 0x4bfa8
callq 0x54d0
movq 0x176c2(%rip), %rdi     # 0x4bff0
leaq 0x531d(%rip), %rsi      # 0x39c52
callq 0x5460
movq %rax, %rdi
movq 0x17664(%rip), %rsi     # 0x4bfa8
callq 0x54d0
movl $0x1, %edi
callq 0x5590
movq -0x10(%rbp), %rax
movq 0x5a20(%rax), %rdi
callq 0x5280
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x5a28(%rax)
movq 0x5a20(%rax), %rcx
movq 0xe8(%rcx), %rcx
movslq 0x5a28(%rax), %rdx
shlq $0x7, %rdx
addq %rdx, %rcx
movq 0x60(%rcx), %rcx
movl %ecx, 0x5a48(%rax)
movq 0x5a20(%rax), %rcx
movq 0xe8(%rcx), %rcx
movslq 0x5a28(%rax), %rdx
shlq $0x7, %rdx
addq %rdx, %rcx
movq 0x58(%rcx), %rcx
movl %ecx, 0x5a4c(%rax)
movq 0x5a20(%rax), %rdi
movq 0x5a20(%rax), %rcx
movq 0xe8(%rcx), %rcx
movq 0x5a20(%rax), %rdx
movslq 0xe0(%rdx), %rdx
shlq $0x7, %rdx
addq %rdx, %rcx
movq 0x10(%rcx), %rsi
movzbl 0x5a90(%rax), %ecx
imull $0xa0, %ecx, %r8d
movzbl 0x5a90(%rax), %ecx
imull $0x90, %ecx, %r9d
movl 0x5a4c(%rax), %ecx
movl %ecx, %r10d
movl 0x5a48(%rax), %eax
xorl %ecx, %ecx
movl %ecx, %edx
movl $0x5, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x5110
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x5a30(%rax)
movq 0x5a20(%rax), %rdi
movq 0x5a30(%rax), %rsi
leaq 0x5210(%rip), %rdx      # 0x39c61
leaq 0x5210(%rip), %rcx      # 0x39c68
xorl %eax, %eax
movl %eax, %r9d
xorl %eax, %eax
movq %r9, %r8
movl $0x0, (%rsp)
movq $0x0, 0x8(%rsp)
callq 0x51f0
movq -0x10(%rbp), %rax
movq 0x5a20(%rax), %rdi
movq 0x5a30(%rax), %rsi
movl $0x8005, %edx           # imm = 0x8005
callq 0x52d0
movq -0x10(%rbp), %rax
movq 0x5a20(%rax), %rdi
movq 0x5a30(%rax), %rsi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x5210
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x5a38(%rax)
movq 0x5a20(%rax), %rdi
movq 0x5a38(%rax), %rsi
movl 0x5a4c(%rax), %eax
movl %eax, %edx
callq 0x5120
movq -0x10(%rbp), %rax
movq 0x5a20(%rax), %rdi
movq 0x5a38(%rax), %rsi
movl 0x5a4c(%rax), %eax
movl %eax, %edx
callq 0x5600
movq -0x10(%rbp), %rax
movq 0x5a20(%rax), %rdi
movq 0x5a30(%rax), %rsi
callq 0x5420
movq -0x10(%rbp), %rax
movq 0x5a20(%rax), %rdi
movq 0x5a30(%rax), %rsi
callq 0x5830
movq -0x10(%rbp), %rax
movq 0x5a20(%rax), %rdi
movq 0x5a30(%rax), %rsi
movq 0x5a38(%rax), %rdx
movl $0x5, %r8d
leaq 0x5124(%rip), %r9       # 0x39c71
movl %r8d, %ecx
movl $0xd, (%rsp)
callq 0x5480
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)