// File: /workspace/github_c_makefile_star2_O2/tsoding[P]x11-double-buffering/main.c
// Function: main
int main(void)
{
    printf("Double Buffering Implementation: "DB_IMPL_NAME"\n");

    DB db = {0};

    Display *display = XOpenDisplay(NULL);
    if (display == NULL) {
        fprintf(stderr, "ERROR: could not open the default display\n");
        exit(1);
    }

    Window window = XCreateSimpleWindow(
                        display,
                        XDefaultRootWindow(display),
                        0, 0,
                        WIDTH, HEIGHT,
                        0,
                        0,
                        0);

    db_init(&db, display, window);

    XStoreName(display, window, "DB Implementation: "DB_IMPL_NAME);

    Atom wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False);
    XSetWMProtocols(display, window, &wm_delete_window, 1);

    XSelectInput(display, window, KeyPressMask);

    XMapWindow(display, window);

    int rect_x = 10;
    int rect_y = 10;

    int rect_dx = -1;
    int rect_dy = -1;

    int quit = 0;
    while (!quit) {
        while (XPending(display) > 0) {
            XEvent event = {0};
            XNextEvent(display, &event);
            switch (event.type) {
            case KeyPress: {
                switch (XLookupKeysym(&event.xkey, 0)) {
                case 'q':
                    quit = 1;
                    break;
                default:
                {}
                }
            } break;
            case ClientMessage: {
                if ((Atom) event.xclient.data.l[0] == wm_delete_window) {
                    quit = 1;
                }
            }
            break;
            }
        }

        db_clear(&db);
        db_fill_rect(&db, rect_x, rect_y, RECT_WIDTH, RECT_HEIGHT);
        db_swap_buffers(&db);

        int rect_nx = rect_x + rect_dx;
        if (rect_nx <= 0 || rect_nx + RECT_WIDTH >= WIDTH) {
            rect_dx *= -1;
        } else {
            rect_x = rect_nx;
        }

        int rect_ny = rect_y + rect_dy;
        if (rect_ny <= 0 || rect_ny + RECT_HEIGHT >= HEIGHT) {
            rect_dy *= -1;
        } else {
            rect_y = rect_ny;
        }

        usleep(1000*1000/FPS);
    }

    XCloseDisplay(display);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
leaq 0xcb2(%rip), %rdi       # 0x2076
callq 0x1030
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x1080
testq %rax, %rax
je 0x15c1
movq %rax, %rbx
movq %rax, %rdi
callq 0x1110
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rsp)
andl $0x0, (%rsp)
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0x320, %r8d            # imm = 0x320
movl $0x258, %r9d            # imm = 0x258
callq 0x1100
movq %rax, %r15
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x128c
leaq 0xc16(%rip), %rdx       # 0x204b
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1040
leaq 0xc1e(%rip), %rsi       # 0x2065
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10c0
leaq 0x38(%rsp), %rdx
movq %rax, (%rdx)
pushq $0x1
popq %rbp
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %ecx
callq 0x1090
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x10e0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1130
pushq $0xa
popq %r13
pushq $-0x1
popq %rcx
leaq 0x68(%rsp), %r15
movl %ecx, %eax
movl %r13d, %edx
testl %r14d, %r14d
jne 0x15a5
movq %rdx, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
movq %rax, 0x30(%rsp)
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0x1050
testl %eax, %eax
jle 0x1505
movl $0xc0, %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0x1070
movq %rbx, %rdi
movq %r15, %rsi
callq 0x10b0
movl 0x68(%rsp), %eax
cmpl $0x21, %eax
je 0x14f2
cmpl $0x2, %eax
jne 0x14ae
movq %r15, %rdi
xorl %esi, %esi
callq 0x10d0
cmpq $0x71, %rax
jmp 0x14ff
movq 0xa0(%rsp), %rax
cmpq 0x38(%rsp), %rax
cmovel %ebp, %r14d
jmp 0x14ae
leaq 0x40(%rsp), %r12
movq %r12, %rdi
callq 0x12d7
movq %r12, %rdi
movq 0x28(%rsp), %rbp
movl %ebp, %esi
movl %r13d, %edx
movl $0x12c, %ecx            # imm = 0x12C
movl $0x12c, %r8d            # imm = 0x12C
callq 0x1315
movq %r12, %rdi
callq 0x136d
movq 0x20(%rsp), %rcx
leal (%rcx,%rbp), %eax
addl $0xfffffe0c, %eax       # imm = 0xFFFFFE0C
movl %ecx, %r12d
negl %r12d
cmpl $0xfffffe0d, %eax       # imm = 0xFFFFFE0D
cmovael %ecx, %r12d
leal (%rbp,%rcx), %eax
cmovbl %ebp, %eax
movl %eax, 0x20(%rsp)
movq 0x30(%rsp), %rdx
leal (%r13,%rdx), %ebp
leal (%rdx,%r13), %eax
addl $0xfffffed4, %eax       # imm = 0xFFFFFED4
movq %r13, %rcx
movl %edx, %r13d
negl %r13d
cmpl $0xfffffed5, %eax       # imm = 0xFFFFFED5
cmovael %edx, %r13d
cmovbl %ecx, %ebp
movl $0x208d, %edi           # imm = 0x208D
callq 0x1180
movl %r12d, %ecx
movl %r13d, %eax
movl %ebp, %r13d
pushq $0x1
popq %rbp
movl 0x20(%rsp), %edx
jmp 0x1493
movq %rbx, %rdi
callq 0x1160
xorl %eax, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2a30(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xa4e(%rip), %rdi       # 0x2020
pushq $0x2a
popq %rsi
pushq $0x1
popq %rdx
callq 0x1150
pushq $0x1
popq %rdi
callq 0x1140