// File: /workspace/github_c_makefile_star2_O3/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 0xc6e(%rip), %rdi       # 0x205a
callq 0x1030
xorl %edi, %edi
callq 0x1080
testq %rax, %rax
je 0x168c
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 0x48(%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 0xb9f(%rip), %rdx       # 0x202f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1040
leaq 0xba7(%rip), %rsi       # 0x2049
movq %rbx, %rdi
xorl %edx, %edx
callq 0x10c0
leaq 0x40(%rsp), %rdx
movq %rax, (%rdx)
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
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq %rdx, 0x30(%rsp)
movq %rcx, 0x10(%rsp)
movq %rax, 0x38(%rsp)
movq %rbx, %rdi
callq 0x1050
testl %eax, %eax
jle 0x1581
xorl %r12d, %r12d
movl $0x1, %r14d
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 0x155a
cmpl $0x2, %eax
jne 0x156b
movq %rbp, %rdi
xorl %esi, %esi
callq 0x10d0
cmpq $0x71, %rax
jmp 0x1567
movq 0xa0(%rsp), %rax
cmpq 0x40(%rsp), %rax
cmovel %r14d, %r12d
movq %rbx, %rdi
callq 0x1050
testl %eax, %eax
jg 0x1522
testl %r12d, %r12d
sete 0xf(%rsp)
jmp 0x1586
movb $0x1, 0xf(%rsp)
leaq 0x48(%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
movq 0x38(%rsp), %r12
movl %r12d, %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 %r12, %rcx
leal (%r12,%r13), %eax
addl $0xfffffe0c, %eax       # imm = 0xFFFFFE0C
movl %r13d, %r12d
negl %r12d
cmpl $0xfffffe0d, %eax       # imm = 0xFFFFFE0D
cmovael %r13d, %r12d
leal (%rcx,%r13), %r13d
cmovbl %ecx, %r13d
movq 0x38(%rsp), %rcx
movq 0x10(%rsp), %rdx
leal (%rdx,%rcx), %r15d
leal (%rdx,%rcx), %eax
addl $0xfffffed4, %eax       # imm = 0xFFFFFED4
movl %ecx, %r14d
negl %r14d
cmpl $0xfffffed5, %eax       # imm = 0xFFFFFED5
cmovael %ecx, %r14d
cmovbl %edx, %r15d
movl $0x208d, %edi           # imm = 0x208D
callq 0x1180
movl %r13d, %edx
movl %r15d, %ecx
movl %r14d, %eax
movl %r12d, %r13d
cmpb $0x0, 0xf(%rsp)
jne 0x14fe
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