// File: /workspace/github_cpp_makefile_star2_O2/nicoeps[P]NicoGB/src/main.cpp
// Function: run(NicoGB&)
void run(NicoGB& nicogb) {
    const int SCALE = 4;
    const int WIDTH = 160;
    const int HEIGHT = 144;

    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window *window = SDL_CreateWindow("NicoGB",
        SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
        WIDTH*SCALE, HEIGHT*SCALE, 0);
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
    SDL_Texture *texture = SDL_CreateTexture(renderer,
    SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 160, 144);
    SDL_UpdateTexture(texture, NULL, nicogb.framebuffer.data(), 160 * sizeof(uint32_t));

    SDL_Event event;
    SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
    std::string path;
    Key key;

    auto last = millis();
    bool run = true;
    while (run) {
        nicogb.tick();

        if (millis() - last >= 1000/60) {
            last = millis();

            SDL_UpdateTexture(texture, NULL, nicogb.framebuffer.data(), 160 * sizeof(uint32_t));
            SDL_RenderClear(renderer);
            SDL_RenderCopy(renderer, texture, NULL, NULL);
            SDL_RenderPresent(renderer);

            while (SDL_PollEvent(&event)) {
                switch (event.type) {
                    case SDL_QUIT:
                        run = false;
                        break;

                    case SDL_DROPFILE:
                        path = event.drop.file;
                        nicogb.load(path);
                        SDL_SetWindowTitle(window, (std::string("NicoGB - ") + nicogb.title).c_str());
                        break;

                    case SDL_KEYDOWN:
                        switch (event.key.keysym.sym) {
                            case SDLK_q:
                                run = false;
                                break;
                            case SDLK_r:
                                nicogb.init();
                                break;
                            case SDLK_SPACE:
                                nicogb.speed = !nicogb.speed;
                                break;
                            default:
                                key = getKey(event.key.keysym.sym);
                                nicogb.keyDown(key);
                                break;
                        }
                        break;

                    case SDL_KEYUP:
                        key = getKey(event.key.keysym.sym);
                        nicogb.keyUp(key);
                        break;

                    default: break;
                }
            }
        }
    }
    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdi, %r14
pushq $0x20
popq %rdi
callq 0x21b0
leaq 0x422a(%rip), %rdi      # 0x9968
movl $0x1fff0000, %esi       # imm = 0x1FFF0000
movl $0x1fff0000, %edx       # imm = 0x1FFF0000
movl $0x280, %ecx            # imm = 0x280
movl $0x240, %r8d            # imm = 0x240
xorl %r9d, %r9d
callq 0x2050
pushq $-0x1
popq %rsi
movq %rax, 0x18(%rsp)
movq %rax, %rdi
xorl %edx, %edx
callq 0x2040
pushq $0x2
popq %rdx
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movl $0x16362004, %esi       # imm = 0x16362004
movl $0xa0, %ecx
movl $0x90, %r8d
callq 0x22d0
movq %rax, %rdi
movq 0x2c0(%r14), %rax
movq (%rax), %rdx
movq %rdi, 0x8(%rsp)
xorl %esi, %esi
movl $0x280, %ecx            # imm = 0x280
callq 0x2180
pushq $0x1
popq %rsi
movl $0x1000, %edi           # imm = 0x1000
callq 0x2120
leaq 0x30(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movb $0x1, %bpl
callq 0x56a5
movq %rax, %r15
leaq 0x60(%rsp), %r13
leaq 0xb8(%rsp), %r12
leaq 0x98(%rsp), %rbx
testb $0x1, %bpl
je 0x5981
movq %r14, %rdi
callq 0x6a4e
callq 0x56a5
subq %r15, %rax
cmpq $0x10, %rax
jl 0x57f3
callq 0x56a5
movq %rax, %r15
movq 0x2c0(%r14), %rax
movq (%rax), %rdx
movq 0x8(%rsp), %rdi
xorl %esi, %esi
movl $0x280, %ecx            # imm = 0x280
callq 0x2180
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
callq 0x21c0
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x21f0
movq %rbp, %rdi
callq 0x2080
movb $0x1, %al
movl %eax, %ebp
movq %r13, %rdi
callq 0x2150
testl %eax, %eax
je 0x57e9
movl 0x60(%rsp), %ecx
cmpl $0x100, %ecx            # imm = 0x100
je 0x5955
cmpl $0x300, %ecx            # imm = 0x300
je 0x5913
cmpl $0x301, %ecx            # imm = 0x301
je 0x593b
movl %ebp, %eax
cmpl $0x1000, %ecx           # imm = 0x1000
jne 0x5854
movq 0x68(%rsp), %rsi
leaq 0x20(%rsp), %rdi
callq 0x22e0
movq %r12, %rdi
leaq 0x20(%rsp), %rsi
callq 0x2240
movq %r14, %rdi
movq %r12, %rsi
callq 0x69f8
movq %r12, %rdi
callq 0x2110
movq %rbx, %rdi
leaq 0x40a2(%rip), %rsi      # 0x996f
leaq 0x7(%rsp), %rdx
callq 0x5a72
movq 0x2b8(%r14), %rdx
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
callq 0x5a52
movq 0x40(%rsp), %rsi
movq 0x18(%rsp), %rdi
callq 0x22a0
leaq 0x40(%rsp), %rdi
callq 0x2110
movq %rbx, %rdi
callq 0x2110
movl %ebp, %eax
jmp 0x5854
movl 0x74(%rsp), %edi
cmpl $0x20, %edi
je 0x595c
xorl %eax, %eax
cmpl $0x71, %edi
je 0x5854
cmpl $0x72, %edi
jne 0x596b
movq %r14, %rdi
callq 0x69b2
movl %ebp, %eax
jmp 0x5854
movl 0x74(%rsp), %edi
callq 0x56be
movq %r14, %rdi
movl %eax, %esi
callq 0x6ad6
movl %ebp, %eax
jmp 0x5854
xorl %eax, %eax
jmp 0x5854
xorb $0x1, 0x2a8(%r14)
movl %ebp, %eax
jmp 0x5854
callq 0x56be
movq %r14, %rdi
movl %eax, %esi
callq 0x6acc
movl %ebp, %eax
jmp 0x5854
movq 0x8(%rsp), %rdi
callq 0x2170
movq 0x10(%rsp), %rdi
callq 0x20f0
movq 0x18(%rsp), %rdi
callq 0x2030
callq 0x2070
leaq 0x20(%rsp), %rdi
callq 0x2110
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x59f6
jmp 0x59f6
movq %rax, %rbx
jmp 0x59e5
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
jmp 0x59ed
jmp 0x59f6
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x2110
leaq 0x98(%rsp), %rdi
callq 0x2110
jmp 0x59f9
jmp 0x59f6
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x2110
movq %rbx, %rdi
callq 0x22b0