// File: /workspace/github_c_makefile_star2_O1/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 0xc8a(%rip), %rdi       # 0x2076
callq 0x1030
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
xorl %edi, %edi
callq 0x1080
testq %rax, %rax
je 0x168b
movq %rax, %rbx
movq %rax, %rdi
callq 0x1110
subq $0x20, %rsp
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rsp)
movl $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
addq $0x20, %rsp
movq %rax, %r14
leaq 0x40(%rsp), %r15
movq %rbx, (%r15)
movq %rax, 0x8(%r15)
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x10f0
movq %rax, 0x20(%rsp)
movq %rax, 0x10(%r15)
movq %rbx, %rdi
movq %r14, %rsi
movl $0x320, %edx            # imm = 0x320
movl $0x258, %ecx            # imm = 0x258
movl $0x18, %r8d
callq 0x1170
movq %rax, 0x18(%rsp)
movq %rax, 0x18(%r15)
leaq 0xbae(%rip), %rdx       # 0x204b
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1040
leaq 0xbb6(%rip), %rsi       # 0x2065
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10c0
leaq 0x38(%rsp), %rdx
movq %rax, (%rdx)
movl $0x1, %r15d
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %ecx
callq 0x1090
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x10e0
movq %rbx, %rdi
movq %r14, 0x28(%rsp)
movq %r14, %rsi
callq 0x1130
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0xa, %edx
leaq 0x68(%rsp), %rbp
movl $0xa, %ecx
xorl %r12d, %r12d
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %rsi, 0x8(%rsp)
movq %rdx, %r13
movq %rcx, 0x10(%rsp)
movq %rax, 0x30(%rsp)
movq %rbx, %rdi
callq 0x1050
testl %eax, %eax
jle 0x157c
movl $0xc0, %edx
movq %rbp, %rdi
xorl %esi, %esi
callq 0x1070
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x10b0
movl 0x68(%rsp), %eax
cmpl $0x21, %eax
je 0x1569
cmpl $0x2, %eax
jne 0x1525
movq %rbp, %rdi
xorl %esi, %esi
callq 0x10d0
cmpq $0x71, %rax
jmp 0x1576
movq 0xa0(%rsp), %rax
cmpq 0x38(%rsp), %rax
cmovel %r15d, %r12d
jmp 0x1525
leaq 0x40(%rsp), %rdi
callq 0x1309
movl $0xff0000, %edx         # imm = 0xFF0000
movq %rbx, %rdi
movq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0x1120
subq $0x8, %rsp
movq %rbx, %rdi
movq 0x20(%rsp), %r15
movq %r15, %rsi
movq %r14, %rdx
movl %r13d, %ecx
movq 0x18(%rsp), %r8
movl $0x12c, %r9d            # imm = 0x12C
pushq $0x12c                  # imm = 0x12C
callq 0x1060
addq $0x10, %rsp
movq %rbx, %rdi
movq %r15, %rsi
movq 0x28(%rsp), %rdx
movq %r14, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %eax, %eax
pushq %rax
pushq %rax
pushq $0x258                  # imm = 0x258
pushq $0x320                  # imm = 0x320
callq 0x10a0
addq $0x20, %rsp
movq 0x8(%rsp), %rcx
leal (%rcx,%r13), %eax
addl $0xfffffe0c, %eax       # imm = 0xFFFFFE0C
movl %ecx, %r15d
negl %r15d
cmpl $0xfffffe0d, %eax       # imm = 0xFFFFFE0D
cmovael %ecx, %r15d
leal (%r13,%rcx), %eax
cmovbl %r13d, %eax
movl %eax, 0x8(%rsp)
movq 0x30(%rsp), %rcx
movq 0x10(%rsp), %rdx
leal (%rdx,%rcx), %r14d
leal (%rdx,%rcx), %eax
addl $0xfffffed4, %eax       # imm = 0xFFFFFED4
movl %ecx, %r13d
negl %r13d
cmpl $0xfffffed5, %eax       # imm = 0xFFFFFED5
cmovael %ecx, %r13d
cmovbl %edx, %r14d
movl $0x208d, %edi           # imm = 0x208D
callq 0x1180
movl 0x8(%rsp), %eax
movl %eax, %edx
movl %r14d, %ecx
movl %r13d, %eax
movl %r15d, %esi
movl $0x1, %r15d
testl %r12d, %r12d
je 0x1513
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
callq 0x11a0