// File: /workspace/github_cpp_makefile_star2_O3/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 $0xe8, %rsp
movq %rdi, %rbx
movl $0x20, %edi
callq 0x2190
leaq 0x39a7(%rip), %rdi      # 0x9950
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
movq %rax, 0x20(%rsp)
movq %rax, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0x2040
movq %rax, 0x8(%rsp)
movq %rax, %rdi
movl $0x16362004, %esi       # imm = 0x16362004
movl $0x2, %edx
movl $0xa0, %ecx
movl $0x90, %r8d
callq 0x2290
movq %rax, %rdi
movq 0x2c0(%rbx), %rax
movq (%rax), %rdx
movq %rdi, 0x10(%rsp)
xorl %esi, %esi
movl $0x280, %ecx            # imm = 0x280
callq 0x2160
movl $0x1000, %edi           # imm = 0x1000
movl $0x1, %esi
callq 0x2100
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
callq 0x2060
subq 0x7127(%rip), %rax      # 0xd170
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %rdx
addq %rax, %rdx
movq %rdx, 0x18(%rsp)
leaq 0xa0(%rsp), %r13
leaq 0x58(%rsp), %rbp
movq %rbx, 0x88(%rsp)
movq %rbx, %rdi
callq 0x7730
callq 0x2060
subq 0x70de(%rip), %rax      # 0xd170
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %rdx
addq %rax, %rdx
subq 0x18(%rsp), %rdx
movb $0x1, %r12b
cmpq $0x10, %rdx
jl 0x634c
callq 0x2060
subq 0x70a5(%rip), %rax      # 0xd170
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
imulq %rcx
movq %rdx, %r14
movq 0x2c0(%rbx), %rax
movq (%rax), %rdx
movq 0x10(%rsp), %rdi
xorl %esi, %esi
movl $0x280, %ecx            # imm = 0x280
callq 0x2160
movq 0x8(%rsp), %rdi
callq 0x21a0
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x21d0
movq 0x8(%rsp), %rdi
callq 0x2080
movq %r14, %rax
shrq $0x3f, %rax
sarq $0x12, %r14
addq %rax, %r14
movq %r14, 0x18(%rsp)
movb $0x1, %al
leaq 0xb0(%rsp), %r14
movl %eax, %r12d
movq %r14, %rdi
callq 0x2130
testl %eax, %eax
je 0x634c
movl 0xb0(%rsp), %ecx
cmpl $0x300, %ecx            # imm = 0x300
jg 0x61a0
cmpl $0x100, %ecx            # imm = 0x100
je 0x629e
movl %r12d, %eax
cmpl $0x300, %ecx            # imm = 0x300
jne 0x613a
movl 0xc4(%rsp), %edi
cmpl $0x20, %edi
je 0x6326
xorl %eax, %eax
cmpl $0x71, %edi
je 0x613a
cmpl $0x72, %edi
jne 0x6335
movq %rbx, %rdi
callq 0x7668
movl %r12d, %eax
jmp 0x613a
cmpl $0x301, %ecx            # imm = 0x301
je 0x62a5
movl %r12d, %eax
cmpl $0x1000, %ecx           # imm = 0x1000
jne 0x613a
movq 0xb8(%rsp), %rbx
movq 0x30(%rsp), %r15
movq %rbx, %rdi
callq 0x20f0
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
movq %r15, %rdx
leaq 0xb0(%rsp), %r14
movq %rbx, %rcx
movq 0x88(%rsp), %rbx
movq %rax, %r8
callq 0x2210
movq %r13, 0x90(%rsp)
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq %rsi, %rdx
leaq 0x90(%rsp), %rdi
callq 0x65c4
movq %rbx, %rdi
leaq 0x90(%rsp), %rsi
callq 0x76ae
movq 0x90(%rsp), %rdi
cmpq %r13, %rdi
je 0x6240
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x21c0
leaq 0x78(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0x68(%rsp), %r15
movq %r15, %rdi
leaq 0x36fe(%rip), %rsi      # 0x9957
leaq 0x3700(%rip), %rdx      # 0x9960
callq 0x665a
movq 0x2b8(%rbx), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r15, %rdi
callq 0x20c0
movq %rbp, 0x48(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x62c3
movq %rdx, 0x48(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x58(%rsp)
jmp 0x62ca
xorl %eax, %eax
jmp 0x613a
movl 0xc4(%rsp), %edi
callq 0x5f0f
movq %rbx, %rdi
movl %eax, %esi
callq 0x77f8
movl %r12d, %eax
jmp 0x613a
movups (%rcx), %xmm0
movups %xmm0, (%rbp)
movq 0x8(%rax), %rdx
movq %rdx, 0x50(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x48(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x2260
movq 0x48(%rsp), %rdi
cmpq %rbp, %rdi
je 0x6308
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x632d
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21c0
jmp 0x632d
xorb $0x1, 0x2a8(%rbx)
movl %r12d, %eax
jmp 0x613a
callq 0x5f0f
movq %rbx, %rdi
movl %eax, %esi
callq 0x77ee
movl %r12d, %eax
jmp 0x613a
testb $0x1, %r12b
jne 0x607e
movq 0x10(%rsp), %rdi
callq 0x2150
movq 0x8(%rsp), %rdi
callq 0x20e0
movq 0x20(%rsp), %rdi
callq 0x2030
callq 0x2070
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x6395
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6404
jmp 0x6404
movq %rax, %rbx
jmp 0x63ca
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %rbp, %rdi
je 0x63ca
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21c0
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x6407
movq 0x78(%rsp), %rsi
jmp 0x63fa
jmp 0x6404
movq %rax, %rbx
movq 0x90(%rsp), %rdi
cmpq %r13, %rdi
je 0x6407
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x21c0
jmp 0x6407
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x6423
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
movq %rbx, %rdi
callq 0x2270