// File: /workspace/github_cpp_makefile_star2_O3/IsseW[P]bytepusher/src/main.cpp
// Function: main
int main(){
    Renderer renderer{"some title", GRID_WIDTH, GRID_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT};

    Grid grid = Grid(GRID_WIDTH, GRID_HEIGHT);
    grid.set_tile(4, 4, Tile::Alive);
    grid.set_tile(4, 5, Tile::Alive);
    grid.set_tile(4, 6, Tile::Alive);
    grid.set_tile(4, 7, Tile::Alive);
    grid.set_tile(4, 8, Tile::Alive);
    grid.set_tile(4, 9, Tile::Alive);
    grid.set_tile(4, 10, Tile::Alive);

    uint64_t now, last;
    double delta_time = 0;

    double next_tick = TICK_TIME;

    auto callback = [&](const SDL_Event& e) {
        switch (e.type) {
            case SDL_MOUSEBUTTONDOWN: {
                uint32_t x = e.button.x * GRID_WIDTH / WINDOW_WIDTH;
                uint32_t y = e.button.y * GRID_HEIGHT / WINDOW_HEIGHT;
                switch (e.button.button) {
                    case SDL_BUTTON_LEFT:
                        grid.set_tile(x, y, Tile::Alive);
                        break;
                    
                }
                break;
            }
        }
    };
    renderer.set_manual_event_handler(callback);

    while(renderer.poll_events()) {
        last = now;
        now = SDL_GetPerformanceCounter();
        delta_time = (now - last) / (double)SDL_GetPerformanceFrequency();

        next_tick -= delta_time;
        if (next_tick < 0.0) {
            next_tick = TICK_TIME;
            grid.update_with_rule(game_of_life_rule);
        }

        renderer.bulk_send_data(grid.get_texture());
        renderer.finalize();
    }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa0, %rsp
leaq 0x10(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0xcc4(%rip), %rsi       # 0x3040
leaq 0xcc7(%rip), %rdx       # 0x304a
movq %rsp, %rdi
callq 0x26f4
leaq 0x48(%rsp), %rdi
movq %rsp, %rsi
movl $0x20, %edx
movl $0x20, %ecx
movl $0x140, %r8d            # imm = 0x140
movl $0x140, %r9d            # imm = 0x140
callq 0x2b86
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0x23c4
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2170
movq %rsp, %rdi
movl $0x20, %esi
movl $0x20, %edx
callq 0x278c
movq %rsp, %rdi
movl $0x4, %esi
movl $0x4, %edx
movl $0xff, %ecx
callq 0x288e
movq %rsp, %rdi
movl $0x4, %esi
movl $0x5, %edx
movl $0xff, %ecx
callq 0x288e
movq %rsp, %rdi
movl $0x4, %esi
movl $0x6, %edx
movl $0xff, %ecx
callq 0x288e
movq %rsp, %rdi
movl $0x4, %esi
movl $0x7, %edx
movl $0xff, %ecx
callq 0x288e
movq %rsp, %rdi
movl $0x4, %esi
movl $0x8, %edx
movl $0xff, %ecx
callq 0x288e
movq %rsp, %rdi
movl $0x4, %esi
movl $0x9, %edx
movl $0xff, %ecx
callq 0x288e
movq %rsp, %rbx
movl $0x4, %esi
movl $0xa, %edx
movq %rbx, %rdi
movl $0xff, %ecx
callq 0x288e
leaq 0x28(%rsp), %rsi
movq $0x0, 0x8(%rsi)
movq %rbx, (%rsi)
leaq 0x1bf(%rip), %rax       # 0x2650
movq %rax, 0x18(%rsi)
leaq 0x1f4(%rip), %rax       # 0x2690
movq %rax, 0x10(%rsi)
leaq 0x48(%rsp), %rdi
callq 0x2b7c
movq 0x38(%rsp), %rax
testq %rax, %rax
je 0x24c3
leaq 0x28(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movsd 0xb3d(%rip), %xmm0      # 0x3008
leaq 0x48(%rsp), %rbx
leaq 0x559(%rip), %r14       # 0x2a30
movq %rsp, %r15
movsd %xmm0, 0x20(%rsp)
movq %rbx, %rdi
callq 0x2d0e
testb %al, %al
je 0x259f
callq 0x21c0
movq %rax, %r12
callq 0x2130
movq %r12, %rcx
subq %r13, %rcx
movq %rcx, %xmm0
movq 0xb10(%rip), %xmm2      # 0x3020
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0xb14(%rip), %xmm3      # 0x3030
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movq %rax, %xmm0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
subpd %xmm3, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
divsd %xmm2, %xmm1
movsd 0x20(%rsp), %xmm2
subsd %xmm1, %xmm2
xorpd %xmm0, %xmm0
movsd %xmm2, 0x20(%rsp)
ucomisd %xmm2, %xmm0
jbe 0x257c
movq %r15, %rdi
movq %r14, %rsi
callq 0x2822
movsd 0xa92(%rip), %xmm0      # 0x3008
movsd %xmm0, 0x20(%rsp)
movq %r15, %rdi
callq 0x2a28
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2c96
movq %rbx, %rdi
callq 0x2cca
movq %r12, %r13
jmp 0x24e0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x25ae
callq 0x21b0
movq $0x0, 0x10(%rsp)
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x25c6
callq 0x21b0
leaq 0x48(%rsp), %rdi
callq 0x2c46
xorl %eax, %eax
addq $0xa0, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
jmp 0x2603
movq %rax, %rbx
movq 0x38(%rsp), %rax
testq %rax, %rax
je 0x2635
leaq 0x28(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x2635
movq %rax, %rdi
callq 0x26e8
movq %rax, %rbx
jmp 0x263d
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0x2647
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x2170
jmp 0x2647
movq %rax, %rbx
jmp 0x2647
jmp 0x2632
movq %rax, %rbx
movq %rsp, %rdi
callq 0x26b6
leaq 0x48(%rsp), %rdi
callq 0x2c46
movq %rbx, %rdi
callq 0x2200
nop